/* compile with: nvcc -O3 hw1.cu -o hw1 */

#include <stdio.h>
#include <cuda.h>
#include <sys/time.h>

///////////////////////////////////////////////// DO NOT CHANGE ///////////////////////////////////////
#define IMG_HEIGHT 256
#define IMG_WIDTH 256
//#define N_IMAGES 10000
#define N_IMAGES 500

#define NUM_THREADS 256


typedef unsigned char uchar;

#define CUDA_CHECK(f) do {                                                                  \
    cudaError_t e = f;                                                                      \
    if (e != cudaSuccess) {                                                                 \
        printf("Cuda failure %s:%d: '%s'\n", __FILE__, __LINE__, cudaGetErrorString(e));    \
        exit(1);                                                                            \
    }                                                                                       \
} while (0)

#define SQR(a) ((a) * (a))

void process_image(uchar *img_in, uchar *img_out) {
    int histogram[256] = { 0 };
    for (int i = 0; i < IMG_WIDTH * IMG_HEIGHT; i++) {
        histogram[img_in[i]]++;
    }

    int cdf[256] = { 0 };
    int hist_sum = 0;
    for (int i = 0; i < 256; i++) {
        hist_sum += histogram[i];
        cdf[i] = hist_sum;
    }

    int cdf_min = 0;
    for (int i = 0; i < 256; i++) {
        if (cdf[i] != 0) {
            cdf_min = cdf[i];
            break;
        }
    }

    uchar map[256] = { 0 };
    for (int i = 0; i < 256; i++) {
        int map_value = (float)(cdf[i] - cdf_min) / (IMG_WIDTH * IMG_HEIGHT - cdf_min) * 255;
        map[i] = (uchar)map_value;
    }

    for (int i = 0; i < IMG_WIDTH * IMG_HEIGHT; i++) {
        img_out[i] = map[img_in[i]];
    }
}

double static inline get_time_msec(void) {
    struct timeval t;
    gettimeofday(&t, NULL);
    return t.tv_sec * 1e+3 + t.tv_usec * 1e-3;
}

long long int distance_sqr_between_image_arrays(uchar *img_arr1, uchar *img_arr2) {
    long long int distance_sqr = 0;
    for (int i = 0; i < N_IMAGES * IMG_WIDTH * IMG_HEIGHT; i++) {
        distance_sqr += SQR(img_arr1[i] - img_arr2[i]);
    }
    return distance_sqr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

__device__ int arr_min(int arr[], int arr_size) {//return the min
	int tid = threadIdx.x;
	int half_length = arr_size / 2;

	while (half_length >= 1) {
		for (int i = tid; i < half_length; i += blockDim.x) {
			if(arr[tid + i] < arr[i]) arr[i] = arr[tid + i];
		}
		__syncthreads();
		half_length /= 2;
	}
    return arr[0]; //TODO
}

// this function implements the Kiggle-Stone algorithm
__device__ void prefix_sum(int arr[], int arr_size, int histogram[]) {

	int tbsize = blockDim.x;
	int tid = threadIdx.x;
	int inc;
 
 	for (int stride = 1; stride < tbsize; stride *= 2) {
	
		if (tid >= arr_size)
			continue;

 		if (tid >= stride) {
 		inc = arr[tid - stride];
 		}
 		__syncthreads();

 		if (tid >= stride) {
			arr[tid] += inc;
 		}

 		__syncthreads();
 	}

    return;
}

__device__ void mapCalc(int map[], int min, int cdf[]) {
	int id = threadIdx.x;
	int map_value = ((double)(cdf[id] - min)/(IMG_WIDTH * IMG_HEIGHT - min)) * 255;
    map[id] = map_value;
}

__global__ void process_image_kernel(uchar *in, uchar *out) {

    __shared__ int l_histogram[256];
    __shared__ int l_cdf[256];
    __shared__ int map[256];
	int tid = threadIdx.x;
	int bid = blockIdx.x;
	int tbsize = blockDim.x;

	// zero histogram
	l_histogram[tid] = 0;

	__syncthreads();

	for(int i = tid; i < IMG_WIDTH * IMG_HEIGHT; i += tbsize)
		atomicAdd(&l_histogram[in[(IMG_WIDTH * IMG_HEIGHT)*bid + i]], 1);

	__syncthreads();

	// prepare the cdf array in advance
	l_cdf[tid] = l_histogram[tid];

	__syncthreads();

	prefix_sum(l_cdf, 256, l_histogram);

	__syncthreads();

    int min = arr_min(l_histogram, 256);

	__syncthreads();

	mapCalc(map, min, l_cdf);

	__syncthreads();
   

    for(int i = tid; i < IMG_WIDTH * IMG_HEIGHT; i += tbsize) {
		out[(IMG_WIDTH * IMG_HEIGHT)*bid + i] =
			map[in[(IMG_WIDTH * IMG_HEIGHT)*bid + i]];
    }

	__syncthreads();

    return ; //TODO
}

int main() {
///////////////////////////////////////////////// DO NOT CHANGE ///////////////////////////////////////
    uchar *images_in;
    uchar *images_out_cpu; //output of CPU computation. In CPU memory.
    uchar *images_out_gpu_serial; //output of GPU task serial computation. In CPU memory.
    uchar *images_out_gpu_bulk; //output of GPU bulk computation. In CPU memory.
    CUDA_CHECK( cudaHostAlloc(&images_in, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );
    CUDA_CHECK( cudaHostAlloc(&images_out_cpu, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );
    CUDA_CHECK( cudaHostAlloc(&images_out_gpu_serial, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );
    CUDA_CHECK( cudaHostAlloc(&images_out_gpu_bulk, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );

    /* instead of loading real images, we'll load the arrays with random data */
    srand(0);
    for (long long int i = 0; i < N_IMAGES * IMG_WIDTH * IMG_HEIGHT; i++) {
        images_in[i] = rand() % 256;
    }

    double t_start, t_finish;

    // CPU computation. For reference. Do not change
    printf("\n=== CPU ===\n");
    t_start = get_time_msec();
    for (int i = 0; i < N_IMAGES; i++) {
        uchar *img_in = &images_in[i * IMG_WIDTH * IMG_HEIGHT];
        uchar *img_out = &images_out_cpu[i * IMG_WIDTH * IMG_HEIGHT];
		process_image(img_in, img_out);
    }
    t_finish = get_time_msec();
    printf("total time %f [msec]\n", t_finish - t_start);

    long long int distance_sqr;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
	uchar *image_in;
	uchar *image_out;

    // GPU task serial computation
    printf("\n=== GPU Task Serial ===\n"); //Do not change

    //TODO: allocate GPU memory for a single input image and a single output image
    CUDA_CHECK( cudaMalloc((void **)&image_in, IMG_HEIGHT * IMG_WIDTH) );
    CUDA_CHECK( cudaMalloc((void **)&image_out, IMG_HEIGHT * IMG_WIDTH) );

    t_start = get_time_msec(); //Do not change

    //TODO: in a for loop:
    for (int i=0; i < N_IMAGES; i++) {
		// Copying src image from the input images
		CUDA_CHECK(cudaMemcpy(image_in, &images_in[i * IMG_WIDTH*IMG_HEIGHT], IMG_WIDTH*IMG_HEIGHT, cudaMemcpyDefault));

		process_image_kernel <<< 1, NUM_THREADS >>> (image_in, image_out);  

		CUDA_CHECK(cudaDeviceSynchronize());

		CUDA_CHECK(cudaMemcpy(&images_out_gpu_serial[i * IMG_HEIGHT*IMG_WIDTH], image_out, IMG_WIDTH*IMG_HEIGHT, cudaMemcpyDefault));
    }

	cudaFree(image_in);
	cudaFree(image_out);

    t_finish = get_time_msec(); //Do not change
    distance_sqr = distance_sqr_between_image_arrays(images_out_cpu, images_out_gpu_serial); // Do not change
    printf("total time %f [msec]  distance from baseline %lld (should be zero)\n", t_finish - t_start, distance_sqr); //Do not change

    // GPU bulk
    printf("\n=== GPU Bulk ===\n"); //Do not change
    //TODO: allocate GPU memory for a all input images and all output images
	CUDA_CHECK( cudaMalloc((void **)&image_in, N_IMAGES * IMG_HEIGHT * IMG_WIDTH) );
    CUDA_CHECK( cudaMalloc((void **)&image_out, N_IMAGES * IMG_HEIGHT * IMG_WIDTH) );

    //TODO: copy all input images from images_in to the GPU memory you allocated
    t_start = get_time_msec(); //Do not change
	CUDA_CHECK(cudaMemcpy(image_in, images_in, N_IMAGES*IMG_WIDTH*IMG_HEIGHT, cudaMemcpyDefault));

    //TODO: invoke a kernel with N_IMAGES threadblocks, each working on a different image
	process_image_kernel <<< N_IMAGES, NUM_THREADS >>> (image_in, image_out);
	CUDA_CHECK(cudaDeviceSynchronize());

    //TODO: copy output images from GPU memory to images_out_gpu_bulk
	CUDA_CHECK(cudaMemcpy(images_out_gpu_bulk, image_out, N_IMAGES*IMG_WIDTH*IMG_HEIGHT, cudaMemcpyDefault));

    t_finish = get_time_msec(); //Do not change

	cudaFree(image_in);
	cudaFree(image_out);

    distance_sqr = distance_sqr_between_image_arrays(images_out_cpu, images_out_gpu_bulk); // Do not change
    printf("total time %f [msec]  distance from baseline %lld (should be zero)\n", t_finish - t_start, distance_sqr); //Do not chhange

    return 0;
}

	.file	"tmpxft_00217b29_00000000-6_hw1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL13get_time_msecv, @function
_ZL13get_time_msecv:
.LFB2028:
	.cfi_startproc
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	8(%rsp), %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsi2sdq	16(%rsp), %xmm1
	mulsd	.LC1(%rip), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L2
	call	__stack_chk_fail@PLT
.L2:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_ZL13get_time_msecv, .-_ZL13get_time_msecv
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2036:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2036:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z13process_imagePhS_
	.type	_Z13process_imagePhS_, @function
_Z13process_imagePhS_:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$2328, %rsp
	.cfi_def_cfa_offset 2336
	movq	%rdi, %rdx
	movl	$256, %ecx
	movq	%fs:40, %rax
	movq	%rax, 2312(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	leaq	8(%rsp), %r8
	rep stosl
	xorl	%eax, %eax
.L7:
	movzbl	(%rdx,%rax), %ecx
	incq	%rax
	incl	8(%rsp,%rcx,4)
	cmpq	$65536, %rax
	jne	.L7
	xorl	%eax, %eax
	leaq	1032(%rsp), %rdi
	movl	$256, %ecx
	rep stosl
	leaq	1032(%rsp), %r9
	xorl	%eax, %eax
.L8:
	addl	(%r8,%rax), %ecx
	movl	%ecx, (%r9,%rax)
	addq	$4, %rax
	cmpq	$1024, %rax
	jne	.L8
	xorl	%eax, %eax
.L10:
	movl	(%r9,%rax,4), %r8d
	testl	%r8d, %r8d
	jne	.L9
	incq	%rax
	cmpq	$256, %rax
	jne	.L10
.L9:
	xorl	%eax, %eax
	leaq	2056(%rsp), %rdi
	movl	$64, %ecx
	movss	.LC2(%rip), %xmm1
	rep stosl
	movl	$65536, %ecx
	xorl	%eax, %eax
	leaq	2056(%rsp), %rdi
	subl	%r8d, %ecx
	cvtsi2ssl	%ecx, %xmm2
.L11:
	movl	(%r9,%rax,4), %ecx
	subl	%r8d, %ecx
	cvtsi2ssl	%ecx, %xmm0
	divss	%xmm2, %xmm0
	mulss	%xmm1, %xmm0
	cvttss2sil	%xmm0, %ecx
	movb	%cl, (%rdi,%rax)
	incq	%rax
	cmpq	$256, %rax
	jne	.L11
	xorl	%eax, %eax
.L12:
	movzbl	(%rdx,%rax), %ecx
	movb	2056(%rsp,%rcx), %cl
	movb	%cl, (%rsi,%rax)
	incq	%rax
	cmpq	$65536, %rax
	jne	.L12
	movq	2312(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$2328, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z13process_imagePhS_, .-_Z13process_imagePhS_
	.globl	_Z33distance_sqr_between_image_arraysPhS_
	.type	_Z33distance_sqr_between_image_arraysPhS_, @function
_Z33distance_sqr_between_image_arraysPhS_:
.LFB2029:
	.cfi_startproc
	endbr64
	xorl	%edx, %edx
	xorl	%ecx, %ecx
.L21:
	movzbl	(%rdi,%rdx), %eax
	movzbl	(%rsi,%rdx), %r8d
	incq	%rdx
	subl	%r8d, %eax
	imull	%eax, %eax
	cltq
	addq	%rax, %rcx
	cmpq	$32768000, %rdx
	jne	.L21
	movq	%rcx, %rax
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z33distance_sqr_between_image_arraysPhS_, .-_Z33distance_sqr_between_image_arraysPhS_
	.globl	_Z7arr_minPii
	.type	_Z7arr_minPii, @function
_Z7arr_minPii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z7arr_minPii, .-_Z7arr_minPii
	.globl	_Z10prefix_sumPiiS_
	.type	_Z10prefix_sumPiiS_, @function
_Z10prefix_sumPiiS_:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z10prefix_sumPiiS_, .-_Z10prefix_sumPiiS_
	.globl	_Z7mapCalcPiiS_
	.type	_Z7mapCalcPiiS_, @function
_Z7mapCalcPiiS_:
.LFB2179:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2179:
	.size	_Z7mapCalcPiiS_, .-_Z7mapCalcPiiS_
	.globl	_Z42__device_stub__Z20process_image_kernelPhS_PhS_
	.type	_Z42__device_stub__Z20process_image_kernelPhS_PhS_, @function
_Z42__device_stub__Z20process_image_kernelPhS_PhS_:
.LFB2058:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z20process_image_kernelPhS_(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L29:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2058:
	.size	_Z42__device_stub__Z20process_image_kernelPhS_PhS_, .-_Z42__device_stub__Z20process_image_kernelPhS_PhS_
	.globl	_Z20process_image_kernelPhS_
	.type	_Z20process_image_kernelPhS_, @function
_Z20process_image_kernelPhS_:
.LFB2059:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z20process_image_kernelPhS_PhS_
	.cfi_endproc
.LFE2059:
	.size	_Z20process_image_kernelPhS_, .-_Z20process_image_kernelPhS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/ShayAgros/cuda_ca1/master/hw1.cu"
.LC4:
	.string	"Cuda failure %s:%d: '%s'\n"
.LC5:
	.string	"\n=== CPU ===\n"
.LC6:
	.string	"total time %f [msec]\n"
.LC7:
	.string	"\n=== GPU Task Serial ===\n"
.LC8:
	.string	"total time %f [msec]  distance from baseline %lld (should be zero)\n"
.LC9:
	.string	"\n=== GPU Bulk ===\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	xorl	%edx, %edx
	movl	$32768000, %esi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$104, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rdi
	call	cudaHostAlloc@PLT
	testl	%eax, %eax
	je	.L35
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	movl	$175, %ecx
	movq	%rax, %r8
	jmp	.L59
.L35:
	leaq	24(%rsp), %rdi
	xorl	%edx, %edx
	movl	$32768000, %esi
	call	cudaHostAlloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L36
	call	cudaGetErrorString@PLT
	movl	$176, %ecx
	movq	%rax, %r8
.L59:
	movl	$2, %edi
	leaq	.LC3(%rip), %rdx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L36:
	leaq	32(%rsp), %rdi
	xorl	%edx, %edx
	movl	$32768000, %esi
	call	cudaHostAlloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L37
	call	cudaGetErrorString@PLT
	movl	$177, %ecx
	movq	%rax, %r8
	jmp	.L59
.L37:
	leaq	40(%rsp), %rdi
	xorl	%edx, %edx
	movl	$32768000, %esi
	call	cudaHostAlloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L38
	call	cudaGetErrorString@PLT
	movl	$178, %ecx
	movq	%rax, %r8
	jmp	.L59
.L38:
	xorl	%edi, %edi
	xorl	%ebx, %ebx
	movl	$256, %ebp
	call	srand@PLT
.L39:
	call	rand@PLT
	cltd
	idivl	%ebp
	movq	16(%rsp), %rax
	movb	%dl, (%rax,%rbx)
	incq	%rbx
	cmpq	$32768000, %rbx
	jne	.L39
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
	call	_ZL13get_time_msecv
	movsd	%xmm0, (%rsp)
.L40:
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	addq	%rbx, %rsi
	addq	%rbx, %rdi
	addq	$65536, %rbx
	call	_Z13process_imagePhS_
	cmpq	$32768000, %rbx
	jne	.L40
	call	_ZL13get_time_msecv
	subsd	(%rsp), %xmm0
	leaq	.LC6(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	leaq	48(%rsp), %r12
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r12, %rdi
	movl	$65536, %esi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L41
	call	cudaGetErrorString@PLT
	movl	$208, %ecx
	movq	%rax, %r8
	jmp	.L59
.L41:
	leaq	56(%rsp), %rbp
	movl	$65536, %esi
	movq	%rbp, %rdi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L42
	call	cudaGetErrorString@PLT
	movl	$209, %ecx
	movq	%rax, %r8
	jmp	.L59
.L42:
	call	_ZL13get_time_msecv
	xorl	%ebx, %ebx
	movabsq	$4294967552, %r13
	movsd	%xmm0, (%rsp)
.L47:
	movq	16(%rsp), %rsi
	movq	48(%rsp), %rdi
	movl	$4, %ecx
	movl	$65536, %edx
	addq	%rbx, %rsi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L43
	call	cudaGetErrorString@PLT
	movl	$216, %ecx
	movq	%rax, %r8
	jmp	.L59
.L43:
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r13, %rdx
	movl	$1, %ecx
	movabsq	$4294967297, %rdi
	movl	$1, %esi
	movq	%r13, 76(%rsp)
	movl	$1, 84(%rsp)
	movq	%rdi, 64(%rsp)
	movl	$1, 72(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L44
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdi
	call	_Z42__device_stub__Z20process_image_kernelPhS_PhS_
.L44:
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L45
	call	cudaGetErrorString@PLT
	movl	$220, %ecx
	movq	%rax, %r8
	jmp	.L59
.L45:
	movq	32(%rsp), %rdi
	movq	56(%rsp), %rsi
	movl	$4, %ecx
	movl	$65536, %edx
	addq	%rbx, %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L46
	call	cudaGetErrorString@PLT
	movl	$222, %ecx
	movq	%rax, %r8
	jmp	.L59
.L46:
	addq	$65536, %rbx
	cmpq	$32768000, %rbx
	jne	.L47
	movq	48(%rsp), %rdi
	leaq	.LC8(%rip), %rbx
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	call	_ZL13get_time_msecv
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z33distance_sqr_between_image_arraysPhS_
	subsd	(%rsp), %xmm0
	movq	%rbx, %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	movb	$1, %al
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r12, %rdi
	movl	$32768000, %esi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L48
	call	cudaGetErrorString@PLT
	movl	$235, %ecx
	movq	%rax, %r8
	jmp	.L59
.L48:
	movq	%rbp, %rdi
	movl	$32768000, %esi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L49
	call	cudaGetErrorString@PLT
	movl	$236, %ecx
	movq	%rax, %r8
	jmp	.L59
.L49:
	call	_ZL13get_time_msecv
	movq	48(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	$4, %ecx
	movl	$32768000, %edx
	movsd	%xmm0, (%rsp)
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L50
	call	cudaGetErrorString@PLT
	movl	$240, %ecx
	movq	%rax, %r8
	jmp	.L59
.L50:
	movl	$16777217, %edx
	movl	$1073741949, %edi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	salq	$8, %rdx
	salq	$2, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdx, 76(%rsp)
	movl	$1, 84(%rsp)
	movq	%rdi, 64(%rsp)
	movl	$1, 72(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L51
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdi
	call	_Z42__device_stub__Z20process_image_kernelPhS_PhS_
.L51:
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L52
	call	cudaGetErrorString@PLT
	movl	$244, %ecx
	movq	%rax, %r8
	jmp	.L59
.L52:
	movq	40(%rsp), %rdi
	movq	56(%rsp), %rsi
	movl	$4, %ecx
	movl	$32768000, %edx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L53
	call	cudaGetErrorString@PLT
	movl	$247, %ecx
	movq	%rax, %r8
	jmp	.L59
.L53:
	call	_ZL13get_time_msecv
	movq	48(%rsp), %rdi
	movsd	%xmm0, 8(%rsp)
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z33distance_sqr_between_image_arraysPhS_
	movsd	8(%rsp), %xmm0
	subsd	(%rsp), %xmm0
	movq	%rbx, %rsi
	movq	%rax, %rdx
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L54
	call	__stack_chk_fail@PLT
.L54:
	addq	$104, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z20process_image_kernelPhS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z20process_image_kernelPhS_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2061:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1083129856
	.align 8
.LC1:
	.long	-755914244
	.long	1062232653
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC2:
	.long	1132396544
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

/* compile with: nvcc -O3 hw1.cu -o hw1 */

#include <stdio.h>
#include <hip/hip_runtime.h>
#include <sys/time.h>

///////////////////////////////////////////////// DO NOT CHANGE ///////////////////////////////////////
#define IMG_HEIGHT 256
#define IMG_WIDTH 256
//#define N_IMAGES 10000
#define N_IMAGES 500

#define NUM_THREADS 256


typedef unsigned char uchar;

#define CUDA_CHECK(f) do {                                                                  \
    hipError_t e = f;                                                                      \
    if (e != hipSuccess) {                                                                 \
        printf("Cuda failure %s:%d: '%s'\n", __FILE__, __LINE__, hipGetErrorString(e));    \
        exit(1);                                                                            \
    }                                                                                       \
} while (0)

#define SQR(a) ((a) * (a))

void process_image(uchar *img_in, uchar *img_out) {
    int histogram[256] = { 0 };
    for (int i = 0; i < IMG_WIDTH * IMG_HEIGHT; i++) {
        histogram[img_in[i]]++;
    }

    int cdf[256] = { 0 };
    int hist_sum = 0;
    for (int i = 0; i < 256; i++) {
        hist_sum += histogram[i];
        cdf[i] = hist_sum;
    }

    int cdf_min = 0;
    for (int i = 0; i < 256; i++) {
        if (cdf[i] != 0) {
            cdf_min = cdf[i];
            break;
        }
    }

    uchar map[256] = { 0 };
    for (int i = 0; i < 256; i++) {
        int map_value = (float)(cdf[i] - cdf_min) / (IMG_WIDTH * IMG_HEIGHT - cdf_min) * 255;
        map[i] = (uchar)map_value;
    }

    for (int i = 0; i < IMG_WIDTH * IMG_HEIGHT; i++) {
        img_out[i] = map[img_in[i]];
    }
}

double static inline get_time_msec(void) {
    struct timeval t;
    gettimeofday(&t, NULL);
    return t.tv_sec * 1e+3 + t.tv_usec * 1e-3;
}

long long int distance_sqr_between_image_arrays(uchar *img_arr1, uchar *img_arr2) {
    long long int distance_sqr = 0;
    for (int i = 0; i < N_IMAGES * IMG_WIDTH * IMG_HEIGHT; i++) {
        distance_sqr += SQR(img_arr1[i] - img_arr2[i]);
    }
    return distance_sqr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

__device__ int arr_min(int arr[], int arr_size) {//return the min
	int tid = threadIdx.x;
	int half_length = arr_size / 2;

	while (half_length >= 1) {
		for (int i = tid; i < half_length; i += blockDim.x) {
			if(arr[tid + i] < arr[i]) arr[i] = arr[tid + i];
		}
		__syncthreads();
		half_length /= 2;
	}
    return arr[0]; //TODO
}

// this function implements the Kiggle-Stone algorithm
__device__ void prefix_sum(int arr[], int arr_size, int histogram[]) {

	int tbsize = blockDim.x;
	int tid = threadIdx.x;
	int inc;
 
 	for (int stride = 1; stride < tbsize; stride *= 2) {
	
		if (tid >= arr_size)
			continue;

 		if (tid >= stride) {
 		inc = arr[tid - stride];
 		}
 		__syncthreads();

 		if (tid >= stride) {
			arr[tid] += inc;
 		}

 		__syncthreads();
 	}

    return;
}

__device__ void mapCalc(int map[], int min, int cdf[]) {
	int id = threadIdx.x;
	int map_value = ((double)(cdf[id] - min)/(IMG_WIDTH * IMG_HEIGHT - min)) * 255;
    map[id] = map_value;
}

__global__ void process_image_kernel(uchar *in, uchar *out) {

    __shared__ int l_histogram[256];
    __shared__ int l_cdf[256];
    __shared__ int map[256];
	int tid = threadIdx.x;
	int bid = blockIdx.x;
	int tbsize = blockDim.x;

	// zero histogram
	l_histogram[tid] = 0;

	__syncthreads();

	for(int i = tid; i < IMG_WIDTH * IMG_HEIGHT; i += tbsize)
		atomicAdd(&l_histogram[in[(IMG_WIDTH * IMG_HEIGHT)*bid + i]], 1);

	__syncthreads();

	// prepare the cdf array in advance
	l_cdf[tid] = l_histogram[tid];

	__syncthreads();

	prefix_sum(l_cdf, 256, l_histogram);

	__syncthreads();

    int min = arr_min(l_histogram, 256);

	__syncthreads();

	mapCalc(map, min, l_cdf);

	__syncthreads();
   

    for(int i = tid; i < IMG_WIDTH * IMG_HEIGHT; i += tbsize) {
		out[(IMG_WIDTH * IMG_HEIGHT)*bid + i] =
			map[in[(IMG_WIDTH * IMG_HEIGHT)*bid + i]];
    }

	__syncthreads();

    return ; //TODO
}

int main() {
///////////////////////////////////////////////// DO NOT CHANGE ///////////////////////////////////////
    uchar *images_in;
    uchar *images_out_cpu; //output of CPU computation. In CPU memory.
    uchar *images_out_gpu_serial; //output of GPU task serial computation. In CPU memory.
    uchar *images_out_gpu_bulk; //output of GPU bulk computation. In CPU memory.
    CUDA_CHECK( hipHostAlloc(&images_in, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );
    CUDA_CHECK( hipHostAlloc(&images_out_cpu, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );
    CUDA_CHECK( hipHostAlloc(&images_out_gpu_serial, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );
    CUDA_CHECK( hipHostAlloc(&images_out_gpu_bulk, N_IMAGES * IMG_HEIGHT * IMG_WIDTH, 0) );

    /* instead of loading real images, we'll load the arrays with random data */
    srand(0);
    for (long long int i = 0; i < N_IMAGES * IMG_WIDTH * IMG_HEIGHT; i++) {
        images_in[i] = rand() % 256;
    }

    double t_start, t_finish;

    // CPU computation. For reference. Do not change
    printf("\n=== CPU ===\n");
    t_start = get_time_msec();
    for (int i = 0; i < N_IMAGES; i++) {
        uchar *img_in = &images_in[i * IMG_WIDTH * IMG_HEIGHT];
        uchar *img_out = &images_out_cpu[i * IMG_WIDTH * IMG_HEIGHT];
		process_image(img_in, img_out);
    }
    t_finish = get_time_msec();
    printf("total time %f [msec]\n", t_finish - t_start);

    long long int distance_sqr;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
	uchar *image_in;
	uchar *image_out;

    // GPU task serial computation
    printf("\n=== GPU Task Serial ===\n"); //Do not change

    //TODO: allocate GPU memory for a single input image and a single output image
    CUDA_CHECK( hipMalloc((void **)&image_in, IMG_HEIGHT * IMG_WIDTH) );
    CUDA_CHECK( hipMalloc((void **)&image_out, IMG_HEIGHT * IMG_WIDTH) );

    t_start = get_time_msec(); //Do not change

    //TODO: in a for loop:
    for (int i=0; i < N_IMAGES; i++) {
		// Copying src image from the input images
		CUDA_CHECK(hipMemcpy(image_in, &images_in[i * IMG_WIDTH*IMG_HEIGHT], IMG_WIDTH*IMG_HEIGHT, hipMemcpyDefault));

		process_image_kernel <<< 1, NUM_THREADS >>> (image_in, image_out);  

		CUDA_CHECK(hipDeviceSynchronize());

		CUDA_CHECK(hipMemcpy(&images_out_gpu_serial[i * IMG_HEIGHT*IMG_WIDTH], image_out, IMG_WIDTH*IMG_HEIGHT, hipMemcpyDefault));
    }

	hipFree(image_in);
	hipFree(image_out);

    t_finish = get_time_msec(); //Do not change
    distance_sqr = distance_sqr_between_image_arrays(images_out_cpu, images_out_gpu_serial); // Do not change
    printf("total time %f [msec]  distance from baseline %lld (should be zero)\n", t_finish - t_start, distance_sqr); //Do not change

    // GPU bulk
    printf("\n=== GPU Bulk ===\n"); //Do not change
    //TODO: allocate GPU memory for a all input images and all output images
	CUDA_CHECK( hipMalloc((void **)&image_in, N_IMAGES * IMG_HEIGHT * IMG_WIDTH) );
    CUDA_CHECK( hipMalloc((void **)&image_out, N_IMAGES * IMG_HEIGHT * IMG_WIDTH) );

    //TODO: copy all input images from images_in to the GPU memory you allocated
    t_start = get_time_msec(); //Do not change
	CUDA_CHECK(hipMemcpy(image_in, images_in, N_IMAGES*IMG_WIDTH*IMG_HEIGHT, hipMemcpyDefault));

    //TODO: invoke a kernel with N_IMAGES threadblocks, each working on a different image
	process_image_kernel <<< N_IMAGES, NUM_THREADS >>> (image_in, image_out);
	CUDA_CHECK(hipDeviceSynchronize());

    //TODO: copy output images from GPU memory to images_out_gpu_bulk
	CUDA_CHECK(hipMemcpy(images_out_gpu_bulk, image_out, N_IMAGES*IMG_WIDTH*IMG_HEIGHT, hipMemcpyDefault));

    t_finish = get_time_msec(); //Do not change

	hipFree(image_in);
	hipFree(image_out);

    distance_sqr = distance_sqr_between_image_arrays(images_out_cpu, images_out_gpu_bulk); // Do not change
    printf("total time %f [msec]  distance from baseline %lld (should be zero)\n", t_finish - t_start, distance_sqr); //Do not chhange

    return 0;
}

	.text
	.file	"hw1.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z13process_imagePhS_
.LCPI0_0:
	.long	0x437f0000                      # float 255
	.text
	.globl	_Z13process_imagePhS_
	.type	_Z13process_imagePhS_,@function
_Z13process_imagePhS_:                  # @_Z13process_imagePhS_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$2312, %rsp                     # imm = 0x908
	.cfi_def_cfa_offset 2352
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movq	%rdi, %r14
	leaq	1280(%rsp), %rdi
	xorl	%r15d, %r15d
	movl	$1024, %edx                     # imm = 0x400
	xorl	%esi, %esi
	callq	memset@PLT
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	movzbl	(%r14,%r15), %eax
	incl	1280(%rsp,%rax,4)
	incq	%r15
	cmpq	$65536, %r15                    # imm = 0x10000
	jne	.LBB0_1
# %bb.2:
	leaq	256(%rsp), %rdi
	xorl	%r15d, %r15d
	movl	$1024, %edx                     # imm = 0x400
	xorl	%esi, %esi
	callq	memset@PLT
	xorl	%eax, %eax
.LBB0_3:                                # =>This Inner Loop Header: Depth=1
	addl	1280(%rsp,%r15,4), %eax
	movl	%eax, 256(%rsp,%r15,4)
	incq	%r15
	cmpq	$256, %r15                      # imm = 0x100
	jne	.LBB0_3
# %bb.4:                                # %.preheader33.preheader
	xorl	%eax, %eax
.LBB0_5:                                # %.preheader33
                                        # =>This Inner Loop Header: Depth=1
	movl	256(%rsp,%rax,4), %ebp
	testl	%ebp, %ebp
	jne	.LBB0_7
# %bb.6:                                # %.preheader33
                                        #   in Loop: Header=BB0_5 Depth=1
	leaq	1(%rax), %rcx
	cmpq	$255, %rax
	movq	%rcx, %rax
	jne	.LBB0_5
.LBB0_7:
	movq	%rsp, %rdi
	xorl	%r15d, %r15d
	movl	$256, %edx                      # imm = 0x100
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$65536, %eax                    # imm = 0x10000
	subl	%ebp, %eax
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
.LBB0_8:                                # =>This Inner Loop Header: Depth=1
	movl	256(%rsp,%r15,4), %eax
	subl	%ebp, %eax
	xorps	%xmm2, %xmm2
	cvtsi2ss	%eax, %xmm2
	divss	%xmm0, %xmm2
	mulss	%xmm1, %xmm2
	cvttss2si	%xmm2, %eax
	movb	%al, (%rsp,%r15)
	incq	%r15
	cmpq	$256, %r15                      # imm = 0x100
	jne	.LBB0_8
# %bb.9:                                # %.preheader.preheader
	xorl	%eax, %eax
.LBB0_10:                               # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%r14,%rax), %ecx
	movb	(%rsp,%rcx), %cl
	movb	%cl, (%rbx,%rax)
	incq	%rax
	cmpq	$65536, %rax                    # imm = 0x10000
	jne	.LBB0_10
# %bb.11:
	addq	$2312, %rsp                     # imm = 0x908
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z13process_imagePhS_, .Lfunc_end0-_Z13process_imagePhS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z33distance_sqr_between_image_arraysPhS_ # -- Begin function _Z33distance_sqr_between_image_arraysPhS_
	.type	_Z33distance_sqr_between_image_arraysPhS_,@function
_Z33distance_sqr_between_image_arraysPhS_: # @_Z33distance_sqr_between_image_arraysPhS_
	.cfi_startproc
# %bb.0:
	xorl	%ecx, %ecx
	xorl	%eax, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movzbl	(%rdi,%rcx), %edx
	movzbl	(%rsi,%rcx), %r8d
	subl	%r8d, %edx
	imull	%edx, %edx
	addq	%rdx, %rax
	incq	%rcx
	cmpq	$32768000, %rcx                 # imm = 0x1F40000
	jne	.LBB1_1
# %bb.2:
	retq
.Lfunc_end1:
	.size	_Z33distance_sqr_between_image_arraysPhS_, .Lfunc_end1-_Z33distance_sqr_between_image_arraysPhS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z35__device_stub__process_image_kernelPhS_ # -- Begin function _Z35__device_stub__process_image_kernelPhS_
	.type	_Z35__device_stub__process_image_kernelPhS_,@function
_Z35__device_stub__process_image_kernelPhS_: # @_Z35__device_stub__process_image_kernelPhS_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%rsp, %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z20process_image_kernelPhS_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z35__device_stub__process_image_kernelPhS_, .Lfunc_end2-_Z35__device_stub__process_image_kernelPhS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x3f50624dd2f1a9fc              # double 0.001
.LCPI3_1:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rdi
	movl	$32768000, %esi                 # imm = 0x1F40000
	xorl	%edx, %edx
	callq	hipHostAlloc
	testl	%eax, %eax
	jne	.LBB3_1
# %bb.3:
	leaq	32(%rsp), %rdi
	movl	$32768000, %esi                 # imm = 0x1F40000
	xorl	%edx, %edx
	callq	hipHostAlloc
	testl	%eax, %eax
	jne	.LBB3_4
# %bb.5:
	leaq	48(%rsp), %rdi
	movl	$32768000, %esi                 # imm = 0x1F40000
	xorl	%edx, %edx
	callq	hipHostAlloc
	testl	%eax, %eax
	jne	.LBB3_6
# %bb.7:
	leaq	40(%rsp), %rdi
	movl	$32768000, %esi                 # imm = 0x1F40000
	xorl	%edx, %edx
	callq	hipHostAlloc
	testl	%eax, %eax
	jne	.LBB3_8
# %bb.9:
	xorl	%ebx, %ebx
	xorl	%edi, %edi
	callq	srand
.LBB3_10:                               # =>This Inner Loop Header: Depth=1
	callq	rand
	movq	24(%rsp), %rcx
	movb	%al, (%rcx,%rbx)
	incq	%rbx
	cmpq	$32768000, %rbx                 # imm = 0x1F40000
	jne	.LBB3_10
# %bb.11:
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%r14d, %r14d
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm0
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%rbx), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	movsd	%xmm0, 72(%rsp)                 # 8-byte Spill
.LBB3_12:                               # =>This Inner Loop Header: Depth=1
	movq	24(%rsp), %rdi
	addq	%r14, %rdi
	movq	32(%rsp), %rsi
	addq	%r14, %rsi
	callq	_Z13process_imagePhS_
	addq	$65536, %r14                    # imm = 0x10000
	cmpq	$32768000, %r14                 # imm = 0x1F40000
	jne	.LBB3_12
# %bb.13:
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	mulsd	.LCPI3_1(%rip), %xmm0
	addsd	72(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm1
	mulsd	.LCPI3_1(%rip), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%rbx), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	16(%rsp), %xmm0                 # 8-byte Folded Reload
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	leaq	8(%rsp), %rdi
	movl	$65536, %esi                    # imm = 0x10000
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_14
# %bb.15:
	movq	%rsp, %rdi
	movl	$65536, %esi                    # imm = 0x10000
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_16
# %bb.22:
	movabsq	$4294967552, %rbx               # imm = 0x100000100
	xorl	%r15d, %r15d
	leaq	56(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r14), %xmm0
	mulsd	.LCPI3_1(%rip), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r14), %xmm1
	mulsd	.LCPI3_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 16(%rsp)                 # 8-byte Spill
	leaq	-255(%rbx), %r14
.LBB3_23:                               # =>This Inner Loop Header: Depth=1
	movq	8(%rsp), %rdi
	movq	24(%rsp), %rsi
	addq	%r15, %rsi
	movl	$65536, %edx                    # imm = 0x10000
	movl	$4, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_24
# %bb.25:                               #   in Loop: Header=BB3_23 Depth=1
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_27
# %bb.26:                               #   in Loop: Header=BB3_23 Depth=1
	movq	8(%rsp), %rdi
	movq	(%rsp), %rsi
	callq	_Z35__device_stub__process_image_kernelPhS_
.LBB3_27:                               #   in Loop: Header=BB3_23 Depth=1
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB3_28
# %bb.29:                               #   in Loop: Header=BB3_23 Depth=1
	movq	48(%rsp), %rdi
	addq	%r15, %rdi
	movq	(%rsp), %rsi
	movl	$65536, %edx                    # imm = 0x10000
	movl	$4, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_30
# %bb.17:                               #   in Loop: Header=BB3_23 Depth=1
	addq	$65536, %r15                    # imm = 0x10000
	cmpq	$32768000, %r15                 # imm = 0x1F40000
	jne	.LBB3_23
# %bb.18:
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	xorl	%r15d, %r15d
	leaq	56(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r14), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r14), %xmm1
	mulsd	.LCPI3_0(%rip), %xmm1
	movq	32(%rsp), %rax
	movq	48(%rsp), %rcx
	xorl	%esi, %esi
.LBB3_19:                               # =>This Inner Loop Header: Depth=1
	movzbl	(%rax,%r15), %edx
	movzbl	(%rcx,%r15), %edi
	subl	%edi, %edx
	imull	%edx, %edx
	addq	%rdx, %rsi
	incq	%r15
	cmpq	$32768000, %r15                 # imm = 0x1F40000
	jne	.LBB3_19
# %bb.20:                               # %_Z33distance_sqr_between_image_arraysPhS_.exit
	mulsd	.LCPI3_1(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	16(%rsp), %xmm0                 # 8-byte Folded Reload
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	leaq	8(%rsp), %rdi
	movl	$32768000, %esi                 # imm = 0x1F40000
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_21
# %bb.31:
	movq	%rsp, %rdi
	movl	$32768000, %esi                 # imm = 0x1F40000
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_32
# %bb.33:
	leaq	56(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r14), %r15
	movq	8(%r14), %r14
	movq	8(%rsp), %rdi
	movq	24(%rsp), %rsi
	movl	$32768000, %edx                 # imm = 0x1F40000
	movl	$4, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_34
# %bb.35:
	leaq	244(%rbx), %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_37
# %bb.36:
	movq	8(%rsp), %rdi
	movq	(%rsp), %rsi
	callq	_Z35__device_stub__process_image_kernelPhS_
.LBB3_37:
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB3_38
# %bb.39:
	movq	40(%rsp), %rdi
	movq	(%rsp), %rsi
	movl	$32768000, %edx                 # imm = 0x1F40000
	movl	$4, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_40
# %bb.41:
	xorl	%r12d, %r12d
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rbx), %r13
	movq	8(%rbx), %rbx
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rax
	movq	40(%rsp), %rcx
	xorl	%esi, %esi
.LBB3_42:                               # =>This Inner Loop Header: Depth=1
	movzbl	(%rax,%r12), %edx
	movzbl	(%rcx,%r12), %edi
	subl	%edi, %edx
	imull	%edx, %edx
	addq	%rdx, %rsi
	incq	%r12
	cmpq	$32768000, %r12                 # imm = 0x1F40000
	jne	.LBB3_42
# %bb.43:                               # %_Z33distance_sqr_between_image_arraysPhS_.exit91
	cvtsi2sd	%r13, %xmm1
	cvtsi2sd	%rbx, %xmm0
	movsd	.LCPI3_1(%rip), %xmm4           # xmm4 = mem[0],zero
	mulsd	%xmm4, %xmm1
	movsd	.LCPI3_0(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm2, %xmm0
	addsd	%xmm1, %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sd	%r15, %xmm1
	cvtsi2sd	%r14, %xmm3
	mulsd	%xmm4, %xmm1
	mulsd	%xmm2, %xmm3
	addsd	%xmm1, %xmm3
	subsd	%xmm3, %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$80, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB3_30:
	.cfi_def_cfa_offset 128
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$222, %edx
	jmp	.LBB3_2
.LBB3_28:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$220, %edx
	jmp	.LBB3_2
.LBB3_24:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$216, %edx
.LBB3_2:
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.LBB3_1:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$175, %edx
	jmp	.LBB3_2
.LBB3_4:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$176, %edx
	jmp	.LBB3_2
.LBB3_6:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$177, %edx
	jmp	.LBB3_2
.LBB3_8:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$178, %edx
	jmp	.LBB3_2
.LBB3_14:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$208, %edx
	jmp	.LBB3_2
.LBB3_16:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$209, %edx
	jmp	.LBB3_2
.LBB3_21:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$235, %edx
	jmp	.LBB3_2
.LBB3_32:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$236, %edx
	jmp	.LBB3_2
.LBB3_34:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$240, %edx
	jmp	.LBB3_2
.LBB3_38:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$244, %edx
	jmp	.LBB3_2
.LBB3_40:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$247, %edx
	jmp	.LBB3_2
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z20process_image_kernelPhS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB5_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z20process_image_kernelPhS_,@object # @_Z20process_image_kernelPhS_
	.section	.rodata,"a",@progbits
	.globl	_Z20process_image_kernelPhS_
	.p2align	3, 0x0
_Z20process_image_kernelPhS_:
	.quad	_Z35__device_stub__process_image_kernelPhS_
	.size	_Z20process_image_kernelPhS_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Cuda failure %s:%d: '%s'\n"
	.size	.L.str, 26

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/ShayAgros/cuda_ca1/master/hw1.hip"
	.size	.L.str.1, 96

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"total time %f [msec]\n"
	.size	.L.str.3, 22

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"total time %f [msec]  distance from baseline %lld (should be zero)\n"
	.size	.L.str.5, 68

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z20process_image_kernelPhS_"
	.size	.L__unnamed_1, 29

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"\n=== CPU ==="
	.size	.Lstr, 13

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\n=== GPU Task Serial ==="
	.size	.Lstr.1, 25

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\n=== GPU Bulk ==="
	.size	.Lstr.2, 18

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z35__device_stub__process_image_kernelPhS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z20process_image_kernelPhS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z20process_image_kernelPhS_ ; -- Begin function _Z20process_image_kernelPhS_
	.globl	_Z20process_image_kernelPhS_
	.p2align	8
	.type	_Z20process_image_kernelPhS_,@function
_Z20process_image_kernelPhS_:           ; @_Z20process_image_kernelPhS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s5, s[0:1], 0x1c
	s_load_b128 s[0:3], s[0:1], 0x0
	v_lshl_or_b32 v4, s15, 16, v0
	v_dual_mov_b32 v1, 0 :: v_dual_mov_b32 v6, 1
	v_lshlrev_b32_e32 v3, 2, v0
	s_mov_b32 s6, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_ashrrev_i32_e32 v5, 31, v4
	ds_store_b32 v3, v1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_b32 s4, s5, 0xffff
	v_add_co_u32 v1, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v5, vcc_lo
	s_mov_b64 s[0:1], 0
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instid1(SALU_CYCLE_1)
	v_add_co_u32 v7, vcc_lo, v1, s0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v8, vcc_lo, s1, v2, vcc_lo
	s_add_u32 s0, s0, s4
	s_addc_u32 s1, s1, 0
	global_load_u8 v7, v[7:8], off
	s_waitcnt vmcnt(0)
	v_lshlrev_b32_e32 v7, 2, v7
	ds_add_u32 v7, v6
	v_add_nc_u32_e32 v7, s0, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_lt_u32_e32 vcc_lo, 0xffff, v7
	s_or_b32 s6, vcc_lo, s6
	s_and_not1_b32 exec_lo, exec_lo, s6
	s_cbranch_execnz .LBB0_1
; %bb.2:
	s_or_b32 exec_lo, exec_lo, s6
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v7, v3
	v_cmp_lt_u16_e64 s0, s5, 2
	v_add_nc_u32_e32 v6, 0x800, v3
	s_delay_alu instid0(VALU_DEP_2)
	s_and_b32 vcc_lo, exec_lo, s0
	s_waitcnt lgkmcnt(0)
	ds_store_b32 v3, v7 offset:2048
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_vccnz .LBB0_11
; %bb.3:                                ; %.lr.ph.i
	v_cmp_gt_u32_e32 vcc_lo, 0x100, v0
	s_mov_b32 s1, 1
                                        ; implicit-def: $vgpr7
.LBB0_4:                                ; =>This Inner Loop Header: Depth=1
	s_and_saveexec_b32 s5, vcc_lo
	s_cbranch_execz .LBB0_10
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=1
	v_cmp_le_u32_e64 s0, s1, v0
	s_delay_alu instid0(VALU_DEP_1)
	s_and_saveexec_b32 s6, s0
	s_cbranch_execz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_4 Depth=1
	v_subrev_nc_u32_e32 v7, s1, v0
	s_delay_alu instid0(VALU_DEP_1)
	v_lshlrev_b32_e32 v7, 2, v7
	ds_load_b32 v7, v7 offset:2048
.LBB0_7:                                ;   in Loop: Header=BB0_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s6
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s6, s0
	s_cbranch_execz .LBB0_9
; %bb.8:                                ;   in Loop: Header=BB0_4 Depth=1
	ds_load_b32 v8, v6
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v8, v8, v7
	ds_store_b32 v6, v8
.LBB0_9:                                ;   in Loop: Header=BB0_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s6
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_10:                               ; %Flow70
                                        ;   in Loop: Header=BB0_4 Depth=1
	s_or_b32 exec_lo, exec_lo, s5
	s_lshl_b32 s1, s1, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_u32 s1, s4
	s_cbranch_scc1 .LBB0_4
.LBB0_11:                               ; %_Z10prefix_sumPiiS_.exit
	v_lshlrev_b32_e32 v7, 3, v0
	s_lshl_b32 s0, s4, 2
	s_movk_i32 s1, 0x80
	s_barrier
	buffer_gl0_inv
.LBB0_12:                               ; %.preheader.i
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_14 Depth 2
	s_mov_b32 s5, exec_lo
	v_cmpx_gt_u32_e64 s1, v0
	s_cbranch_execz .LBB0_17
; %bb.13:                               ; %.lr.ph.i24.preheader
                                        ;   in Loop: Header=BB0_12 Depth=1
	v_mov_b32_e32 v8, v0
	s_mov_b32 s6, 0
	s_mov_b32 s7, 0
.LBB0_14:                               ; %.lr.ph.i24
                                        ;   Parent Loop BB0_12 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_add_nc_u32_e32 v10, s6, v7
	v_add_nc_u32_e32 v9, s6, v3
	s_mov_b32 s8, exec_lo
	ds_load_b32 v10, v10
	ds_load_b32 v11, v9
	s_waitcnt lgkmcnt(0)
	v_cmpx_lt_i32_e64 v10, v11
	s_cbranch_execz .LBB0_16
; %bb.15:                               ;   in Loop: Header=BB0_14 Depth=2
	ds_store_b32 v9, v10
.LBB0_16:                               ;   in Loop: Header=BB0_14 Depth=2
	s_or_b32 exec_lo, exec_lo, s8
	v_add_nc_u32_e32 v8, s4, v8
	s_add_i32 s6, s6, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_le_i32_e32 vcc_lo, s1, v8
	s_or_b32 s7, vcc_lo, s7
	s_and_not1_b32 exec_lo, exec_lo, s7
	s_cbranch_execnz .LBB0_14
.LBB0_17:                               ; %Flow69
                                        ;   in Loop: Header=BB0_12 Depth=1
	s_or_b32 exec_lo, exec_lo, s5
	s_lshr_b32 s5, s1, 1
	s_cmp_lt_u32 s1, 2
	s_mov_b32 s1, s5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_12
; %bb.18:                               ; %_Z7arr_minPii.exit
	v_mov_b32_e32 v7, 0
	s_mov_b32 s1, 0
	ds_load_b32 v7, v7
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v6, v6
	v_sub_nc_u32_e32 v8, 0x10000, v7
	s_waitcnt lgkmcnt(0)
	v_sub_nc_u32_e32 v6, v6, v7
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cvt_f64_i32_e32 v[8:9], v8
	v_cvt_f64_i32_e32 v[6:7], v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_scale_f64 v[10:11], null, v[8:9], v[8:9], v[6:7]
	v_div_scale_f64 v[16:17], vcc_lo, v[6:7], v[8:9], v[6:7]
	v_rcp_f64_e32 v[12:13], v[10:11]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[14:15], -v[10:11], v[12:13], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[12:13], v[12:13], v[14:15], v[12:13]
	v_fma_f64 v[14:15], -v[10:11], v[12:13], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[12:13], v[12:13], v[14:15], v[12:13]
	v_mul_f64 v[14:15], v[16:17], v[12:13]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[10:11], -v[10:11], v[14:15], v[16:17]
	v_div_fmas_f64 v[10:11], v[10:11], v[12:13], v[14:15]
	v_add_co_u32 v4, vcc_lo, s2, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s3, v5, vcc_lo
	s_mov_b64 s[2:3], 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fixup_f64 v[6:7], v[10:11], v[8:9], v[6:7]
	v_mul_f64 v[6:7], 0x406fe000, v[6:7]
	s_delay_alu instid0(VALU_DEP_1)
	v_cvt_i32_f64_e32 v6, v[6:7]
	ds_store_b32 v3, v6 offset:1024
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_19:                               ; =>This Inner Loop Header: Depth=1
	v_add_co_u32 v6, vcc_lo, v1, s2
	v_add_co_ci_u32_e32 v7, vcc_lo, s3, v2, vcc_lo
	global_load_u8 v3, v[6:7], off
	v_add_co_u32 v6, vcc_lo, v4, s2
	s_add_u32 s2, s2, s4
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v7, s2, v0
	v_cmp_lt_u32_e64 s0, 0xffff, v7
	v_add_co_ci_u32_e32 v7, vcc_lo, s3, v5, vcc_lo
	s_addc_u32 s3, s3, 0
	s_delay_alu instid0(VALU_DEP_2)
	s_or_b32 s1, s0, s1
	s_waitcnt vmcnt(0)
	v_lshlrev_b32_e32 v3, 2, v3
	ds_load_b32 v3, v3 offset:1024
	s_waitcnt lgkmcnt(0)
	global_store_b8 v[6:7], v3, off
	s_and_not1_b32 exec_lo, exec_lo, s1
	s_cbranch_execnz .LBB0_19
; %bb.20:
	s_or_b32 exec_lo, exec_lo, s1
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z20process_image_kernelPhS_
		.amdhsa_group_segment_fixed_size 3072
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 18
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z20process_image_kernelPhS_, .Lfunc_end0-_Z20process_image_kernelPhS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 952
; NumSgprs: 18
; NumVgprs: 18
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 3072 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 18
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 3072
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z20process_image_kernelPhS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z20process_image_kernelPhS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     18
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
