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

#define MAX_ARGS 10
#define REC_LENGTH 49 // size of a record in db

#ifndef REC_WINDOW
 #define REC_WINDOW 15000 // number of records to take in at a time
#endif

#define LATITUDE_POS 28	// character position of the latitude value in each record
#define OPEN 10000	// initial value of nearest neighbors

struct neighbor
{
  	char entry[REC_LENGTH];
  	float dist;
};

/**
* Kernel
* Executed on GPU
* Calculates the Euclidean distance from each record in the database to the target position
*/
__global__ void euclid(char *data, float x2, float y2,float *z, int N, int W, int L_POS)
{
	int idx=blockIdx.x*blockDim.x+threadIdx.x;
	float tmp_lat=0.0, tmp_long=0.0;
	int position = ( idx * W ) + L_POS - 1;	
	
	if(idx < N) {
		char temp1[5];
		for( int i = 0 ; i < 5 ; i++ ) {
			temp1[i] = data[position+i];
		}
		char temp2[5];
		for( int i = 0 ; i < 5 ; i++ ) {
			temp2[i] = data[position+6+i];
		}
		
		int dig1, dig2, dig3, dig_1;
		if( temp1[0] == ' ' ) { dig1 = 0; }
		else {
			dig1 = temp1[0] - 48;
			tmp_lat += dig1 * 100;
		}
		if( temp1[1] == ' ' ) { dig2 = 0; }
		else {
			dig2 = temp1[1] - 48;
			tmp_lat += dig2 * 10;
		}
		if( temp1[2] == ' ' ) { dig3 = 0; }
		else {
			dig3 = temp1[2] - 48;
			tmp_lat += dig3 * 1;
		}
		dig_1 = temp1[4] - 48;
		tmp_lat += (float) dig_1 / 10;

		if( temp2[0] == ' ' ) { dig1 = 0; }
		else {
			dig1 = temp2[0] - 48;
			tmp_long += dig1 * 100;
		}
		if( temp2[1] == ' ' ) { dig2 = 0; }
		else {
			dig2 = temp2[1] - 48;
			tmp_long += dig2 * 10;
		}
		if( temp2[2] == ' ' ) { dig3 = 0; }
		else {
			dig3 = temp2[2] - 48;
			tmp_long += dig3 * 1;
		}
		dig_1 = temp2[4] - 48;
		tmp_long += (float) dig_1 / 10;

		z[idx]=sqrt(((tmp_lat-x2)*(tmp_lat-x2))+((tmp_long-y2)*(tmp_long-y2)));
	}
}

/**
* This program finds the k-nearest neighbors
* Usage:	./nn <filelist> <num> <target latitude> <target longitude>
*			filelist: File with the filenames to the records
*			num: Number of nearest neighbors to find
*			target lat: Latitude coordinate for distance calculations
*			target long: Longitude coordinate for distance calculations
* The filelist and data are generated by hurricane_gen.c
* REC_WINDOW has been assigned to read in a large number of records to increase work
*/
int main(int argc, char* argv[])
{
	FILE   *flist,*fp;
	int    i=0,j=0, k=0, rec_count=0, done=0;
	char   sandbox[REC_WINDOW * REC_LENGTH], dbname[64];
	struct neighbor *neighbors = NULL;
	float target_lat, target_long;
	char* goldfile;
	if(argc < 6)
	{
		fprintf(stderr, "Invalid set of arguments\n");
		exit(-1);
	}

	flist = fopen(argv[1], "r");

	if(!flist)
	{
		printf("error opening flist\n");
		exit(1);
	}

	k = atoi(argv[2]);
	target_lat = atof(argv[3]);
	target_long = atof(argv[4]);
	goldfile = argv[5];
	neighbors = (struct neighbor*) malloc(k*sizeof(struct neighbor));

	if(neighbors == NULL)
	{
		fprintf(stderr, "no room for neighbors\n");
		exit(0);
	}
  
	for(j=0;j<k;j++)
	{
		neighbors[j].dist = OPEN;
	}

	/**Main processing **/
	if(fscanf(flist, "%s\n", dbname) != 1)
	{
		fprintf(stderr, "error reading filelist\n");
		exit(0);
	}

	fp = fopen(dbname, "r");  
	if(!fp)
	{
		printf("error opening flist\n");
		exit(1);
	}	

        //Pointers to host memory
	float *z;
	//Pointers to device memory
	float *z_d;
	char *data;

	/**
	* Allocate memory on host and device
	*/
	z  = (float *) malloc(REC_WINDOW * sizeof(float));
	cudaMalloc((void **) &data, sizeof(char) * REC_WINDOW * REC_LENGTH);	
	cudaMalloc((void **) &z_d, sizeof(float) * REC_WINDOW);
	for(unsigned i=0; i<REC_WINDOW * REC_LENGTH;i++)
	{
		sandbox[i]=' ';
	}
	while(!done) {
		/**
		* Read in REC_WINDOW records of length REC_LENGTH
		* If this is the last file in the filelist, then done
		* else open next file to be read next iteration
		*/
		rec_count = fread(sandbox, REC_LENGTH, REC_WINDOW, fp);
		if(rec_count != REC_WINDOW) {
			if(!ferror(flist)) {// an eof occured
				fclose(fp);

				if(feof(flist))
					done = 1;
				else {
					if(fscanf(flist, "%s\n", dbname) != 1) {
						fprintf(stderr, "error reading filelist\n");
						exit(0);
					}

					fp = fopen(dbname, "r");

					if(!fp) {
						printf("error opening a db\n");
						exit(1);
					}
				}
			} else {
				perror("Error");
				exit(0);
			}
		}

	   	//Initialize a and b
		float x2 = target_lat;
		float y2 = target_long;

		/**
		* Transfer data from host to device
		*/
		cudaMemcpy( data, sandbox, (sizeof(char)*REC_WINDOW*REC_LENGTH), cudaMemcpyHostToDevice);

		/**
		* Execute kernel
		*/
		//Compute the execution configuration
		int block_size = 16;
		dim3 dimBlock(block_size);
		dim3 dimGrid( (REC_WINDOW/dimBlock.x) + (!(REC_WINDOW%dimBlock.x)?0:1) );

		//Add a and b, store in c
		euclid<<<dimGrid,dimBlock>>>(data, x2, y2, z_d, REC_WINDOW, REC_LENGTH, LATITUDE_POS);
		cudaThreadSynchronize();
		
		//Copy data from device memory to host memory
		cudaMemcpy( z, z_d, sizeof(float)*REC_WINDOW, cudaMemcpyDeviceToHost );
	  
		/**
		* Update list of nearest neighbors
		*/       
		for(i=0;i< rec_count;i++) {
			float max_dist = -1;
			int max_idx = 0;
			// find a neighbor with greatest dist and take his spot if allowed!
			for( j = 0 ; j < k ; j++ ) {
				if(neighbors[j].dist > max_dist) {
					max_dist = neighbors[j].dist;
					max_idx = j;
				}
			}
			// compare each record with max value to find the nearest neighbor
			if(z[i] < neighbors[max_idx].dist) {
				sandbox[(i+1)*REC_LENGTH-1] = '\0';
			  	strcpy(neighbors[max_idx].entry, sandbox +i*REC_LENGTH);
			  	neighbors[max_idx].dist = z[i];
			}
		}
	} //End while
        //Free memory
	free(z);
	cudaFree(data);
	cudaFree(z_d);

	fprintf(stderr, "The %d nearest neighbors are:\n", k);
	FILE* fpo = fopen("result.txt", "w");
	for( j = 0 ; j < k ; j++ ) {
	  if(!(neighbors[j].dist==OPEN)){
		 fprintf(stderr, "%s --> %f\n", neighbors[j].entry, neighbors[j].dist);
		 fprintf(fpo, "%s --> %f\n", neighbors[j].entry, neighbors[j].dist);
	  }
	}
	fclose(fpo);
	if(goldfile){
		FILE *gold = fopen(goldfile, "r");
		FILE *result = fopen("result.txt", "r");
		int result_error=0;
		while(!feof(gold)&&!feof(result)){
			if (fgetc(gold)!=fgetc(result)) {
				result_error = 1;
				break;
			}
		}
		if((feof(gold)^feof(result)) | result_error) {
			printf("\nFAILED\n");
		} else {
			printf("\nPASSED\n");
		}

		fclose(gold);
		fclose(result);
	}

	free(neighbors);
	fclose(flist);
}

	.file	"tmpxft_00343083_00000000-6_nn_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii
	.type	_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii, @function
_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 24(%rsp)
	leaq	76(%rsp), %rsi
	movl	%edx, 20(%rsp)
	leaq	48(%rsp), %rdx
	movl	%ecx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 12(%rsp)
	movss	%xmm0, 36(%rsp)
	movss	%xmm1, 32(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	56(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z6euclidPcffPfiii(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 208
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L2:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii, .-_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii
	.globl	_Z6euclidPcffPfiii
	.type	_Z6euclidPcffPfiii, @function
_Z6euclidPcffPfiii:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii
	.cfi_endproc
.LFE2053:
	.size	_Z6euclidPcffPfiii, .-_Z6euclidPcffPfiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Invalid set of arguments\n"
.LC2:
	.string	"r"
.LC3:
	.string	"error opening flist\n"
.LC4:
	.string	"no room for neighbors\n"
.LC6:
	.string	"%s\n"
.LC7:
	.string	"error reading filelist\n"
.LC8:
	.string	"error opening a db\n"
.LC9:
	.string	"Error"
.LC10:
	.string	"The %d nearest neighbors are:\n"
.LC11:
	.string	"w"
.LC12:
	.string	"result.txt"
.LC13:
	.string	"%s --> %f\n"
.LC14:
	.string	"\nFAILED\n"
.LC15:
	.string	"\nPASSED\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	leaq	-733184(%rsp), %r11
	.cfi_def_cfa 11, 733240
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$2008, %rsp
	.cfi_def_cfa_offset 735248
	movq	%fs:40, %rax
	movq	%rax, 735176(%rsp)
	xorl	%eax, %eax
	cmpl	$5, %edi
	jg	.L9
	movq	stderr(%rip), %rdi
	leaq	.LC1(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	jmp	.L58
.L9:
	movq	8(%rsi), %rdi
	leaq	.LC2(%rip), %r12
	movq	%rsi, %rbx
	movq	%r12, %rsi
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	jne	.L10
.L14:
	leaq	.LC3(%rip), %rsi
.L59:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
.L58:
	call	exit@PLT
.L10:
	movq	16(%rbx), %rdi
	call	atoi@PLT
	movq	24(%rbx), %rdi
	movslq	%eax, %r15
	call	atof@PLT
	movq	%r15, %r13
	movq	32(%rbx), %rdi
	imulq	$56, %r15, %r15
	movsd	%xmm0, 24(%rsp)
	call	atof@PLT
	movq	40(%rbx), %rax
	movq	%r15, %rdi
	movsd	%xmm0, 32(%rsp)
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movss	.LC5(%rip), %xmm0
	movq	%rax, %rbp
	xorl	%eax, %eax
	testq	%rbp, %rbp
	jne	.L11
	movq	stderr(%rip), %rdi
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	jmp	.L61
.L11:
	cmpl	%eax, %r13d
	jle	.L63
	imulq	$56, %rax, %rdx
	incq	%rax
	movss	%xmm0, 52(%rbp,%rdx)
	jmp	.L11
.L63:
	movq	8(%rsp), %rdi
	leaq	112(%rsp), %rbx
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	movq	%rbx, %rdx
	call	__isoc23_fscanf@PLT
	decl	%eax
	je	.L13
.L62:
	movq	stderr(%rip), %rdi
	leaq	.LC7(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
.L61:
	call	__fprintf_chk@PLT
	jmp	.L60
.L13:
	movq	%rbx, %rdi
	movq	%r12, %rsi
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.L14
	movl	$60000, %edi
	call	malloc@PLT
	leaq	80(%rsp), %rdi
	movl	$735000, %esi
	movq	%rax, %r14
	call	cudaMalloc@PLT
	leaq	72(%rsp), %rdi
	movl	$60000, %esi
	call	cudaMalloc@PLT
	leaq	176(%rsp), %rdx
	movl	$735000, %ecx
	movb	$32, %al
	movq	%rdx, %rdi
	cvtsd2ss	24(%rsp), %xmm2
	cvtsd2ss	32(%rsp), %xmm3
	movss	%xmm2, 24(%rsp)
	rep stosb
	movss	%xmm3, 32(%rsp)
.L26:
	leaq	176(%rsp), %rdi
	movq	%rbx, %r8
	movl	$15000, %ecx
	movl	$49, %edx
	movl	$735000, %esi
	call	__fread_chk@PLT
	movl	%eax, 56(%rsp)
	cmpl	$15000, %eax
	je	.L40
	movq	8(%rsp), %rdi
	call	ferror@PLT
	testl	%eax, %eax
	jne	.L16
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	8(%rsp), %rdi
	call	feof@PLT
	movl	%eax, %r12d
	testl	%eax, %eax
	jne	.L41
	movq	8(%rsp), %rdi
	leaq	112(%rsp), %rbx
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	movq	%rbx, %rdx
	call	__isoc23_fscanf@PLT
	decl	%eax
	jne	.L62
	movq	%rbx, %rdi
	leaq	.LC2(%rip), %rsi
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L15
	leaq	.LC8(%rip), %rsi
	jmp	.L59
.L16:
	leaq	.LC9(%rip), %rdi
	call	perror@PLT
.L60:
	xorl	%edi, %edi
	jmp	.L58
.L40:
	xorl	%r12d, %r12d
	jmp	.L15
.L41:
	movl	$1, %r12d
.L15:
	movq	80(%rsp), %rdi
	movl	$1, %ecx
	movl	$735000, %edx
	leaq	176(%rsp), %rsi
	call	cudaMemcpy@PLT
	movl	$2147484117, %edi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$268435457, %edx
	addq	%rdi, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	salq	$4, %rdx
	movq	%rdi, 100(%rsp)
	movl	$1, 108(%rsp)
	movq	%rdx, 88(%rsp)
	movl	$1, 96(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	movq	72(%rsp), %rsi
	movss	32(%rsp), %xmm1
	movl	$49, %ecx
	movl	$28, %r8d
	movss	24(%rsp), %xmm0
	movq	80(%rsp), %rdi
	movl	$15000, %edx
	call	_Z32__device_stub__Z6euclidPcffPfiiiPcffPfiii
.L18:
	call	cudaThreadSynchronize@PLT
	movq	72(%rsp), %rsi
	movl	$2, %ecx
	movq	%r14, %rdi
	movl	$60000, %edx
	call	cudaMemcpy@PLT
	xorl	%ecx, %ecx
	leaq	176(%rsp), %r8
.L19:
	cmpl	%ecx, 56(%rsp)
	jle	.L64
	movss	.LC0(%rip), %xmm0
	xorl	%edx, %edx
	xorl	%eax, %eax
.L25:
	cmpl	%edx, %r13d
	jle	.L65
	imulq	$56, %rdx, %rsi
	movss	52(%rbp,%rsi), %xmm1
	comiss	%xmm0, %xmm1
	maxss	%xmm0, %xmm1
	cmova	%edx, %eax
	movaps	%xmm1, %xmm0
	incq	%rdx
	jmp	.L25
.L65:
	cltq
	movss	(%r14,%rcx,4), %xmm0
	imulq	$56, %rax, %rax
	leaq	0(%rbp,%rax), %rdi
	movss	52(%rdi), %xmm1
	comiss	%xmm0, %xmm1
	jbe	.L23
	imull	$49, %ecx, %esi
	cmpq	%rax, %r15
	movq	%rcx, 48(%rsp)
	movq	%r8, 40(%rsp)
	movss	%xmm0, 60(%rsp)
	leal	48(%rsi), %edx
	movslq	%esi, %rsi
	movslq	%edx, %rdx
	movb	$0, 176(%rsp,%rdx)
	movq	%rax, %rdx
	cmovnb	%r15, %rdx
	subq	%rax, %rdx
	movl	$49, %eax
	cmpq	%rax, %rdx
	cmova	%rax, %rdx
	addq	%r8, %rsi
	call	__strcpy_chk@PLT
	movss	60(%rsp), %xmm0
	movq	48(%rsp), %rcx
	movq	40(%rsp), %r8
	movss	%xmm0, 52(%rax)
.L23:
	incq	%rcx
	jmp	.L19
.L64:
	testl	%r12d, %r12d
	je	.L26
	movq	%r14, %rdi
	xorl	%ebx, %ebx
	leaq	.LC13(%rip), %r15
	movq	%rbp, %r14
	call	free@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movl	%r13d, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	stderr(%rip), %rdi
	leaq	.LC10(%rip), %rdx
	call	__fprintf_chk@PLT
	leaq	.LC11(%rip), %rsi
	leaq	.LC12(%rip), %rdi
	call	fopen@PLT
	movq	%rax, 24(%rsp)
.L27:
	cmpl	%r13d, %ebx
	jge	.L66
	movss	52(%r14), %xmm0
	ucomiss	.LC5(%rip), %xmm0
	jp	.L45
	je	.L28
.L45:
	movq	stderr(%rip), %rdi
	movq	%r14, %rcx
	movq	%r15, %rdx
	movb	$1, %al
	movl	$2, %esi
	cvtss2sd	%xmm0, %xmm0
	call	__fprintf_chk@PLT
	movq	24(%rsp), %rdi
	movq	%r14, %rcx
	movq	%r15, %rdx
	movl	$2, %esi
	movb	$1, %al
	cvtss2sd	52(%r14), %xmm0
	call	__fprintf_chk@PLT
.L28:
	incl	%ebx
	addq	$56, %r14
	jmp	.L27
.L66:
	movq	24(%rsp), %rdi
	call	fclose@PLT
	cmpq	$0, 16(%rsp)
	je	.L31
	leaq	.LC2(%rip), %rbx
	movq	16(%rsp), %rdi
	movq	%rbx, %rsi
	call	fopen@PLT
	movq	%rbx, %rsi
	leaq	.LC12(%rip), %rdi
	movq	%rax, %r13
	call	fopen@PLT
	movq	%rax, %rbx
.L35:
	movq	%r13, %rdi
	call	feof@PLT
	testl	%eax, %eax
	je	.L32
.L34:
	xorl	%r12d, %r12d
	jmp	.L33
.L32:
	movq	%rbx, %rdi
	call	feof@PLT
	testl	%eax, %eax
	jne	.L34
	movq	%r13, %rdi
	call	fgetc@PLT
	movq	%rbx, %rdi
	movl	%eax, %r14d
	call	fgetc@PLT
	cmpl	%eax, %r14d
	je	.L35
.L33:
	movq	%r13, %rdi
	call	feof@PLT
	movq	%rbx, %rdi
	movl	%eax, %r14d
	call	feof@PLT
	leaq	.LC14(%rip), %rsi
	xorl	%eax, %r14d
	orl	%r12d, %r14d
	jne	.L57
	leaq	.LC15(%rip), %rsi
.L57:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	fclose@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
.L31:
	movq	%rbp, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	735176(%rsp), %rax
	subq	%fs:40, %rax
	je	.L38
	call	__stack_chk_fail@PLT
.L38:
	addq	$735192, %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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC16:
	.string	"_Z6euclidPcffPfiii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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	.LC16(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6euclidPcffPfiii(%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
.LFE2055:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	-1082130432
	.align 4
.LC5:
	.long	1176256512
	.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 <sys/time.h>
#include "hip/hip_runtime.h"
#include <string.h>

#define MAX_ARGS 10
#define REC_LENGTH 49 // size of a record in db

#ifndef REC_WINDOW
 #define REC_WINDOW 15000 // number of records to take in at a time
#endif

#define LATITUDE_POS 28	// character position of the latitude value in each record
#define OPEN 10000	// initial value of nearest neighbors

struct neighbor
{
  	char entry[REC_LENGTH];
  	float dist;
};

/**
* Kernel
* Executed on GPU
* Calculates the Euclidean distance from each record in the database to the target position
*/
__global__ void euclid(char *data, float x2, float y2,float *z, int N, int W, int L_POS)
{
	int idx=blockIdx.x*blockDim.x+threadIdx.x;
	float tmp_lat=0.0, tmp_long=0.0;
	int position = ( idx * W ) + L_POS - 1;	
	
	if(idx < N) {
		char temp1[5];
		for( int i = 0 ; i < 5 ; i++ ) {
			temp1[i] = data[position+i];
		}
		char temp2[5];
		for( int i = 0 ; i < 5 ; i++ ) {
			temp2[i] = data[position+6+i];
		}
		
		int dig1, dig2, dig3, dig_1;
		if( temp1[0] == ' ' ) { dig1 = 0; }
		else {
			dig1 = temp1[0] - 48;
			tmp_lat += dig1 * 100;
		}
		if( temp1[1] == ' ' ) { dig2 = 0; }
		else {
			dig2 = temp1[1] - 48;
			tmp_lat += dig2 * 10;
		}
		if( temp1[2] == ' ' ) { dig3 = 0; }
		else {
			dig3 = temp1[2] - 48;
			tmp_lat += dig3 * 1;
		}
		dig_1 = temp1[4] - 48;
		tmp_lat += (float) dig_1 / 10;

		if( temp2[0] == ' ' ) { dig1 = 0; }
		else {
			dig1 = temp2[0] - 48;
			tmp_long += dig1 * 100;
		}
		if( temp2[1] == ' ' ) { dig2 = 0; }
		else {
			dig2 = temp2[1] - 48;
			tmp_long += dig2 * 10;
		}
		if( temp2[2] == ' ' ) { dig3 = 0; }
		else {
			dig3 = temp2[2] - 48;
			tmp_long += dig3 * 1;
		}
		dig_1 = temp2[4] - 48;
		tmp_long += (float) dig_1 / 10;

		z[idx]=sqrt(((tmp_lat-x2)*(tmp_lat-x2))+((tmp_long-y2)*(tmp_long-y2)));
	}
}

/**
* This program finds the k-nearest neighbors
* Usage:	./nn <filelist> <num> <target latitude> <target longitude>
*			filelist: File with the filenames to the records
*			num: Number of nearest neighbors to find
*			target lat: Latitude coordinate for distance calculations
*			target long: Longitude coordinate for distance calculations
* The filelist and data are generated by hurricane_gen.c
* REC_WINDOW has been assigned to read in a large number of records to increase work
*/
int main(int argc, char* argv[])
{
	FILE   *flist,*fp;
	int    i=0,j=0, k=0, rec_count=0, done=0;
	char   sandbox[REC_WINDOW * REC_LENGTH], dbname[64];
	struct neighbor *neighbors = NULL;
	float target_lat, target_long;
	char* goldfile;
	if(argc < 6)
	{
		fprintf(stderr, "Invalid set of arguments\n");
		exit(-1);
	}

	flist = fopen(argv[1], "r");

	if(!flist)
	{
		printf("error opening flist\n");
		exit(1);
	}

	k = atoi(argv[2]);
	target_lat = atof(argv[3]);
	target_long = atof(argv[4]);
	goldfile = argv[5];
	neighbors = (struct neighbor*) malloc(k*sizeof(struct neighbor));

	if(neighbors == NULL)
	{
		fprintf(stderr, "no room for neighbors\n");
		exit(0);
	}
  
	for(j=0;j<k;j++)
	{
		neighbors[j].dist = OPEN;
	}

	/**Main processing **/
	if(fscanf(flist, "%s\n", dbname) != 1)
	{
		fprintf(stderr, "error reading filelist\n");
		exit(0);
	}

	fp = fopen(dbname, "r");  
	if(!fp)
	{
		printf("error opening flist\n");
		exit(1);
	}	

        //Pointers to host memory
	float *z;
	//Pointers to device memory
	float *z_d;
	char *data;

	/**
	* Allocate memory on host and device
	*/
	z  = (float *) malloc(REC_WINDOW * sizeof(float));
	hipMalloc((void **) &data, sizeof(char) * REC_WINDOW * REC_LENGTH);	
	hipMalloc((void **) &z_d, sizeof(float) * REC_WINDOW);
	for(unsigned i=0; i<REC_WINDOW * REC_LENGTH;i++)
	{
		sandbox[i]=' ';
	}
	while(!done) {
		/**
		* Read in REC_WINDOW records of length REC_LENGTH
		* If this is the last file in the filelist, then done
		* else open next file to be read next iteration
		*/
		rec_count = fread(sandbox, REC_LENGTH, REC_WINDOW, fp);
		if(rec_count != REC_WINDOW) {
			if(!ferror(flist)) {// an eof occured
				fclose(fp);

				if(feof(flist))
					done = 1;
				else {
					if(fscanf(flist, "%s\n", dbname) != 1) {
						fprintf(stderr, "error reading filelist\n");
						exit(0);
					}

					fp = fopen(dbname, "r");

					if(!fp) {
						printf("error opening a db\n");
						exit(1);
					}
				}
			} else {
				perror("Error");
				exit(0);
			}
		}

	   	//Initialize a and b
		float x2 = target_lat;
		float y2 = target_long;

		/**
		* Transfer data from host to device
		*/
		hipMemcpy( data, sandbox, (sizeof(char)*REC_WINDOW*REC_LENGTH), hipMemcpyHostToDevice);

		/**
		* Execute kernel
		*/
		//Compute the execution configuration
		int block_size = 16;
		dim3 dimBlock(block_size);
		dim3 dimGrid( (REC_WINDOW/dimBlock.x) + (!(REC_WINDOW%dimBlock.x)?0:1) );

		//Add a and b, store in c
		euclid<<<dimGrid,dimBlock>>>(data, x2, y2, z_d, REC_WINDOW, REC_LENGTH, LATITUDE_POS);
		hipDeviceSynchronize();
		
		//Copy data from device memory to host memory
		hipMemcpy( z, z_d, sizeof(float)*REC_WINDOW, hipMemcpyDeviceToHost );
	  
		/**
		* Update list of nearest neighbors
		*/       
		for(i=0;i< rec_count;i++) {
			float max_dist = -1;
			int max_idx = 0;
			// find a neighbor with greatest dist and take his spot if allowed!
			for( j = 0 ; j < k ; j++ ) {
				if(neighbors[j].dist > max_dist) {
					max_dist = neighbors[j].dist;
					max_idx = j;
				}
			}
			// compare each record with max value to find the nearest neighbor
			if(z[i] < neighbors[max_idx].dist) {
				sandbox[(i+1)*REC_LENGTH-1] = '\0';
			  	strcpy(neighbors[max_idx].entry, sandbox +i*REC_LENGTH);
			  	neighbors[max_idx].dist = z[i];
			}
		}
	} //End while
        //Free memory
	free(z);
	hipFree(data);
	hipFree(z_d);

	fprintf(stderr, "The %d nearest neighbors are:\n", k);
	FILE* fpo = fopen("result.txt", "w");
	for( j = 0 ; j < k ; j++ ) {
	  if(!(neighbors[j].dist==OPEN)){
		 fprintf(stderr, "%s --> %f\n", neighbors[j].entry, neighbors[j].dist);
		 fprintf(fpo, "%s --> %f\n", neighbors[j].entry, neighbors[j].dist);
	  }
	}
	fclose(fpo);
	if(goldfile){
		FILE *gold = fopen(goldfile, "r");
		FILE *result = fopen("result.txt", "r");
		int result_error=0;
		while(!feof(gold)&&!feof(result)){
			if (fgetc(gold)!=fgetc(result)) {
				result_error = 1;
				break;
			}
		}
		if((feof(gold)^feof(result)) | result_error) {
			printf("\nFAILED\n");
		} else {
			printf("\nPASSED\n");
		}

		fclose(gold);
		fclose(result);
	}

	free(neighbors);
	fclose(flist);
}

	.text
	.file	"nn_cuda.hip"
	.globl	_Z21__device_stub__euclidPcffPfiii # -- Begin function _Z21__device_stub__euclidPcffPfiii
	.type	_Z21__device_stub__euclidPcffPfiii,@function
_Z21__device_stub__euclidPcffPfiii:     # @_Z21__device_stub__euclidPcffPfiii
	.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	$160, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	28(%rsp), %rdi
	movss	%xmm0, (%rdi)
	leaq	24(%rsp), %r9
	movss	%xmm1, (%r9)
	leaq	48(%rsp), %r10
	movq	%rsi, (%r10)
	leaq	20(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	16(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	12(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%r9, 16(%rbx)
	movq	%r10, 24(%rbx)
	movq	%rsi, 32(%rbx)
	movq	%rdx, 40(%rbx)
	movq	%rcx, 48(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	40(%rsp), %r12
	leaq	32(%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	$_Z6euclidPcffPfiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	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	_Z21__device_stub__euclidPcffPfiii, .Lfunc_end0-_Z21__device_stub__euclidPcffPfiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0xbf800000                      # float -1
.LCPI1_1:
	.long	0x461c4000                      # float 1.0E+4
	.text
	.globl	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	$735160, %rsp                   # imm = 0xB37B8
	.cfi_def_cfa_offset 735216
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$5, %edi
	jle	.LBB1_47
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	movl	$.L.str.1, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_45
# %bb.2:
	movq	%rax, %r15
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	24(%rbx), %rdi
	callq	atof
	movsd	%xmm0, 40(%rsp)                 # 8-byte Spill
	movq	32(%rbx), %rdi
	callq	atof
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	movq	40(%rbx), %r12
	movslq	%ebp, %rax
	imulq	$56, %rax, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB1_48
# %bb.3:                                # %.preheader134
	movl	%ebp, %ebx
	testl	%ebp, %ebp
	jle	.LBB1_6
# %bb.4:                                # %.lr.ph.preheader
	imulq	$56, %rbx, %rdx
	xorl	%ecx, %ecx
.LBB1_5:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$1176256512, 52(%rax,%rcx)      # imm = 0x461C4000
	addq	$56, %rcx
	cmpq	%rcx, %rdx
	jne	.LBB1_5
.LBB1_6:                                # %._crit_edge
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	leaq	96(%rsp), %rdx
	movl	$.L.str.4, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB1_41
# %bb.7:
	leaq	96(%rsp), %rdi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	testq	%rax, %rax
	je	.LBB1_45
# %bb.8:                                # %.preheader133
	movq	%r12, 80(%rsp)                  # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtsd2ss	40(%rsp), %xmm0                 # 8-byte Folded Reload
	movss	%xmm0, 68(%rsp)                 # 4-byte Spill
	xorps	%xmm0, %xmm0
	cvtsd2ss	32(%rsp), %xmm0                 # 8-byte Folded Reload
	movss	%xmm0, 64(%rsp)                 # 4-byte Spill
	movabsq	$4294967312, %r12               # imm = 0x100000010
	movl	$60000, %edi                    # imm = 0xEA60
	callq	malloc
	movq	%rax, %r13
	leaq	48(%rsp), %rdi
	movl	$735000, %esi                   # imm = 0xB3718
	callq	hipMalloc
	leaq	56(%rsp), %rdi
	movl	$60000, %esi                    # imm = 0xEA60
	callq	hipMalloc
	leaq	160(%rsp), %rdi
	movl	$735000, %edx                   # imm = 0xB3718
	movl	$32, %esi
	callq	memset@PLT
	movq	8(%rsp), %rax                   # 8-byte Reload
	addq	$52, %rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	leaq	922(%r12), %rax
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	leaq	96(%rsp), %r14
	movq	%r15, 72(%rsp)                  # 8-byte Spill
.LBB1_9:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_19 Depth 2
                                        #       Child Loop BB1_21 Depth 3
	movl	$49, %esi
	movl	$15000, %edx                    # imm = 0x3A98
	leaq	160(%rsp), %rdi
	movq	24(%rsp), %rcx                  # 8-byte Reload
	callq	fread
	movq	%rax, %r12
	movb	$1, %al
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	cmpl	$15000, %r12d                   # imm = 0x3A98
	je	.LBB1_15
# %bb.10:                               #   in Loop: Header=BB1_9 Depth=1
	movq	%r15, %rdi
	callq	ferror
	testl	%eax, %eax
	jne	.LBB1_40
# %bb.11:                               #   in Loop: Header=BB1_9 Depth=1
	movq	24(%rsp), %rdi                  # 8-byte Reload
	callq	fclose
	movq	%r15, %rdi
	callq	feof
	testl	%eax, %eax
	je	.LBB1_13
# %bb.12:                               #   in Loop: Header=BB1_9 Depth=1
	movl	$0, 20(%rsp)                    # 4-byte Folded Spill
	jmp	.LBB1_15
.LBB1_13:                               #   in Loop: Header=BB1_9 Depth=1
	movl	$.L.str.4, %esi
	movq	%r15, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB1_41
# %bb.14:                               #   in Loop: Header=BB1_9 Depth=1
	movl	$.L.str.1, %esi
	movq	%r14, %rdi
	callq	fopen
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	testq	%rax, %rax
	je	.LBB1_44
.LBB1_15:                               #   in Loop: Header=BB1_9 Depth=1
	movq	48(%rsp), %rdi
	movl	$735000, %edx                   # imm = 0xB3718
	leaq	160(%rsp), %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	88(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movabsq	$4294967312, %rdx               # imm = 0x100000010
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_17
# %bb.16:                               #   in Loop: Header=BB1_9 Depth=1
	movq	48(%rsp), %rdi
	movq	56(%rsp), %rsi
	movss	68(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	movss	64(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	movl	$15000, %edx                    # imm = 0x3A98
	movl	$49, %ecx
	movl	$28, %r8d
	callq	_Z21__device_stub__euclidPcffPfiii
.LBB1_17:                               #   in Loop: Header=BB1_9 Depth=1
	callq	hipDeviceSynchronize
	movq	56(%rsp), %rsi
	movl	$60000, %edx                    # imm = 0xEA60
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%r12d, %r12d
	movq	8(%rsp), %rsi                   # 8-byte Reload
	jle	.LBB1_26
# %bb.18:                               # %.preheader.preheader
                                        #   in Loop: Header=BB1_9 Depth=1
	movl	%r12d, %r12d
	xorl	%r15d, %r15d
.LBB1_19:                               # %.preheader
                                        #   Parent Loop BB1_9 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_21 Depth 3
	testl	%ebp, %ebp
	jle	.LBB1_22
# %bb.20:                               # %.lr.ph140.preheader
                                        #   in Loop: Header=BB1_19 Depth=2
	xorl	%ecx, %ecx
	movq	32(%rsp), %rdx                  # 8-byte Reload
	xorl	%eax, %eax
	movss	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
.LBB1_21:                               # %.lr.ph140
                                        #   Parent Loop BB1_9 Depth=1
                                        #     Parent Loop BB1_19 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movss	(%rdx), %xmm1                   # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	maxss	%xmm0, %xmm1
	cmoval	%ecx, %eax
	incq	%rcx
	addq	$56, %rdx
	movaps	%xmm1, %xmm0
	cmpq	%rcx, %rbx
	jne	.LBB1_21
	jmp	.LBB1_23
.LBB1_22:                               #   in Loop: Header=BB1_19 Depth=2
	xorl	%eax, %eax
.LBB1_23:                               # %._crit_edge141
                                        #   in Loop: Header=BB1_19 Depth=2
	movss	(%r13,%r15,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	cltq
	imulq	$56, %rax, %rdi
	movss	52(%rsi,%rdi), %xmm0            # xmm0 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jbe	.LBB1_25
# %bb.24:                               #   in Loop: Header=BB1_19 Depth=2
	movq	%r13, %r14
	movl	%ebp, %r13d
	leaq	(%rsi,%rdi), %rbp
	addq	$52, %rbp
	addq	%rsi, %rdi
	imull	$49, %r15d, %eax
	leal	48(%rax), %ecx
	movslq	%ecx, %rcx
	movb	$0, 160(%rsp,%rcx)
	leaq	(%rsp,%rax), %rsi
	addq	$160, %rsi
	movss	%xmm1, 40(%rsp)                 # 4-byte Spill
	callq	strcpy
	movq	8(%rsp), %rsi                   # 8-byte Reload
	movss	40(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rbp)
	movl	%r13d, %ebp
	movq	%r14, %r13
	leaq	96(%rsp), %r14
.LBB1_25:                               #   in Loop: Header=BB1_19 Depth=2
	incq	%r15
	cmpq	%r12, %r15
	jne	.LBB1_19
.LBB1_26:                               # %.loopexit
                                        #   in Loop: Header=BB1_9 Depth=1
	cmpb	$0, 20(%rsp)                    # 1-byte Folded Reload
	movq	72(%rsp), %r15                  # 8-byte Reload
	jne	.LBB1_9
# %bb.27:
	movq	%r13, %rdi
	callq	free
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	stderr(%rip), %rdi
	movl	$.L.str.8, %esi
	movl	%ebp, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.9, %edi
	movl	$.L.str.10, %esi
	callq	fopen
	movq	%rax, %r12
	testl	%ebp, %ebp
	movq	8(%rsp), %r15                   # 8-byte Reload
	jle	.LBB1_32
# %bb.28:                               # %.lr.ph146.preheader
	movss	.LCPI1_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
.LBB1_29:                               # %.lr.ph146
                                        # =>This Inner Loop Header: Depth=1
	movss	52(%r15), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jne	.LBB1_31
	jp	.LBB1_31
.LBB1_30:                               #   in Loop: Header=BB1_29 Depth=1
	addq	$56, %r15
	decq	%rbx
	jne	.LBB1_29
	jmp	.LBB1_32
.LBB1_31:                               #   in Loop: Header=BB1_29 Depth=1
	movq	stderr(%rip), %rdi
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.11, %esi
	movq	%r15, %rdx
	movb	$1, %al
	callq	fprintf
	xorps	%xmm0, %xmm0
	cvtss2sd	52(%r15), %xmm0
	movl	$.L.str.11, %esi
	movq	%r12, %rdi
	movq	%r15, %rdx
	movb	$1, %al
	callq	fprintf
	movss	.LCPI1_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	jmp	.LBB1_30
.LBB1_32:                               # %._crit_edge147
	movq	%r12, %rdi
	callq	fclose
	movq	80(%rsp), %rdi                  # 8-byte Reload
	testq	%rdi, %rdi
	je	.LBB1_39
# %bb.33:
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	movl	$.L.str.9, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r15
	xorl	%r12d, %r12d
.LBB1_34:                               # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	feof
	testl	%eax, %eax
	jne	.LBB1_38
# %bb.35:                               #   in Loop: Header=BB1_34 Depth=1
	movq	%r15, %rdi
	callq	feof
	testl	%eax, %eax
	jne	.LBB1_38
# %bb.36:                               #   in Loop: Header=BB1_34 Depth=1
	movq	%rbx, %rdi
	callq	fgetc
	movl	%eax, %ebp
	movq	%r15, %rdi
	callq	fgetc
	cmpl	%eax, %ebp
	je	.LBB1_34
# %bb.37:
	movl	$1, %r12d
.LBB1_38:                               # %.critedge
	movq	%rbx, %rdi
	callq	feof
	movl	%eax, %ebp
	movq	%r15, %rdi
	callq	feof
	xorl	%ebp, %eax
	orl	%r12d, %eax
	movl	$.Lstr.2, %eax
	movl	$.Lstr.3, %edi
	cmoveq	%rax, %rdi
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	fclose
	movq	%r15, %rdi
	callq	fclose
.LBB1_39:
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	free
	movq	72(%rsp), %rdi                  # 8-byte Reload
	callq	fclose
	xorl	%eax, %eax
	addq	$735160, %rsp                   # imm = 0xB37B8
	.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
.LBB1_40:
	.cfi_def_cfa_offset 735216
	movl	$.L.str.7, %edi
	callq	perror
	jmp	.LBB1_43
.LBB1_41:
	movq	stderr(%rip), %rcx
	movl	$.L.str.5, %edi
	movl	$23, %esi
.LBB1_42:
	movl	$1, %edx
	callq	fwrite@PLT
.LBB1_43:
	xorl	%edi, %edi
	callq	exit
.LBB1_44:
	movl	$.Lstr.4, %edi
	jmp	.LBB1_46
.LBB1_45:
	movl	$.Lstr.1, %edi
.LBB1_46:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.LBB1_47:
	movq	stderr(%rip), %rcx
	movl	$.L.str, %edi
	movl	$25, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %edi
	callq	exit
.LBB1_48:
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$22, %esi
	jmp	.LBB1_42
.Lfunc_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6euclidPcffPfiii, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z6euclidPcffPfiii,@object      # @_Z6euclidPcffPfiii
	.section	.rodata,"a",@progbits
	.globl	_Z6euclidPcffPfiii
	.p2align	3, 0x0
_Z6euclidPcffPfiii:
	.quad	_Z21__device_stub__euclidPcffPfiii
	.size	_Z6euclidPcffPfiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Invalid set of arguments\n"
	.size	.L.str, 26

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"r"
	.size	.L.str.1, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"no room for neighbors\n"
	.size	.L.str.3, 23

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"error reading filelist\n"
	.size	.L.str.5, 24

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Error"
	.size	.L.str.7, 6

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"The %d nearest neighbors are:\n"
	.size	.L.str.8, 31

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"result.txt"
	.size	.L.str.9, 11

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"w"
	.size	.L.str.10, 2

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"%s --> %f\n"
	.size	.L.str.11, 11

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6euclidPcffPfiii"
	.size	.L__unnamed_1, 19

	.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.1,@object                 # @str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr.1:
	.asciz	"error opening flist"
	.size	.Lstr.1, 20

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\nPASSED"
	.size	.Lstr.2, 8

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"\nFAILED"
	.size	.Lstr.3, 8

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"error opening a db"
	.size	.Lstr.4, 19

	.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 _Z21__device_stub__euclidPcffPfiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6euclidPcffPfiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6euclidPcffPfiii      ; -- Begin function _Z6euclidPcffPfiii
	.globl	_Z6euclidPcffPfiii
	.p2align	8
	.type	_Z6euclidPcffPfiii,@function
_Z6euclidPcffPfiii:                     ; @_Z6euclidPcffPfiii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b128 s[4:7], s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_i32_e64 s4, v1
	s_cbranch_execz .LBB0_10
; %bb.1:                                ; %.preheader58.preheader
	s_load_b64 s[2:3], s[0:1], 0x0
	v_mad_u64_u32 v[2:3], null, v1, s5, s[6:7]
	s_mov_b64 s[4:5], 0
                                        ; implicit-def: $vgpr12
                                        ; implicit-def: $vgpr14
                                        ; implicit-def: $vgpr11
                                        ; implicit-def: $vgpr13
                                        ; implicit-def: $vgpr3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v4, 31, v2
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v5, vcc_lo, v2, s2
	v_add_co_ci_u32_e64 v0, s2, s3, v4, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v8, s2, v5, -1
	v_add_co_ci_u32_e64 v9, s2, -1, v0, s2
.LBB0_2:                                ; %.preheader58
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v6, s2, v8, s4
	v_add_co_ci_u32_e64 v7, s2, s5, v9, s2
	v_and_b32_e32 v2, 0xff, v12
	v_lshlrev_b16 v10, 8, v13
	v_and_b32_e32 v3, 0xff, v3
	global_load_u8 v0, v[6:7], off
	v_lshlrev_b16 v6, 8, v14
	v_and_b32_e32 v7, 0xff, v11
	s_lshl_b32 s2, s4, 3
	v_and_b32_e32 v3, 0xffff, v3
	s_lshl_b64 s[6:7], 0xff, s2
	v_or_b32_e32 v2, v2, v6
	v_or_b32_e32 v6, v7, v10
	s_add_u32 s4, s4, 1
	s_addc_u32 s5, s5, 0
	s_cmp_eq_u32 s4, 5
	v_and_b32_e32 v2, 0xffff, v2
	v_lshlrev_b32_e32 v6, 16, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_or_b32_e32 v2, v2, v6
	s_waitcnt vmcnt(0)
	v_perm_b32 v0, v0, v0, 0x4040404
	v_bfi_b32 v3, s7, v0, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_bfi_b32 v2, s6, v0, v2
	v_lshrrev_b64 v[10:11], 24, v[2:3]
	v_lshrrev_b32_e32 v6, 16, v2
	v_lshrrev_b32_e32 v7, 8, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_and_b32_e32 v0, 0xff, v6
	v_lshlrev_b16 v10, 8, v10
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshlrev_b16 v12, 8, v7
	v_or_b32_e32 v11, v0, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_and_b32_e32 v13, 0xffff, v12
	v_and_b32_e32 v10, 0xff, v2
	v_lshlrev_b32_e32 v14, 16, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_or_b32_e32 v12, v10, v12
	v_or_b32_e32 v15, v13, v14
	v_lshrrev_b32_e32 v13, 24, v14
	s_delay_alu instid0(VALU_DEP_2)
	v_lshrrev_b32_e32 v14, 8, v15
	s_cbranch_scc0 .LBB0_2
; %bb.3:                                ; %.preheader
	v_add_co_ci_u32_e32 v4, vcc_lo, s3, v4, vcc_lo
	v_add_co_u32 v11, vcc_lo, v5, 5
	s_mov_b64 s[2:3], 0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v12, vcc_lo, 0, v4, vcc_lo
                                        ; implicit-def: $vgpr16
                                        ; implicit-def: $vgpr18
                                        ; implicit-def: $vgpr14
                                        ; implicit-def: $vgpr17
                                        ; implicit-def: $vgpr5
.LBB0_4:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v8, vcc_lo, v11, s2
	v_add_co_ci_u32_e32 v9, vcc_lo, s3, v12, vcc_lo
	v_and_b32_e32 v13, 0xff, v14
	v_lshlrev_b16 v14, 8, v17
	v_and_b32_e32 v5, 0xff, v5
	global_load_u8 v4, v[8:9], off
	v_and_b32_e32 v8, 0xff, v16
	v_lshlrev_b16 v9, 8, v18
	s_lshl_b32 s4, s2, 3
	v_and_b32_e32 v5, 0xffff, v5
	s_lshl_b64 s[4:5], 0xff, s4
	s_add_u32 s2, s2, 1
	v_or_b32_e32 v8, v8, v9
	v_or_b32_e32 v9, v13, v14
	s_addc_u32 s3, s3, 0
	s_cmp_eq_u32 s2, 5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_and_b32_e32 v8, 0xffff, v8
	v_lshlrev_b32_e32 v9, 16, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_or_b32_e32 v8, v8, v9
	s_waitcnt vmcnt(0)
	v_perm_b32 v4, v4, v4, 0x4040404
	v_bfi_b32 v5, s5, v4, v5
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_bfi_b32 v4, s4, v4, v8
	v_lshrrev_b64 v[14:15], 24, v[4:5]
	v_lshrrev_b32_e32 v9, 16, v4
	v_lshrrev_b32_e32 v13, 8, v4
	v_and_b32_e32 v15, 0xff, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_and_b32_e32 v8, 0xff, v9
	v_lshlrev_b16 v14, 8, v14
	v_lshlrev_b16 v16, 8, v13
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_or_b32_e32 v14, v8, v14
	v_and_b32_e32 v17, 0xffff, v16
	v_or_b32_e32 v16, v15, v16
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b32_e32 v18, 16, v14
	v_or_b32_e32 v19, v17, v18
	v_lshrrev_b32_e32 v17, 24, v18
	s_delay_alu instid0(VALU_DEP_2)
	v_lshrrev_b32_e32 v18, 8, v19
	s_cbranch_scc0 .LBB0_4
; %bb.5:
	v_bfe_i32 v2, v2, 0, 8
	s_movk_i32 s2, 0x64
	v_cmp_ne_u16_e32 vcc_lo, 32, v10
	s_mov_b32 s3, exec_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_i32_i24 v2, v2, s2, 0xffffed40
	v_cvt_f32_i32_e32 v2, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v2, 0, v2 :: v_dual_and_b32 v11, 0xff, v7
	v_cmpx_ne_u16_e32 32, v11
; %bb.6:
	v_bfe_i32 v7, v7, 0, 8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_i32_i24 v7, v7, 10, 0xfffffe20
	v_cvt_f32_i32_e32 v7, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f32_e32 v2, v2, v7
; %bb.7:
	s_or_b32 exec_lo, exec_lo, s3
	v_bfe_i32 v4, v4, 0, 8
	v_cmp_ne_u16_e32 vcc_lo, 32, v15
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_i32_i24 v4, v4, s2, 0xffffed40
	s_mov_b32 s2, exec_lo
	v_cvt_f32_i32_e32 v4, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v4, 0, v4 :: v_dual_and_b32 v7, 0xff, v13
	v_cmpx_ne_u16_e32 32, v7
; %bb.8:
	v_bfe_i32 v7, v13, 0, 8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_i32_i24 v7, v7, 10, 0xfffffe20
	v_cvt_f32_i32_e32 v7, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f32_e32 v4, v4, v7
; %bb.9:
	s_or_b32 exec_lo, exec_lo, s2
	v_bfe_i32 v3, v3, 0, 8
	v_bfe_i32 v5, v5, 0, 8
	v_bfe_i32 v9, v9, 0, 8
	s_load_b128 s[4:7], s[0:1], 0x8
	v_cmp_ne_u16_e64 s0, 32, v8
	v_subrev_nc_u32_e32 v3, 48, v3
	v_subrev_nc_u32_e32 v5, 48, v5
	v_subrev_nc_u32_e32 v9, 48, v9
	v_bfe_i32 v6, v6, 0, 8
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cvt_f32_i32_e32 v3, v3
	v_cvt_f32_i32_e32 v5, v5
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cvt_f32_i32_e32 v9, v9
	v_subrev_nc_u32_e32 v6, 48, v6
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_div_scale_f32 v7, null, 0x41200000, 0x41200000, v3
	v_div_scale_f32 v10, null, 0x41200000, 0x41200000, v5
	v_div_scale_f32 v15, vcc_lo, v3, 0x41200000, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_rcp_f32_e32 v11, v7
	v_rcp_f32_e32 v12, v10
	v_cndmask_b32_e64 v8, 0x80000000, v9, s0
	v_cvt_f32_i32_e32 v6, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_add_f32_e32 v4, v8, v4
	s_waitcnt_depctr 0xfff
	v_fma_f32 v13, -v7, v11, 1.0
	v_fma_f32 v14, -v10, v12, 1.0
	v_dual_fmac_f32 v11, v13, v11 :: v_dual_fmac_f32 v12, v14, v12
	v_div_scale_f32 v13, s2, v5, 0x41200000, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v14, v15, v11
	v_mul_f32_e32 v16, v13, v12
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v17, -v7, v14, v15
	v_fma_f32 v18, -v10, v16, v13
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fmac_f32_e32 v14, v17, v11
	v_fmac_f32_e32 v16, v18, v12
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v7, -v7, v14, v15
	v_fma_f32 v9, -v10, v16, v13
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_fmas_f32 v7, v7, v11, v14
	s_mov_b32 vcc_lo, s2
	v_div_fmas_f32 v9, v9, v12, v16
	v_cmp_ne_u16_e32 vcc_lo, 32, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_div_fixup_f32 v5, v9, 0x41200000, v5
	v_cndmask_b32_e32 v0, 0x80000000, v6, vcc_lo
	v_add_f32_e32 v0, v0, v2
	v_div_fixup_f32 v2, v7, 0x41200000, v3
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f32_e32 v3, v5, v4
	v_add_f32_e32 v0, v2, v0
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_subrev_f32_e32 v2, s5, v3
	v_subrev_f32_e32 v0, s4, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v2, v2, v2
	v_fmac_f32_e32 v2, v0, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v0, 0x4f800000, v2
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v2
	v_cndmask_b32_e32 v3, v2, v0, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_sqrt_f32_e32 v0, v3
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v2, -1, v0
	v_add_nc_u32_e32 v4, 1, v0
	v_fma_f32 v5, -v2, v0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v6, -v4, v0, v3
	v_cmp_ge_f32_e64 s0, 0, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v0, v0, v2, s0
	v_cmp_lt_f32_e64 s0, 0, v6
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v0, v0, v4, s0
	v_mul_f32_e32 v4, 0x37800000, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cndmask_b32_e32 v4, v0, v4, vcc_lo
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_cmp_class_f32_e64 vcc_lo, v3, 0x260
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e32 v2, v4, v3, vcc_lo
	v_add_co_u32 v0, vcc_lo, s6, v0
	s_delay_alu instid0(VALU_DEP_4)
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6euclidPcffPfiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.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 20
		.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	_Z6euclidPcffPfiii, .Lfunc_end0-_Z6euclidPcffPfiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1440
; NumSgprs: 18
; NumVgprs: 20
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 20
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6euclidPcffPfiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6euclidPcffPfiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     20
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
