#include <stdlib.h>
#include <stdio.h>
#include <emscripten/emscripten.h> // For EMSCRIPTEN_KEEPALIVE

#define ARRAY_SIZE 15000576  // 15 million items (still define for context, but not used directly in loops anymore)

EMSCRIPTEN_KEEPALIVE
int* alloc( int size ) {
	return ( int * ) malloc( size );
}

EMSCRIPTEN_KEEPALIVE
void dealloc( int * ptr, int size ) {
	free( ptr );
}

EMSCRIPTEN_KEEPALIVE
float* alloc_float_array(int size) {
    return ( float * ) malloc(size * sizeof(float));
}

EMSCRIPTEN_KEEPALIVE
void dealloc_float_array(float* ptr) {
    free(ptr);
}

EMSCRIPTEN_KEEPALIVE
void init_arrays(float* a, float* b) {
    for (int i = 0; i < ARRAY_SIZE; i++) {
        a[i] = i;
        b[i] = i * 2;
    }
}


// Modified init_arrays to operate on a specific range
EMSCRIPTEN_KEEPALIVE
void init_arrays_range(float* a, float* b, int start_idx, int end_idx) {
    for (int i = start_idx; i < end_idx; i++) {
        a[i] = (float)i;
        b[i] = (float)i * 2.0f;
    }
}

// Modified calculate to operate on a specific range
EMSCRIPTEN_KEEPALIVE
void calculate_range(float* a, float* b, float* c, int start, int end) {
    for (int i = start; i < end; ++i) {
        c[i] = a[i] + b[i];
    }
}