#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <sys/time.h>
#include <png.h>
#include <math.h>

#define FILTER_RADIUS     3                                                         // M
#define FILTER_AREA       ( (2 * FILTER_RADIUS + 1) * (2 * FILTER_RADIUS + 1) )     // (N ^ 2)
#define INV_FILTER_AREA   ( 1.0f / (float)FILTER_AREA )                             // (1 / r ^ 2)
#define WEIGHT_THRESHOLD  0.02f
#define LERP_THRESHOLD    0.66f
#define NOISE_VAL         0.32f
#define NOISE             ( 1.0f / (NOISE_VAL * NOISE_VAL) )                        // (1 / h ^ 2)
#define LERPC             0.16f


int width, height;
png_byte color_type;
png_byte bit_depth;
png_bytep *row_pointers = NULL;

__host__
void read_png_file(char *filename) 
{
  FILE *fp = fopen(filename, "rb");

  png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if(!png) abort();

  png_infop info = png_create_info_struct(png);
  if(!info) abort();

  if(setjmp(png_jmpbuf(png))) abort();

  png_init_io(png, fp);

  png_read_info(png, info);

  width      = png_get_image_width(png, info);
  height     = png_get_image_height(png, info);
  color_type = png_get_color_type(png, info);
  bit_depth  = png_get_bit_depth(png, info);

  // Read any color_type into 8bit depth, RGBA format.
  // See http://www.libpng.org/pub/png/libpng-manual.txt

  if(bit_depth == 16)
    png_set_strip_16(png);

  if(color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_palette_to_rgb(png);

  // PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
  if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
    png_set_expand_gray_1_2_4_to_8(png);

  if(png_get_valid(png, info, PNG_INFO_tRNS))
    png_set_tRNS_to_alpha(png);

  // These color_type don't have an alpha channel then fill it with 0xff.
  if(color_type == PNG_COLOR_TYPE_RGB ||
     color_type == PNG_COLOR_TYPE_GRAY ||
     color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_filler(png, 0xFF, PNG_FILLER_AFTER);

  if(color_type == PNG_COLOR_TYPE_GRAY ||
     color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
    png_set_gray_to_rgb(png);

  png_read_update_info(png, info);

  if (row_pointers) abort();

  row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
  for(int y = 0; y < height; y++) {
    row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png,info));
  }

  png_read_image(png, row_pointers);

  fclose(fp);

  png_destroy_read_struct(&png, &info, NULL);
}

__host__
void write_png_file(char *filename) 
{
  FILE *fp = fopen(filename, "wb");
  if(!fp) abort();

  png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!png) abort();

  png_infop info = png_create_info_struct(png);
  if (!info) abort();

  if (setjmp(png_jmpbuf(png))) abort();

  png_init_io(png, fp);

  // Output is 8bit depth, RGBA format.
  png_set_IHDR(
    png,
    info,
    width, height,
    8,
    PNG_COLOR_TYPE_RGBA,
    PNG_INTERLACE_NONE,
    PNG_COMPRESSION_TYPE_DEFAULT,
    PNG_FILTER_TYPE_DEFAULT
  );
  png_write_info(png, info);

  // To remove the alpha channel for PNG_COLOR_TYPE_RGB format,
  // Use png_set_filler().
  //png_set_filler(png, 0, PNG_FILLER_AFTER);

  if (!row_pointers) abort();

  png_write_image(png, row_pointers);
  png_write_end(png, NULL);

  for(int y = 0; y < height; y++) {
    free(row_pointers[y]);
  }
  free(row_pointers);

  fclose(fp);

  png_destroy_write_struct(&png, &info);
}

__host__
void image_to_array(png_byte *image)
{
  for(int y = 0; y < height; y++) {
    png_byte *row = row_pointers[y];
    for(int x = 0; x < width; x++) {
      png_byte *px = &(row[x * 4]);
      image[(y * width + x) * 4] = px[0];
      image[(y * width + x) * 4 + 1] = px[1];
      image[(y * width + x) * 4 + 2] = px[2];
      image[(y * width + x) * 4 + 3] = px[3];
    }
  }
}

__host__
void array_to_image(png_byte *image)
{
  for(int y = 0; y < height; y++) {
    png_byte *row = row_pointers[y];
    for(int x = 0; x < width; x++) {
      png_byte *px = &(row[x * 4]);
      px[0] = image[(y * width + x) * 4];
      px[1] = image[(y * width + x) * 4 + 1];
      px[2] = image[(y * width + x) * 4 + 2];
      px[3] = image[(y * width + x) * 4 + 3];
    }
  }
}

__device__ float colorDistance(float4 a, float4 b)
{
  return (
      (b.x - a.x) / 255.0f * (b.x - a.x) / 255.0f + (b.y - a.y) / 255.0f * (b.y - a.y) / 255.0f + (b.z - a.z) / 255.0f * (b.z - a.z) / 255.0f
  );
}

__device__ float pixelDistance(float x, float y)
{
  return (
      x * x + y * y
  );
}

__device__ float lerpf(float a, float b, float c){
  return a + (b - a) * c;
}

__host__
void errorexit(const char *s) 
{
    printf("\n%s",s);	
    exit(EXIT_FAILURE);	 	
}

__host__
long getTime(struct timeval *start,struct timeval *stop) 
{
  long time=1000000*(stop->tv_sec-start->tv_sec)+stop->tv_usec-start->tv_usec;

  return time / 1000;
}

__global__ 
void knn_filter(png_byte *img, png_byte *img_out, int width, int height)
{
    const int idx = blockIdx.x * blockDim.x + threadIdx.x;
    const int idy = blockIdx.y * blockDim.y + threadIdx.y;

    if (idx >= width || idy >= height) return;

    // Normalized counter for the weight threshold
    float fCount = 0;

    // Total sum of pixel weights
    float sum_weights = 0;

    // Result accumulator
    float3 color = {0, 0, 0};

    // Center of the filter
    int pos = (idy * width + idx) * 4;
    float4 color_center = {(float)img[pos], (float)img[pos + 1], (float)img[pos + 2], (float)img[pos + 3]};

    for (int y = -FILTER_RADIUS; y <= FILTER_RADIUS; y++)
    {
      for (int x = -FILTER_RADIUS; x <= FILTER_RADIUS; x++)
      {
        if (idy + y < 0 || idy + y >= height || idx + x < 0 || idx + x >= width)
          continue;

        int curr_pos = ((idy + y) * width + (idx + x)) * 4;
        float4 color_xy = {(float)img[curr_pos], (float)img[curr_pos + 1], (float)img[curr_pos + 2], (float)img[curr_pos + 3]};

        float pixel_distance = pixelDistance((float)x, (float)y);

        float color_distance = colorDistance(color_center, color_xy);
        
        // Denosing
        float weight_xy = expf(-(pixel_distance * INV_FILTER_AREA + color_distance * NOISE));
        
        color.x += color_xy.x * weight_xy;
        color.y += color_xy.y * weight_xy;
        color.z += color_xy.z * weight_xy;

        sum_weights += weight_xy;
        fCount += (weight_xy > WEIGHT_THRESHOLD) ? INV_FILTER_AREA : 0;
      }
    }

    // Normalize result color
    sum_weights = 1.0f / sum_weights;
    color.x *= sum_weights;
    color.y *= sum_weights;
    color.z *= sum_weights;

    float lerpQ = (fCount > LERP_THRESHOLD) ? LERPC : 1.0f - LERPC;

    color.x = lerpf(color.x, color_center.x, lerpQ);
    color.y = lerpf(color.y, color_center.y, lerpQ);
    color.z = lerpf(color.z, color_center.z, lerpQ);

    // Result to memory
    img_out[pos] = (png_byte)color.x;
    img_out[pos+1] = (png_byte)color.y;
    img_out[pos+2] = (png_byte)color.z;
    img_out[pos+3] = img[pos+3];
}

int main(int argc,char **argv) {

  struct timeval start, stop;
  gettimeofday(&start, NULL);

  png_byte *host_img;

  // read png file to an array
  read_png_file(argv[1]);

  // allocate memory
  int size = width * height * sizeof(png_byte) * 4;

  cudaMallocHost((void**)&host_img, size);
  
  png_byte *device_img = NULL;
  if (cudaSuccess!=cudaMalloc((void **)&device_img, size))
    errorexit("Error allocating memory on the GPU 1");

  png_byte *device_output = NULL;
  if (cudaSuccess!=cudaMalloc((void **)&device_output, size))
    errorexit("Error allocating memory on the GPU 2");

  // copy image array to allocated memory
  image_to_array(host_img);

  // copy image array to device
  if (cudaSuccess!=cudaMemcpy(device_img, host_img, size, cudaMemcpyHostToDevice))
    errorexit("Error copying data to device");

  // kernel block/thread configuration
  dim3 threadsPerBlock(8, 8);
  dim3 numBlocks(ceil((float)width / threadsPerBlock.x), ceil((float)height / threadsPerBlock.y));

  // kernel
  knn_filter<<<numBlocks,threadsPerBlock>>>(device_img, device_output, width, height);
  if (cudaSuccess!=cudaGetLastError())
    errorexit("Error during kernel launch");
  
  cudaDeviceSynchronize();

  // copy memory back to host
  if (cudaSuccess!=cudaMemcpy(host_img, device_output, size, cudaMemcpyDeviceToHost))
      errorexit("Error copying results to host");
  
  // prepare array to write png
  array_to_image(host_img);

  // release resources
  if (cudaSuccess!=cudaFreeHost(host_img))
    errorexit("Error when deallocating space on host");
  if (cudaSuccess!=cudaFree(device_img))
    errorexit("Error when deallocating space on the GPU");
  if (cudaSuccess!=cudaFree(device_output))
    errorexit("Error when deallocating output space on the GPU");

  // write array to new png file
  write_png_file(argv[2]);
  gettimeofday(&stop, NULL);
  long timeElapsed = getTime(&start, &stop);

    // printf("Size: %dx%d Time elapsed: %ld ms\n", width, height, timeElapsed);

  FILE *pFile;
  pFile = fopen("knn_tests.txt", "a");
  fprintf(pFile, "Size: %dx%d Time elapsed: %ld ms\n", width, height, timeElapsed);
  fclose(pFile);
  printf("Success.\n");

  return 0;
}

	.file	"tmpxft_0032dca8_00000000-6_knn_image_denoiser.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2039:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2039:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"1.6.43"
	.text
	.globl	_Z13read_png_filePc
	.type	_Z13read_png_filePc, @function
_Z13read_png_filePc:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC0(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	xorl	%ecx, %ecx
	xorl	%edx, %edx
	xorl	%esi, %esi
	leaq	.LC1(%rip), %rdi
	movq	%rax, 8(%rsp)
	call	png_create_read_struct@PLT
	movq	%rax, 24(%rsp)
	testq	%rax, %rax
	jne	.L3
.L4:
	call	abort@PLT
.L3:
	movq	%rax, %rdi
	call	png_create_info_struct@PLT
	movq	%rax, 32(%rsp)
	testq	%rax, %rax
	je	.L4
	movq	24(%rsp), %rdi
	movq	__longjmp_chk@GOTPCREL(%rip), %rsi
	movl	$200, %edx
	call	png_set_longjmp_fn@PLT
	movq	%rax, %rdi
	call	_setjmp@PLT
	endbr64
	testl	%eax, %eax
	jne	.L4
	movq	8(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	png_init_io@PLT
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	png_read_info@PLT
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	png_get_image_width@PLT
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	%eax, width(%rip)
	call	png_get_image_height@PLT
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	%eax, height(%rip)
	call	png_get_color_type@PLT
	movq	24(%rsp), %rdi
	movq	32(%rsp), %rsi
	movb	%al, color_type(%rip)
	call	png_get_bit_depth@PLT
	movq	24(%rsp), %rdi
	movb	%al, bit_depth(%rip)
	cmpb	$16, %al
	je	.L6
.L9:
	cmpb	$3, color_type(%rip)
	movq	24(%rsp), %rdi
	jne	.L8
	jmp	.L26
.L6:
	call	png_set_strip_16@PLT
	jmp	.L9
.L26:
	call	png_set_palette_to_rgb@PLT
.L8:
	cmpb	$0, color_type(%rip)
	movq	24(%rsp), %rdi
	jne	.L11
	cmpb	$7, bit_depth(%rip)
	ja	.L11
	call	png_set_expand_gray_1_2_4_to_8@PLT
.L11:
	movq	24(%rsp), %rdi
	movq	32(%rsp), %rsi
	movl	$16, %edx
	call	png_get_valid@PLT
	movq	24(%rsp), %rdi
	testl	%eax, %eax
	je	.L16
	call	png_set_tRNS_to_alpha@PLT
.L16:
	movb	color_type(%rip), %al
	movq	24(%rsp), %rdi
	leal	-2(%rax), %edx
	cmpb	$1, %dl
	jbe	.L14
	testb	%al, %al
	jne	.L15
.L14:
	movl	$1, %edx
	movl	$255, %esi
	call	png_set_filler@PLT
.L15:
	movq	24(%rsp), %rdi
	testb	$-5, color_type(%rip)
	jne	.L18
	call	png_set_gray_to_rgb@PLT
.L18:
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	png_read_update_info@PLT
	cmpq	$0, row_pointers(%rip)
	jne	.L4
	movslq	height(%rip), %rdi
	xorl	%ebx, %ebx
	salq	$3, %rdi
	call	malloc@PLT
	movq	%rax, row_pointers(%rip)
.L20:
	cmpl	%ebx, height(%rip)
	movq	24(%rsp), %rdi
	jle	.L27
	movq	32(%rsp), %rsi
	call	png_get_rowbytes@PLT
	movq	%rax, %rdi
	movq	row_pointers(%rip), %rax
	leaq	(%rax,%rbx,8), %rbp
	incq	%rbx
	call	malloc@PLT
	movq	%rax, 0(%rbp)
	jmp	.L20
.L27:
	movq	row_pointers(%rip), %rsi
	call	png_read_image@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
	xorl	%edx, %edx
	leaq	32(%rsp), %rsi
	leaq	24(%rsp), %rdi
	call	png_destroy_read_struct@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$56, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z13read_png_filePc, .-_Z13read_png_filePc
	.section	.rodata.str1.1
.LC2:
	.string	"wb"
	.text
	.globl	_Z14write_png_filePc
	.type	_Z14write_png_filePc, @function
_Z14write_png_filePc:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	.LC2(%rip), %rsi
	subq	$48, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	jne	.L29
.L30:
	call	abort@PLT
.L29:
	leaq	.LC1(%rip), %rdi
	xorl	%ecx, %ecx
	xorl	%edx, %edx
	xorl	%esi, %esi
	call	png_create_write_struct@PLT
	movq	%rax, 24(%rsp)
	movq	%rax, %rdi
	testq	%rax, %rax
	je	.L30
	call	png_create_info_struct@PLT
	movq	%rax, 32(%rsp)
	testq	%rax, %rax
	je	.L30
	movq	24(%rsp), %rdi
	movq	__longjmp_chk@GOTPCREL(%rip), %rsi
	movl	$200, %edx
	call	png_set_longjmp_fn@PLT
	movq	%rax, %rdi
	call	_setjmp@PLT
	endbr64
	testl	%eax, %eax
	jne	.L30
	movq	8(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	png_init_io@PLT
	movl	height(%rip), %ecx
	movl	width(%rip), %edx
	movl	$6, %r9d
	pushq	%rax
	.cfi_def_cfa_offset 72
	movl	$8, %r8d
	pushq	$0
	.cfi_def_cfa_offset 80
	pushq	$0
	.cfi_def_cfa_offset 88
	pushq	$0
	.cfi_def_cfa_offset 96
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	call	png_set_IHDR@PLT
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 64
	call	png_write_info@PLT
	movq	row_pointers(%rip), %rsi
	testq	%rsi, %rsi
	je	.L30
	movq	24(%rsp), %rdi
	xorl	%ebx, %ebx
	call	png_write_image@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	png_write_end@PLT
.L32:
	cmpl	%ebx, height(%rip)
	movq	row_pointers(%rip), %rdi
	jle	.L37
	movq	(%rdi,%rbx,8), %rdi
	incq	%rbx
	call	free@PLT
	jmp	.L32
.L37:
	call	free@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
	leaq	32(%rsp), %rsi
	leaq	24(%rsp), %rdi
	call	png_destroy_write_struct@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$48, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z14write_png_filePc, .-_Z14write_png_filePc
	.globl	_Z14image_to_arrayPh
	.type	_Z14image_to_arrayPh, @function
_Z14image_to_arrayPh:
.LFB2029:
	.cfi_startproc
	endbr64
	movq	%rdi, %rcx
	xorl	%esi, %esi
.L39:
	cmpl	%esi, height(%rip)
	movl	%esi, %edi
	jle	.L43
	movq	row_pointers(%rip), %rax
	movq	(%rax,%rsi,8), %r8
	xorl	%eax, %eax
.L40:
	movl	width(%rip), %edx
	cmpl	%eax, %edx
	jle	.L44
	imull	%edi, %edx
	movb	(%r8,%rax,4), %r9b
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	%r9b, (%rcx,%rdx)
	movl	width(%rip), %edx
	movb	1(%r8,%rax,4), %r9b
	imull	%edi, %edx
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	%r9b, 1(%rcx,%rdx)
	movl	width(%rip), %edx
	movb	2(%r8,%rax,4), %r9b
	imull	%edi, %edx
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	%r9b, 2(%rcx,%rdx)
	movl	width(%rip), %edx
	movb	3(%r8,%rax,4), %r9b
	imull	%edi, %edx
	addl	%eax, %edx
	incq	%rax
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	%r9b, 3(%rcx,%rdx)
	jmp	.L40
.L44:
	incq	%rsi
	jmp	.L39
.L43:
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z14image_to_arrayPh, .-_Z14image_to_arrayPh
	.globl	_Z14array_to_imagePh
	.type	_Z14array_to_imagePh, @function
_Z14array_to_imagePh:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	%rdi, %rcx
	xorl	%esi, %esi
.L46:
	cmpl	%esi, height(%rip)
	movl	%esi, %edi
	jle	.L50
	movq	row_pointers(%rip), %rax
	movq	(%rax,%rsi,8), %r8
	xorl	%eax, %eax
.L47:
	movl	width(%rip), %edx
	cmpl	%eax, %edx
	jle	.L51
	imull	%edi, %edx
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	(%rcx,%rdx), %dl
	movb	%dl, (%r8,%rax,4)
	movl	width(%rip), %edx
	imull	%edi, %edx
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	1(%rcx,%rdx), %dl
	movb	%dl, 1(%r8,%rax,4)
	movl	width(%rip), %edx
	imull	%edi, %edx
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	2(%rcx,%rdx), %dl
	movb	%dl, 2(%r8,%rax,4)
	movl	width(%rip), %edx
	imull	%edi, %edx
	addl	%eax, %edx
	sall	$2, %edx
	movslq	%edx, %rdx
	movb	3(%rcx,%rdx), %dl
	movb	%dl, 3(%r8,%rax,4)
	incq	%rax
	jmp	.L47
.L51:
	incq	%rsi
	jmp	.L46
.L50:
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z14array_to_imagePh, .-_Z14array_to_imagePh
	.globl	_Z13colorDistance6float4S_
	.type	_Z13colorDistance6float4S_, @function
_Z13colorDistance6float4S_:
.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	_Z13colorDistance6float4S_, .-_Z13colorDistance6float4S_
	.globl	_Z13pixelDistanceff
	.type	_Z13pixelDistanceff, @function
_Z13pixelDistanceff:
.LFB2032:
	.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
.LFE2032:
	.size	_Z13pixelDistanceff, .-_Z13pixelDistanceff
	.globl	_Z5lerpffff
	.type	_Z5lerpffff, @function
_Z5lerpffff:
.LFB2033:
	.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
.LFE2033:
	.size	_Z5lerpffff, .-_Z5lerpffff
	.section	.rodata.str1.1
.LC3:
	.string	"\n%s"
	.text
	.globl	_Z9errorexitPKc
	.type	_Z9errorexitPKc, @function
_Z9errorexitPKc:
.LFB2034:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	leaq	.LC3(%rip), %rsi
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rdx
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2034:
	.size	_Z9errorexitPKc, .-_Z9errorexitPKc
	.globl	_Z7getTimeP7timevalS0_
	.type	_Z7getTimeP7timevalS0_, @function
_Z7getTimeP7timevalS0_:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	(%rsi), %rax
	subq	(%rdi), %rax
	movl	$1000, %ecx
	imulq	$1000000, %rax, %rax
	addq	8(%rsi), %rax
	subq	8(%rdi), %rax
	cqto
	idivq	%rcx
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z7getTimeP7timevalS0_, .-_Z7getTimeP7timevalS0_
	.globl	_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii
	.type	_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii, @function
_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii:
.LFB2061:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L61
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z10knn_filterPhS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L61:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L63
	call	__stack_chk_fail@PLT
.L63:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2061:
	.size	_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii, .-_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii
	.globl	_Z10knn_filterPhS_ii
	.type	_Z10knn_filterPhS_ii, @function
_Z10knn_filterPhS_ii:
.LFB2062:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii
	.cfi_endproc
.LFE2062:
	.size	_Z10knn_filterPhS_ii, .-_Z10knn_filterPhS_ii
	.section	.rodata.str1.1
.LC4:
	.string	"Error allocating memory on the GPU 1"
.LC5:
	.string	"Error allocating memory on the GPU 2"
.LC6:
	.string	"Error copying data to device"
.LC8:
	.string	"Error during kernel launch"
.LC9:
	.string	"Error copying results to host"
.LC10:
	.string	"Error when deallocating space on host"
.LC11:
	.string	"Error when deallocating space on the GPU"
.LC12:
	.string	"Error when deallocating output space on the GPU"
.LC13:
	.string	"a"
.LC14:
	.string	"knn_tests.txt"
.LC15:
	.string	"Size: %dx%d Time elapsed: %ld ms\n"
.LC16:
	.string	"Success.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2036:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rsi, %rbp
	xorl	%esi, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$112, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	72(%rsp), %r12
	movq	%r12, %rdi
	call	gettimeofday@PLT
	movq	8(%rbp), %rdi
	call	_Z13read_png_filePc
	movl	width(%rip), %ebx
	leaq	24(%rsp), %rdi
	imull	height(%rip), %ebx
	sall	$2, %ebx
	movslq	%ebx, %rbx
	movq	%rbx, %rsi
	call	cudaMallocHost@PLT
	xorl	%edx, %edx
	leaq	32(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rdx, 32(%rsp)
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L67
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC4(%rip), %rdi
	je	.L87
	jmp	.L88
.L67:
	xorl	%eax, %eax
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rax, 40(%rsp)
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L69
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC5(%rip), %rdi
	je	.L87
	jmp	.L88
.L69:
	movq	24(%rsp), %rdi
	call	_Z14image_to_arrayPh
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L71
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC6(%rip), %rdi
	jne	.L88
.L87:
	call	_Z9errorexitPKc
.L71:
	cvtsi2ssl	height(%rip), %xmm0
	mulss	.LC7(%rip), %xmm0
	call	ceilf@PLT
	movss	%xmm0, 8(%rsp)
	cvtsi2ssl	width(%rip), %xmm0
	mulss	.LC7(%rip), %xmm0
	call	ceilf@PLT
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	cvttss2siq	%xmm0, %rax
	movss	8(%rsp), %xmm1
	movl	$1, %esi
	movabsq	$34359738376, %rdx
	movl	%eax, 60(%rsp)
	cvttss2siq	%xmm1, %rax
	movl	%eax, 64(%rsp)
	movq	60(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L73
	movl	height(%rip), %ecx
	movl	width(%rip), %edx
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z34__device_stub__Z10knn_filterPhS_iiPhS_ii
.L73:
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L74
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC8(%rip), %rdi
	je	.L87
	jmp	.L88
.L74:
	call	cudaDeviceSynchronize@PLT
	movq	40(%rsp), %rsi
	movq	24(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L76
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC9(%rip), %rdi
	je	.L87
	jmp	.L88
.L76:
	movq	24(%rsp), %rdi
	call	_Z14array_to_imagePh
	movq	24(%rsp), %rdi
	call	cudaFreeHost@PLT
	testl	%eax, %eax
	je	.L78
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC10(%rip), %rdi
	je	.L87
	jmp	.L88
.L78:
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	testl	%eax, %eax
	je	.L80
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC11(%rip), %rdi
	je	.L87
	jmp	.L88
.L80:
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	testl	%eax, %eax
	je	.L82
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC12(%rip), %rdi
	je	.L87
	jmp	.L88
.L82:
	movq	16(%rbp), %rdi
	leaq	88(%rsp), %rbx
	call	_Z14write_png_filePc
	xorl	%esi, %esi
	movq	%rbx, %rdi
	call	gettimeofday@PLT
	movq	%rbx, %rsi
	movq	%r12, %rdi
	call	_Z7getTimeP7timevalS0_
	leaq	.LC13(%rip), %rsi
	leaq	.LC14(%rip), %rdi
	movq	%rax, 8(%rsp)
	call	fopen@PLT
	movq	8(%rsp), %r9
	movl	width(%rip), %ecx
	movl	$2, %esi
	movl	height(%rip), %r8d
	movq	%rax, %rdi
	movq	%rax, %rbx
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rdx
	call	__fprintf_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC16(%rip), %rsi
	call	__printf_chk@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L84
.L88:
	call	__stack_chk_fail@PLT
.L84:
	addq	$112, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	main, .-main
	.section	.rodata.str1.1
.LC17:
	.string	"_Z10knn_filterPhS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2064:
	.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	.LC17(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10knn_filterPhS_ii(%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
.LFE2064:
	.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
	.globl	row_pointers
	.bss
	.align 8
	.type	row_pointers, @object
	.size	row_pointers, 8
row_pointers:
	.zero	8
	.globl	bit_depth
	.type	bit_depth, @object
	.size	bit_depth, 1
bit_depth:
	.zero	1
	.globl	color_type
	.type	color_type, @object
	.size	color_type, 1
color_type:
	.zero	1
	.globl	height
	.align 4
	.type	height, @object
	.size	height, 4
height:
	.zero	4
	.globl	width
	.align 4
	.type	width, @object
	.size	width, 4
width:
	.zero	4
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC7:
	.long	1040187392
	.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:

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

#define FILTER_RADIUS     3                                                         // M
#define FILTER_AREA       ( (2 * FILTER_RADIUS + 1) * (2 * FILTER_RADIUS + 1) )     // (N ^ 2)
#define INV_FILTER_AREA   ( 1.0f / (float)FILTER_AREA )                             // (1 / r ^ 2)
#define WEIGHT_THRESHOLD  0.02f
#define LERP_THRESHOLD    0.66f
#define NOISE_VAL         0.32f
#define NOISE             ( 1.0f / (NOISE_VAL * NOISE_VAL) )                        // (1 / h ^ 2)
#define LERPC             0.16f


int width, height;
png_byte color_type;
png_byte bit_depth;
png_bytep *row_pointers = NULL;

__host__
void read_png_file(char *filename) 
{
  FILE *fp = fopen(filename, "rb");

  png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if(!png) abort();

  png_infop info = png_create_info_struct(png);
  if(!info) abort();

  if(setjmp(png_jmpbuf(png))) abort();

  png_init_io(png, fp);

  png_read_info(png, info);

  width      = png_get_image_width(png, info);
  height     = png_get_image_height(png, info);
  color_type = png_get_color_type(png, info);
  bit_depth  = png_get_bit_depth(png, info);

  // Read any color_type into 8bit depth, RGBA format.
  // See http://www.libpng.org/pub/png/libpng-manual.txt

  if(bit_depth == 16)
    png_set_strip_16(png);

  if(color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_palette_to_rgb(png);

  // PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
  if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
    png_set_expand_gray_1_2_4_to_8(png);

  if(png_get_valid(png, info, PNG_INFO_tRNS))
    png_set_tRNS_to_alpha(png);

  // These color_type don't have an alpha channel then fill it with 0xff.
  if(color_type == PNG_COLOR_TYPE_RGB ||
     color_type == PNG_COLOR_TYPE_GRAY ||
     color_type == PNG_COLOR_TYPE_PALETTE)
    png_set_filler(png, 0xFF, PNG_FILLER_AFTER);

  if(color_type == PNG_COLOR_TYPE_GRAY ||
     color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
    png_set_gray_to_rgb(png);

  png_read_update_info(png, info);

  if (row_pointers) abort();

  row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
  for(int y = 0; y < height; y++) {
    row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png,info));
  }

  png_read_image(png, row_pointers);

  fclose(fp);

  png_destroy_read_struct(&png, &info, NULL);
}

__host__
void write_png_file(char *filename) 
{
  FILE *fp = fopen(filename, "wb");
  if(!fp) abort();

  png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (!png) abort();

  png_infop info = png_create_info_struct(png);
  if (!info) abort();

  if (setjmp(png_jmpbuf(png))) abort();

  png_init_io(png, fp);

  // Output is 8bit depth, RGBA format.
  png_set_IHDR(
    png,
    info,
    width, height,
    8,
    PNG_COLOR_TYPE_RGBA,
    PNG_INTERLACE_NONE,
    PNG_COMPRESSION_TYPE_DEFAULT,
    PNG_FILTER_TYPE_DEFAULT
  );
  png_write_info(png, info);

  // To remove the alpha channel for PNG_COLOR_TYPE_RGB format,
  // Use png_set_filler().
  //png_set_filler(png, 0, PNG_FILLER_AFTER);

  if (!row_pointers) abort();

  png_write_image(png, row_pointers);
  png_write_end(png, NULL);

  for(int y = 0; y < height; y++) {
    free(row_pointers[y]);
  }
  free(row_pointers);

  fclose(fp);

  png_destroy_write_struct(&png, &info);
}

__host__
void image_to_array(png_byte *image)
{
  for(int y = 0; y < height; y++) {
    png_byte *row = row_pointers[y];
    for(int x = 0; x < width; x++) {
      png_byte *px = &(row[x * 4]);
      image[(y * width + x) * 4] = px[0];
      image[(y * width + x) * 4 + 1] = px[1];
      image[(y * width + x) * 4 + 2] = px[2];
      image[(y * width + x) * 4 + 3] = px[3];
    }
  }
}

__host__
void array_to_image(png_byte *image)
{
  for(int y = 0; y < height; y++) {
    png_byte *row = row_pointers[y];
    for(int x = 0; x < width; x++) {
      png_byte *px = &(row[x * 4]);
      px[0] = image[(y * width + x) * 4];
      px[1] = image[(y * width + x) * 4 + 1];
      px[2] = image[(y * width + x) * 4 + 2];
      px[3] = image[(y * width + x) * 4 + 3];
    }
  }
}

__device__ float colorDistance(float4 a, float4 b)
{
  return (
      (b.x - a.x) / 255.0f * (b.x - a.x) / 255.0f + (b.y - a.y) / 255.0f * (b.y - a.y) / 255.0f + (b.z - a.z) / 255.0f * (b.z - a.z) / 255.0f
  );
}

__device__ float pixelDistance(float x, float y)
{
  return (
      x * x + y * y
  );
}

__device__ float lerpf(float a, float b, float c){
  return a + (b - a) * c;
}

__host__
void errorexit(const char *s) 
{
    printf("\n%s",s);	
    exit(EXIT_FAILURE);	 	
}

__host__
long getTime(struct timeval *start,struct timeval *stop) 
{
  long time=1000000*(stop->tv_sec-start->tv_sec)+stop->tv_usec-start->tv_usec;

  return time / 1000;
}

__global__ 
void knn_filter(png_byte *img, png_byte *img_out, int width, int height)
{
    const int idx = blockIdx.x * blockDim.x + threadIdx.x;
    const int idy = blockIdx.y * blockDim.y + threadIdx.y;

    if (idx >= width || idy >= height) return;

    // Normalized counter for the weight threshold
    float fCount = 0;

    // Total sum of pixel weights
    float sum_weights = 0;

    // Result accumulator
    float3 color = {0, 0, 0};

    // Center of the filter
    int pos = (idy * width + idx) * 4;
    float4 color_center = {(float)img[pos], (float)img[pos + 1], (float)img[pos + 2], (float)img[pos + 3]};

    for (int y = -FILTER_RADIUS; y <= FILTER_RADIUS; y++)
    {
      for (int x = -FILTER_RADIUS; x <= FILTER_RADIUS; x++)
      {
        if (idy + y < 0 || idy + y >= height || idx + x < 0 || idx + x >= width)
          continue;

        int curr_pos = ((idy + y) * width + (idx + x)) * 4;
        float4 color_xy = {(float)img[curr_pos], (float)img[curr_pos + 1], (float)img[curr_pos + 2], (float)img[curr_pos + 3]};

        float pixel_distance = pixelDistance((float)x, (float)y);

        float color_distance = colorDistance(color_center, color_xy);
        
        // Denosing
        float weight_xy = expf(-(pixel_distance * INV_FILTER_AREA + color_distance * NOISE));
        
        color.x += color_xy.x * weight_xy;
        color.y += color_xy.y * weight_xy;
        color.z += color_xy.z * weight_xy;

        sum_weights += weight_xy;
        fCount += (weight_xy > WEIGHT_THRESHOLD) ? INV_FILTER_AREA : 0;
      }
    }

    // Normalize result color
    sum_weights = 1.0f / sum_weights;
    color.x *= sum_weights;
    color.y *= sum_weights;
    color.z *= sum_weights;

    float lerpQ = (fCount > LERP_THRESHOLD) ? LERPC : 1.0f - LERPC;

    color.x = lerpf(color.x, color_center.x, lerpQ);
    color.y = lerpf(color.y, color_center.y, lerpQ);
    color.z = lerpf(color.z, color_center.z, lerpQ);

    // Result to memory
    img_out[pos] = (png_byte)color.x;
    img_out[pos+1] = (png_byte)color.y;
    img_out[pos+2] = (png_byte)color.z;
    img_out[pos+3] = img[pos+3];
}

int main(int argc,char **argv) {

  struct timeval start, stop;
  gettimeofday(&start, NULL);

  png_byte *host_img;

  // read png file to an array
  read_png_file(argv[1]);

  // allocate memory
  int size = width * height * sizeof(png_byte) * 4;

  hipHostMalloc((void**)&host_img, size, hipHostMallocDefault);
  
  png_byte *device_img = NULL;
  if (hipSuccess!=hipMalloc((void **)&device_img, size))
    errorexit("Error allocating memory on the GPU 1");

  png_byte *device_output = NULL;
  if (hipSuccess!=hipMalloc((void **)&device_output, size))
    errorexit("Error allocating memory on the GPU 2");

  // copy image array to allocated memory
  image_to_array(host_img);

  // copy image array to device
  if (hipSuccess!=hipMemcpy(device_img, host_img, size, hipMemcpyHostToDevice))
    errorexit("Error copying data to device");

  // kernel block/thread configuration
  dim3 threadsPerBlock(8, 8);
  dim3 numBlocks(ceil((float)width / threadsPerBlock.x), ceil((float)height / threadsPerBlock.y));

  // kernel
  knn_filter<<<numBlocks,threadsPerBlock>>>(device_img, device_output, width, height);
  if (hipSuccess!=hipGetLastError())
    errorexit("Error during kernel launch");
  
  hipDeviceSynchronize();

  // copy memory back to host
  if (hipSuccess!=hipMemcpy(host_img, device_output, size, hipMemcpyDeviceToHost))
      errorexit("Error copying results to host");
  
  // prepare array to write png
  array_to_image(host_img);

  // release resources
  if (hipSuccess!=hipHostFree(host_img))
    errorexit("Error when deallocating space on host");
  if (hipSuccess!=hipFree(device_img))
    errorexit("Error when deallocating space on the GPU");
  if (hipSuccess!=hipFree(device_output))
    errorexit("Error when deallocating output space on the GPU");

  // write array to new png file
  write_png_file(argv[2]);
  gettimeofday(&stop, NULL);
  long timeElapsed = getTime(&start, &stop);

    // printf("Size: %dx%d Time elapsed: %ld ms\n", width, height, timeElapsed);

  FILE *pFile;
  pFile = fopen("knn_tests.txt", "a");
  fprintf(pFile, "Size: %dx%d Time elapsed: %ld ms\n", width, height, timeElapsed);
  fclose(pFile);
  printf("Success.\n");

  return 0;
}

	.text
	.file	"knn_image_denoiser.hip"
	.globl	_Z13read_png_filePc             # -- Begin function _Z13read_png_filePc
	.type	_Z13read_png_filePc,@function
_Z13read_png_filePc:                    # @_Z13read_png_filePc
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %r14
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	movl	%eax, %ecx
	movq	%rcx, %rsi
	movq	%rcx, %rdx
	callq	png_create_read_struct
	movq	%rax, 16(%rsp)
	testq	%rax, %rax
	je	.LBB0_24
# %bb.1:
	movq	%rax, %rbx
	movq	%rax, %rdi
	callq	png_create_info_struct
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	je	.LBB0_24
# %bb.2:
	movq	%rax, %r15
	movl	$longjmp, %esi
	movl	$200, %edx
	movq	%rbx, %rdi
	callq	png_set_longjmp_fn
	movq	%rax, %rdi
	callq	_setjmp
	testl	%eax, %eax
	jne	.LBB0_24
# %bb.3:
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	png_init_io
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_read_info
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_get_image_width
	movl	%eax, width(%rip)
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_get_image_height
	movl	%eax, height(%rip)
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_get_color_type
	movb	%al, color_type(%rip)
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_get_bit_depth
	movb	%al, bit_depth(%rip)
	cmpb	$16, %al
	jne	.LBB0_5
# %bb.4:
	movq	%rbx, %rdi
	callq	png_set_strip_16
.LBB0_5:
	movb	color_type(%rip), %cl
	movl	%ecx, %eax
	cmpb	$3, %cl
	jne	.LBB0_7
# %bb.6:
	movq	%rbx, %rdi
	callq	png_set_palette_to_rgb
	movb	color_type(%rip), %al
.LBB0_7:
	testb	%al, %al
	jne	.LBB0_10
# %bb.8:
	cmpb	$7, bit_depth(%rip)
	ja	.LBB0_10
# %bb.9:
	movq	%rbx, %rdi
	callq	png_set_expand_gray_1_2_4_to_8
.LBB0_10:
	movq	%rbx, %rdi
	movq	%r15, %rsi
	movl	$16, %edx
	callq	png_get_valid
	testl	%eax, %eax
	je	.LBB0_12
# %bb.11:
	movq	%rbx, %rdi
	callq	png_set_tRNS_to_alpha
.LBB0_12:
	movzbl	color_type(%rip), %ecx
	movl	%ecx, %eax
	cmpl	$3, %ecx
	ja	.LBB0_15
# %bb.13:
	cmpl	$1, %ecx
	je	.LBB0_15
# %bb.14:
	movq	%rbx, %rdi
	movl	$255, %esi
	movl	$1, %edx
	callq	png_set_filler
	movb	color_type(%rip), %al
.LBB0_15:
	testb	$-5, %al
	jne	.LBB0_17
# %bb.16:
	movq	%rbx, %rdi
	callq	png_set_gray_to_rgb
.LBB0_17:
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_read_update_info
	cmpq	$0, row_pointers(%rip)
	jne	.LBB0_24
# %bb.18:
	movslq	height(%rip), %r12
	leaq	(,%r12,8), %rdi
	callq	malloc
	movq	%rax, row_pointers(%rip)
	testq	%r12, %r12
	jle	.LBB0_19
# %bb.22:                               # %.lr.ph.preheader
	xorl	%eax, %eax
                                        # kill: def $rax killed $eax
.LBB0_23:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rax, %r12
	movq	%rbx, %rdi
	movq	%r15, %rsi
	callq	png_get_rowbytes
	movq	%rax, %rdi
	callq	malloc
	movq	row_pointers(%rip), %rcx
	movq	%rax, (%rcx,%r12,8)
	incq	%r12
	movslq	height(%rip), %rcx
	movq	%r12, %rax
	cmpq	%rcx, %r12
	jl	.LBB0_23
# %bb.20:                               # %._crit_edge.loopexit
	movq	row_pointers(%rip), %rsi
	jmp	.LBB0_21
.LBB0_19:
	movq	%rax, %rsi
.LBB0_21:                               # %._crit_edge
	movq	%rbx, %rdi
	callq	png_read_image
	movq	%r14, %rdi
	callq	fclose
	xorl	%eax, %eax
	movl	%eax, %edx
	leaq	16(%rsp), %rdi
	leaq	8(%rsp), %rsi
	callq	png_destroy_read_struct
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_24:
	.cfi_def_cfa_offset 64
	callq	abort
.Lfunc_end0:
	.size	_Z13read_png_filePc, .Lfunc_end0-_Z13read_png_filePc
	.cfi_endproc
                                        # -- End function
	.globl	_Z14write_png_filePc            # -- Begin function _Z14write_png_filePc
	.type	_Z14write_png_filePc,@function
_Z14write_png_filePc:                   # @_Z14write_png_filePc
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.L.str.2, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_9
# %bb.1:
	movq	%rax, %rbx
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	movl	%eax, %ecx
	movq	%rcx, %rsi
	movq	%rcx, %rdx
	callq	png_create_write_struct
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	je	.LBB1_9
# %bb.2:
	movq	%rax, %r14
	movq	%rax, %rdi
	callq	png_create_info_struct
	movq	%rax, (%rsp)
	testq	%rax, %rax
	je	.LBB1_9
# %bb.3:
	movq	%rax, %r15
	movl	$longjmp, %esi
	movl	$200, %edx
	movq	%r14, %rdi
	callq	png_set_longjmp_fn
	movq	%rax, %rdi
	callq	_setjmp
	testl	%eax, %eax
	jne	.LBB1_9
# %bb.4:
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	png_init_io
	movl	width(%rip), %edx
	movl	height(%rip), %ecx
	subq	$8, %rsp
	.cfi_adjust_cfa_offset 8
	xorl	%eax, %eax
	movl	%eax, %r10d
	movq	%r14, %rdi
	movq	%r15, %rsi
	movl	$8, %r8d
	movl	$6, %r9d
	pushq	%r10
	.cfi_adjust_cfa_offset 8
	pushq	%r10
	.cfi_adjust_cfa_offset 8
                                        # kill: def $eax killed $eax def $rax
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	callq	png_set_IHDR
	addq	$32, %rsp
	.cfi_adjust_cfa_offset -32
	movq	%r14, %rdi
	movq	%r15, %rsi
	callq	png_write_info
	movq	row_pointers(%rip), %rsi
	testq	%rsi, %rsi
	je	.LBB1_9
# %bb.5:
	movq	%r14, %rdi
	callq	png_write_image
	xorl	%eax, %eax
	movl	%eax, %esi
	movq	%r14, %rdi
	callq	png_write_end
	cmpl	$0, height(%rip)
	jle	.LBB1_8
# %bb.6:                                # %.lr.ph.preheader
	xorl	%eax, %eax
                                        # kill: def $rax killed $eax
.LBB1_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rax, %r14
	movq	row_pointers(%rip), %rax
	movq	(%rax,%r14,8), %rdi
	callq	free
	incq	%r14
	movslq	height(%rip), %rcx
	movq	%r14, %rax
	cmpq	%rcx, %r14
	jl	.LBB1_7
.LBB1_8:                                # %._crit_edge
	movq	row_pointers(%rip), %rdi
	callq	free
	movq	%rbx, %rdi
	callq	fclose
	leaq	8(%rsp), %rdi
	movq	%rsp, %rsi
	callq	png_destroy_write_struct
	addq	$16, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB1_9:
	.cfi_def_cfa_offset 48
	callq	abort
.Lfunc_end1:
	.size	_Z14write_png_filePc, .Lfunc_end1-_Z14write_png_filePc
	.cfi_endproc
                                        # -- End function
	.globl	_Z14image_to_arrayPh            # -- Begin function _Z14image_to_arrayPh
	.type	_Z14image_to_arrayPh,@function
_Z14image_to_arrayPh:                   # @_Z14image_to_arrayPh
	.cfi_startproc
# %bb.0:
	movl	height(%rip), %edx
	testl	%edx, %edx
	jle	.LBB2_7
# %bb.1:                                # %.lr.ph27.preheader
	movl	width(%rip), %ecx
	xorl	%eax, %eax
.LBB2_2:                                # %.lr.ph27
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_4 Depth 2
	testl	%ecx, %ecx
	jle	.LBB2_6
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB2_2 Depth=1
	movq	row_pointers(%rip), %rdx
	movq	(%rdx,%rax,8), %rdx
	xorl	%esi, %esi
.LBB2_4:                                # %.lr.ph
                                        #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movb	(%rdx,%rsi,4), %r8b
	imull	%eax, %ecx
	addl	%esi, %ecx
	movslq	%ecx, %rcx
	movb	%r8b, (%rdi,%rcx,4)
	movb	1(%rdx,%rsi,4), %cl
	movl	width(%rip), %r8d
	imull	%eax, %r8d
	addl	%esi, %r8d
	shll	$2, %r8d
	movslq	%r8d, %r8
	movb	%cl, 1(%rdi,%r8)
	movb	2(%rdx,%rsi,4), %cl
	movl	width(%rip), %r8d
	imull	%eax, %r8d
	addl	%esi, %r8d
	shll	$2, %r8d
	movslq	%r8d, %r8
	movb	%cl, 2(%rdi,%r8)
	movb	3(%rdx,%rsi,4), %cl
	movl	width(%rip), %r8d
	imull	%eax, %r8d
	addl	%esi, %r8d
	shll	$2, %r8d
	movslq	%r8d, %r8
	movb	%cl, 3(%rdi,%r8)
	incq	%rsi
	movslq	width(%rip), %rcx
	cmpq	%rcx, %rsi
	jl	.LBB2_4
# %bb.5:                                # %._crit_edge.loopexit
                                        #   in Loop: Header=BB2_2 Depth=1
	movl	height(%rip), %edx
.LBB2_6:                                # %._crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	incq	%rax
	movslq	%edx, %rsi
	cmpq	%rsi, %rax
	jl	.LBB2_2
.LBB2_7:                                # %._crit_edge28
	retq
.Lfunc_end2:
	.size	_Z14image_to_arrayPh, .Lfunc_end2-_Z14image_to_arrayPh
	.cfi_endproc
                                        # -- End function
	.globl	_Z14array_to_imagePh            # -- Begin function _Z14array_to_imagePh
	.type	_Z14array_to_imagePh,@function
_Z14array_to_imagePh:                   # @_Z14array_to_imagePh
	.cfi_startproc
# %bb.0:
	movl	height(%rip), %edx
	testl	%edx, %edx
	jle	.LBB3_7
# %bb.1:                                # %.lr.ph27.preheader
	movl	width(%rip), %ecx
	xorl	%eax, %eax
.LBB3_2:                                # %.lr.ph27
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_4 Depth 2
	testl	%ecx, %ecx
	jle	.LBB3_6
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB3_2 Depth=1
	movq	row_pointers(%rip), %rdx
	movq	(%rdx,%rax,8), %rdx
	xorl	%esi, %esi
.LBB3_4:                                # %.lr.ph
                                        #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	imull	%eax, %ecx
	addl	%esi, %ecx
	movslq	%ecx, %rcx
	movb	(%rdi,%rcx,4), %cl
	movb	%cl, (%rdx,%rsi,4)
	movl	width(%rip), %ecx
	imull	%eax, %ecx
	addl	%esi, %ecx
	shll	$2, %ecx
	movslq	%ecx, %rcx
	movb	1(%rdi,%rcx), %cl
	movb	%cl, 1(%rdx,%rsi,4)
	movl	width(%rip), %ecx
	imull	%eax, %ecx
	addl	%esi, %ecx
	shll	$2, %ecx
	movslq	%ecx, %rcx
	movb	2(%rdi,%rcx), %cl
	movb	%cl, 2(%rdx,%rsi,4)
	movl	width(%rip), %ecx
	imull	%eax, %ecx
	addl	%esi, %ecx
	shll	$2, %ecx
	movslq	%ecx, %rcx
	movb	3(%rdi,%rcx), %cl
	movb	%cl, 3(%rdx,%rsi,4)
	incq	%rsi
	movslq	width(%rip), %rcx
	cmpq	%rcx, %rsi
	jl	.LBB3_4
# %bb.5:                                # %._crit_edge.loopexit
                                        #   in Loop: Header=BB3_2 Depth=1
	movl	height(%rip), %edx
.LBB3_6:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	incq	%rax
	movslq	%edx, %rsi
	cmpq	%rsi, %rax
	jl	.LBB3_2
.LBB3_7:                                # %._crit_edge28
	retq
.Lfunc_end3:
	.size	_Z14array_to_imagePh, .Lfunc_end3-_Z14array_to_imagePh
	.cfi_endproc
                                        # -- End function
	.globl	_Z9errorexitPKc                 # -- Begin function _Z9errorexitPKc
	.type	_Z9errorexitPKc,@function
_Z9errorexitPKc:                        # @_Z9errorexitPKc
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rsi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end4:
	.size	_Z9errorexitPKc, .Lfunc_end4-_Z9errorexitPKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z7getTimeP7timevalS0_          # -- Begin function _Z7getTimeP7timevalS0_
	.type	_Z7getTimeP7timevalS0_,@function
_Z7getTimeP7timevalS0_:                 # @_Z7getTimeP7timevalS0_
	.cfi_startproc
# %bb.0:
	movq	(%rsi), %rax
	subq	(%rdi), %rax
	imulq	$1000000, %rax, %rax            # imm = 0xF4240
	addq	8(%rsi), %rax
	subq	8(%rdi), %rax
	movabsq	$2361183241434822607, %rcx      # imm = 0x20C49BA5E353F7CF
	imulq	%rcx
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$7, %rdx
	addq	%rdx, %rax
	retq
.Lfunc_end5:
	.size	_Z7getTimeP7timevalS0_, .Lfunc_end5-_Z7getTimeP7timevalS0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z25__device_stub__knn_filterPhS_ii # -- Begin function _Z25__device_stub__knn_filterPhS_ii
	.type	_Z25__device_stub__knn_filterPhS_ii,@function
_Z25__device_stub__knn_filterPhS_ii:    # @_Z25__device_stub__knn_filterPhS_ii
	.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	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%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	$_Z10knn_filterPhS_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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_end6:
	.size	_Z25__device_stub__knn_filterPhS_ii, .Lfunc_end6-_Z25__device_stub__knn_filterPhS_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI7_0:
	.long	0x3e000000                      # float 0.125
	.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	%rbx
	.cfi_def_cfa_offset 32
	subq	$64, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	leaq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%rbx), %rdi
	callq	_Z13read_png_filePc
	movl	width(%rip), %eax
	imull	height(%rip), %eax
	shll	$2, %eax
	movslq	%eax, %r14
	leaq	8(%rsp), %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	24(%rsp), %rdi
	movq	$0, (%rdi)
	movq	%r14, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB7_1
# %bb.3:
	leaq	16(%rsp), %rdi
	movq	$0, (%rdi)
	movq	%r14, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB7_4
# %bb.5:
	movq	8(%rsp), %rdi
	callq	_Z14image_to_arrayPh
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB7_6
# %bb.7:
	cvtsi2ssl	width(%rip), %xmm0
	mulss	.LCPI7_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %r15
	xorps	%xmm0, %xmm0
	cvtsi2ssl	height(%rip), %xmm0
	mulss	.LCPI7_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %rdi
	movl	%r15d, %eax
	shlq	$32, %rdi
	orq	%rax, %rdi
	movabsq	$34359738376, %rdx              # imm = 0x800000008
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB7_9
# %bb.8:
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	width(%rip), %edx
	movl	height(%rip), %ecx
	callq	_Z25__device_stub__knn_filterPhS_ii
.LBB7_9:
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB7_10
# %bb.11:
	callq	hipDeviceSynchronize
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB7_12
# %bb.13:
	movq	8(%rsp), %rdi
	callq	_Z14array_to_imagePh
	movq	8(%rsp), %rdi
	callq	hipHostFree
	testl	%eax, %eax
	jne	.LBB7_14
# %bb.15:
	movq	24(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB7_16
# %bb.17:
	movq	16(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB7_18
# %bb.19:
	movq	16(%rbx), %rdi
	callq	_Z14write_png_filePc
	leaq	48(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rbx), %rax
	subq	32(%rsp), %rax
	imulq	$1000000, %rax, %rax            # imm = 0xF4240
	addq	8(%rbx), %rax
	subq	40(%rsp), %rax
	movabsq	$2361183241434822607, %rcx      # imm = 0x20C49BA5E353F7CF
	imulq	%rcx
	movq	%rdx, %rbx
	shrq	$63, %rbx
	sarq	$7, %rdx
	addq	%rdx, %rbx
	movl	$.L.str.12, %edi
	movl	$.L.str.13, %esi
	callq	fopen
	movq	%rax, %r14
	movl	width(%rip), %edx
	movl	height(%rip), %ecx
	movl	$.L.str.14, %esi
	movq	%rax, %rdi
	movq	%rbx, %r8
	xorl	%eax, %eax
	callq	fprintf
	movq	%r14, %rdi
	callq	fclose
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	addq	$64, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB7_1:
	.cfi_def_cfa_offset 96
	movl	$.L.str.3, %edi
	movl	$.L.str.4, %esi
	jmp	.LBB7_2
.LBB7_4:
	movl	$.L.str.3, %edi
	movl	$.L.str.5, %esi
	jmp	.LBB7_2
.LBB7_6:
	movl	$.L.str.3, %edi
	movl	$.L.str.6, %esi
	jmp	.LBB7_2
.LBB7_10:
	movl	$.L.str.3, %edi
	movl	$.L.str.7, %esi
	jmp	.LBB7_2
.LBB7_12:
	movl	$.L.str.3, %edi
	movl	$.L.str.8, %esi
	jmp	.LBB7_2
.LBB7_14:
	movl	$.L.str.3, %edi
	movl	$.L.str.9, %esi
	jmp	.LBB7_2
.LBB7_16:
	movl	$.L.str.3, %edi
	movl	$.L.str.10, %esi
	jmp	.LBB7_2
.LBB7_18:
	movl	$.L.str.3, %edi
	movl	$.L.str.11, %esi
.LBB7_2:
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end7:
	.size	main, .Lfunc_end7-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	.LBB8_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB8_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z10knn_filterPhS_ii, %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_end8:
	.size	__hip_module_ctor, .Lfunc_end8-__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	.LBB9_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
.LBB9_2:
	retq
.Lfunc_end9:
	.size	__hip_module_dtor, .Lfunc_end9-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	width,@object                   # @width
	.bss
	.globl	width
	.p2align	2, 0x0
width:
	.long	0                               # 0x0
	.size	width, 4

	.type	height,@object                  # @height
	.globl	height
	.p2align	2, 0x0
height:
	.long	0                               # 0x0
	.size	height, 4

	.type	color_type,@object              # @color_type
	.globl	color_type
color_type:
	.byte	0                               # 0x0
	.size	color_type, 1

	.type	bit_depth,@object               # @bit_depth
	.globl	bit_depth
bit_depth:
	.byte	0                               # 0x0
	.size	bit_depth, 1

	.type	row_pointers,@object            # @row_pointers
	.globl	row_pointers
	.p2align	3, 0x0
row_pointers:
	.quad	0
	.size	row_pointers, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"rb"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"1.6.43"
	.size	.L.str.1, 7

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"wb"
	.size	.L.str.2, 3

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\n%s"
	.size	.L.str.3, 4

	.type	_Z10knn_filterPhS_ii,@object    # @_Z10knn_filterPhS_ii
	.section	.rodata,"a",@progbits
	.globl	_Z10knn_filterPhS_ii
	.p2align	3, 0x0
_Z10knn_filterPhS_ii:
	.quad	_Z25__device_stub__knn_filterPhS_ii
	.size	_Z10knn_filterPhS_ii, 8

	.type	.L.str.4,@object                # @.str.4
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.4:
	.asciz	"Error allocating memory on the GPU 1"
	.size	.L.str.4, 37

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Error allocating memory on the GPU 2"
	.size	.L.str.5, 37

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Error copying data to device"
	.size	.L.str.6, 29

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Error during kernel launch"
	.size	.L.str.7, 27

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Error copying results to host"
	.size	.L.str.8, 30

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Error when deallocating space on host"
	.size	.L.str.9, 38

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Error when deallocating space on the GPU"
	.size	.L.str.10, 41

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Error when deallocating output space on the GPU"
	.size	.L.str.11, 48

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"knn_tests.txt"
	.size	.L.str.12, 14

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"a"
	.size	.L.str.13, 2

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Size: %dx%d Time elapsed: %ld ms\n"
	.size	.L.str.14, 34

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10knn_filterPhS_ii"
	.size	.L__unnamed_1, 21

	.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	"Success."
	.size	.Lstr, 9

	.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 longjmp
	.addrsig_sym _Z25__device_stub__knn_filterPhS_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z10knn_filterPhS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10knn_filterPhS_ii    ; -- Begin function _Z10knn_filterPhS_ii
	.globl	_Z10knn_filterPhS_ii
	.p2align	8
	.type	_Z10knn_filterPhS_ii,@function
_Z10knn_filterPhS_ii:                   ; @_Z10knn_filterPhS_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[8:9], s[0:1], 0x10
	v_and_b32_e32 v6, 0x3ff, v0
	v_bfe_u32 v0, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s2, 0xffff
	s_lshr_b32 s2, s2, 16
	s_mul_i32 s14, s14, s3
	v_mad_u64_u32 v[2:3], null, s15, s2, v[0:1]
	v_add_nc_u32_e32 v5, s14, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_gt_i32_e32 vcc_lo, s8, v5
	v_cmp_gt_i32_e64 s2, s9, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_10
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_mul_lo_u32 v0, v2, s8
	v_add_nc_u32_e32 v9, -3, v2
	v_dual_mov_b32 v11, 0 :: v_dual_mov_b32 v10, 0
	v_mov_b32_e32 v13, 0
	s_mov_b32 s1, -3
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v9, s8, v9
	s_lshl_b32 s2, s8, 2
	v_add_lshl_u32 v3, v0, v5, 2
	v_ashrrev_i32_e32 v4, 31, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add3_u32 v9, v6, v9, s14
	v_mov_b32_e32 v6, 0
	v_lshl_add_u32 v14, v9, 2, -12
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v3
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v4, vcc_lo
	s_clause 0x1
	global_load_u16 v7, v[0:1], off
	global_load_u8 v8, v[0:1], off offset:2
	s_waitcnt vmcnt(1)
	v_cvt_f32_ubyte0_e32 v9, v7
	v_cvt_f32_ubyte1_e32 v12, v7
	s_waitcnt vmcnt(0)
	v_cvt_f32_ubyte0_e32 v7, v8
	v_mov_b32_e32 v8, 0
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	v_cvt_f32_i32_e32 v16, s1
	v_add_nc_u32_e32 v15, s1, v2
	s_mov_b32 s10, -3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v16, v16, v16
	v_cmp_lt_i32_e32 vcc_lo, -1, v15
	v_cmp_gt_i32_e64 s0, s9, v15
	v_mov_b32_e32 v15, v14
	s_delay_alu instid0(VALU_DEP_2)
	s_and_b32 s3, vcc_lo, s0
.LBB0_3:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s11, s3
	s_cbranch_execz .LBB0_7
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=2
	v_add_nc_u32_e32 v17, s10, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cmp_lt_i32_e32 vcc_lo, -1, v17
	v_cmp_gt_i32_e64 s0, s8, v17
	s_and_b32 s0, vcc_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s12, s0
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_3 Depth=2
	v_ashrrev_i32_e32 v18, 31, v15
	v_add_co_u32 v17, vcc_lo, s4, v15
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v18, vcc_lo, s5, v18, vcc_lo
	s_clause 0x1
	global_load_u16 v19, v[17:18], off
	global_load_u8 v17, v[17:18], off offset:2
	s_waitcnt vmcnt(1)
	v_cvt_f32_ubyte0_e32 v18, v19
	v_cvt_f32_ubyte1_e32 v19, v19
	s_waitcnt vmcnt(0)
	v_cvt_f32_ubyte0_e32 v17, v17
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_f32_e32 v20, v18, v9
	v_div_scale_f32 v21, null, 0x437f0000, 0x437f0000, v20
	v_div_scale_f32 v24, vcc_lo, v20, 0x437f0000, v20
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v22, v21
	s_waitcnt_depctr 0xfff
	v_fma_f32 v23, -v21, v22, 1.0
	v_fmac_f32_e32 v22, v23, v22
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v23, v24, v22
	v_fma_f32 v25, -v21, v23, v24
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v23, v25, v22
	v_fma_f32 v21, -v21, v23, v24
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v21, v21, v22, v23
	v_div_fixup_f32 v21, v21, 0x437f0000, v20
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_mul_f32 v20, v20, v21 :: v_dual_sub_f32 v21, v19, v12
	v_div_scale_f32 v22, null, 0x437f0000, 0x437f0000, v20
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_div_scale_f32 v23, null, 0x437f0000, 0x437f0000, v21
	v_div_scale_f32 v28, vcc_lo, v20, 0x437f0000, v20
	v_rcp_f32_e32 v24, v22
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v25, v23
	s_waitcnt_depctr 0xfff
	v_fma_f32 v26, -v22, v24, 1.0
	v_fma_f32 v27, -v23, v25, 1.0
	v_dual_fmac_f32 v24, v26, v24 :: v_dual_fmac_f32 v25, v27, v25
	v_div_scale_f32 v26, s0, v21, 0x437f0000, v21
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v27, v28, v24
	v_mul_f32_e32 v29, v26, v25
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v30, -v22, v27, v28
	v_fma_f32 v31, -v23, v29, v26
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fmac_f32_e32 v27, v30, v24
	v_fmac_f32_e32 v29, v31, v25
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v22, -v22, v27, v28
	v_fma_f32 v23, -v23, v29, v26
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_fmas_f32 v22, v22, v24, v27
	s_mov_b32 vcc_lo, s0
	v_div_fmas_f32 v23, v23, v25, v29
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_div_fixup_f32 v20, v22, 0x437f0000, v20
	v_div_fixup_f32 v23, v23, 0x437f0000, v21
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v21, v21, v23
	v_sub_f32_e32 v23, v17, v7
	v_div_scale_f32 v24, null, 0x437f0000, 0x437f0000, v21
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_div_scale_f32 v25, null, 0x437f0000, 0x437f0000, v23
	v_div_scale_f32 v30, vcc_lo, v21, 0x437f0000, v21
	v_rcp_f32_e32 v26, v24
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_rcp_f32_e32 v27, v25
	s_waitcnt_depctr 0xfff
	v_fma_f32 v28, -v24, v26, 1.0
	v_fma_f32 v29, -v25, v27, 1.0
	v_fmac_f32_e32 v26, v28, v26
	v_div_scale_f32 v28, s0, v23, 0x437f0000, v23
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fmac_f32_e32 v27, v29, v27
	v_mul_f32_e32 v29, v30, v26
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v31, v28, v27
	v_fma_f32 v32, -v24, v29, v30
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v33, -v25, v31, v28
	v_fmac_f32_e32 v29, v32, v26
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fmac_f32_e32 v31, v33, v27
	v_fma_f32 v24, -v24, v29, v30
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v25, -v25, v31, v28
	v_div_fmas_f32 v24, v24, v26, v29
	s_mov_b32 vcc_lo, s0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_div_fmas_f32 v25, v25, v27, v31
	v_div_fixup_f32 v21, v24, 0x437f0000, v21
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_div_fixup_f32 v25, v25, 0x437f0000, v23
	v_add_f32_e32 v20, v20, v21
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v23, v23, v25
	v_div_scale_f32 v25, null, 0x437f0000, 0x437f0000, v23
	v_div_scale_f32 v28, vcc_lo, v23, 0x437f0000, v23
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v26, v25
	s_waitcnt_depctr 0xfff
	v_fma_f32 v27, -v25, v26, 1.0
	v_fmac_f32_e32 v26, v27, v26
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v27, v28, v26
	v_fma_f32 v29, -v25, v27, v28
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v27, v29, v26
	v_fma_f32 v25, -v25, v27, v28
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v22, v25, v26, v27
	v_div_fixup_f32 v21, v22, 0x437f0000, v23
	v_cvt_f32_i32_e32 v22, s10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f32_e32 v20, v21, v20
	v_fma_f32 v21, v22, v22, v16
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v20, 0x411c4000, v20
	v_fmac_f32_e32 v20, 0x3ca72f05, v21
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v21, 0xbfb8aa3b, v20
	v_cmp_nlt_f32_e32 vcc_lo, 0x42ce8ed0, v20
	v_fma_f32 v22, 0xbfb8aa3b, v20, -v21
	v_rndne_f32_e32 v23, v21
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_fmac_f32 v22, 0xb2a5705f, v20 :: v_dual_sub_f32 v21, v21, v23
	v_add_f32_e32 v21, v21, v22
	v_cvt_i32_f32_e32 v22, v23
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_exp_f32_e32 v21, v21
	s_waitcnt_depctr 0xfff
	v_ldexp_f32 v21, v21, v22
	v_cndmask_b32_e32 v21, 0, v21, vcc_lo
	v_cmp_ngt_f32_e32 vcc_lo, 0xc2b17218, v20
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v20, 0x7f800000, v21, vcc_lo
	v_fmac_f32_e32 v8, v20, v17
	v_cmp_lt_f32_e32 vcc_lo, 0x3ca3d70a, v20
	v_fmac_f32_e32 v6, v20, v18
	v_dual_fmac_f32 v10, v20, v19 :: v_dual_add_f32 v13, v13, v20
	v_cndmask_b32_e64 v21, 0, 0x3ca72f05, vcc_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f32_e32 v11, v11, v21
.LBB0_6:                                ; %Flow
                                        ;   in Loop: Header=BB0_3 Depth=2
	s_or_b32 exec_lo, exec_lo, s12
.LBB0_7:                                ;   in Loop: Header=BB0_3 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s11
	v_add_nc_u32_e32 v15, 4, v15
	s_add_i32 s10, s10, 1
	s_cmp_eq_u32 s10, 4
	s_cbranch_scc0 .LBB0_3
; %bb.8:                                ;   in Loop: Header=BB0_2 Depth=1
	v_add_nc_u32_e32 v14, s2, v14
	s_add_i32 s1, s1, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s1, 4
	s_cbranch_scc0 .LBB0_2
; %bb.9:
	v_div_scale_f32 v2, null, v13, v13, 1.0
	v_div_scale_f32 v15, vcc_lo, 1.0, v13, 1.0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v5, v2
	s_waitcnt_depctr 0xfff
	v_fma_f32 v14, -v2, v5, 1.0
	v_fmac_f32_e32 v5, v14, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v14, v15, v5
	v_fma_f32 v16, -v2, v14, v15
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v14, v16, v5
	v_fma_f32 v2, -v2, v14, v15
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f32 v2, v2, v5, v14
	v_mov_b32_e32 v5, 0x3e23d70a
	v_cmp_lt_f32_e32 vcc_lo, 0x3f28f5c3, v11
	v_div_fixup_f32 v2, v2, v13, 1.0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cndmask_b32_e32 v5, 0x3f570a3e, v5, vcc_lo
	v_mul_f32_e32 v11, v10, v2
	v_fma_f32 v10, -v10, v2, v12
	v_mul_f32_e32 v12, v6, v2
	v_fma_f32 v6, -v6, v2, v9
	v_mul_f32_e32 v9, v8, v2
	v_fma_f32 v2, -v8, v2, v7
	v_fmac_f32_e32 v11, v10, v5
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fmac_f32_e32 v12, v6, v5
	v_fmac_f32_e32 v9, v2, v5
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_cvt_i32_f32_e32 v6, v11
	v_add_co_u32 v2, vcc_lo, s6, v3
	v_cvt_i32_f32_e32 v5, v12
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v4, vcc_lo
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b16 v6, 8, v6
	v_cvt_i32_f32_e32 v4, v9
	v_or_b32_e32 v5, v5, v6
	s_clause 0x1
	global_store_b8 v[2:3], v4, off offset:2
	global_store_b16 v[2:3], v5, off
	global_load_u8 v0, v[0:1], off offset:3
	s_waitcnt vmcnt(0)
	global_store_b8 v[2:3], v0, off offset:3
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10knn_filterPhS_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 34
		.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	_Z10knn_filterPhS_ii, .Lfunc_end0-_Z10knn_filterPhS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1576
; NumSgprs: 18
; NumVgprs: 34
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 4
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 34
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10knn_filterPhS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10knn_filterPhS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     34
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
