#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define PIXELSIZE 4


double* ClusterCore;
unsigned int*  ClusterPixelCount;
double* ClusterColorSum;

__constant__ double DevClusterCore[4*32];

__global__ void PixelToCluster(unsigned char* Image, int ClusterCount, int Width, int Height, int* LastIter)
{
	int ElementsInStr=PIXELSIZE*Width;
	int Distance=0;
	int SelectedCluster=0;
	int a,b,c;  

	int i,j;

	for(int offset=4*(blockIdx.x*blockDim.x+threadIdx.x); offset<Height*ElementsInStr; offset+=4*(gridDim.x*blockDim.x))
	{
	   i=offset/ElementsInStr;
	   j=offset%ElementsInStr;

	   a=(DevClusterCore[0]-Image[i*ElementsInStr+j])*(DevClusterCore[0]-Image[i*ElementsInStr+j]);
	   b=(DevClusterCore[1]-Image[i*ElementsInStr+j+1])*(DevClusterCore[1]-Image[i*ElementsInStr+j+1]);
	   c=(DevClusterCore[2]-Image[i*ElementsInStr+j+2])*(DevClusterCore[2]-Image[i*ElementsInStr+j+2]);

	   Distance=a+b+c;
	   SelectedCluster=0;

	   for(int k=1; k<ClusterCount; k++)		
		{
			a=(DevClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j])*(DevClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j]);
			b=(DevClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1])*(DevClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1]);
			c=(DevClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2])*(DevClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2]);

			if((a+b+c)<Distance)
			{
		    	Distance=a+b+c;
				SelectedCluster=k;
			}
		}

	   if(Image[i*ElementsInStr+j+3]!=SelectedCluster) (*LastIter)=0;  
		Image[i*ElementsInStr+j+3]=SelectedCluster;


	}
}

__host__ void ClusterDustribution(unsigned char* Image, int ClusterCount, int Width, int Height, int* LastIter)
{
	int ElementsInStr=PIXELSIZE*Width;
	int Distance=0;
	int SelectedCluster=0;
	int a,b,c;  

	*LastIter=1;

	for(int i=0; i<Height; i++)
	{
		for(int j=0; j<ElementsInStr; j+=PIXELSIZE)
		{
			a=(ClusterCore[0]-Image[i*ElementsInStr+j])*(ClusterCore[0]-Image[i*ElementsInStr+j]);
			b=(ClusterCore[1]-Image[i*ElementsInStr+j+1])*(ClusterCore[1]-Image[i*ElementsInStr+j+1]);
			c=(ClusterCore[2]-Image[i*ElementsInStr+j+2])*(ClusterCore[2]-Image[i*ElementsInStr+j+2]);

            Distance=a+b+c;
			SelectedCluster=0;
			for(int k=1; k<ClusterCount; k++)		
			{
				a=(ClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j])*(ClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j]);
			    b=(ClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1])*(ClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1]);
				c=(ClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2])*(ClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2]);

				if((a+b+c)<Distance)
				{
					Distance=a+b+c;
					SelectedCluster=k;
				}
			}
			if(Image[i*ElementsInStr+j+3]!=SelectedCluster) *LastIter=0;  
			Image[i*ElementsInStr+j+3]=SelectedCluster;
		}	
	}

}

__host__ void ClusterOffset(unsigned char* Image, int ClusterCount, int Width, int Height) 
{
	for(int i=0; i<ClusterCount; i++)
		ClusterPixelCount[i]=0;

	for(int i=0; i<4*ClusterCount; i++)
		ClusterColorSum[i]=0;

	int ElementsInStr=PIXELSIZE*Width;
	int ClusterNum=0;

	for(int i=0; i<Height; i++)
	{
		for(int j=0; j<ElementsInStr; j+=PIXELSIZE)
		{
			ClusterNum=Image[i*ElementsInStr+j+3];
			ClusterPixelCount[ClusterNum]++;
			ClusterColorSum[ClusterNum*PIXELSIZE]+=Image[i*ElementsInStr+j];
			ClusterColorSum[ClusterNum*PIXELSIZE+1]+=Image[i*ElementsInStr+j+1];
			ClusterColorSum[ClusterNum*PIXELSIZE+2]+=Image[i*ElementsInStr+j+2];		    
		}
	}

	for(int i=0; i<ClusterCount; i++)
	{
		ClusterCore[i*PIXELSIZE]=ClusterColorSum[i*PIXELSIZE]/ClusterPixelCount[i];	
		ClusterCore[i*PIXELSIZE+1]=ClusterColorSum[i*PIXELSIZE+1]/ClusterPixelCount[i];	
		ClusterCore[i*PIXELSIZE+2]=ClusterColorSum[i*PIXELSIZE+2]/ClusterPixelCount[i];	
	}
}


int main()
{
   char InPath[256];
   char OutPath[256];

   scanf("%s", InPath);

   FILE* InPut = fopen(InPath, "rb");
    if (InPut == NULL)
    {
        fprintf(stderr, "Cannot open in.data");
        exit(0);
    }

	scanf("%s", OutPath);
	FILE* OutPut = fopen(OutPath, "wb");
    if (OutPut == NULL)
    {
        fprintf(stderr, "Cannot create out.data");
        exit(0);
    }

	int ClusterNumber;     

	scanf("%d", &ClusterNumber);

	int* Xcoords = (int*)malloc(ClusterNumber*sizeof(int)); 
	int* Ycoords = (int*)malloc(ClusterNumber*sizeof(int)); 

	for(int i=0; i<ClusterNumber; i++)
	{
		scanf("%d", &Ycoords[i]);
		scanf("%d", &Xcoords[i]);
	}


    ClusterCore = (double*)malloc(4*ClusterNumber*sizeof(double));
	ClusterPixelCount = (unsigned int*)malloc(ClusterNumber*sizeof(unsigned int));
	ClusterColorSum = (double*)malloc(4*ClusterNumber*sizeof(double));

	int Width;
	int Height;

	fread(&Width, sizeof(int), 1, InPut);
	fread(&Height, sizeof(int), 1, InPut);


	unsigned char* Image = (unsigned char*)malloc(4*Width*Height*sizeof(unsigned char));
	fread(Image, 4*Width*Height*sizeof(unsigned char), 1, InPut);


	unsigned char* Dev_Image;
	cudaMalloc((void**)&Dev_Image, 4*Width*Height*sizeof(unsigned char));
	cudaMemcpy(Dev_Image, Image, 4*Width*Height*sizeof(unsigned char), cudaMemcpyHostToDevice);


	for(int i=0; i<ClusterNumber; i++)
	{
		ClusterCore[i*PIXELSIZE]=Image[4*Width*Xcoords[i]+PIXELSIZE*Ycoords[i]];
		
		ClusterCore[i*PIXELSIZE+1]=Image[4*Width*Xcoords[i]+PIXELSIZE*Ycoords[i]+1];

		ClusterCore[i*PIXELSIZE+2]=Image[4*Width*Xcoords[i]+PIXELSIZE*Ycoords[i]+2];

		ClusterCore[i*PIXELSIZE+3]=0;
	}
	
    int* NotLastIter;     
	int* HostNotLastIter = (int*)malloc(sizeof(int));
	*HostNotLastIter=1;


	cudaMalloc((void**)&NotLastIter, sizeof(int));
	cudaMemcpy(NotLastIter, HostNotLastIter, sizeof(int), cudaMemcpyHostToDevice);
	
	cudaMemcpyToSymbol(DevClusterCore, ClusterCore, 4*ClusterNumber*sizeof(double));

      while(1)
	{
		PixelToCluster<<<128, 512>>>(Dev_Image, ClusterNumber, Width, Height, NotLastIter);
		cudaMemcpy(HostNotLastIter, NotLastIter, sizeof(int), cudaMemcpyDeviceToHost);
		cudaMemcpy(Image, Dev_Image, 4*Width*Height*sizeof(unsigned char), cudaMemcpyDeviceToHost);

		if((*HostNotLastIter)==1) break;

		ClusterOffset(Image, ClusterNumber, Width, Height);                   
		cudaMemcpyToSymbol(DevClusterCore, ClusterCore, 4*ClusterNumber*sizeof(double));
		*HostNotLastIter=1;
		cudaMemcpy(NotLastIter, HostNotLastIter, sizeof(int), cudaMemcpyHostToDevice);
	}
	

	fwrite(&Width, sizeof(int), 1 ,OutPut);
	fwrite(&Height, sizeof(int), 1, OutPut);
	fwrite(Image, 4*Width*Height*sizeof(unsigned char),1, OutPut);



	cudaFree(NotLastIter);
	cudaFree(Dev_Image);
	free(Image);
	free(ClusterColorSum);
	free(ClusterCore);
	free(ClusterPixelCount);	
    return 0;
}


	.file	"tmpxft_002291b2_00000000-6_kernel4.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z19ClusterDustributionPhiiiPi
	.type	_Z19ClusterDustributionPhiiiPi, @function
_Z19ClusterDustributionPhiiiPi:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r10
	xorl	%r11d, %r11d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%ecx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	leal	0(,%rdx,4), %esi
	movl	$1, (%r8)
.L3:
	cmpl	%r12d, %ebp
	jge	.L2
	movslq	%r11d, %rdx
	xorl	%r9d, %r9d
	addq	%r10, %rdx
.L10:
	cmpl	%esi, %r9d
	jge	.L14
	movzbl	(%rdx), %ecx
	movq	ClusterCore(%rip), %rax
	xorl	%r13d, %r13d
	cvtsi2sdl	%ecx, %xmm3
	movzbl	1(%rdx), %ecx
	movsd	(%rax), %xmm2
	addq	$32, %rax
	movsd	-24(%rax), %xmm1
	movsd	-16(%rax), %xmm0
	cvtsi2sdl	%ecx, %xmm4
	movzbl	2(%rdx), %ecx
	cvtsi2sdl	%ecx, %xmm5
	subsd	%xmm3, %xmm2
	subsd	%xmm4, %xmm1
	mulsd	%xmm2, %xmm2
	subsd	%xmm5, %xmm0
	mulsd	%xmm1, %xmm1
	mulsd	%xmm0, %xmm0
	cvttsd2sil	%xmm2, %ecx
	cvttsd2sil	%xmm1, %edi
	addl	%edi, %ecx
	cvttsd2sil	%xmm0, %edi
	addl	%edi, %ecx
	movl	$1, %edi
.L4:
	cmpl	%ebx, %edi
	jge	.L15
	movsd	(%rax), %xmm2
	movsd	8(%rax), %xmm1
	movsd	16(%rax), %xmm0
	subsd	%xmm3, %xmm2
	subsd	%xmm4, %xmm1
	subsd	%xmm5, %xmm0
	mulsd	%xmm2, %xmm2
	mulsd	%xmm1, %xmm1
	mulsd	%xmm0, %xmm0
	cvttsd2sil	%xmm2, %r14d
	cvttsd2sil	%xmm1, %r15d
	addl	%r15d, %r14d
	cvttsd2sil	%xmm0, %r15d
	addl	%r15d, %r14d
	cmpl	%ecx, %r14d
	jl	.L11
	movl	%ecx, %r14d
	jmp	.L5
.L11:
	movl	%edi, %r13d
.L5:
	incl	%edi
	addq	$32, %rax
	movl	%r14d, %ecx
	jmp	.L4
.L15:
	movzbl	3(%rdx), %eax
	cmpl	%r13d, %eax
	je	.L7
	xorl	%eax, %eax
	movl	%eax, (%r8)
.L7:
	movb	%r13b, 3(%rdx)
	addl	$4, %r9d
	addq	$4, %rdx
	jmp	.L10
.L14:
	incl	%ebp
	addl	%esi, %r11d
	jmp	.L3
.L2:
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.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
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z19ClusterDustributionPhiiiPi, .-_Z19ClusterDustributionPhiiiPi
	.globl	_Z13ClusterOffsetPhiii
	.type	_Z13ClusterOffsetPhiii, @function
_Z13ClusterOffsetPhiii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdi, %r10
	movq	ClusterPixelCount(%rip), %rdi
	xorl	%eax, %eax
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
.L17:
	cmpl	%eax, %esi
	jle	.L30
	xorl	%ecx, %ecx
	movl	%ecx, (%rdi,%rax,4)
	incq	%rax
	jmp	.L17
.L30:
	movq	ClusterColorSum(%rip), %rcx
	leal	0(,%rsi,4), %r8d
	xorl	%eax, %eax
.L19:
	cmpl	%eax, %r8d
	jle	.L31
	movq	$0x000000000, (%rcx,%rax,8)
	incq	%rax
	jmp	.L19
.L31:
	sall	$2, %edx
	xorl	%r11d, %r11d
	xorl	%ebx, %ebx
.L21:
	cmpl	%ebp, %ebx
	jge	.L23
	movslq	%r11d, %rax
	xorl	%r9d, %r9d
	addq	%r10, %rax
.L24:
	cmpl	%edx, %r9d
	jge	.L32
	movzbl	3(%rax), %r12d
	addl	$4, %r9d
	addq	$4, %rax
	leaq	0(,%r12,4), %r8
	incl	(%rdi,%r12,4)
	movzbl	-4(%rax), %r13d
	andl	$1020, %r8d
	cvtsi2sdl	%r13d, %xmm0
	leaq	(%rcx,%r8,8), %r12
	addsd	(%r12), %xmm0
	leaq	8(,%r8,8), %r8
	movsd	%xmm0, (%r12)
	movzbl	-3(%rax), %r13d
	leaq	(%rcx,%r8), %r12
	leaq	8(%rcx,%r8), %r8
	cvtsi2sdl	%r13d, %xmm0
	addsd	(%r12), %xmm0
	movsd	%xmm0, (%r12)
	movzbl	-2(%rax), %r12d
	cvtsi2sdl	%r12d, %xmm0
	addsd	(%r8), %xmm0
	movsd	%xmm0, (%r8)
	jmp	.L24
.L32:
	incl	%ebx
	addl	%edx, %r11d
	jmp	.L21
.L23:
	movq	ClusterCore(%rip), %r8
	xorl	%eax, %eax
	xorl	%edx, %edx
.L25:
	cmpl	%edx, %esi
	jle	.L33
	movl	(%rdi,%rdx,4), %r9d
	movsd	(%rcx,%rax), %xmm0
	incq	%rdx
	cvtsi2sdq	%r9, %xmm1
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%r8,%rax)
	movsd	8(%rcx,%rax), %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%r8,%rax)
	movsd	16(%rcx,%rax), %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 16(%r8,%rax)
	addq	$32, %rax
	jmp	.L25
.L33:
	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
.LFE2028:
	.size	_Z13ClusterOffsetPhiii, .-_Z13ClusterOffsetPhiii
	.globl	_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi
	.type	_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi, @function
_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 16(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 12(%rsp)
	leaq	40(%rsp), %rcx
	movq	%r8, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z14PixelToClusterPhiiiPi(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L34:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi, .-_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi
	.globl	_Z14PixelToClusterPhiiiPi
	.type	_Z14PixelToClusterPhiiiPi, @function
_Z14PixelToClusterPhiiiPi:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi
	.cfi_endproc
.LFE2055:
	.size	_Z14PixelToClusterPhiiiPi, .-_Z14PixelToClusterPhiiiPi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%s"
.LC2:
	.string	"rb"
.LC3:
	.string	"Cannot open in.data"
.LC4:
	.string	"wb"
.LC5:
	.string	"Cannot create out.data"
.LC6:
	.string	"%d"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	leaq	.LC1(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r12, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$600, %rsp
	.cfi_def_cfa_offset 656
	movq	%fs:40, %rax
	movq	%rax, 584(%rsp)
	xorl	%eax, %eax
	leaq	72(%rsp), %rbx
	movq	%rbx, %rsi
	call	__isoc23_scanf@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rbx, %rdi
	call	fopen@PLT
	leaq	.LC3(%rip), %rdx
	testq	%rax, %rax
	je	.L51
	leaq	328(%rsp), %rbx
	movq	%r12, %rdi
	movq	%rax, %rbp
	xorl	%eax, %eax
	movq	%rbx, %rsi
	call	__isoc23_scanf@PLT
	leaq	.LC4(%rip), %rsi
	movq	%rbx, %rdi
	call	fopen@PLT
	movq	%rax, %r12
	testq	%rax, %rax
	jne	.L41
	leaq	.LC5(%rip), %rdx
.L51:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L41:
	leaq	20(%rsp), %rsi
	leaq	.LC6(%rip), %rdi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	call	__isoc23_scanf@PLT
	movslq	20(%rsp), %r14
	salq	$2, %r14
	movq	%r14, %rdi
	call	malloc@PLT
	movq	%r14, %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%r13, %r14
	movq	%rax, %r15
	movq	%rax, %rcx
.L42:
	movl	20(%rsp), %eax
	cmpl	%ebx, %eax
	jle	.L52
	movq	%rcx, %rsi
	leaq	.LC6(%rip), %rdi
	xorl	%eax, %eax
	movq	%rcx, (%rsp)
	call	__isoc23_scanf@PLT
	movq	%r14, %rsi
	xorl	%eax, %eax
	incl	%ebx
	leaq	.LC6(%rip), %rdi
	addq	$4, %r14
	call	__isoc23_scanf@PLT
	movq	(%rsp), %rcx
	addq	$4, %rcx
	jmp	.L42
.L52:
	leal	0(,%rax,4), %ebx
	movslq	%ebx, %rbx
	leaq	0(,%rbx,8), %r14
	movq	%r14, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, ClusterCore(%rip)
	call	malloc@PLT
	movq	%r14, %rdi
	leaq	24(%rsp), %r14
	movq	%rax, ClusterPixelCount(%rip)
	call	malloc@PLT
	movq	%rbp, %r8
	movl	$1, %ecx
	movq	%r14, %rdi
	movl	$4, %edx
	movl	$4, %esi
	movq	%rax, ClusterColorSum(%rip)
	call	__fread_chk@PLT
	leaq	28(%rsp), %rax
	movq	%rbp, %r8
	movl	$1, %ecx
	movl	$4, %edx
	movl	$4, %esi
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	__fread_chk@PLT
	movl	24(%rsp), %esi
	imull	28(%rsp), %esi
	sall	$2, %esi
	movslq	%esi, %rsi
	movq	%rsi, %rdi
	movq	%rsi, 8(%rsp)
	call	malloc@PLT
	movq	8(%rsp), %rsi
	movq	%rbp, %r8
	movl	$1, %ecx
	movq	%rax, %rdi
	movq	%rax, %rbx
	movq	%rsi, %rdx
	call	__fread_chk@PLT
	movl	24(%rsp), %esi
	imull	28(%rsp), %esi
	leaq	32(%rsp), %rdi
	sall	$2, %esi
	movslq	%esi, %rsi
	call	cudaMalloc@PLT
	movl	24(%rsp), %edx
	imull	28(%rsp), %edx
	movq	%rbx, %rsi
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	sall	$2, %edx
	movslq	%edx, %rdx
	call	cudaMemcpy@PLT
	movl	20(%rsp), %esi
	movl	24(%rsp), %edi
	xorl	%ecx, %ecx
	movq	ClusterCore(%rip), %rdx
.L44:
	cmpl	%ecx, %esi
	jle	.L53
	movl	0(%r13,%rcx,4), %eax
	addq	$32, %rdx
	imull	%edi, %eax
	addl	(%r15,%rcx,4), %eax
	incq	%rcx
	sall	$2, %eax
	cltq
	movzbl	(%rbx,%rax), %r8d
	cvtsi2sdl	%r8d, %xmm0
	movsd	%xmm0, -32(%rdx)
	movzbl	1(%rbx,%rax), %r8d
	cvtsi2sdl	%r8d, %xmm0
	movsd	%xmm0, -24(%rdx)
	movzbl	2(%rbx,%rax), %eax
	movq	$0x000000000, -8(%rdx)
	cvtsi2sdl	%eax, %xmm0
	movsd	%xmm0, -16(%rdx)
	jmp	.L44
.L53:
	movl	$4, %edi
	leaq	_ZL14DevClusterCore(%rip), %r13
	movabsq	$4294967808, %r15
	call	malloc@PLT
	leaq	40(%rsp), %rdi
	movl	$4, %esi
	movl	$1, (%rax)
	movq	%rax, %rbp
	call	cudaMalloc@PLT
	movq	40(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	movl	20(%rsp), %eax
	xorl	%ecx, %ecx
	movq	%r13, %rdi
	movq	ClusterCore(%rip), %rsi
	movl	$1, %r8d
	leal	0(,%rax,4), %edx
	movslq	%edx, %rdx
	salq	$3, %rdx
	call	cudaMemcpyToSymbol@PLT
.L48:
	movl	$33554433, %edi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r15, %rdx
	salq	$7, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L46
	movq	40(%rsp), %r8
	movl	28(%rsp), %ecx
	movl	24(%rsp), %edx
	movl	20(%rsp), %esi
	movq	32(%rsp), %rdi
	call	_Z39__device_stub__Z14PixelToClusterPhiiiPiPhiiiPi
.L46:
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movl	$4, %edx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	movl	24(%rsp), %edx
	imull	28(%rsp), %edx
	movq	%rbx, %rdi
	movq	32(%rsp), %rsi
	movl	$2, %ecx
	sall	$2, %edx
	movslq	%edx, %rdx
	call	cudaMemcpy@PLT
	cmpl	$1, 0(%rbp)
	je	.L47
	movl	28(%rsp), %ecx
	movl	24(%rsp), %edx
	movq	%rbx, %rdi
	movl	20(%rsp), %esi
	call	_Z13ClusterOffsetPhiii
	movl	20(%rsp), %eax
	xorl	%ecx, %ecx
	movq	%r13, %rdi
	movq	ClusterCore(%rip), %rsi
	movl	$1, %r8d
	leal	0(,%rax,4), %edx
	movslq	%edx, %rdx
	salq	$3, %rdx
	call	cudaMemcpyToSymbol@PLT
	movl	$1, 0(%rbp)
	movq	40(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	movl	$4, %edx
	call	cudaMemcpy@PLT
	jmp	.L48
.L47:
	movq	%r12, %rcx
	movl	$1, %edx
	movl	$4, %esi
	movq	%r14, %rdi
	call	fwrite@PLT
	movq	(%rsp), %rdi
	movq	%r12, %rcx
	movl	$1, %edx
	movl	$4, %esi
	call	fwrite@PLT
	movl	24(%rsp), %esi
	movq	%r12, %rcx
	movq	%rbx, %rdi
	imull	28(%rsp), %esi
	movl	$1, %edx
	sall	$2, %esi
	movslq	%esi, %rsi
	call	fwrite@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	ClusterColorSum(%rip), %rdi
	call	free@PLT
	movq	ClusterCore(%rip), %rdi
	call	free@PLT
	movq	ClusterPixelCount(%rip), %rdi
	call	free@PLT
	movq	584(%rsp), %rax
	subq	%fs:40, %rax
	je	.L49
	call	__stack_chk_fail@PLT
.L49:
	addq	$600, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.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
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC7:
	.string	"_Z14PixelToClusterPhiiiPi"
.LC8:
	.string	"DevClusterCore"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC7(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z14PixelToClusterPhiiiPi(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC8(%rip), %rdx
	movl	$1024, %r9d
	leaq	_ZL14DevClusterCore(%rip), %rsi
	pushq	$1
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2057:
	.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
	.local	_ZL14DevClusterCore
	.comm	_ZL14DevClusterCore,1024,32
	.globl	ClusterColorSum
	.bss
	.align 8
	.type	ClusterColorSum, @object
	.size	ClusterColorSum, 8
ClusterColorSum:
	.zero	8
	.globl	ClusterPixelCount
	.align 8
	.type	ClusterPixelCount, @object
	.size	ClusterPixelCount, 8
ClusterPixelCount:
	.zero	8
	.globl	ClusterCore
	.align 8
	.type	ClusterCore, @object
	.size	ClusterCore, 8
ClusterCore:
	.zero	8
	.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 "hip/hip_runtime.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define PIXELSIZE 4


double* ClusterCore;
unsigned int*  ClusterPixelCount;
double* ClusterColorSum;

__constant__ double DevClusterCore[4*32];

__global__ void PixelToCluster(unsigned char* Image, int ClusterCount, int Width, int Height, int* LastIter)
{
	int ElementsInStr=PIXELSIZE*Width;
	int Distance=0;
	int SelectedCluster=0;
	int a,b,c;  

	int i,j;

	for(int offset=4*(blockIdx.x*blockDim.x+threadIdx.x); offset<Height*ElementsInStr; offset+=4*(gridDim.x*blockDim.x))
	{
	   i=offset/ElementsInStr;
	   j=offset%ElementsInStr;

	   a=(DevClusterCore[0]-Image[i*ElementsInStr+j])*(DevClusterCore[0]-Image[i*ElementsInStr+j]);
	   b=(DevClusterCore[1]-Image[i*ElementsInStr+j+1])*(DevClusterCore[1]-Image[i*ElementsInStr+j+1]);
	   c=(DevClusterCore[2]-Image[i*ElementsInStr+j+2])*(DevClusterCore[2]-Image[i*ElementsInStr+j+2]);

	   Distance=a+b+c;
	   SelectedCluster=0;

	   for(int k=1; k<ClusterCount; k++)		
		{
			a=(DevClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j])*(DevClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j]);
			b=(DevClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1])*(DevClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1]);
			c=(DevClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2])*(DevClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2]);

			if((a+b+c)<Distance)
			{
		    	Distance=a+b+c;
				SelectedCluster=k;
			}
		}

	   if(Image[i*ElementsInStr+j+3]!=SelectedCluster) (*LastIter)=0;  
		Image[i*ElementsInStr+j+3]=SelectedCluster;


	}
}

__host__ void ClusterDustribution(unsigned char* Image, int ClusterCount, int Width, int Height, int* LastIter)
{
	int ElementsInStr=PIXELSIZE*Width;
	int Distance=0;
	int SelectedCluster=0;
	int a,b,c;  

	*LastIter=1;

	for(int i=0; i<Height; i++)
	{
		for(int j=0; j<ElementsInStr; j+=PIXELSIZE)
		{
			a=(ClusterCore[0]-Image[i*ElementsInStr+j])*(ClusterCore[0]-Image[i*ElementsInStr+j]);
			b=(ClusterCore[1]-Image[i*ElementsInStr+j+1])*(ClusterCore[1]-Image[i*ElementsInStr+j+1]);
			c=(ClusterCore[2]-Image[i*ElementsInStr+j+2])*(ClusterCore[2]-Image[i*ElementsInStr+j+2]);

            Distance=a+b+c;
			SelectedCluster=0;
			for(int k=1; k<ClusterCount; k++)		
			{
				a=(ClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j])*(ClusterCore[k*PIXELSIZE]-Image[i*ElementsInStr+j]);
			    b=(ClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1])*(ClusterCore[k*PIXELSIZE+1]-Image[i*ElementsInStr+j+1]);
				c=(ClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2])*(ClusterCore[k*PIXELSIZE+2]-Image[i*ElementsInStr+j+2]);

				if((a+b+c)<Distance)
				{
					Distance=a+b+c;
					SelectedCluster=k;
				}
			}
			if(Image[i*ElementsInStr+j+3]!=SelectedCluster) *LastIter=0;  
			Image[i*ElementsInStr+j+3]=SelectedCluster;
		}	
	}

}

__host__ void ClusterOffset(unsigned char* Image, int ClusterCount, int Width, int Height) 
{
	for(int i=0; i<ClusterCount; i++)
		ClusterPixelCount[i]=0;

	for(int i=0; i<4*ClusterCount; i++)
		ClusterColorSum[i]=0;

	int ElementsInStr=PIXELSIZE*Width;
	int ClusterNum=0;

	for(int i=0; i<Height; i++)
	{
		for(int j=0; j<ElementsInStr; j+=PIXELSIZE)
		{
			ClusterNum=Image[i*ElementsInStr+j+3];
			ClusterPixelCount[ClusterNum]++;
			ClusterColorSum[ClusterNum*PIXELSIZE]+=Image[i*ElementsInStr+j];
			ClusterColorSum[ClusterNum*PIXELSIZE+1]+=Image[i*ElementsInStr+j+1];
			ClusterColorSum[ClusterNum*PIXELSIZE+2]+=Image[i*ElementsInStr+j+2];		    
		}
	}

	for(int i=0; i<ClusterCount; i++)
	{
		ClusterCore[i*PIXELSIZE]=ClusterColorSum[i*PIXELSIZE]/ClusterPixelCount[i];	
		ClusterCore[i*PIXELSIZE+1]=ClusterColorSum[i*PIXELSIZE+1]/ClusterPixelCount[i];	
		ClusterCore[i*PIXELSIZE+2]=ClusterColorSum[i*PIXELSIZE+2]/ClusterPixelCount[i];	
	}
}


int main()
{
   char InPath[256];
   char OutPath[256];

   scanf("%s", InPath);

   FILE* InPut = fopen(InPath, "rb");
    if (InPut == NULL)
    {
        fprintf(stderr, "Cannot open in.data");
        exit(0);
    }

	scanf("%s", OutPath);
	FILE* OutPut = fopen(OutPath, "wb");
    if (OutPut == NULL)
    {
        fprintf(stderr, "Cannot create out.data");
        exit(0);
    }

	int ClusterNumber;     

	scanf("%d", &ClusterNumber);

	int* Xcoords = (int*)malloc(ClusterNumber*sizeof(int)); 
	int* Ycoords = (int*)malloc(ClusterNumber*sizeof(int)); 

	for(int i=0; i<ClusterNumber; i++)
	{
		scanf("%d", &Ycoords[i]);
		scanf("%d", &Xcoords[i]);
	}


    ClusterCore = (double*)malloc(4*ClusterNumber*sizeof(double));
	ClusterPixelCount = (unsigned int*)malloc(ClusterNumber*sizeof(unsigned int));
	ClusterColorSum = (double*)malloc(4*ClusterNumber*sizeof(double));

	int Width;
	int Height;

	fread(&Width, sizeof(int), 1, InPut);
	fread(&Height, sizeof(int), 1, InPut);


	unsigned char* Image = (unsigned char*)malloc(4*Width*Height*sizeof(unsigned char));
	fread(Image, 4*Width*Height*sizeof(unsigned char), 1, InPut);


	unsigned char* Dev_Image;
	hipMalloc((void**)&Dev_Image, 4*Width*Height*sizeof(unsigned char));
	hipMemcpy(Dev_Image, Image, 4*Width*Height*sizeof(unsigned char), hipMemcpyHostToDevice);


	for(int i=0; i<ClusterNumber; i++)
	{
		ClusterCore[i*PIXELSIZE]=Image[4*Width*Xcoords[i]+PIXELSIZE*Ycoords[i]];
		
		ClusterCore[i*PIXELSIZE+1]=Image[4*Width*Xcoords[i]+PIXELSIZE*Ycoords[i]+1];

		ClusterCore[i*PIXELSIZE+2]=Image[4*Width*Xcoords[i]+PIXELSIZE*Ycoords[i]+2];

		ClusterCore[i*PIXELSIZE+3]=0;
	}
	
    int* NotLastIter;     
	int* HostNotLastIter = (int*)malloc(sizeof(int));
	*HostNotLastIter=1;


	hipMalloc((void**)&NotLastIter, sizeof(int));
	hipMemcpy(NotLastIter, HostNotLastIter, sizeof(int), hipMemcpyHostToDevice);
	
	hipMemcpyToSymbol(HIP_SYMBOL(DevClusterCore), ClusterCore, 4*ClusterNumber*sizeof(double));

      while(1)
	{
		PixelToCluster<<<128, 512>>>(Dev_Image, ClusterNumber, Width, Height, NotLastIter);
		hipMemcpy(HostNotLastIter, NotLastIter, sizeof(int), hipMemcpyDeviceToHost);
		hipMemcpy(Image, Dev_Image, 4*Width*Height*sizeof(unsigned char), hipMemcpyDeviceToHost);

		if((*HostNotLastIter)==1) break;

		ClusterOffset(Image, ClusterNumber, Width, Height);                   
		hipMemcpyToSymbol(HIP_SYMBOL(DevClusterCore), ClusterCore, 4*ClusterNumber*sizeof(double));
		*HostNotLastIter=1;
		hipMemcpy(NotLastIter, HostNotLastIter, sizeof(int), hipMemcpyHostToDevice);
	}
	

	fwrite(&Width, sizeof(int), 1 ,OutPut);
	fwrite(&Height, sizeof(int), 1, OutPut);
	fwrite(Image, 4*Width*Height*sizeof(unsigned char),1, OutPut);



	hipFree(NotLastIter);
	hipFree(Dev_Image);
	free(Image);
	free(ClusterColorSum);
	free(ClusterCore);
	free(ClusterPixelCount);	
    return 0;
}


	.text
	.file	"kernel4.hip"
	.globl	_Z29__device_stub__PixelToClusterPhiiiPi # -- Begin function _Z29__device_stub__PixelToClusterPhiiiPi
	.type	_Z29__device_stub__PixelToClusterPhiiiPi,@function
_Z29__device_stub__PixelToClusterPhiiiPi: # @_Z29__device_stub__PixelToClusterPhiiiPi
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.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	12(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	8(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%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	$_Z14PixelToClusterPhiiiPi, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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_end0:
	.size	_Z29__device_stub__PixelToClusterPhiiiPi, .Lfunc_end0-_Z29__device_stub__PixelToClusterPhiiiPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z19ClusterDustributionPhiiiPi  # -- Begin function _Z19ClusterDustributionPhiiiPi
	.type	_Z19ClusterDustributionPhiiiPi,@function
_Z19ClusterDustributionPhiiiPi:         # @_Z19ClusterDustributionPhiiiPi
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
                                        # kill: def $edx killed $edx def $rdx
	movq	%rdx, -16(%rsp)                 # 8-byte Spill
	movl	$1, (%r8)
	testl	%ecx, %ecx
	jle	.LBB1_11
# %bb.1:                                # %.preheader.lr.ph
	movq	-16(%rsp), %rax                 # 8-byte Reload
	leal	(,%rax,4), %eax
	movl	%eax, -20(%rsp)                 # 4-byte Spill
	movslq	%eax, %r9
	movl	%ecx, %eax
	movq	%rax, -8(%rsp)                  # 8-byte Spill
	movl	%esi, %r10d
	xorl	%r11d, %r11d
.LBB1_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
                                        #       Child Loop BB1_6 Depth 3
	cmpl	$0, -16(%rsp)                   # 4-byte Folded Reload
	jle	.LBB1_10
# %bb.3:                                # %.lr.ph98
                                        #   in Loop: Header=BB1_2 Depth=1
	movl	-20(%rsp), %eax                 # 4-byte Reload
	movl	%eax, %ebx
	imull	%r11d, %ebx
	xorl	%r14d, %r14d
.LBB1_4:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_6 Depth 3
	leaq	(%r14,%rbx), %r15
	xorl	%ebp, %ebp
	cmpl	$2, %esi
	jl	.LBB1_7
# %bb.5:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB1_4 Depth=2
	movzbl	(%rdi,%r15), %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movzbl	1(%rdi,%r15), %eax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%eax, %xmm1
	movq	ClusterCore(%rip), %r12
	movzbl	2(%rdi,%r15), %eax
	xorps	%xmm2, %xmm2
	cvtsi2sd	%eax, %xmm2
	movsd	(%r12), %xmm3                   # xmm3 = mem[0],zero
	movsd	8(%r12), %xmm4                  # xmm4 = mem[0],zero
	subsd	%xmm1, %xmm4
	mulsd	%xmm4, %xmm4
	cvttsd2si	%xmm4, %eax
	subsd	%xmm0, %xmm3
	mulsd	%xmm3, %xmm3
	cvttsd2si	%xmm3, %ebp
	addl	%eax, %ebp
	movsd	16(%r12), %xmm3                 # xmm3 = mem[0],zero
	subsd	%xmm2, %xmm3
	mulsd	%xmm3, %xmm3
	cvttsd2si	%xmm3, %r13d
	addl	%ebp, %r13d
	addq	$48, %r12
	xorl	%ebp, %ebp
	movl	$1, %eax
.LBB1_6:                                # %.lr.ph
                                        #   Parent Loop BB1_2 Depth=1
                                        #     Parent Loop BB1_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	-16(%r12), %xmm3                # xmm3 = mem[0],zero
	movsd	-8(%r12), %xmm4                 # xmm4 = mem[0],zero
	subsd	%xmm0, %xmm3
	mulsd	%xmm3, %xmm3
	cvttsd2si	%xmm3, %ecx
	subsd	%xmm1, %xmm4
	mulsd	%xmm4, %xmm4
	cvttsd2si	%xmm4, %edx
	addl	%ecx, %edx
	movsd	(%r12), %xmm3                   # xmm3 = mem[0],zero
	subsd	%xmm2, %xmm3
	mulsd	%xmm3, %xmm3
	cvttsd2si	%xmm3, %ecx
	addl	%edx, %ecx
	cmpl	%r13d, %ecx
	cmovll	%ecx, %r13d
	cmovll	%eax, %ebp
	incq	%rax
	addq	$32, %r12
	cmpq	%rax, %r10
	jne	.LBB1_6
.LBB1_7:                                # %._crit_edge
                                        #   in Loop: Header=BB1_4 Depth=2
	movzbl	3(%rdi,%r15), %eax
	cmpl	%eax, %ebp
	je	.LBB1_9
# %bb.8:                                #   in Loop: Header=BB1_4 Depth=2
	movl	$0, (%r8)
.LBB1_9:                                #   in Loop: Header=BB1_4 Depth=2
	orq	$3, %r15
	movb	%bpl, (%rdi,%r15)
	addq	$4, %r14
	cmpq	%r9, %r14
	jl	.LBB1_4
.LBB1_10:                               # %._crit_edge99
                                        #   in Loop: Header=BB1_2 Depth=1
	incq	%r11
	cmpq	-8(%rsp), %r11                  # 8-byte Folded Reload
	jne	.LBB1_2
.LBB1_11:                               # %._crit_edge101
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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_end1:
	.size	_Z19ClusterDustributionPhiiiPi, .Lfunc_end1-_Z19ClusterDustributionPhiiiPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z13ClusterOffsetPhiii          # -- Begin function _Z13ClusterOffsetPhiii
	.type	_Z13ClusterOffsetPhiii,@function
_Z13ClusterOffsetPhiii:                 # @_Z13ClusterOffsetPhiii
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %ebp
	movl	%edx, %r14d
	movl	%esi, %ebx
	movq	%rdi, %r15
	movl	%esi, %r12d
	testl	%esi, %esi
	jle	.LBB2_2
# %bb.1:                                # %.lr.ph56
	movq	ClusterPixelCount(%rip), %rdi
	leaq	(,%r12,4), %rdx
	xorl	%esi, %esi
	callq	memset@PLT
	leal	(,%rbx,4), %eax
	movq	ClusterColorSum(%rip), %rdi
	cmpl	$2, %eax
	movl	$1, %edx
	cmovgel	%eax, %edx
	shlq	$3, %rdx
	xorl	%esi, %esi
	callq	memset@PLT
.LBB2_2:                                # %._crit_edge
	testl	%ebp, %ebp
	jle	.LBB2_8
# %bb.3:                                # %.preheader52.lr.ph
	leal	(,%r14,4), %eax
	movq	ClusterPixelCount(%rip), %rcx
	movq	ClusterColorSum(%rip), %rdx
	movslq	%eax, %rsi
	movl	%ebp, %edi
	addq	$3, %r15
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
.LBB2_4:                                # %.preheader52
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_6 Depth 2
	testl	%r14d, %r14d
	jle	.LBB2_7
# %bb.5:                                # %.lr.ph58
                                        #   in Loop: Header=BB2_4 Depth=1
	movl	%r8d, %r10d
	addq	%r15, %r10
	xorl	%r11d, %r11d
.LBB2_6:                                #   Parent Loop BB2_4 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movzbl	(%r10,%r11), %r13d
	incl	(%rcx,%r13,4)
	movzbl	-3(%r10,%r11), %ebp
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebp, %xmm0
	shll	$5, %r13d
	addsd	(%rdx,%r13), %xmm0
	movsd	%xmm0, (%rdx,%r13)
	movzbl	-2(%r10,%r11), %ebp
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebp, %xmm0
	addsd	8(%rdx,%r13), %xmm0
	movsd	%xmm0, 8(%rdx,%r13)
	movzbl	-1(%r10,%r11), %ebp
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebp, %xmm0
	addsd	16(%rdx,%r13), %xmm0
	movsd	%xmm0, 16(%rdx,%r13)
	addq	$4, %r11
	cmpq	%rsi, %r11
	jl	.LBB2_6
.LBB2_7:                                # %._crit_edge59
                                        #   in Loop: Header=BB2_4 Depth=1
	incq	%r9
	addl	%eax, %r8d
	cmpq	%rdi, %r9
	jne	.LBB2_4
.LBB2_8:                                # %.preheader
	testl	%ebx, %ebx
	jle	.LBB2_11
# %bb.9:                                # %.lr.ph62
	movq	ClusterColorSum(%rip), %rax
	movq	ClusterPixelCount(%rip), %rcx
	movq	ClusterCore(%rip), %rdx
	shlq	$2, %r12
	xorl	%esi, %esi
.LBB2_10:                               # =>This Inner Loop Header: Depth=1
	movsd	(%rax,%rsi,8), %xmm0            # xmm0 = mem[0],zero
	movl	(%rcx,%rsi), %edi
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rdi, %xmm1
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%rdx,%rsi,8)
	movsd	8(%rax,%rsi,8), %xmm0           # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%rdx,%rsi,8)
	movsd	16(%rax,%rsi,8), %xmm0          # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 16(%rdx,%rsi,8)
	addq	$4, %rsi
	cmpq	%rsi, %r12
	jne	.LBB2_10
.LBB2_11:                               # %._crit_edge63
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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_end2:
	.size	_Z13ClusterOffsetPhiii, .Lfunc_end2-_Z13ClusterOffsetPhiii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$584, %rsp                      # imm = 0x248
	.cfi_def_cfa_offset 640
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	320(%rsp), %rbx
	movl	$.L.str, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.1, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_1
# %bb.3:
	movq	%rax, %r14
	leaq	64(%rsp), %rbx
	movl	$.L.str, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_4
# %bb.5:
	movq	%r14, 40(%rsp)                  # 8-byte Spill
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	leaq	12(%rsp), %rbx
	movl	$.L.str.5, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movslq	(%rbx), %r13
	leaq	(,%r13,4), %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r12
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %rbx
	testq	%r13, %r13
	movq	%r12, 48(%rsp)                  # 8-byte Spill
	jle	.LBB3_9
# %bb.6:                                # %.lr.ph.preheader
	movq	%rbx, %r15
	xorl	%ebp, %ebp
.LBB3_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.5, %edi
	movq	%r15, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.5, %edi
	movq	%r12, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	incq	%rbp
	movl	12(%rsp), %r13d
	movslq	%r13d, %r14
	addq	$4, %r12
	addq	$4, %r15
	cmpq	%r14, %rbp
	jl	.LBB3_7
# %bb.8:                                # %._crit_edge.loopexit
	shlq	$2, %r14
.LBB3_9:                                # %._crit_edge
	shll	$2, %r13d
	movslq	%r13d, %r15
	shlq	$3, %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, ClusterCore(%rip)
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, ClusterPixelCount(%rip)
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, ClusterColorSum(%rip)
	leaq	36(%rsp), %r14
	movl	$4, %esi
	movl	$1, %edx
	movq	%r14, %rdi
	movq	40(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rcx
	callq	fread
	leaq	32(%rsp), %r12
	movl	$4, %esi
	movl	$1, %edx
	movq	%r12, %rdi
	movq	%r15, %rcx
	callq	fread
	movslq	(%r14), %r15
	leaq	(,%r15,4), %r13
	movl	(%r12), %ebp
	movl	%r13d, %eax
	imull	%ebp, %eax
	movslq	%eax, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r14
	movl	$1, %edx
	movq	%rax, %rdi
	movq	%r12, %rsi
	movq	40(%rsp), %rcx                  # 8-byte Reload
	callq	fread
	leaq	24(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	24(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r14, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	12(%rsp), %eax
	testl	%eax, %eax
	movq	48(%rsp), %r8                   # 8-byte Reload
	jle	.LBB3_12
# %bb.10:                               # %.lr.ph58
	movq	ClusterCore(%rip), %rcx
	shlq	$2, %rax
	xorl	%edx, %edx
.LBB3_11:                               # =>This Inner Loop Header: Depth=1
	movslq	(%r8,%rdx), %rsi
	imulq	%r13, %rsi
	movslq	(%rbx,%rdx), %rdi
	leaq	(%rsi,%rdi,4), %rsi
	movzbl	(%r14,%rsi), %edi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%edi, %xmm0
	movsd	%xmm0, (%rcx,%rdx,8)
	movslq	%esi, %rsi
	movzbl	1(%r14,%rsi), %edi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%edi, %xmm0
	movsd	%xmm0, 8(%rcx,%rdx,8)
	movzbl	2(%r14,%rsi), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	movsd	%xmm0, 16(%rcx,%rdx,8)
	movq	$0, 24(%rcx,%rdx,8)
	addq	$4, %rdx
	cmpq	%rdx, %rax
	jne	.LBB3_11
.LBB3_12:                               # %._crit_edge59
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r13
	movl	$1, (%rax)
	leaq	16(%rsp), %rbx
	movl	$4, %esi
	movq	%rbx, %rdi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movl	$4, %edx
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	ClusterCore(%rip), %rsi
	movslq	12(%rsp), %rdx
	shlq	$5, %rdx
	movl	$DevClusterCore, %edi
	xorl	%ecx, %ecx
	movl	$1, %r8d
	callq	hipMemcpyToSymbol
	movabsq	$4294967424, %rax               # imm = 0x100000080
	leaq	384(%rax), %rbx
.LBB3_13:                               # =>This Inner Loop Header: Depth=1
	movabsq	$4294967424, %rdi               # imm = 0x100000080
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_15
# %bb.14:                               #   in Loop: Header=BB3_13 Depth=1
	movq	24(%rsp), %rdi
	movl	12(%rsp), %esi
	movq	16(%rsp), %r8
	movl	%r15d, %edx
	movl	%ebp, %ecx
	callq	_Z29__device_stub__PixelToClusterPhiiiPi
.LBB3_15:                               #   in Loop: Header=BB3_13 Depth=1
	movq	16(%rsp), %rsi
	movl	$4, %edx
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	cmpl	$1, (%r13)
	je	.LBB3_17
# %bb.16:                               #   in Loop: Header=BB3_13 Depth=1
	movl	12(%rsp), %esi
	movq	%r14, %rdi
	movl	%r15d, %edx
	movl	%ebp, %ecx
	callq	_Z13ClusterOffsetPhiii
	movq	ClusterCore(%rip), %rsi
	movslq	12(%rsp), %rdx
	shlq	$5, %rdx
	movl	$DevClusterCore, %edi
	xorl	%ecx, %ecx
	movl	$1, %r8d
	callq	hipMemcpyToSymbol
	movl	$1, (%r13)
	movq	16(%rsp), %rdi
	movl	$4, %edx
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	jmp	.LBB3_13
.LBB3_17:
	leaq	36(%rsp), %rbx
	movl	$4, %esi
	movl	$1, %edx
	movq	%rbx, %rdi
	movq	56(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rcx
	callq	fwrite
	leaq	32(%rsp), %r15
	movl	$4, %esi
	movl	$1, %edx
	movq	%r15, %rdi
	movq	%r12, %rcx
	callq	fwrite
	movslq	(%rbx), %rax
	movslq	(%r15), %rsi
	imulq	%rax, %rsi
	shlq	$2, %rsi
	movl	$1, %edx
	movq	%r14, %rdi
	movq	%r12, %rcx
	callq	fwrite
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	%r14, %rdi
	callq	free
	movq	ClusterColorSum(%rip), %rdi
	callq	free
	movq	ClusterCore(%rip), %rdi
	callq	free
	movq	ClusterPixelCount(%rip), %rdi
	callq	free
	xorl	%eax, %eax
	addq	$584, %rsp                      # imm = 0x248
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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
.LBB3_1:
	.cfi_def_cfa_offset 640
	movq	stderr(%rip), %rcx
	movl	$.L.str.2, %edi
	movl	$19, %esi
	jmp	.LBB3_2
.LBB3_4:
	movq	stderr(%rip), %rcx
	movl	$.L.str.4, %edi
	movl	$22, %esi
.LBB3_2:
	movl	$1, %edx
	callq	fwrite
	xorl	%edi, %edi
	callq	exit
.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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14PixelToClusterPhiiiPi, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$0, 8(%rsp)
	movl	$1, (%rsp)
	movl	$DevClusterCore, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movl	$1024, %r9d                     # imm = 0x400
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	callq	__hipRegisterVar
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.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	ClusterCore,@object             # @ClusterCore
	.bss
	.globl	ClusterCore
	.p2align	3, 0x0
ClusterCore:
	.quad	0
	.size	ClusterCore, 8

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

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

	.type	DevClusterCore,@object          # @DevClusterCore
	.local	DevClusterCore
	.comm	DevClusterCore,1024,16
	.type	_Z14PixelToClusterPhiiiPi,@object # @_Z14PixelToClusterPhiiiPi
	.section	.rodata,"a",@progbits
	.globl	_Z14PixelToClusterPhiiiPi
	.p2align	3, 0x0
_Z14PixelToClusterPhiiiPi:
	.quad	_Z29__device_stub__PixelToClusterPhiiiPi
	.size	_Z14PixelToClusterPhiiiPi, 8

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

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"rb"
	.size	.L.str.1, 3

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Cannot open in.data"
	.size	.L.str.2, 20

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Cannot create out.data"
	.size	.L.str.4, 23

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%d"
	.size	.L.str.5, 3

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14PixelToClusterPhiiiPi"
	.size	.L__unnamed_1, 26

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"DevClusterCore"
	.size	.L__unnamed_2, 15

	.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

	.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 _Z29__device_stub__PixelToClusterPhiiiPi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym DevClusterCore
	.addrsig_sym _Z14PixelToClusterPhiiiPi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14PixelToClusterPhiiiPi ; -- Begin function _Z14PixelToClusterPhiiiPi
	.globl	_Z14PixelToClusterPhiiiPi
	.p2align	8
	.type	_Z14PixelToClusterPhiiiPi,@function
_Z14PixelToClusterPhiiiPi:              ; @_Z14PixelToClusterPhiiiPi
; %bb.0:
	s_clause 0x1
	s_load_b32 s8, s[0:1], 0x2c
	s_load_b128 s[4:7], s[0:1], 0x8
	s_add_u32 s2, s0, 32
	s_addc_u32 s3, s1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s12, s8, 0xffff
	s_mul_i32 s5, s5, s6
	s_mul_i32 s15, s15, s12
	s_lshl_b32 s5, s5, 2
	v_add_lshl_u32 v8, s15, v0, 2
	s_mov_b32 s6, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s5, v8
	s_cbranch_execz .LBB0_8
; %bb.1:                                ; %.lr.ph100
	s_getpc_b64 s[10:11]
	s_add_u32 s10, s10, DevClusterCore@rel32@lo+4
	s_addc_u32 s11, s11, DevClusterCore@rel32@hi+12
	v_mov_b32_e32 v9, 0
	s_load_b32 s13, s[2:3], 0x0
	s_clause 0x1
	s_load_b64 s[6:7], s[0:1], 0x0
	s_load_b64 s[8:9], s[0:1], 0x18
	s_clause 0x1
	s_load_b128 s[0:3], s[10:11], 0x0
	s_load_b64 s[10:11], s[10:11], 0x10
	s_cmp_gt_i32 s4, 1
	s_mov_b32 s16, 0
	s_cselect_b32 s14, -1, 0
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s13, s13, s12
	s_delay_alu instid0(SALU_CYCLE_1)
	s_lshl_b32 s15, s13, 2
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	v_ashrrev_i32_e32 v1, 31, v8
	v_add_co_u32 v0, vcc_lo, s6, v8
	v_mov_b32_e32 v10, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	s_and_not1_b32 vcc_lo, exec_lo, s14
	s_cbranch_vccnz .LBB0_5
; %bb.3:                                ; %.lr.ph.preheader
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_clause 0x1
	global_load_u16 v2, v[0:1], off
	global_load_u8 v3, v[0:1], off offset:2
	s_mov_b32 s17, 1
	s_getpc_b64 s[12:13]
	s_add_u32 s12, s12, DevClusterCore@rel32@lo+52
	s_addc_u32 s13, s13, DevClusterCore@rel32@hi+60
	s_waitcnt vmcnt(1)
	v_and_b32_e32 v4, 0xff, v2
	v_bfe_u32 v5, v2, 8, 8
	s_waitcnt vmcnt(0)
	v_cvt_f64_u32_e32 v[2:3], v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_and_b32_e32 v6, 0xffff, v4
	v_cvt_f64_u32_e32 v[4:5], v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cvt_f64_u32_e32 v[6:7], v6
	v_add_f64 v[10:11], s[10:11], -v[2:3]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[12:13], s[2:3], -v[4:5]
	v_add_f64 v[14:15], s[0:1], -v[6:7]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_f64 v[10:11], v[10:11], v[10:11]
	v_mul_f64 v[12:13], v[12:13], v[12:13]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_f64 v[14:15], v[14:15], v[14:15]
	v_cvt_i32_f64_e32 v10, v[10:11]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cvt_i32_f64_e32 v11, v[12:13]
	v_cvt_i32_f64_e32 v12, v[14:15]
	s_delay_alu instid0(VALU_DEP_1)
	v_add3_u32 v11, v11, v12, v10
	v_mov_b32_e32 v10, 0
.LBB0_4:                                ; %.lr.ph
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_add_u32 s18, s12, -16
	s_addc_u32 s19, s13, -1
	s_clause 0x1
	s_load_b128 s[20:23], s[18:19], 0x0
	s_load_b64 s[18:19], s[12:13], 0x0
	s_waitcnt lgkmcnt(0)
	v_add_f64 v[12:13], s[20:21], -v[6:7]
	v_add_f64 v[14:15], s[22:23], -v[4:5]
	v_add_f64 v[16:17], s[18:19], -v[2:3]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_f64 v[12:13], v[12:13], v[12:13]
	v_mul_f64 v[14:15], v[14:15], v[14:15]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_f64 v[16:17], v[16:17], v[16:17]
	v_cvt_i32_f64_e32 v12, v[12:13]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cvt_i32_f64_e32 v13, v[14:15]
	v_cvt_i32_f64_e32 v14, v[16:17]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v12, v13, v12, v14
	v_cmp_lt_i32_e32 vcc_lo, v12, v11
	v_min_i32_e32 v11, v12, v11
	v_cndmask_b32_e64 v10, v10, s17, vcc_lo
	s_add_i32 s17, s17, 1
	s_add_u32 s12, s12, 32
	s_addc_u32 s13, s13, 0
	s_cmp_eq_u32 s4, s17
	s_cbranch_scc0 .LBB0_4
.LBB0_5:                                ; %Flow116
                                        ;   in Loop: Header=BB0_2 Depth=1
	global_load_u8 v2, v[0:1], off offset:3
	s_mov_b32 s12, exec_lo
	s_waitcnt vmcnt(0)
	v_cmpx_ne_u32_e64 v10, v2
	s_cbranch_execz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_2 Depth=1
	global_store_b32 v9, v9, s[8:9]
.LBB0_7:                                ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s12
	v_add_nc_u32_e32 v8, s15, v8
	global_store_b8 v[0:1], v10, off offset:3
	v_cmp_le_i32_e32 vcc_lo, s5, v8
	s_or_b32 s16, vcc_lo, s16
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s16
	s_cbranch_execnz .LBB0_2
.LBB0_8:                                ; %Flow118
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14PixelToClusterPhiiiPi
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 24
		.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	_Z14PixelToClusterPhiiiPi, .Lfunc_end0-_Z14PixelToClusterPhiiiPi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 636
; NumSgprs: 26
; NumVgprs: 18
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 3
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 26
; 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
	.protected	DevClusterCore          ; @DevClusterCore
	.type	DevClusterCore,@object
	.section	.bss,"aw",@nobits
	.globl	DevClusterCore
	.p2align	4, 0x0
DevClusterCore:
	.zero	1024
	.size	DevClusterCore, 1024

	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.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 DevClusterCore
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14PixelToClusterPhiiiPi
    .private_segment_fixed_size: 0
    .sgpr_count:     26
    .sgpr_spill_count: 0
    .symbol:         _Z14PixelToClusterPhiiiPi.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
