#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <cuda_runtime.h>

/*
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600

#else
__device__ double atomicAdd(double* address, double val)
{
    unsigned long long int* address_as_ull =
                              (unsigned long long int*)address;
    unsigned long long int old = *address_as_ull, assumed;

    do {
        assumed = old;
        old = atomicCAS(address_as_ull, assumed,
                        __double_as_longlong(val +
                               __longlong_as_double(assumed)));

    // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
    } while (assumed != old);
    return __longlong_as_double(old);
}
#endif
*/


__device__ 
double calculateDistance(double* data, double* center, int size){
    // euclidean distance
    double distance = 0;
    for(int i=0;i<size;i++){
        distance += pow(data[i] - center[i], 2);
    }
    return sqrt(distance);
}


__global__ 
void kmean(int offset, double* data, double* centers, int rows, int cols, int k, int* result, int* class_count){
    int id = blockIdx.x*blockDim.x+threadIdx.x + offset;
    if(id < rows){ 
        extern __shared__ double s_centers[];
        if(id < k * cols){
            s_centers[id] = centers[id];
        }
        double distance;
        double min_distance;
        int min_distance_cluster;
        __syncthreads();
        for(int i=0;i<k;i++){
            distance = calculateDistance(&data[id*cols], &s_centers[i*cols], cols);
            if(i == 0 ){
                min_distance = distance;
                min_distance_cluster = i;
            } else {
                if(distance < min_distance){
                    min_distance = distance;
                    min_distance_cluster = i;
                }
            }
        }
        //printf("cluster: %d\n", min_distance_cluster);
        result[id] = min_distance_cluster;
        atomicAdd(&class_count[min_distance_cluster], 1);
        
        /*
        for(int j=0;j<cols;j++){
            atomicAdd(&new_centers[min_distance_cluster * cols + j], data[id*cols + j]);
        }
        */
    }
}

/*
__global__
void divideNewCenters(double* new_centers, int k, int cols, int* class_count){
    int thread_id = blockIdx.x*blockDim.x+threadIdx.x;
    if(thread_id >= k*cols){
        return;
    }
    int cluster_id = thread_id/cols;
    new_centers[thread_id] /= class_count[cluster_id];

}
*/


void loadData(char* file_url, int col_start, int cols, int rows, double* data_ptr){
    FILE* stream = fopen(file_url, "r");
    char line[1024];
    char* clipboard;

    // skip first row
    fgets(line, 1024, stream);

    for(int i=0;i<rows;i++){
        fgets(line, 1024, stream);
        clipboard = strtok( line, "," );
        int j = 0;
        while( clipboard != NULL )
        {
            if(j >= col_start){
                data_ptr[i * cols + j-col_start] = atof(clipboard);
            }
            clipboard = strtok( NULL, "," );
            j++;
            if(j >= col_start + cols){
                break;
            }
        }
    }
}

void saveResultAsCsv(int * data, int rows){
    FILE * file = fopen("result.csv", "w+");
    fprintf(file,"Id,Cluster\n");
    for(int i=0;i<rows;i++){
        fprintf(file, "%d, %d\n", i, data[i]);
    }
    fclose(file);
}
void saveClusterCentersAsCsv(double * data, int k, int cols){
    FILE * file = fopen("centers.csv", "w+");
    //fprintf(file,"Cluster id, Cluster\n");
    for(int i=0;i<k;i++){
        for(int j=0;j<cols;j++){
            if(j==cols-1){
                fprintf(file, "%f\n", data[i*cols+j]);
            } else {
                fprintf(file, "%f,", data[i*cols+j]);
            }
        }
    }
    fclose(file);
}


int main(int argc, char** argv){
    int k = 12;
     // weather - cols:6
    // iris - cols:2, rows:150, k: 3
    int rows = 1586822;
    int cols = 6;
    int steps = 30;
    int offset = 0;
    char* file_url = "wind_data_prepared.csv";

     // load arguments
     if(argc == 5){
        file_url = argv[1];
        rows = atoi(argv[2]);
        cols = atoi(argv[3]);
        k = atoi(argv[4]);
    } else {
        printf("Continue with default parameters \n");
    }

    size_t data_size = cols * rows * sizeof(double);
    size_t centers_size = k * cols * sizeof(double);
    size_t calc_classes_size = rows * sizeof(int);
    size_t class_count_size = k * sizeof(int);

    double * h_data;
    //double * h_data = (double*) malloc(data_size); // data[row * cols + col]
    double * h_centers = (double *) malloc(centers_size); // center[k*cols + col]
    int * h_calc_classes = (int*) malloc(calc_classes_size);
    int * h_class_count = (int*) malloc(class_count_size);

    cudaMallocHost((void**)&h_data, data_size);

   
   
        

    srand(time(NULL)); 
    loadData(file_url, 1, cols, rows, h_data);
    // initialize clusters centers
    for(int i=0; i < k ; i++){
        int data_index = (rand() % rows) *cols;
        for(int j=0;j<cols;j++){
            h_centers[i*cols +j] = h_data[data_index + j];
        }
       
    }

    double * d_data;
    double * d_centers;
    //double * d_new_centers;
    int * d_calc_classes;
    int * d_class_count;
    cudaError_t error;
    

    dim3 threadsPerBlock(1024, 1, 1);
    dim3 blocksPerGrid((rows + threadsPerBlock.x - 1) / threadsPerBlock.x, 1, 1);
	
    int max_rows = blocksPerGrid.x * threadsPerBlock.x;
    cudaMalloc(&d_data, data_size);
    cudaMalloc(&d_centers, centers_size);
    //cudaMalloc(&d_new_centers, centers_size);
    cudaMalloc(&d_calc_classes, calc_classes_size);
    cudaMalloc(&d_class_count, class_count_size);
    cudaMemcpy(d_data, h_data, data_size, cudaMemcpyHostToDevice);

    for(int step=0;step<steps;step++){
        cudaMemset(d_class_count, 0, class_count_size);
        cudaMemcpy(d_centers, h_centers, centers_size, cudaMemcpyHostToDevice);
        //cudaMemset(d_new_centers, 0, centers_size);

        for(offset=0;offset<rows;offset+=max_rows){
            // call device function
            kmean<<<blocksPerGrid, threadsPerBlock, k*cols*sizeof(double)>>>(offset, d_data, d_centers, rows, cols, k,  d_calc_classes, d_class_count);

            if(offset+max_rows < rows){
                calc_classes_size = max_rows * sizeof(int);
            } else{
                calc_classes_size = (rows - offset) * sizeof(int);
            }
        }
        cudaMemcpy(h_calc_classes , d_calc_classes, calc_classes_size, cudaMemcpyDeviceToHost);
        // recalculate clusters (mean value)
        if(step != steps-1){
            //cudaDeviceSynchronize();
            //divideNewCenters<<<1, cols*k>>>(d_new_centers, k, cols, d_class_count);
            //cudaMemcpy(d_centers, d_new_centers, centers_size, cudaMemcpyDeviceToDevice);
            //memset(h_centers, 0, centers_size);
            for(int i=0;i<k;i++){
                for(int j=0;j<cols;j++){
                    h_centers[i*cols+j] = 0;
                }
            }
            cudaMemcpy(h_class_count, d_class_count, class_count_size, cudaMemcpyDeviceToHost);
            for(int i=0;i<rows;i++){
                // sum all values
                for(int j=0;j<cols;j++){
                    h_centers[h_calc_classes[i] * cols + j] += h_data[i*cols + j];
                } 
            }
            for(int i=0;i<k;i++){
                for(int j=0;j<cols;j++){
                    if(h_class_count[i] != 0){
                        h_centers[i*cols + j] /= h_class_count[i];
                    }
                }
            }
        }
    }
        
    // send results to host
    //cudaMemcpy(h_centers, d_new_centers, centers_size, cudaMemcpyDeviceToHost); 
    
                
    // check if error occured
    error = cudaGetLastError();
    if(error != cudaSuccess){
        fprintf(stderr, "Error: %s\n", cudaGetErrorString(error));
        exit(-1);
    }

    saveResultAsCsv(h_calc_classes, rows);
    saveClusterCentersAsCsv(h_centers, k, cols);

    cudaFree(d_data);
    cudaFree(d_data);
    cudaFree(d_class_count);
    cudaFree(d_calc_classes);
    cudaFree(d_centers);
    //free(h_data);
    cudaFreeHost(h_data);
    free(h_calc_classes);
    free(h_centers);
    free(h_class_count);
    return 0;
}
	.file	"tmpxft_002111b0_00000000-6_kmean.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z17calculateDistancePdS_i
	.type	_Z17calculateDistancePdS_i, @function
_Z17calculateDistancePdS_i:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z17calculateDistancePdS_i, .-_Z17calculateDistancePdS_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	","
	.text
	.globl	_Z8loadDataPciiiPd
	.type	_Z8loadDataPciiiPd, @function
_Z8loadDataPciiiPd:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%r8, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	leaq	.LC0(%rip), %rsi
	movl	%ebx, %r14d
	negl	%r14d
	subq	$1080, %rsp
	.cfi_def_cfa_offset 1136
	movl	%ecx, 16(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 1064(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movl	$1024, %edx
	movl	$1024, %esi
	movq	%rax, 8(%rsp)
	leaq	40(%rsp), %rax
	movq	8(%rsp), %rcx
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	__fgets_chk@PLT
.L5:
	movl	16(%rsp), %eax
	cmpl	%eax, %r13d
	jge	.L15
	movq	8(%rsp), %rcx
	movq	(%rsp), %rdi
	movl	$1024, %edx
	xorl	%r12d, %r12d
	movl	$1024, %esi
	call	__fgets_chk@PLT
	movq	(%rsp), %rdi
	leaq	.LC1(%rip), %rsi
	call	strtok@PLT
	movq	%rax, %rdi
	leal	(%rbx,%rbp), %eax
	movl	%eax, 20(%rsp)
.L6:
	testq	%rdi, %rdi
	je	.L9
	cmpl	%ebx, %r12d
	jl	.L7
	leal	(%r12,%r14), %eax
	cltq
	leaq	(%r15,%rax,8), %rax
	movq	%rax, 24(%rsp)
	call	atof@PLT
	movq	24(%rsp), %rax
	movsd	%xmm0, (%rax)
.L7:
	xorl	%edi, %edi
	leaq	.LC1(%rip), %rsi
	incl	%r12d
	call	strtok@PLT
	movq	%rax, %rdi
	cmpl	%r12d, 20(%rsp)
	jg	.L6
.L9:
	incl	%r13d
	addl	%ebp, %r14d
	jmp	.L5
.L15:
	movq	1064(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$1080, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE2028:
	.size	_Z8loadDataPciiiPd, .-_Z8loadDataPciiiPd
	.section	.rodata.str1.1
.LC2:
	.string	"w+"
.LC3:
	.string	"result.csv"
.LC4:
	.string	"Id,Cluster\n"
.LC5:
	.string	"%d, %d\n"
	.text
	.globl	_Z15saveResultAsCsvPii
	.type	_Z15saveResultAsCsvPii, @function
_Z15saveResultAsCsvPii:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC5(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	leaq	.LC3(%rip), %rdi
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movl	%esi, %r12d
	leaq	.LC2(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	call	fopen@PLT
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	movq	%rax, %rdi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L17:
	movl	%ebp, %ecx
	cmpl	%ebp, %r12d
	jle	.L20
	movl	0(%r13,%rbp,4), %r8d
	movq	%r14, %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	incq	%rbp
	call	__fprintf_chk@PLT
	jmp	.L17
.L20:
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z15saveResultAsCsvPii, .-_Z15saveResultAsCsvPii
	.section	.rodata.str1.1
.LC6:
	.string	"centers.csv"
.LC7:
	.string	"%f\n"
.LC8:
	.string	"%f,"
	.text
	.globl	_Z23saveClusterCentersAsCsvPdii
	.type	_Z23saveClusterCentersAsCsvPdii, @function
_Z23saveClusterCentersAsCsvPdii:
.LFB2030:
	.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
	movl	%esi, %r14d
	leaq	.LC2(%rip), %rsi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%rdi, 8(%rsp)
	leaq	.LC6(%rip), %rdi
	call	fopen@PLT
	movq	%rax, (%rsp)
.L22:
	cmpl	%r14d, %r13d
	jge	.L26
	movq	8(%rsp), %rcx
	movslq	%r12d, %rax
	xorl	%ebp, %ebp
	leaq	(%rcx,%rax,8), %r15
.L27:
	cmpl	%ebx, %ebp
	jge	.L30
	leal	-1(%rbx), %eax
	movsd	(%r15,%rbp,8), %xmm0
	leaq	.LC7(%rip), %rdx
	cmpl	%ebp, %eax
	je	.L29
	leaq	.LC8(%rip), %rdx
.L29:
	movq	(%rsp), %rdi
	movl	$2, %esi
	movb	$1, %al
	incq	%rbp
	call	__fprintf_chk@PLT
	jmp	.L27
.L30:
	incl	%r13d
	addl	%ebx, %r12d
	jmp	.L22
.L26:
	movq	(%rsp), %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z23saveClusterCentersAsCsvPdii, .-_Z23saveClusterCentersAsCsvPdii
	.globl	_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_
	.type	_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_, @function
_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	208(%rsp), %rax
	movl	%edi, 44(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rax, 8(%rsp)
	movq	216(%rsp), %rax
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movl	%ecx, 40(%rsp)
	leaq	64(%rsp), %rcx
	movl	%r8d, 20(%rsp)
	movl	%r9d, 16(%rsp)
	movq	%rax, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	44(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	40(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 168(%rsp)
	movq	%rsp, %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L31
	pushq	64(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z5kmeaniPdS_iiiPiS0_(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 224
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L31:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L33
	call	__stack_chk_fail@PLT
.L33:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_, .-_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_
	.globl	_Z5kmeaniPdS_iiiPiS0_
	.type	_Z5kmeaniPdS_iiiPiS0_, @function
_Z5kmeaniPdS_iiiPiS0_:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_
	.cfi_endproc
.LFE2057:
	.size	_Z5kmeaniPdS_iiiPiS0_, .-_Z5kmeaniPdS_iiiPiS0_
	.section	.rodata.str1.1
.LC9:
	.string	"wind_data_prepared.csv"
.LC10:
	.string	"Continue with default parameters \n"
.LC12:
	.string	"Error: %s\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.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
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	cmpl	$5, %edi
	jne	.L37
	movq	16(%rsi), %rdi
	movq	%rsi, %r12
	movq	8(%rsi), %r15
	call	atoi@PLT
	movq	24(%r12), %rdi
	movl	%eax, %ebp
	call	atoi@PLT
	movq	32(%r12), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	movl	%eax, %r12d
	jmp	.L38
.L37:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movl	$6, %ebx
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %r15
	movl	$1586822, %ebp
	movl	$12, %r12d
.L38:
	movl	%ebp, %r14d
	imull	%ebx, %r14d
	movslq	%r14d, %r14
	leaq	0(,%r14,8), %rax
	xorl	%r14d, %r14d
	movq	%rax, 32(%rsp)
	movl	%r12d, %eax
	imull	%ebx, %eax
	cltq
	salq	$3, %rax
	movq	%rax, 8(%rsp)
	movslq	%ebp, %rax
	movq	8(%rsp), %rdi
	salq	$2, %rax
	movq	%rax, (%rsp)
	movslq	%r12d, %rax
	salq	$2, %rax
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movq	(%rsp), %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	16(%rsp), %rdi
	movq	%rax, 24(%rsp)
	call	malloc@PLT
	movq	32(%rsp), %rsi
	leaq	72(%rsp), %rdi
	movq	%rax, 40(%rsp)
	call	cudaMallocHost@PLT
	xorl	%edi, %edi
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	movq	72(%rsp), %r8
	movq	%r15, %rdi
	movl	%ebp, %ecx
	movl	%ebx, %edx
	movl	$1, %esi
	xorl	%r15d, %r15d
	call	_Z8loadDataPciiiPd
.L39:
	cmpl	%r15d, %r12d
	jle	.L71
	call	rand@PLT
	cltd
	idivl	%ebp
	movq	72(%rsp), %rax
	imull	%ebx, %edx
	movslq	%edx, %rdx
	leaq	(%rax,%rdx,8), %rdx
	movslq	%r14d, %rax
	leaq	0(%r13,%rax,8), %rsi
	xorl	%eax, %eax
.L40:
	cmpl	%eax, %ebx
	jle	.L72
	movsd	(%rdx,%rax,8), %xmm0
	movsd	%xmm0, (%rsi,%rax,8)
	incq	%rax
	jmp	.L40
.L72:
	incl	%r15d
	addl	%ebx, %r14d
	jmp	.L39
.L71:
	movabsq	$4294967297, %rax
	movq	32(%rsp), %rsi
	leaq	80(%rsp), %rdi
	movl	$1, 128(%rsp)
	movq	%rax, 116(%rsp)
	leal	1023(%rbp), %eax
	movl	$1, 132(%rsp)
	movl	%eax, %ecx
	andl	$-1024, %eax
	shrl	$10, %ecx
	movl	%eax, %r15d
	movl	%ecx, 56(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	88(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	leaq	96(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rsi
	leaq	104(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdx
	movq	72(%rsp), %rsi
	movl	$1, %ecx
	movq	80(%rsp), %rdi
	call	cudaMemcpy@PLT
	movslq	%r15d, %rax
	movl	$30, 60(%rsp)
	salq	$2, %rax
	movq	%rax, 48(%rsp)
.L59:
	movq	16(%rsp), %rdx
	movq	104(%rsp), %rdi
	xorl	%esi, %esi
	xorl	%r14d, %r14d
	call	cudaMemset@PLT
	movq	8(%rsp), %rdx
	movq	88(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%ebp, 32(%rsp)
.L43:
	cmpl	%r14d, %ebp
	jle	.L73
	movl	56(%rsp), %eax
	movl	$1024, 112(%rsp)
	xorl	%r9d, %r9d
	movl	120(%rsp), %ecx
	movq	8(%rsp), %r8
	movq	112(%rsp), %rdx
	movl	132(%rsp), %esi
	movl	%eax, 124(%rsp)
	movq	124(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L44
	pushq	104(%rsp)
	.cfi_def_cfa_offset 216
	movl	%r12d, %r9d
	movl	%ebx, %r8d
	movl	%ebp, %ecx
	pushq	104(%rsp)
	.cfi_def_cfa_offset 224
	movl	%r14d, %edi
	movq	104(%rsp), %rdx
	movq	96(%rsp), %rsi
	call	_Z35__device_stub__Z5kmeaniPdS_iiiPiS0_iPdS_iiiPiS0_
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L44:
	movq	48(%rsp), %rax
	addl	%r15d, %r14d
	movq	%rax, (%rsp)
	cmpl	%ebp, %r14d
	jl	.L45
	movslq	32(%rsp), %rax
	salq	$2, %rax
	movq	%rax, (%rsp)
.L45:
	subl	%r15d, 32(%rsp)
	jmp	.L43
.L73:
	movq	(%rsp), %rdx
	movq	96(%rsp), %rsi
	movl	$2, %ecx
	movq	24(%rsp), %rdi
	call	cudaMemcpy@PLT
	decl	60(%rsp)
	je	.L74
	xorl	%edx, %edx
	xorl	%ecx, %ecx
.L47:
	cmpl	%ecx, %r12d
	jle	.L50
	movslq	%edx, %rax
	leaq	0(%r13,%rax,8), %rsi
	xorl	%eax, %eax
.L51:
	cmpl	%eax, %ebx
	jle	.L75
	movq	$0x000000000, (%rsi,%rax,8)
	incq	%rax
	jmp	.L51
.L75:
	incl	%ecx
	addl	%ebx, %edx
	jmp	.L47
.L50:
	movq	104(%rsp), %rsi
	movq	16(%rsp), %rdx
	movl	$2, %ecx
	movq	40(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	72(%rsp), %r8
	xorl	%esi, %esi
	xorl	%ecx, %ecx
.L52:
	cmpl	%ecx, %ebp
	jle	.L64
	movslq	%esi, %rax
	leaq	(%r8,%rax,8), %rdi
	xorl	%eax, %eax
.L55:
	cmpl	%eax, %ebx
	jle	.L76
	movq	24(%rsp), %r10
	movl	(%r10,%rcx,4), %edx
	imull	%ebx, %edx
	addl	%eax, %edx
	movslq	%edx, %rdx
	leaq	0(%r13,%rdx,8), %rdx
	movsd	(%rdx), %xmm0
	addsd	(%rdi,%rax,8), %xmm0
	incq	%rax
	movsd	%xmm0, (%rdx)
	jmp	.L55
.L76:
	incq	%rcx
	addl	%ebx, %esi
	jmp	.L52
.L64:
	xorl	%ecx, %ecx
	xorl	%eax, %eax
.L54:
	cmpl	%eax, %r12d
	jle	.L59
	xorl	%edx, %edx
.L58:
	cmpl	%edx, %ebx
	jle	.L77
	movq	40(%rsp), %rsi
	movl	(%rsi,%rax,4), %edi
	testl	%edi, %edi
	je	.L56
	cvtsi2sdl	%edi, %xmm1
	leal	(%rdx,%rcx), %esi
	movslq	%esi, %rsi
	leaq	0(%r13,%rsi,8), %rsi
	movsd	(%rsi), %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%rsi)
.L56:
	incl	%edx
	jmp	.L58
.L77:
	incq	%rax
	addl	%ebx, %ecx
	jmp	.L54
.L74:
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L60
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC12(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L60:
	movq	24(%rsp), %rdi
	movl	%ebp, %esi
	call	_Z15saveResultAsCsvPii
	movl	%ebx, %edx
	movl	%r12d, %esi
	movq	%r13, %rdi
	call	_Z23saveClusterCentersAsCsvPdii
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFreeHost@PLT
	movq	24(%rsp), %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	free@PLT
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L61
	call	__stack_chk_fail@PLT
.L61:
	addq	$152, %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
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z5kmeaniPdS_iiiPiS0_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z5kmeaniPdS_iiiPiS0_(%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
.LFE2059:
	.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
	.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 <string.h>
#include <time.h>
#include <math.h>
#include <hip/hip_runtime.h>

/*
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600

#else
__device__ double atomicAdd(double* address, double val)
{
    unsigned long long int* address_as_ull =
                              (unsigned long long int*)address;
    unsigned long long int old = *address_as_ull, assumed;

    do {
        assumed = old;
        old = atomicCAS(address_as_ull, assumed,
                        __double_as_longlong(val +
                               __longlong_as_double(assumed)));

    // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
    } while (assumed != old);
    return __longlong_as_double(old);
}
#endif
*/


__device__ 
double calculateDistance(double* data, double* center, int size){
    // euclidean distance
    double distance = 0;
    for(int i=0;i<size;i++){
        distance += pow(data[i] - center[i], 2);
    }
    return sqrt(distance);
}


__global__ 
void kmean(int offset, double* data, double* centers, int rows, int cols, int k, int* result, int* class_count){
    int id = blockIdx.x*blockDim.x+threadIdx.x + offset;
    if(id < rows){ 
        extern __shared__ double s_centers[];
        if(id < k * cols){
            s_centers[id] = centers[id];
        }
        double distance;
        double min_distance;
        int min_distance_cluster;
        __syncthreads();
        for(int i=0;i<k;i++){
            distance = calculateDistance(&data[id*cols], &s_centers[i*cols], cols);
            if(i == 0 ){
                min_distance = distance;
                min_distance_cluster = i;
            } else {
                if(distance < min_distance){
                    min_distance = distance;
                    min_distance_cluster = i;
                }
            }
        }
        //printf("cluster: %d\n", min_distance_cluster);
        result[id] = min_distance_cluster;
        atomicAdd(&class_count[min_distance_cluster], 1);
        
        /*
        for(int j=0;j<cols;j++){
            atomicAdd(&new_centers[min_distance_cluster * cols + j], data[id*cols + j]);
        }
        */
    }
}

/*
__global__
void divideNewCenters(double* new_centers, int k, int cols, int* class_count){
    int thread_id = blockIdx.x*blockDim.x+threadIdx.x;
    if(thread_id >= k*cols){
        return;
    }
    int cluster_id = thread_id/cols;
    new_centers[thread_id] /= class_count[cluster_id];

}
*/


void loadData(char* file_url, int col_start, int cols, int rows, double* data_ptr){
    FILE* stream = fopen(file_url, "r");
    char line[1024];
    char* clipboard;

    // skip first row
    fgets(line, 1024, stream);

    for(int i=0;i<rows;i++){
        fgets(line, 1024, stream);
        clipboard = strtok( line, "," );
        int j = 0;
        while( clipboard != NULL )
        {
            if(j >= col_start){
                data_ptr[i * cols + j-col_start] = atof(clipboard);
            }
            clipboard = strtok( NULL, "," );
            j++;
            if(j >= col_start + cols){
                break;
            }
        }
    }
}

void saveResultAsCsv(int * data, int rows){
    FILE * file = fopen("result.csv", "w+");
    fprintf(file,"Id,Cluster\n");
    for(int i=0;i<rows;i++){
        fprintf(file, "%d, %d\n", i, data[i]);
    }
    fclose(file);
}
void saveClusterCentersAsCsv(double * data, int k, int cols){
    FILE * file = fopen("centers.csv", "w+");
    //fprintf(file,"Cluster id, Cluster\n");
    for(int i=0;i<k;i++){
        for(int j=0;j<cols;j++){
            if(j==cols-1){
                fprintf(file, "%f\n", data[i*cols+j]);
            } else {
                fprintf(file, "%f,", data[i*cols+j]);
            }
        }
    }
    fclose(file);
}


int main(int argc, char** argv){
    int k = 12;
     // weather - cols:6
    // iris - cols:2, rows:150, k: 3
    int rows = 1586822;
    int cols = 6;
    int steps = 30;
    int offset = 0;
    char* file_url = "wind_data_prepared.csv";

     // load arguments
     if(argc == 5){
        file_url = argv[1];
        rows = atoi(argv[2]);
        cols = atoi(argv[3]);
        k = atoi(argv[4]);
    } else {
        printf("Continue with default parameters \n");
    }

    size_t data_size = cols * rows * sizeof(double);
    size_t centers_size = k * cols * sizeof(double);
    size_t calc_classes_size = rows * sizeof(int);
    size_t class_count_size = k * sizeof(int);

    double * h_data;
    //double * h_data = (double*) malloc(data_size); // data[row * cols + col]
    double * h_centers = (double *) malloc(centers_size); // center[k*cols + col]
    int * h_calc_classes = (int*) malloc(calc_classes_size);
    int * h_class_count = (int*) malloc(class_count_size);

    hipHostMalloc((void**)&h_data, data_size, hipHostMallocDefault);

   
   
        

    srand(time(NULL)); 
    loadData(file_url, 1, cols, rows, h_data);
    // initialize clusters centers
    for(int i=0; i < k ; i++){
        int data_index = (rand() % rows) *cols;
        for(int j=0;j<cols;j++){
            h_centers[i*cols +j] = h_data[data_index + j];
        }
       
    }

    double * d_data;
    double * d_centers;
    //double * d_new_centers;
    int * d_calc_classes;
    int * d_class_count;
    hipError_t error;
    

    dim3 threadsPerBlock(1024, 1, 1);
    dim3 blocksPerGrid((rows + threadsPerBlock.x - 1) / threadsPerBlock.x, 1, 1);
	
    int max_rows = blocksPerGrid.x * threadsPerBlock.x;
    hipMalloc(&d_data, data_size);
    hipMalloc(&d_centers, centers_size);
    //cudaMalloc(&d_new_centers, centers_size);
    hipMalloc(&d_calc_classes, calc_classes_size);
    hipMalloc(&d_class_count, class_count_size);
    hipMemcpy(d_data, h_data, data_size, hipMemcpyHostToDevice);

    for(int step=0;step<steps;step++){
        hipMemset(d_class_count, 0, class_count_size);
        hipMemcpy(d_centers, h_centers, centers_size, hipMemcpyHostToDevice);
        //cudaMemset(d_new_centers, 0, centers_size);

        for(offset=0;offset<rows;offset+=max_rows){
            // call device function
            kmean<<<blocksPerGrid, threadsPerBlock, k*cols*sizeof(double)>>>(offset, d_data, d_centers, rows, cols, k,  d_calc_classes, d_class_count);

            if(offset+max_rows < rows){
                calc_classes_size = max_rows * sizeof(int);
            } else{
                calc_classes_size = (rows - offset) * sizeof(int);
            }
        }
        hipMemcpy(h_calc_classes , d_calc_classes, calc_classes_size, hipMemcpyDeviceToHost);
        // recalculate clusters (mean value)
        if(step != steps-1){
            //cudaDeviceSynchronize();
            //divideNewCenters<<<1, cols*k>>>(d_new_centers, k, cols, d_class_count);
            //cudaMemcpy(d_centers, d_new_centers, centers_size, cudaMemcpyDeviceToDevice);
            //memset(h_centers, 0, centers_size);
            for(int i=0;i<k;i++){
                for(int j=0;j<cols;j++){
                    h_centers[i*cols+j] = 0;
                }
            }
            hipMemcpy(h_class_count, d_class_count, class_count_size, hipMemcpyDeviceToHost);
            for(int i=0;i<rows;i++){
                // sum all values
                for(int j=0;j<cols;j++){
                    h_centers[h_calc_classes[i] * cols + j] += h_data[i*cols + j];
                } 
            }
            for(int i=0;i<k;i++){
                for(int j=0;j<cols;j++){
                    if(h_class_count[i] != 0){
                        h_centers[i*cols + j] /= h_class_count[i];
                    }
                }
            }
        }
    }
        
    // send results to host
    //cudaMemcpy(h_centers, d_new_centers, centers_size, cudaMemcpyDeviceToHost); 
    
                
    // check if error occured
    error = hipGetLastError();
    if(error != hipSuccess){
        fprintf(stderr, "Error: %s\n", hipGetErrorString(error));
        exit(-1);
    }

    saveResultAsCsv(h_calc_classes, rows);
    saveClusterCentersAsCsv(h_centers, k, cols);

    hipFree(d_data);
    hipFree(d_data);
    hipFree(d_class_count);
    hipFree(d_calc_classes);
    hipFree(d_centers);
    //free(h_data);
    hipHostFree(h_data);
    free(h_calc_classes);
    free(h_centers);
    free(h_class_count);
    return 0;
}
	.text
	.file	"kmean.hip"
	.globl	_Z20__device_stub__kmeaniPdS_iiiPiS0_ # -- Begin function _Z20__device_stub__kmeaniPdS_iiiPiS0_
	.type	_Z20__device_stub__kmeaniPdS_iiiPiS0_,@function
_Z20__device_stub__kmeaniPdS_iiiPiS0_:  # @_Z20__device_stub__kmeaniPdS_iiiPiS0_
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	12(%rsp), %rax
	movl	%edi, (%rax)
	leaq	40(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	movq	%rsp, %r8
	movl	%r9d, (%r8)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	192(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	200(%rsp), %rax
	movq	%rax, 56(%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	$_Z5kmeaniPdS_iiiPiS0_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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	_Z20__device_stub__kmeaniPdS_iiiPiS0_, .Lfunc_end0-_Z20__device_stub__kmeaniPdS_iiiPiS0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z8loadDataPciiiPd              # -- Begin function _Z8loadDataPciiiPd
	.type	_Z8loadDataPciiiPd,@function
_Z8loadDataPciiiPd:                     # @_Z8loadDataPciiiPd
	.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	$1064, %rsp                     # imm = 0x428
	.cfi_def_cfa_offset 1120
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r8, %rbx
	movl	%ecx, %ebp
	movl	%edx, %r15d
	movl	%esi, %r12d
	movl	$.L.str, %esi
	callq	fopen
	leaq	32(%rsp), %rdi
	movl	$1024, %esi                     # imm = 0x400
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movq	%rax, %rdx
	callq	fgets
	movl	%ebp, 12(%rsp)                  # 4-byte Spill
	testl	%ebp, %ebp
	jle	.LBB1_9
# %bb.1:                                # %.lr.ph
	leal	(%r15,%r12), %eax
	movslq	%r12d, %r12
	movslq	%eax, %r13
	movl	%r12d, %ebp
	negl	%ebp
	movl	%r15d, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	xorl	%r14d, %r14d
.LBB1_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	leaq	32(%rsp), %r15
	movq	%r15, %rdi
	movl	$1024, %esi                     # imm = 0x400
	movq	24(%rsp), %rdx                  # 8-byte Reload
	callq	fgets
	movl	$.L.str.1, %esi
	movq	%r15, %rdi
	callq	strtok
	testq	%rax, %rax
	je	.LBB1_8
# %bb.3:                                # %.preheader
                                        #   in Loop: Header=BB1_2 Depth=1
	xorl	%r15d, %r15d
.LBB1_4:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%r12, %r15
	jl	.LBB1_6
# %bb.5:                                #   in Loop: Header=BB1_4 Depth=2
	movq	%rax, %rdi
	callq	atof
	leal	(%r15,%rbp), %eax
	cltq
	movsd	%xmm0, (%rbx,%rax,8)
.LBB1_6:                                #   in Loop: Header=BB1_4 Depth=2
	movl	$.L.str.1, %esi
	xorl	%edi, %edi
	callq	strtok
	incq	%r15
	cmpq	%r13, %r15
	jge	.LBB1_8
# %bb.7:                                #   in Loop: Header=BB1_4 Depth=2
	testq	%rax, %rax
	jne	.LBB1_4
.LBB1_8:                                # %.loopexit
                                        #   in Loop: Header=BB1_2 Depth=1
	incl	%r14d
	addq	16(%rsp), %rbp                  # 8-byte Folded Reload
	cmpl	12(%rsp), %r14d                 # 4-byte Folded Reload
	jne	.LBB1_2
.LBB1_9:                                # %._crit_edge
	addq	$1064, %rsp                     # imm = 0x428
	.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_end1:
	.size	_Z8loadDataPciiiPd, .Lfunc_end1-_Z8loadDataPciiiPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z15saveResultAsCsvPii          # -- Begin function _Z15saveResultAsCsvPii
	.type	_Z15saveResultAsCsvPii,@function
_Z15saveResultAsCsvPii:                 # @_Z15saveResultAsCsvPii
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %rbx
	movl	$.L.str.2, %edi
	movl	$.L.str.3, %esi
	callq	fopen
	movq	%rax, %r14
	movl	$.L.str.4, %edi
	movl	$11, %esi
	movl	$1, %edx
	movq	%rax, %rcx
	callq	fwrite@PLT
	testl	%ebp, %ebp
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r12d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r15,4), %ecx
	movl	$.L.str.5, %esi
	movq	%r14, %rdi
	movl	%r15d, %edx
	xorl	%eax, %eax
	callq	fprintf
	incq	%r15
	cmpq	%r15, %r12
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movq	%r14, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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
	jmp	fclose                          # TAILCALL
.Lfunc_end2:
	.size	_Z15saveResultAsCsvPii, .Lfunc_end2-_Z15saveResultAsCsvPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z23saveClusterCentersAsCsvPdii # -- Begin function _Z23saveClusterCentersAsCsvPdii
	.type	_Z23saveClusterCentersAsCsvPdii,@function
_Z23saveClusterCentersAsCsvPdii:        # @_Z23saveClusterCentersAsCsvPdii
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.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	%edx, %ebx
	movl	%esi, %ebp
	movq	%rdi, 24(%rsp)                  # 8-byte Spill
	movl	$.L.str.6, %edi
	movl	$.L.str.3, %esi
	callq	fopen
	movq	%rax, %r15
	testl	%ebp, %ebp
	jle	.LBB3_6
# %bb.1:                                # %.preheader.lr.ph
	leal	-1(%rbx), %r12d
	movl	%ebp, %eax
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	movl	%ebx, %ebp
	movq	24(%rsp), %rax                  # 8-byte Reload
	leaq	(%rax,%r12,8), %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	shlq	$3, %r12
	shlq	$3, %rbp
	xorl	%eax, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	$.L.str.7, %edx
	xorl	%eax, %eax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movq	%rbx, 32(%rsp)                  # 8-byte Spill
.LBB3_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_4 Depth 2
	testl	%ebx, %ebx
	jle	.LBB3_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB3_2 Depth=1
	movl	16(%rsp), %eax                  # 4-byte Reload
	movq	24(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,8), %r14
	movq	8(%rsp), %rax                   # 8-byte Reload
                                        # kill: def $eax killed $eax killed $rax def $rax
	imull	32(%rsp), %eax                  # 4-byte Folded Reload
	movq	40(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,8), %rbx
	xorl	%r13d, %r13d
.LBB3_4:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leaq	(%r14,%r13), %rax
	cmpq	%r13, %r12
	cmoveq	%rbx, %rax
	movl	$.L.str.8, %esi
	cmoveq	%rdx, %rsi
	movsd	(%rax), %xmm0                   # xmm0 = mem[0],zero
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	movl	$.L.str.7, %edx
	addq	$8, %r13
	cmpq	%r13, %rbp
	jne	.LBB3_4
.LBB3_5:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	movq	8(%rsp), %rcx                   # 8-byte Reload
	incq	%rcx
	movq	32(%rsp), %rbx                  # 8-byte Reload
	movq	16(%rsp), %rax                  # 8-byte Reload
	addl	%ebx, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movq	%rcx, %rax
	movq	%rcx, 8(%rsp)                   # 8-byte Spill
	cmpq	48(%rsp), %rcx                  # 8-byte Folded Reload
	jne	.LBB3_2
.LBB3_6:                                # %._crit_edge22
	movq	%r15, %rdi
	addq	$56, %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
	jmp	fclose                          # TAILCALL
.Lfunc_end3:
	.size	_Z23saveClusterCentersAsCsvPdii, .Lfunc_end3-_Z23saveClusterCentersAsCsvPdii
	.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	$184, %rsp
	.cfi_def_cfa_offset 240
	.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
	jne	.LBB4_2
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movq	16(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	32(%rbx), %rdi
	callq	atoi
	jmp	.LBB4_3
.LBB4_2:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$12, %eax
	movl	$1586822, %r14d                 # imm = 0x183686
	movl	$6, %ebp
	movl	$.L.str.9, %ecx
	movq	%rcx, 16(%rsp)                  # 8-byte Spill
.LBB4_3:
	movl	%r14d, %ecx
	imull	%ebp, %ecx
	movslq	%ecx, %r13
	shlq	$3, %r13
	movl	%eax, %ecx
	imull	%ebp, %ecx
	movslq	%ecx, %rdi
	shlq	$3, %rdi
	movslq	%r14d, %r15
	leaq	(,%r15,4), %r12
	movl	%eax, 12(%rsp)                  # 4-byte Spill
	cltq
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	leaq	(,%rax,4), %rbx
	movq	%rdi, 112(%rsp)                 # 8-byte Spill
	callq	malloc
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	movq	%rbx, 80(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 136(%rsp)                 # 8-byte Spill
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	movq	%r13, 104(%rsp)                 # 8-byte Spill
	movq	%r13, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movq	(%rbx), %r8
	movq	16(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movl	%ebp, %edx
	movq	%r15, 24(%rsp)                  # 8-byte Spill
	movl	%r15d, %ecx
	callq	_Z8loadDataPciiiPd
	movq	32(%rsp), %rcx                  # 8-byte Reload
	movl	12(%rsp), %eax                  # 4-byte Reload
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%ebp, %r13d
	cmpl	$0, 96(%rsp)                    # 4-byte Folded Reload
	movq	%r14, 72(%rsp)                  # 8-byte Spill
	movslq	%ebp, %rax
	jle	.LBB4_4
# %bb.34:                               # %.lr.ph151
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	leaq	(,%rax,8), %rbx
	xorl	%r15d, %r15d
	movq	%rcx, %r14
.LBB4_35:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_37 Depth 2
	callq	rand
	testl	%ebp, %ebp
	jle	.LBB4_38
# %bb.36:                               # %.lr.ph
                                        #   in Loop: Header=BB4_35 Depth=1
	cltd
	idivl	72(%rsp)                        # 4-byte Folded Reload
	imull	%ebp, %edx
	movslq	%edx, %rax
	shlq	$3, %rax
	addq	56(%rsp), %rax
	xorl	%ecx, %ecx
.LBB4_37:                               #   Parent Loop BB4_35 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	(%rax,%rcx,8), %xmm0            # xmm0 = mem[0],zero
	movsd	%xmm0, (%r14,%rcx,8)
	incq	%rcx
	cmpq	%rcx, %r13
	jne	.LBB4_37
.LBB4_38:                               # %._crit_edge
                                        #   in Loop: Header=BB4_35 Depth=1
	incq	%r15
	addq	%rbx, %r14
	cmpq	16(%rsp), %r15                  # 8-byte Folded Reload
	jne	.LBB4_35
# %bb.39:
	movq	72(%rsp), %r14                  # 8-byte Reload
	jmp	.LBB4_5
.LBB4_4:                                # %.._crit_edge152_crit_edge
	movq	%rax, 64(%rsp)                  # 8-byte Spill
.LBB4_5:                                # %._crit_edge152
	leal	1023(%r14), %ebx
	movl	%ebx, %eax
	shrl	$10, %eax
	btsq	$32, %rax
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	andl	$-1024, %ebx                    # imm = 0xFC00
	leaq	48(%rsp), %rdi
	movq	104(%rsp), %r15                 # 8-byte Reload
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	128(%rsp), %rdi
	movq	112(%rsp), %rsi                 # 8-byte Reload
	callq	hipMalloc
	leaq	120(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	movq	80(%rsp), %rsi                  # 8-byte Reload
	callq	hipMalloc
	leaq	48(%rsp), %rax
	movq	(%rax), %rdi
	movq	56(%rsp), %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	(,%r13,8), %rax
	movq	%rax, 168(%rsp)                 # 8-byte Spill
	movl	%r14d, %eax
	movq	%rax, 160(%rsp)                 # 8-byte Spill
	movl	%ebx, 104(%rsp)                 # 4-byte Spill
	movslq	%ebx, %rax
	shlq	$2, %rax
	movq	%rax, 176(%rsp)                 # 8-byte Spill
	movq	24(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rax,%rcx,4), %rax
	movq	%rax, 144(%rsp)                 # 8-byte Spill
	movq	64(%rsp), %rax                  # 8-byte Reload
	leaq	(,%rax,8), %rax
	movq	%rax, 152(%rsp)                 # 8-byte Spill
	xorl	%eax, %eax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movabsq	$4294967296, %r15               # imm = 0x100000000
	addq	$1024, %r15                     # imm = 0x400
.LBB4_6:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_8 Depth 2
                                        #     Child Loop BB4_14 Depth 2
                                        #     Child Loop BB4_19 Depth 2
                                        #       Child Loop BB4_21 Depth 3
                                        #     Child Loop BB4_25 Depth 2
                                        #       Child Loop BB4_27 Depth 3
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	movq	80(%rsp), %rdx                  # 8-byte Reload
	callq	hipMemset
	movq	128(%rsp), %rdi
	movq	32(%rsp), %rsi                  # 8-byte Reload
	movq	112(%rsp), %rdx                 # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%r14d, %r14d
	jle	.LBB4_11
# %bb.7:                                # %.lr.ph155.preheader
                                        #   in Loop: Header=BB4_6 Depth=1
	xorl	%ebx, %ebx
	movq	144(%rsp), %r12                 # 8-byte Reload
.LBB4_8:                                # %.lr.ph155
                                        #   Parent Loop BB4_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	96(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	movq	112(%rsp), %r8                  # 8-byte Reload
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_10
# %bb.9:                                #   in Loop: Header=BB4_8 Depth=2
	movq	48(%rsp), %rsi
	movq	128(%rsp), %rdx
	movl	%ebx, %edi
	movl	%r14d, %ecx
	movl	%ebp, %r8d
	movl	12(%rsp), %r9d                  # 4-byte Reload
	pushq	40(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	128(%rsp)
	.cfi_adjust_cfa_offset 8
	callq	_Z20__device_stub__kmeaniPdS_iiiPiS0_
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
.LBB4_10:                               #   in Loop: Header=BB4_8 Depth=2
	addl	104(%rsp), %ebx                 # 4-byte Folded Reload
	subq	176(%rsp), %r12                 # 8-byte Folded Reload
	cmpl	%r14d, %ebx
	jl	.LBB4_8
.LBB4_11:                               #   in Loop: Header=BB4_6 Depth=1
	movq	120(%rsp), %rsi
	movq	88(%rsp), %rdi                  # 8-byte Reload
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	cmpl	$29, 24(%rsp)                   # 4-byte Folded Reload
	je	.LBB4_32
# %bb.12:                               # %.preheader147
                                        #   in Loop: Header=BB4_6 Depth=1
	cmpl	$0, 12(%rsp)                    # 4-byte Folded Reload
	jle	.LBB4_17
# %bb.13:                               # %.preheader145.preheader
                                        #   in Loop: Header=BB4_6 Depth=1
	xorl	%ebx, %ebx
	movq	16(%rsp), %r14                  # 8-byte Reload
.LBB4_14:                               # %.preheader145
                                        #   Parent Loop BB4_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	testl	%ebp, %ebp
	jle	.LBB4_16
# %bb.15:                               # %.lr.ph160
                                        #   in Loop: Header=BB4_14 Depth=2
	movl	%ebx, %eax
	movq	32(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,8), %rdi
	xorl	%esi, %esi
	movq	168(%rsp), %rdx                 # 8-byte Reload
	callq	memset@PLT
.LBB4_16:                               # %._crit_edge161
                                        #   in Loop: Header=BB4_14 Depth=2
	addl	%ebp, %ebx
	decq	%r14
	jne	.LBB4_14
.LBB4_17:                               # %._crit_edge163
                                        #   in Loop: Header=BB4_6 Depth=1
	movq	40(%rsp), %rsi
	movq	136(%rsp), %rbx                 # 8-byte Reload
	movq	%rbx, %rdi
	movq	80(%rsp), %rdx                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	72(%rsp), %r14                  # 8-byte Reload
	testl	%r14d, %r14d
	movq	32(%rsp), %r8                   # 8-byte Reload
	movq	88(%rsp), %rdi                  # 8-byte Reload
	movq	64(%rsp), %r9                   # 8-byte Reload
	movq	160(%rsp), %r10                 # 8-byte Reload
	movq	152(%rsp), %r11                 # 8-byte Reload
	jle	.LBB4_23
# %bb.18:                               # %.preheader144.lr.ph
                                        #   in Loop: Header=BB4_6 Depth=1
	movq	56(%rsp), %rax
	xorl	%ecx, %ecx
.LBB4_19:                               # %.preheader144
                                        #   Parent Loop BB4_6 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB4_21 Depth 3
	testl	%ebp, %ebp
	jle	.LBB4_22
# %bb.20:                               # %.lr.ph165
                                        #   in Loop: Header=BB4_19 Depth=2
	movslq	(%rdi,%rcx,4), %rdx
	movslq	%ebp, %rsi
	imulq	%rdx, %rsi
	leaq	(%r8,%rsi,8), %rdx
	xorl	%esi, %esi
.LBB4_21:                               #   Parent Loop BB4_6 Depth=1
                                        #     Parent Loop BB4_19 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movsd	(%rax,%rsi,8), %xmm0            # xmm0 = mem[0],zero
	addsd	(%rdx,%rsi,8), %xmm0
	movsd	%xmm0, (%rdx,%rsi,8)
	incq	%rsi
	cmpq	%rsi, %r13
	jne	.LBB4_21
.LBB4_22:                               # %._crit_edge166
                                        #   in Loop: Header=BB4_19 Depth=2
	incq	%rcx
	addq	%r11, %rax
	cmpq	%r10, %rcx
	jne	.LBB4_19
.LBB4_23:                               # %.preheader146
                                        #   in Loop: Header=BB4_6 Depth=1
	cmpl	$0, 12(%rsp)                    # 4-byte Folded Reload
	jle	.LBB4_31
# %bb.24:                               # %.preheader.preheader
                                        #   in Loop: Header=BB4_6 Depth=1
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.LBB4_25:                               # %.preheader
                                        #   Parent Loop BB4_6 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB4_27 Depth 3
	testl	%ebp, %ebp
	jle	.LBB4_30
# %bb.26:                               # %.lr.ph169
                                        #   in Loop: Header=BB4_25 Depth=2
	movl	%eax, %edx
	leaq	(%r8,%rdx,8), %rdx
	movl	(%rbx,%rcx,4), %esi
	xorps	%xmm0, %xmm0
	cvtsi2sd	%esi, %xmm0
	xorl	%edi, %edi
.LBB4_27:                               #   Parent Loop BB4_6 Depth=1
                                        #     Parent Loop BB4_25 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	testl	%esi, %esi
	je	.LBB4_29
# %bb.28:                               #   in Loop: Header=BB4_27 Depth=3
	movsd	(%rdx,%rdi,8), %xmm1            # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	movsd	%xmm1, (%rdx,%rdi,8)
.LBB4_29:                               #   in Loop: Header=BB4_27 Depth=3
	incq	%rdi
	cmpq	%rdi, %r13
	jne	.LBB4_27
.LBB4_30:                               # %._crit_edge170
                                        #   in Loop: Header=BB4_25 Depth=2
	incq	%rcx
	addl	%r9d, %eax
	cmpq	16(%rsp), %rcx                  # 8-byte Folded Reload
	jne	.LBB4_25
.LBB4_31:                               # %.loopexit
                                        #   in Loop: Header=BB4_6 Depth=1
	movq	24(%rsp), %rax                  # 8-byte Reload
	incl	%eax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	cmpl	$30, %eax
	jne	.LBB4_6
.LBB4_32:                               # %.loopexit.thread
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB4_40
# %bb.33:
	movq	88(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rdi
	movl	%r14d, %esi
	callq	_Z15saveResultAsCsvPii
	movq	32(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movl	12(%rsp), %esi                  # 4-byte Reload
	movl	%ebp, %edx
	callq	_Z23saveClusterCentersAsCsvPdii
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	120(%rsp), %rdi
	callq	hipFree
	movq	128(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipHostFree
	movq	%r15, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	movq	136(%rsp), %rdi                 # 8-byte Reload
	callq	free
	xorl	%eax, %eax
	addq	$184, %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
.LBB4_40:
	.cfi_def_cfa_offset 240
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.11, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$-1, %edi
	callq	exit
.Lfunc_end4:
	.size	main, .Lfunc_end4-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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z5kmeaniPdS_iiiPiS0_, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z5kmeaniPdS_iiiPiS0_,@object   # @_Z5kmeaniPdS_iiiPiS0_
	.section	.rodata,"a",@progbits
	.globl	_Z5kmeaniPdS_iiiPiS0_
	.p2align	3, 0x0
_Z5kmeaniPdS_iiiPiS0_:
	.quad	_Z20__device_stub__kmeaniPdS_iiiPiS0_
	.size	_Z5kmeaniPdS_iiiPiS0_, 8

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

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"result.csv"
	.size	.L.str.2, 11

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Id,Cluster\n"
	.size	.L.str.4, 12

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%d, %d\n"
	.size	.L.str.5, 8

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"centers.csv"
	.size	.L.str.6, 12

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%f,"
	.size	.L.str.8, 4

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"wind_data_prepared.csv"
	.size	.L.str.9, 23

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z5kmeaniPdS_iiiPiS0_"
	.size	.L__unnamed_1, 22

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Continue with default parameters "
	.size	.Lstr, 34

	.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 _Z20__device_stub__kmeaniPdS_iiiPiS0_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z5kmeaniPdS_iiiPiS0_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z5kmeaniPdS_iiiPiS0_   ; -- Begin function _Z5kmeaniPdS_iiiPiS0_
	.globl	_Z5kmeaniPdS_iiiPiS0_
	.p2align	8
	.type	_Z5kmeaniPdS_iiiPiS0_,@function
_Z5kmeaniPdS_iiiPiS0_:                  ; @_Z5kmeaniPdS_iiiPiS0_
; %bb.0:
	s_clause 0x2
	s_load_b32 s2, s[0:1], 0x44
	s_load_b32 s3, s[0:1], 0x0
	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_2) | instid1(VALU_DEP_1)
	s_mul_i32 s15, s15, s2
	s_mov_b32 s2, exec_lo
	v_add3_u32 v0, s15, s3, v0
	v_cmpx_gt_i32_e64 s4, v0
	s_cbranch_execz .LBB0_16
; %bb.1:
	s_load_b128 s[8:11], s[0:1], 0x8
	s_mul_i32 s2, s6, s5
	v_ashrrev_i32_e32 v1, 31, v0
	v_cmp_gt_i32_e32 vcc_lo, s2, v0
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_3
; %bb.2:
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[2:3], 3, v[0:1]
	v_lshl_add_u32 v4, v0, 3, 0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s10, v2
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v3, vcc_lo, s11, v3, vcc_lo
	global_load_b64 v[2:3], v[2:3], off
	s_waitcnt vmcnt(0)
	ds_store_b64 v4, v[2:3]
.LBB0_3:
	s_or_b32 exec_lo, exec_lo, s2
	s_cmp_lt_i32 s6, 1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_14
; %bb.4:                                ; %.lr.ph
	v_mul_lo_u32 v2, v0, s5
	s_cmp_gt_i32 s5, 0
	s_mov_b32 s10, 0x55555555
	s_mov_b32 s12, 0x968915a9
	s_mov_b32 s14, 0x4222de17
	s_mov_b32 s16, 0x3abe935a
	s_mov_b32 s18, 0xcfa74449
	s_mov_b32 s20, 0x71bf3c30
	v_ashrrev_i32_e32 v3, 31, v2
	s_mov_b32 s22, 0x1c7792ce
	s_mov_b32 s24, 0x924920da
	s_mov_b32 s26, 0x9999999c
	s_mov_b32 s28, 0xfefa39ef
	v_lshlrev_b64 v[2:3], 3, v[2:3]
	s_mov_b32 s30, 0x3b39803f
	s_mov_b32 s34, 0xd5df274d
	s_mov_b32 s36, 0x652b82fe
	s_mov_b32 s38, 0xfca7ab0c
	s_mov_b32 s40, 0x6a5dcb37
	v_add_co_u32 v2, vcc_lo, s8, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s9, v3, vcc_lo
	s_mov_b32 s8, 0x47e6c9c2
	s_mov_b32 s42, 0x623fde64
	s_mov_b32 s44, 0x7c89e6b0
	s_mov_b32 s46, 0x14761f6e
	s_mov_b32 s48, 0x1852b7b0
	s_mov_b32 s50, 0x11122322
	s_mov_b32 s52, 0x555502a1
	s_mov_b32 s54, 0x55555511
	s_mov_b32 s56, 11
	s_mov_b32 s4, 0
	s_mov_b32 s11, 0x3fe55555
	s_mov_b32 s13, 0x3fba6564
	s_mov_b32 s15, 0x3fbdee67
	s_mov_b32 s17, 0x3fbe25e4
	s_cselect_b32 s7, -1, 0
	s_lshl_b32 s33, s5, 3
	s_mov_b32 s9, 0x3fc110ef
	s_mov_b32 s19, 0x3fc3b13b
	s_mov_b32 s21, 0x3fc745d1
	s_mov_b32 s23, 0x3fcc71c7
	s_mov_b32 s25, 0x3fd24924
	s_mov_b32 s27, 0x3fd99999
	s_mov_b32 s29, 0x3fe62e42
	s_mov_b32 s31, 0x3c7abc9e
	s_mov_b32 s35, 0x3c8543b0
	s_mov_b32 s37, 0x3ff71547
	s_mov_b32 s39, 0x3e928af3
	s_mov_b32 s41, 0x3e5ade15
	s_mov_b32 s43, 0x3ec71dee
	s_mov_b32 s45, 0x3efa0199
	s_mov_b32 s47, 0x3f2a01a0
	s_mov_b32 s49, 0x3f56c16c
	s_mov_b32 s51, 0x3f811111
	s_mov_b32 s53, 0x3fa55555
	s_mov_b32 s55, 0x3fc55555
	s_mov_b32 s57, 0x3fe00000
	s_mov_b32 s59, 0xbfe55555
	s_mov_b32 s61, 0xbfe62e42
	s_mov_b32 s63, 0xbc7abc9e
	s_mov_b32 s64, 0
                                        ; implicit-def: $vgpr4
                                        ; implicit-def: $vgpr5_vgpr6
.LBB0_5:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_7 Depth 2
	v_mov_b32_e32 v7, 0
	v_mov_b32_e32 v8, 0
	s_and_not1_b32 vcc_lo, exec_lo, s7
	s_cbranch_vccnz .LBB0_8
; %bb.6:                                ; %.lr.ph.i.preheader
                                        ;   in Loop: Header=BB0_5 Depth=1
	v_dual_mov_b32 v10, v3 :: v_dual_mov_b32 v9, v2
	s_mov_b32 s65, s5
	s_mov_b32 s66, s4
.LBB0_7:                                ; %.lr.ph.i
                                        ;   Parent Loop BB0_5 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	global_load_b64 v[11:12], v[9:10], off
	v_mov_b32_e32 v13, s66
	s_mov_b32 s58, s10
	s_mov_b32 s60, s28
	s_mov_b32 s62, s30
	s_add_i32 s65, s65, -1
	ds_load_b64 v[13:14], v13
	s_add_i32 s66, s66, 8
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_add_f64 v[11:12], v[11:12], -v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_frexp_mant_f64_e64 v[13:14], |v[11:12]|
	v_cmp_gt_f64_e32 vcc_lo, s[10:11], v[13:14]
	v_cndmask_b32_e64 v15, 0, 1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ldexp_f64 v[13:14], v[13:14], v15
	v_add_f64 v[15:16], v[13:14], 1.0
	v_add_f64 v[21:22], v[13:14], -1.0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[17:18], v[15:16]
	v_add_f64 v[23:24], v[15:16], -1.0
	v_add_f64 v[13:14], v[13:14], -v[23:24]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[19:20], -v[15:16], v[17:18], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[17:18], v[19:20], v[17:18], v[17:18]
	v_fma_f64 v[19:20], -v[15:16], v[17:18], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[17:18], v[19:20], v[17:18], v[17:18]
	v_mul_f64 v[19:20], v[21:22], v[17:18]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[25:26], v[15:16], v[19:20]
	v_fma_f64 v[15:16], v[19:20], v[15:16], -v[25:26]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], v[19:20], v[13:14], v[15:16]
	v_add_f64 v[15:16], v[25:26], v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_f64 v[23:24], v[21:22], -v[15:16]
	v_add_f64 v[25:26], v[15:16], -v[25:26]
	v_add_f64 v[21:22], v[21:22], -v[23:24]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[13:14], v[25:26], -v[13:14]
	v_add_f64 v[15:16], v[21:22], -v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[13:14], v[13:14], v[15:16]
	v_add_f64 v[13:14], v[23:24], v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[13:14], v[17:18], v[13:14]
	v_add_f64 v[15:16], v[19:20], v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_f64 v[17:18], v[15:16], -v[19:20]
	v_mul_f64 v[19:20], v[15:16], v[15:16]
	v_add_f64 v[13:14], v[13:14], -v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f64 v[17:18], v[15:16], v[15:16], -v[19:20]
	v_add_f64 v[21:22], v[13:14], v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[17:18], v[15:16], v[21:22], v[17:18]
	v_add_f64 v[21:22], v[19:20], v[17:18]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_fma_f64 v[23:24], v[21:22], s[14:15], s[12:13]
	v_add_f64 v[19:20], v[21:22], -v[19:20]
	v_mul_f64 v[27:28], v[15:16], v[21:22]
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[16:17]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[17:18], v[17:18], -v[19:20]
	v_fma_f64 v[31:32], v[21:22], v[15:16], -v[27:28]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[8:9]
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[18:19]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[20:21]
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[22:23]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[24:25]
	v_fma_f64 v[23:24], v[21:22], v[23:24], s[26:27]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[25:26], v[21:22], v[23:24]
	v_fma_f64 v[19:20], v[21:22], v[23:24], -v[25:26]
	v_fma_f64 v[21:22], v[21:22], v[13:14], v[31:32]
	v_ldexp_f64 v[13:14], v[13:14], 1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f64 v[19:20], v[17:18], v[23:24], v[19:20]
	v_fma_f64 v[17:18], v[17:18], v[15:16], v[21:22]
	v_ldexp_f64 v[15:16], v[15:16], 1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[23:24], v[25:26], v[19:20]
	v_add_f64 v[21:22], v[27:28], v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_f64 v[29:30], v[23:24], s[10:11]
	v_add_f64 v[25:26], v[23:24], -v[25:26]
	v_add_f64 v[27:28], v[21:22], -v[27:28]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[33:34], v[29:30], s[58:59]
	v_add_f64 v[19:20], v[19:20], -v[25:26]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[17:18], v[17:18], -v[27:28]
	v_add_f64 v[23:24], v[23:24], -v[33:34]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[19:20], v[19:20], s[34:35]
	v_add_f64 v[19:20], v[19:20], v[23:24]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[23:24], v[29:30], v[19:20]
	v_add_f64 v[25:26], v[29:30], -v[23:24]
	v_mul_f64 v[29:30], v[21:22], v[23:24]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[19:20], v[19:20], v[25:26]
	v_fma_f64 v[25:26], v[21:22], v[23:24], -v[29:30]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[19:20], v[21:22], v[19:20], v[25:26]
	v_frexp_exp_i32_f64_e32 v21, v[11:12]
	v_fma_f64 v[17:18], v[17:18], v[23:24], v[19:20]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_subrev_co_ci_u32_e32 v21, vcc_lo, 0, v21, vcc_lo
	v_cvt_f64_i32_e32 v[21:22], v21
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[19:20], v[29:30], v[17:18]
	v_mul_f64 v[27:28], v[21:22], s[28:29]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_f64 v[23:24], v[15:16], v[19:20]
	v_add_f64 v[25:26], v[19:20], -v[29:30]
	v_add_f64 v[15:16], v[23:24], -v[15:16]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_f64 v[17:18], v[17:18], -v[25:26]
	v_fma_f64 v[25:26], v[21:22], s[28:29], -v[27:28]
	v_add_f64 v[15:16], v[19:20], -v[15:16]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[13:14], v[13:14], v[17:18]
	v_fma_f64 v[17:18], v[21:22], s[30:31], v[25:26]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[13:14], v[13:14], v[15:16]
	v_add_f64 v[15:16], v[27:28], v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[19:20], v[23:24], v[13:14]
	v_add_f64 v[27:28], v[15:16], -v[27:28]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_f64 v[21:22], v[15:16], v[19:20]
	v_add_f64 v[23:24], v[19:20], -v[23:24]
	v_add_f64 v[17:18], v[17:18], -v[27:28]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[25:26], v[21:22], -v[15:16]
	v_add_f64 v[13:14], v[13:14], -v[23:24]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_f64 v[29:30], v[21:22], -v[25:26]
	v_add_f64 v[19:20], v[19:20], -v[25:26]
	v_add_f64 v[23:24], v[17:18], v[13:14]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[15:16], v[15:16], -v[29:30]
	v_add_f64 v[15:16], v[19:20], v[15:16]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[19:20], v[23:24], -v[17:18]
	v_add_f64 v[15:16], v[23:24], v[15:16]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_f64 v[23:24], v[23:24], -v[19:20]
	v_add_f64 v[13:14], v[13:14], -v[19:20]
	v_add_f64 v[25:26], v[21:22], v[15:16]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[17:18], v[17:18], -v[23:24]
	v_add_f64 v[19:20], v[25:26], -v[21:22]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_f64 v[13:14], v[13:14], v[17:18]
	v_add_f64 v[15:16], v[15:16], -v[19:20]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[13:14], v[13:14], v[15:16]
	v_add_f64 v[15:16], v[25:26], v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_f64 v[17:18], v[15:16], -v[25:26]
	v_add_f64 v[19:20], v[15:16], v[15:16]
	v_add_f64 v[13:14], v[13:14], -v[17:18]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[15:16], v[15:16], 2.0, -v[19:20]
	v_cmp_class_f64_e64 vcc_lo, v[19:20], 0x204
	v_fma_f64 v[13:14], v[13:14], 2.0, v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[15:16], v[19:20], v[13:14]
	v_dual_cndmask_b32 v18, v16, v20 :: v_dual_cndmask_b32 v17, v15, v19
	v_add_f64 v[15:16], v[15:16], -v[19:20]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_neq_f64_e64 vcc_lo, 0x7ff00000, |v[17:18]|
	v_add_f64 v[13:14], v[13:14], -v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	v_cndmask_b32_e32 v14, 0, v14, vcc_lo
	v_mul_f64 v[21:22], v[17:18], s[36:37]
	v_cmp_nlt_f64_e64 s2, 0x40900000, v[17:18]
	v_cmp_ngt_f64_e64 s3, 0xc090cc00, v[17:18]
	v_rndne_f64_e32 v[21:22], v[21:22]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[23:24], v[21:22], s[60:61], v[17:18]
	v_cvt_i32_f64_e32 v27, v[21:22]
	v_fma_f64 v[23:24], v[21:22], s[62:63], v[23:24]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[25:26], v[23:24], s[40:41], s[38:39]
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[42:43]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[44:45]
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[46:47]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[48:49]
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[50:51]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[52:53]
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[54:55]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[25:26], v[23:24], v[25:26], s[56:57]
	v_fma_f64 v[25:26], v[23:24], v[25:26], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[21:22], v[23:24], v[25:26], 1.0
	v_ldexp_f64 v[19:20], v[21:22], v27
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v15, 0x7ff00000, v20, s2
	v_cndmask_b32_e64 v16, 0, v15, s3
	v_cndmask_b32_e32 v13, 0, v13, vcc_lo
	s_and_b32 vcc_lo, s3, s2
	v_cmp_neq_f64_e64 s2, 0x7ff00000, |v[11:12]|
	v_cndmask_b32_e32 v15, 0, v19, vcc_lo
	v_cmp_neq_f64_e64 s3, 0, v[11:12]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[13:14], v[15:16], v[13:14], v[15:16]
	v_cmp_class_f64_e64 vcc_lo, v[15:16], 0x204
	v_cndmask_b32_e32 v14, v14, v16, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v11, v13, v15 :: v_dual_and_b32 v14, 0x7fffffff, v14
	s_and_b32 vcc_lo, s3, s2
	s_cmp_lg_u32 s65, 0
	v_cndmask_b32_e64 v14, 0x7ff00000, v14, s2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_cndmask_b32_e32 v11, 0, v11, vcc_lo
	v_add_co_u32 v9, vcc_lo, v9, 8
	v_add_co_ci_u32_e32 v10, vcc_lo, 0, v10, vcc_lo
	v_cndmask_b32_e64 v12, 0, v14, s3
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f64 v[7:8], v[7:8], v[11:12]
	s_cbranch_scc1 .LBB0_7
.LBB0_8:                                ; %_Z17calculateDistancePdS_i.exit
                                        ;   in Loop: Header=BB0_5 Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_cmp_gt_f64_e32 vcc_lo, 0x10000000, v[7:8]
	s_cmp_eq_u32 s64, 0
	v_cndmask_b32_e64 v9, 0, 1, vcc_lo
	v_lshlrev_b32_e32 v9, 8, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ldexp_f64 v[7:8], v[7:8], v9
	v_rsq_f64_e32 v[9:10], v[7:8]
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[11:12], v[7:8], v[9:10]
	v_mul_f64 v[9:10], v[9:10], 0.5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], -v[9:10], v[11:12], 0.5
	v_fma_f64 v[11:12], v[11:12], v[13:14], v[11:12]
	v_fma_f64 v[9:10], v[9:10], v[13:14], v[9:10]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], -v[11:12], v[11:12], v[7:8]
	v_fma_f64 v[11:12], v[13:14], v[9:10], v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], -v[11:12], v[11:12], v[7:8]
	v_fma_f64 v[9:10], v[13:14], v[9:10], v[11:12]
	v_cndmask_b32_e64 v11, 0, 0xffffff80, vcc_lo
	v_cmp_class_f64_e64 vcc_lo, v[7:8], 0x260
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ldexp_f64 v[9:10], v[9:10], v11
	v_dual_cndmask_b32 v8, v10, v8 :: v_dual_cndmask_b32 v7, v9, v7
	s_cbranch_scc1 .LBB0_12
; %bb.9:                                ;   in Loop: Header=BB0_5 Depth=1
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_lt_f64_e32 v[7:8], v[5:6]
; %bb.10:                               ;   in Loop: Header=BB0_5 Depth=1
	v_dual_mov_b32 v4, s64 :: v_dual_mov_b32 v5, v7
	v_mov_b32_e32 v6, v8
; %bb.11:                               ; %Flow
                                        ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s2
	s_branch .LBB0_13
.LBB0_12:                               ;   in Loop: Header=BB0_5 Depth=1
	s_delay_alu instid0(VALU_DEP_1)
	v_dual_mov_b32 v5, v7 :: v_dual_mov_b32 v6, v8
	v_mov_b32_e32 v4, 0
.LBB0_13:                               ;   in Loop: Header=BB0_5 Depth=1
	s_add_i32 s64, s64, 1
	s_add_i32 s4, s4, s33
	s_cmp_eq_u32 s64, s6
	s_cbranch_scc0 .LBB0_5
	s_branch .LBB0_15
.LBB0_14:
                                        ; implicit-def: $vgpr4
.LBB0_15:                               ; %Flow71
	s_load_b128 s[0:3], s[0:1], 0x28
	v_ashrrev_i32_e32 v5, 31, v4
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_lshlrev_b64 v[2:3], 2, v[4:5]
	v_mov_b32_e32 v5, 1
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	v_add_co_u32 v2, vcc_lo, s2, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v3, vcc_lo
	global_store_b32 v[0:1], v4, off
	global_atomic_add_u32 v[2:3], v5, off
.LBB0_16:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z5kmeaniPdS_iiiPiS0_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 312
		.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 35
		.amdhsa_next_free_sgpr 67
		.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	_Z5kmeaniPdS_iiiPiS0_, .Lfunc_end0-_Z5kmeaniPdS_iiiPiS0_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 2572
; NumSgprs: 69
; NumVgprs: 35
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 8
; VGPRBlocks: 4
; NumSGPRsForWavesPerEU: 69
; NumVGPRsForWavesPerEU: 35
; 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:
      - .offset:         0
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .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
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         48
        .size:           8
        .value_kind:     global_buffer
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         60
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         64
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         68
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         70
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         72
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         74
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         76
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         78
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         112
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         120
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         176
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 312
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z5kmeaniPdS_iiiPiS0_
    .private_segment_fixed_size: 0
    .sgpr_count:     69
    .sgpr_spill_count: 0
    .symbol:         _Z5kmeaniPdS_iiiPiS0_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     35
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
