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

using namespace std;

#define SUBMATRIX_SIZE 50000
#define NUM_BIN 100
#define HIST_MIN 0.0
#define HIST_MAX 3e9

////////////////////////////////////////////////////////////////////////
__global__ void distance(float *x, float *y, float *z, int xind, int yind, int *dev_hist)
{


    //int idx = xind * blockDim.x + yind;
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    int thread_idx = idx;
    idx += xind;

    float x_idx = x[idx], y_idx =y[idx], z_idx = z[idx];
    float dist_x, dist_y, dist_z, dist;

    //int max = SUBMATRIX_SIZE*

    int ymax = yind + SUBMATRIX_SIZE;
    int bin_index; 
    int offset = 0;

    for(int i=yind; i<ymax; i++)
    {
        //if(idx != i)
        if(idx > i)
        {
            dist_x = x_idx - x[i];
            dist_y = y_idx - y[i];
            dist_z = z_idx - z[i];
            dist = sqrt(dist_x * dist_x + dist_y * dist_y + dist_z * dist_z);
            if(dist < HIST_MIN)
                bin_index = 0; 
            else if(dist >= HIST_MAX)
                bin_index = NUM_BIN + 1;
            else
                bin_index = int(((dist - HIST_MIN) * NUM_BIN / HIST_MAX) +1);    
                //bin_index = 5;

            //dev_hist[bin_index]++;

            offset = ((NUM_BIN+2)*thread_idx);
            bin_index += offset;

           //dev_hist[i] = xind;
           //dev_hist[i+idx] = idx;
           dev_hist[bin_index]++;

           //dev_hist[0+offset] = blockDim.x;
           //dev_hist[1+offset] = blockIdx.x;
           //dev_hist[2+offset] = threadIdx.x;
           //dev_hist[3+offset] = thread_idx;
           //dev_hist[4+offset] = idx;
           //dev_hist[5+offset] = yind;
           //dev_hist[6+offset] = ymax;
        }
    }
    //dev_hist[0 + (threadIdx.x*12)] = threadIdx.x;
    /*
    for (int i=0;i<10;i++)
    {
        offset = i*12;
        //offset = 0.0;
        dev_hist[threadIdx.x+offset] = threadIdx.x;
        //dev_hist[offset] = threadIdx.x;
        //dev_hist[offset] = 999;
    }
    */
}

////////////////////////////////////////////////////////////////////////

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

    float *dev_pos_x, *dev_pos_y, *dev_pos_z;
    float *pos_x, *pos_y, *pos_z;

    int NUM_PARTICLES;

    if (argc < 3)
    {

        printf("\nMust pass in cluster_data file  on command line!\n");
        printf("\nUsage: ", argv[0] );
        printf(" <cluster_data file> <distances file> \n\n");
        exit(1);
    }

    FILE *infile, *outfile ;
    infile = fopen(argv[1],"r");
    outfile = fopen(argv[2], "w");

    //////////////////////////////////////////////////////////////////////
    // Read in the cluster_data file
    ////////////////////////////////////////////////////////////////////////////

    // char axis_titles[256];
    char dummy[256];

    fscanf(infile, "%d", &NUM_PARTICLES);
    //fscanf(infile, "%s %s %s %s", &axis_titles, &dummy, &axis_titles, &dummy);

    int size = NUM_PARTICLES * sizeof(float);    
    printf("# particles: %d\n",NUM_PARTICLES);

    pos_x = (float*)malloc(size);
    pos_y = (float*)malloc(size);
    pos_z = (float*)malloc(size);

    for(int i=0; i<NUM_PARTICLES; i++)
    {
        fscanf(infile, "%e %s %e %s %e %s", &pos_x[i], &dummy, &pos_y[i], &dummy, &pos_z[i], &dummy);
        //printf("%e %s %e %s %e %s\n", pos_x[i], dummy, pos_y[i], dummy, pos_z[i], dummy);
    }
    ////////////////////////////////////////////////////////////////////////////
    //allocation of histogram
    ///////////////////////////////////////////////////////////////////////////

    int *hist, *dev_hist;
    int size_hist = SUBMATRIX_SIZE * (NUM_BIN+2);
    int size_hist_bytes = size_hist*sizeof(int);

    hist = (int*)malloc(size_hist_bytes);
    memset(hist, 0, size_hist_bytes);

    printf("size_hist: %d\n",size_hist_bytes);
    cudaMalloc((void **) &dev_hist, (size_hist_bytes));
    cudaMemset(dev_hist, 0, size_hist_bytes);

    unsigned long  *hist_array;

    hist_array =  (unsigned long*)malloc((NUM_BIN+2) * sizeof(unsigned long));
    memset(hist_array, 0, (NUM_BIN+2)*sizeof(unsigned long)); 

    ////////////////////////////////////////////////////////////////////////////
    // Define the grid and block size
    ////////////////////////////////////////////////////////////////////////////
    dim3 grid, block;
    grid.x =100;
    block.x = SUBMATRIX_SIZE/grid.x; //NUM_PARTICLES/block.x;
    ////////////////////////////////////////////////////////////////////////////

    cudaMalloc((void **) &dev_pos_x, size );
    cudaMalloc((void **) &dev_pos_y, size );
    cudaMalloc((void **) &dev_pos_z, size );

    // Check to see if we allocated enough memory.
    if (0==dev_pos_z || 0==dev_pos_y|| 0==dev_pos_x || 0==dev_hist)
    {
        printf("couldn't allocate memory\n");
        return 1;
    }


    // Initialize array to all 0's
    cudaMemset(dev_pos_x,0,size);
    cudaMemset(dev_pos_z,0,size);
    cudaMemset(dev_pos_y,0,size);

    cudaMemcpy(dev_pos_x, pos_x, size, cudaMemcpyHostToDevice );
    cudaMemcpy(dev_pos_y, pos_y, size, cudaMemcpyHostToDevice );
    cudaMemcpy(dev_pos_z, pos_z, size, cudaMemcpyHostToDevice );

    int x, y;
    int num_submatrices = NUM_PARTICLES / SUBMATRIX_SIZE;


    int bin_index = 0;
    for(int k = 0; k < num_submatrices; k++)
    {
        y = k*SUBMATRIX_SIZE;
//        printf("%d %d\n",k,y);
        for(int j = 0; j < num_submatrices; j++)
        {
                x = j *SUBMATRIX_SIZE; 

                //printf("----\n");
                //printf("%d %d\t\t%d %d\n",k,y,j,x);
                //printf("----\n");

                cudaMemset(dev_hist,0,size_hist_bytes);

                distance<<<grid,block>>>(dev_pos_x, dev_pos_y, dev_pos_z, x, y, dev_hist);
                cudaMemcpy(hist, dev_hist, size_hist_bytes, cudaMemcpyDeviceToHost);


                for(int m=0; m<size_hist; m++)
                {

                    bin_index = m%(NUM_BIN+2);
                    //if(bin_index == 0)
                        //printf("\n");

                    //printf("%3i:%3i ", m, hist[m]);
                    //printf("%3i ", hist[m]);

                    hist_array[bin_index] += hist[m];
                }    
                //printf("\n");
        }  
    }

    // cudaMemcpy(hist, dev_hist, size_hist, cudaMemcpyDeviceToHost);
    /*
    for(int j=0; j<NUM_BIN+2; j++)
        for(int i=0; i<SUBMATRIX_SIZE; i++)
            hist_array[j] += hist[i*(NUM_BIN + 2)+j];
    */
    unsigned long total = 0;
    float  bin_width = (HIST_MAX - HIST_MIN) / NUM_BIN;
    float bins_mid = 0;
     
    for(int k=0; k<NUM_BIN+2; k++)
    {
       if(k>0)
          bins_mid = bin_width*(k - 0.5);
       else 
          bins_mid = -1.;
       fprintf(outfile, "%.3e %s %lu \n", bins_mid, ",",  hist_array[k]);
printf("hist: %lu \n", hist_array[k]);
       total += hist_array[k];

printf("total: %lu \n", total);
    }
    printf("total: %lu \n", total);
    
    fclose(infile);
    fclose(outfile);

    free(pos_x);
    free(pos_y);
    free(pos_z);
    free(hist);

    cudaFree(dev_pos_x);
    cudaFree(dev_pos_y);  
    cudaFree(dev_pos_z);
    cudaFree(dev_hist);

    return 0;
}  
//////////////////////////////////////////////////////////////////////

	.file	"tmpxft_0034c2b7_00000000-6_CUDA_copy_coordinates_with_C_hist.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	_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi
	.type	_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi, @function
_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movl	%ecx, 20(%rsp)
	leaq	64(%rsp), %rcx
	movl	%r8d, 16(%rsp)
	movq	%r9, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%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	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z8distancePfS_S_iiPi(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	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 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	_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi, .-_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi
	.globl	_Z8distancePfS_S_iiPi
	.type	_Z8distancePfS_S_iiPi, @function
_Z8distancePfS_S_iiPi:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi
	.cfi_endproc
.LFE2053:
	.size	_Z8distancePfS_S_iiPi, .-_Z8distancePfS_S_iiPi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"\nMust pass in cluster_data file  on command line!\n"
.LC2:
	.string	"\nUsage: "
.LC3:
	.string	" <cluster_data file> <distances file> \n\n"
.LC4:
	.string	"r"
.LC5:
	.string	"w"
.LC6:
	.string	"%d"
.LC7:
	.string	"# particles: %d\n"
.LC8:
	.string	"%e %s %e %s %e %s"
.LC9:
	.string	"size_hist: %d\n"
.LC10:
	.string	"couldn't allocate memory\n"
.LC13:
	.string	","
.LC14:
	.string	"%.3e %s %lu \n"
.LC15:
	.string	"hist: %lu \n"
.LC16:
	.string	"total: %lu \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
	movq	%rsi, %rbx
	subq	$392, %rsp
	.cfi_def_cfa_offset 448
	movq	%fs:40, %rax
	movq	%rax, 376(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	jg	.L9
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	(%rbx), %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movl	$2, %edi
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	movq	8(%rsi), %rdi
	xorl	%r12d, %r12d
	leaq	.LC4(%rip), %rsi
	xorl	%r15d, %r15d
	leaq	.LC8(%rip), %rbp
	call	fopen@PLT
	movq	16(%rbx), %rdi
	leaq	.LC5(%rip), %rsi
	movq	%rax, 8(%rsp)
	call	fopen@PLT
	movq	8(%rsp), %rdi
	leaq	60(%rsp), %rdx
	leaq	.LC6(%rip), %rsi
	movq	%rax, 32(%rsp)
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	60(%rsp), %ebx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	movl	%ebx, %edx
	sall	$2, %ebx
	call	__printf_chk@PLT
	movslq	%ebx, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r14
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rax, 16(%rsp)
.L10:
	cmpl	%r15d, 60(%rsp)
	jle	.L32
	leaq	120(%rsp), %rcx
	leaq	(%r14,%r12), %rdx
	movq	%rbp, %rsi
	incl	%r15d
	pushq	%rcx
	.cfi_def_cfa_offset 456
	movq	24(%rsp), %rax
	leaq	0(%r13,%r12), %r8
	movq	%rcx, %r9
	addq	%r12, %rax
	addq	$4, %r12
	pushq	%rax
	.cfi_def_cfa_offset 464
	movq	24(%rsp), %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	popq	%rax
	.cfi_def_cfa_offset 456
	popq	%rdx
	.cfi_def_cfa_offset 448
	jmp	.L10
.L32:
	movl	$20400000, %edi
	xorl	%r15d, %r15d
	call	malloc@PLT
	movl	$5100000, %ecx
	movl	$20400000, %edx
	leaq	.LC9(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, %rbp
	movl	%r15d, %eax
	rep stosl
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	88(%rsp), %rdi
	movl	$20400000, %esi
	call	cudaMalloc@PLT
	movq	88(%rsp), %rdi
	movl	$20400000, %edx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movl	$816, %edi
	call	malloc@PLT
	movl	$204, %ecx
	movq	%rbx, %rsi
	movl	$1, 116(%rsp)
	movq	%rax, %rdi
	movq	%rax, %r12
	movl	%r15d, %eax
	rep stosl
	leaq	64(%rsp), %rdi
	movabsq	$4294967297, %rax
	movq	%rax, 100(%rsp)
	addq	$499, %rax
	movq	%rax, 108(%rsp)
	call	cudaMalloc@PLT
	leaq	72(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	80(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	cmpq	$0, 80(%rsp)
	je	.L12
	cmpq	$0, 72(%rsp)
	je	.L12
	movq	64(%rsp), %rdi
	testq	%rdi, %rdi
	je	.L12
	cmpq	$0, 88(%rsp)
	jne	.L13
.L12:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L8
.L13:
	movq	%rbx, %rdx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movq	80(%rsp), %rdi
	movq	%rbx, %rdx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movq	72(%rsp), %rdi
	movq	%rbx, %rdx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movq	64(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	72(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rsi
	movq	80(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%ebx, %ebx
	call	cudaMemcpy@PLT
	movl	60(%rsp), %eax
	movl	$50000, %ecx
	cltd
	movl	%eax, 24(%rsp)
	idivl	%ecx
	movl	%eax, 40(%rsp)
.L15:
	movl	40(%rsp), %eax
	cmpl	%eax, %ebx
	jge	.L33
	imull	$50000, %ebx, %eax
	cmpl	$49999, 24(%rsp)
	movl	%eax, 44(%rsp)
	jle	.L16
	xorl	%r15d, %r15d
.L19:
	movq	88(%rsp), %rdi
	xorl	%esi, %esi
	movl	$20400000, %edx
	call	cudaMemset@PLT
	movl	116(%rsp), %ecx
	movl	104(%rsp), %esi
	xorl	%r9d, %r9d
	movl	$100, 96(%rsp)
	movq	108(%rsp), %rdx
	xorl	%r8d, %r8d
	movq	96(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L17
	imull	$50000, %r15d, %ecx
	movq	88(%rsp), %r9
	movl	44(%rsp), %r8d
	movq	80(%rsp), %rdx
	movq	72(%rsp), %rsi
	movq	64(%rsp), %rdi
	call	_Z35__device_stub__Z8distancePfS_S_iiPiPfS_S_iiPi
.L17:
	movq	88(%rsp), %rsi
	movl	$2, %ecx
	movl	$20400000, %edx
	movq	%rbp, %rdi
	call	cudaMemcpy@PLT
	xorl	%ecx, %ecx
.L18:
	movl	%ecx, %eax
	movl	$102, %esi
	cltd
	idivl	%esi
	movslq	0(%rbp,%rcx,4), %rax
	incq	%rcx
	movslq	%edx, %rdx
	addq	%rax, (%r12,%rdx,8)
	cmpq	$5100000, %rcx
	jne	.L18
	incl	%r15d
	cmpl	%r15d, 40(%rsp)
	jne	.L19
.L16:
	incl	%ebx
	jmp	.L15
.L33:
	xorl	%r9d, %r9d
	xorl	%ebx, %ebx
.L22:
	movss	.LC0(%rip), %xmm0
	testq	%r9, %r9
	je	.L21
	cvtsi2sdl	%r9d, %xmm0
	subsd	.LC11(%rip), %xmm0
	mulsd	.LC12(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
.L21:
	movq	(%r12,%r9,8), %r15
	movq	32(%rsp), %rdi
	movl	$2, %esi
	movb	$1, %al
	leaq	.LC13(%rip), %rcx
	leaq	.LC14(%rip), %rdx
	movq	%r9, 24(%rsp)
	cvtss2sd	%xmm0, %xmm0
	movq	%r15, %r8
	addq	%r15, %rbx
	call	__fprintf_chk@PLT
	movq	%r15, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rsi
	leaq	.LC16(%rip), %r15
	call	__printf_chk@PLT
	movq	%rbx, %rdx
	movq	%r15, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	24(%rsp), %r9
	incq	%r9
	cmpq	$102, %r9
	jne	.L22
	movq	%rbx, %rdx
	movq	%r15, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	32(%rsp), %rdi
	call	fclose@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L8:
	movq	376(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$392, %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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC17:
	.string	"_Z8distancePfS_S_iiPi"
	.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	.LC17(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8distancePfS_S_iiPi(%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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC11:
	.long	0
	.long	1071644672
	.align 8
.LC12:
	.long	0
	.long	1098685496
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

#define SUBMATRIX_SIZE 50000
#define NUM_BIN 100
#define HIST_MIN 0.0
#define HIST_MAX 3e9

////////////////////////////////////////////////////////////////////////
__global__ void distance(float *x, float *y, float *z, int xind, int yind, int *dev_hist)
{


    //int idx = xind * blockDim.x + yind;
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    int thread_idx = idx;
    idx += xind;

    float x_idx = x[idx], y_idx =y[idx], z_idx = z[idx];
    float dist_x, dist_y, dist_z, dist;

    //int max = SUBMATRIX_SIZE*

    int ymax = yind + SUBMATRIX_SIZE;
    int bin_index; 
    int offset = 0;

    for(int i=yind; i<ymax; i++)
    {
        //if(idx != i)
        if(idx > i)
        {
            dist_x = x_idx - x[i];
            dist_y = y_idx - y[i];
            dist_z = z_idx - z[i];
            dist = sqrt(dist_x * dist_x + dist_y * dist_y + dist_z * dist_z);
            if(dist < HIST_MIN)
                bin_index = 0; 
            else if(dist >= HIST_MAX)
                bin_index = NUM_BIN + 1;
            else
                bin_index = int(((dist - HIST_MIN) * NUM_BIN / HIST_MAX) +1);    
                //bin_index = 5;

            //dev_hist[bin_index]++;

            offset = ((NUM_BIN+2)*thread_idx);
            bin_index += offset;

           //dev_hist[i] = xind;
           //dev_hist[i+idx] = idx;
           dev_hist[bin_index]++;

           //dev_hist[0+offset] = blockDim.x;
           //dev_hist[1+offset] = blockIdx.x;
           //dev_hist[2+offset] = threadIdx.x;
           //dev_hist[3+offset] = thread_idx;
           //dev_hist[4+offset] = idx;
           //dev_hist[5+offset] = yind;
           //dev_hist[6+offset] = ymax;
        }
    }
    //dev_hist[0 + (threadIdx.x*12)] = threadIdx.x;
    /*
    for (int i=0;i<10;i++)
    {
        offset = i*12;
        //offset = 0.0;
        dev_hist[threadIdx.x+offset] = threadIdx.x;
        //dev_hist[offset] = threadIdx.x;
        //dev_hist[offset] = 999;
    }
    */
}

////////////////////////////////////////////////////////////////////////

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

    float *dev_pos_x, *dev_pos_y, *dev_pos_z;
    float *pos_x, *pos_y, *pos_z;

    int NUM_PARTICLES;

    if (argc < 3)
    {

        printf("\nMust pass in cluster_data file  on command line!\n");
        printf("\nUsage: ", argv[0] );
        printf(" <cluster_data file> <distances file> \n\n");
        exit(1);
    }

    FILE *infile, *outfile ;
    infile = fopen(argv[1],"r");
    outfile = fopen(argv[2], "w");

    //////////////////////////////////////////////////////////////////////
    // Read in the cluster_data file
    ////////////////////////////////////////////////////////////////////////////

    // char axis_titles[256];
    char dummy[256];

    fscanf(infile, "%d", &NUM_PARTICLES);
    //fscanf(infile, "%s %s %s %s", &axis_titles, &dummy, &axis_titles, &dummy);

    int size = NUM_PARTICLES * sizeof(float);    
    printf("# particles: %d\n",NUM_PARTICLES);

    pos_x = (float*)malloc(size);
    pos_y = (float*)malloc(size);
    pos_z = (float*)malloc(size);

    for(int i=0; i<NUM_PARTICLES; i++)
    {
        fscanf(infile, "%e %s %e %s %e %s", &pos_x[i], &dummy, &pos_y[i], &dummy, &pos_z[i], &dummy);
        //printf("%e %s %e %s %e %s\n", pos_x[i], dummy, pos_y[i], dummy, pos_z[i], dummy);
    }
    ////////////////////////////////////////////////////////////////////////////
    //allocation of histogram
    ///////////////////////////////////////////////////////////////////////////

    int *hist, *dev_hist;
    int size_hist = SUBMATRIX_SIZE * (NUM_BIN+2);
    int size_hist_bytes = size_hist*sizeof(int);

    hist = (int*)malloc(size_hist_bytes);
    memset(hist, 0, size_hist_bytes);

    printf("size_hist: %d\n",size_hist_bytes);
    hipMalloc((void **) &dev_hist, (size_hist_bytes));
    hipMemset(dev_hist, 0, size_hist_bytes);

    unsigned long  *hist_array;

    hist_array =  (unsigned long*)malloc((NUM_BIN+2) * sizeof(unsigned long));
    memset(hist_array, 0, (NUM_BIN+2)*sizeof(unsigned long)); 

    ////////////////////////////////////////////////////////////////////////////
    // Define the grid and block size
    ////////////////////////////////////////////////////////////////////////////
    dim3 grid, block;
    grid.x =100;
    block.x = SUBMATRIX_SIZE/grid.x; //NUM_PARTICLES/block.x;
    ////////////////////////////////////////////////////////////////////////////

    hipMalloc((void **) &dev_pos_x, size );
    hipMalloc((void **) &dev_pos_y, size );
    hipMalloc((void **) &dev_pos_z, size );

    // Check to see if we allocated enough memory.
    if (0==dev_pos_z || 0==dev_pos_y|| 0==dev_pos_x || 0==dev_hist)
    {
        printf("couldn't allocate memory\n");
        return 1;
    }


    // Initialize array to all 0's
    hipMemset(dev_pos_x,0,size);
    hipMemset(dev_pos_z,0,size);
    hipMemset(dev_pos_y,0,size);

    hipMemcpy(dev_pos_x, pos_x, size, hipMemcpyHostToDevice );
    hipMemcpy(dev_pos_y, pos_y, size, hipMemcpyHostToDevice );
    hipMemcpy(dev_pos_z, pos_z, size, hipMemcpyHostToDevice );

    int x, y;
    int num_submatrices = NUM_PARTICLES / SUBMATRIX_SIZE;


    int bin_index = 0;
    for(int k = 0; k < num_submatrices; k++)
    {
        y = k*SUBMATRIX_SIZE;
//        printf("%d %d\n",k,y);
        for(int j = 0; j < num_submatrices; j++)
        {
                x = j *SUBMATRIX_SIZE; 

                //printf("----\n");
                //printf("%d %d\t\t%d %d\n",k,y,j,x);
                //printf("----\n");

                hipMemset(dev_hist,0,size_hist_bytes);

                distance<<<grid,block>>>(dev_pos_x, dev_pos_y, dev_pos_z, x, y, dev_hist);
                hipMemcpy(hist, dev_hist, size_hist_bytes, hipMemcpyDeviceToHost);


                for(int m=0; m<size_hist; m++)
                {

                    bin_index = m%(NUM_BIN+2);
                    //if(bin_index == 0)
                        //printf("\n");

                    //printf("%3i:%3i ", m, hist[m]);
                    //printf("%3i ", hist[m]);

                    hist_array[bin_index] += hist[m];
                }    
                //printf("\n");
        }  
    }

    // cudaMemcpy(hist, dev_hist, size_hist, cudaMemcpyDeviceToHost);
    /*
    for(int j=0; j<NUM_BIN+2; j++)
        for(int i=0; i<SUBMATRIX_SIZE; i++)
            hist_array[j] += hist[i*(NUM_BIN + 2)+j];
    */
    unsigned long total = 0;
    float  bin_width = (HIST_MAX - HIST_MIN) / NUM_BIN;
    float bins_mid = 0;
     
    for(int k=0; k<NUM_BIN+2; k++)
    {
       if(k>0)
          bins_mid = bin_width*(k - 0.5);
       else 
          bins_mid = -1.;
       fprintf(outfile, "%.3e %s %lu \n", bins_mid, ",",  hist_array[k]);
printf("hist: %lu \n", hist_array[k]);
       total += hist_array[k];

printf("total: %lu \n", total);
    }
    printf("total: %lu \n", total);
    
    fclose(infile);
    fclose(outfile);

    free(pos_x);
    free(pos_y);
    free(pos_z);
    free(hist);

    hipFree(dev_pos_x);
    hipFree(dev_pos_y);  
    hipFree(dev_pos_z);
    hipFree(dev_hist);

    return 0;
}  
//////////////////////////////////////////////////////////////////////

	.text
	.file	"CUDA_copy_coordinates_with_C_hist.hip"
	.globl	_Z23__device_stub__distancePfS_S_iiPi # -- Begin function _Z23__device_stub__distancePfS_S_iiPi
	.type	_Z23__device_stub__distancePfS_S_iiPi,@function
_Z23__device_stub__distancePfS_S_iiPi:  # @_Z23__device_stub__distancePfS_S_iiPi
	.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	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	12(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	8(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	32(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%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	80(%rsp), %r14
	leaq	64(%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	$_Z8distancePfS_S_iiPi, %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	_Z23__device_stub__distancePfS_S_iiPi, .Lfunc_end0-_Z23__device_stub__distancePfS_S_iiPi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0xbfe0000000000000              # double -0.5
.LCPI1_1:
	.quad	0x417c9c3800000000              # double 3.0E+7
.LCPI1_2:
	.quad	0xbff0000000000000              # double -1
	.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	$376, %rsp                      # imm = 0x178
	.cfi_def_cfa_offset 432
	.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	%rsi, %r14
	cmpl	$2, %edi
	jle	.LBB1_24
# %bb.1:
	movq	8(%r14), %rdi
	movl	$.L.str.3, %esi
	callq	fopen
	movq	%rax, %r12
	movq	16(%r14), %rdi
	movl	$.L.str.4, %esi
	callq	fopen
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	leaq	28(%rsp), %r14
	movl	$.L.str.5, %esi
	movq	%r12, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r14), %esi
	leal	(,%rsi,4), %ebx
	movl	$.L.str.6, %edi
                                        # kill: def $esi killed $esi killed $rsi
	xorl	%eax, %eax
	callq	printf
	movslq	%ebx, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	movq	%rbx, 72(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	cmpl	$0, (%r14)
	jle	.LBB1_4
# %bb.2:                                # %.lr.ph.preheader
	leaq	112(%rsp), %r14
	movq	64(%rsp), %r15                  # 8-byte Reload
	movq	56(%rsp), %r13                  # 8-byte Reload
	movq	48(%rsp), %rbx                  # 8-byte Reload
	xorl	%ebp, %ebp
.LBB1_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.7, %esi
	movq	%r12, %rdi
	movq	%r15, %rdx
	movq	%r14, %rcx
	movq	%r13, %r8
	movq	%r14, %r9
	xorl	%eax, %eax
	pushq	%r14
	.cfi_adjust_cfa_offset 8
	pushq	%rbx
	.cfi_adjust_cfa_offset 8
	callq	__isoc23_fscanf
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
	incq	%rbp
	movslq	28(%rsp), %rax
	addq	$4, %rbx
	addq	$4, %r13
	addq	$4, %r15
	cmpq	%rax, %rbp
	jl	.LBB1_3
.LBB1_4:                                # %._crit_edge
	movq	%r12, 96(%rsp)                  # 8-byte Spill
	movl	$1, %edi
	movl	$20400000, %esi                 # imm = 0x1374780
	callq	calloc@PLT
	movq	%rax, %rbp
	movl	$.L.str.8, %edi
	movl	$20400000, %esi                 # imm = 0x1374780
	xorl	%eax, %eax
	callq	printf
	leaq	8(%rsp), %r14
	movl	$20400000, %esi                 # imm = 0x1374780
	movq	%r14, %rdi
	callq	hipMalloc
	movq	(%r14), %rdi
	movl	$20400000, %edx                 # imm = 0x1374780
	xorl	%esi, %esi
	callq	hipMemset
	movl	$1, %edi
	movl	$816, %esi                      # imm = 0x330
	callq	calloc@PLT
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	leaq	40(%rsp), %rdi
	movq	72(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %r14
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	cmpq	$0, (%r14)
	je	.LBB1_8
# %bb.5:                                # %._crit_edge
	cmpq	$0, 16(%rsp)
	je	.LBB1_8
# %bb.6:                                # %._crit_edge
	movq	40(%rsp), %rdi
	testq	%rdi, %rdi
	je	.LBB1_8
# %bb.7:                                # %._crit_edge
	cmpq	$0, 8(%rsp)
	je	.LBB1_8
# %bb.9:
	xorl	%r14d, %r14d
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	hipMemset
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	hipMemset
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	hipMemset
	movq	40(%rsp), %rdi
	movq	64(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	movq	56(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	32(%rsp), %rdi
	movq	48(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movslq	28(%rsp), %rax
	cmpq	$50000, %rax                    # imm = 0xC350
	jl	.LBB1_18
# %bb.10:                               # %.lr.ph115.preheader.preheader
	imulq	$351843721, %rax, %rbx          # imm = 0x14F8B589
	movq	%rbx, %rax
	shrq	$63, %rax
	sarq	$44, %rbx
	addl	%eax, %ebx
	movabsq	$4294967396, %r12               # imm = 0x100000064
	leaq	400(%r12), %r13
	movabsq	$-6872316419617283935, %r15     # imm = 0xA0A0A0A0A0A0A0A1
.LBB1_11:                               # %.lr.ph115.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_12 Depth 2
                                        #       Child Loop BB1_15 Depth 3
	movq	%r14, 104(%rsp)                 # 8-byte Spill
	imull	$50000, %r14d, %eax             # imm = 0xC350
	movl	%eax, 72(%rsp)                  # 4-byte Spill
	xorl	%r14d, %r14d
.LBB1_12:                               # %.lr.ph115
                                        #   Parent Loop BB1_11 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_15 Depth 3
	movq	8(%rsp), %rdi
	movl	$20400000, %edx                 # imm = 0x1374780
	xorl	%esi, %esi
	callq	hipMemset
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_14
# %bb.13:                               #   in Loop: Header=BB1_12 Depth=2
	imull	$50000, %r14d, %ecx             # imm = 0xC350
	movq	40(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	32(%rsp), %rdx
	movq	8(%rsp), %r9
	movl	72(%rsp), %r8d                  # 4-byte Reload
	callq	_Z23__device_stub__distancePfS_S_iiPi
.LBB1_14:                               #   in Loop: Header=BB1_12 Depth=2
	movq	8(%rsp), %rsi
	movl	$20400000, %edx                 # imm = 0x1374780
	movq	%rbp, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	80(%rsp), %rsi                  # 8-byte Reload
	xorl	%ecx, %ecx
.LBB1_15:                               #   Parent Loop BB1_11 Depth=1
                                        #     Parent Loop BB1_12 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movq	%rcx, %rax
	mulq	%r15
	shrq	$6, %rdx
	imulq	$-816, %rdx, %rax               # imm = 0xFCD0
	movslq	(%rbp,%rcx,4), %rdx
	addq	%rdx, (%rsi,%rax)
	incq	%rcx
	addq	$8, %rsi
	cmpq	$5100000, %rcx                  # imm = 0x4DD1E0
	jne	.LBB1_15
# %bb.16:                               #   in Loop: Header=BB1_12 Depth=2
	incl	%r14d
	cmpl	%ebx, %r14d
	jne	.LBB1_12
# %bb.17:                               # %._crit_edge116
                                        #   in Loop: Header=BB1_11 Depth=1
	movq	104(%rsp), %r14                 # 8-byte Reload
	incl	%r14d
	cmpl	%ebx, %r14d
	jne	.LBB1_11
.LBB1_18:                               # %.preheader.preheader
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
	movq	96(%rsp), %r13                  # 8-byte Reload
	movq	88(%rsp), %r15                  # 8-byte Reload
.LBB1_19:                               # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm1, %xmm1
	cvtsi2sd	%ebx, %xmm1
	addsd	.LCPI1_0(%rip), %xmm1
	mulsd	.LCPI1_1(%rip), %xmm1
	movsd	.LCPI1_2(%rip), %xmm0           # xmm0 = mem[0],zero
	testq	%rbx, %rbx
	je	.LBB1_21
# %bb.20:                               # %.preheader
                                        #   in Loop: Header=BB1_19 Depth=1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
.LBB1_21:                               # %.preheader
                                        #   in Loop: Header=BB1_19 Depth=1
	movq	80(%rsp), %rax                  # 8-byte Reload
	movq	(%rax,%rbx,8), %r12
	movl	$.L.str.10, %esi
	movl	$.L.str.11, %edx
	movq	%r15, %rdi
	movq	%r12, %rcx
	movb	$1, %al
	callq	fprintf
	movl	$.L.str.12, %edi
	movq	%r12, %rsi
	xorl	%eax, %eax
	callq	printf
	addq	%r12, %r14
	movl	$.L.str.13, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	incq	%rbx
	cmpq	$102, %rbx
	jne	.LBB1_19
# %bb.22:
	movq	%r15, %rbx
	xorl	%r15d, %r15d
	movl	$.L.str.13, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	%r13, %rdi
	callq	fclose
	movq	%rbx, %rdi
	callq	fclose
	movq	64(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	56(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	48(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%rbp, %rdi
	callq	free
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	jmp	.LBB1_23
.LBB1_8:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$1, %r15d
.LBB1_23:
	movl	%r15d, %eax
	addq	$376, %rsp                      # imm = 0x178
	.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_24:
	.cfi_def_cfa_offset 432
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movq	(%r14), %rsi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.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	$_Z8distancePfS_S_iiPi, %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	_Z8distancePfS_S_iiPi,@object   # @_Z8distancePfS_S_iiPi
	.section	.rodata,"a",@progbits
	.globl	_Z8distancePfS_S_iiPi
	.p2align	3, 0x0
_Z8distancePfS_S_iiPi:
	.quad	_Z23__device_stub__distancePfS_S_iiPi
	.size	_Z8distancePfS_S_iiPi, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"\nUsage: "
	.size	.L.str.1, 9

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

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

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

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"# particles: %d\n"
	.size	.L.str.6, 17

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"%e %s %e %s %e %s"
	.size	.L.str.7, 18

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

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"%.3e %s %lu \n"
	.size	.L.str.10, 14

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"hist: %lu \n"
	.size	.L.str.12, 12

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"total: %lu \n"
	.size	.L.str.13, 13

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z8distancePfS_S_iiPi"
	.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	"couldn't allocate memory"
	.size	.Lstr, 25

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\nMust pass in cluster_data file  on command line!"
	.size	.Lstr.1, 50

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	" <cluster_data file> <distances file> \n"
	.size	.Lstr.2, 40

	.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 _Z23__device_stub__distancePfS_S_iiPi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z8distancePfS_S_iiPi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8distancePfS_S_iiPi   ; -- Begin function _Z8distancePfS_S_iiPi
	.globl	_Z8distancePfS_S_iiPi
	.p2align	8
	.type	_Z8distancePfS_S_iiPi,@function
_Z8distancePfS_S_iiPi:                  ; @_Z8distancePfS_S_iiPi
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b256 s[4:11], s[0:1], 0x0
	s_mov_b32 s12, -2.0
	s_mov_b32 s13, 0x41e65a0b
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[4:5], null, s15, s2, v[0:1]
	s_load_b64 s[2:3], s[0:1], 0x20
	s_ashr_i32 s1, s11, 31
	s_mov_b32 s0, s11
	s_lshl_b64 s[14:15], s[0:1], 2
	s_add_i32 s1, s11, 0xc34f
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v0, s10, v4
	v_mul_lo_u32 v4, 0x66, v4
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[0:1]
	v_add_co_u32 v5, vcc_lo, s4, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v2, vcc_lo
	v_add_co_u32 v7, vcc_lo, s6, v1
	v_add_co_ci_u32_e32 v8, vcc_lo, s7, v2, vcc_lo
	v_add_co_u32 v9, vcc_lo, s8, v1
	v_add_co_ci_u32_e32 v10, vcc_lo, s9, v2, vcc_lo
	global_load_b32 v1, v[5:6], off
	global_load_b32 v2, v[7:8], off
	global_load_b32 v3, v[9:10], off
	s_add_u32 s4, s4, s14
	s_addc_u32 s5, s5, s15
	s_add_u32 s6, s6, s14
	s_addc_u32 s7, s7, s15
	s_add_u32 s8, s8, s14
	s_addc_u32 s9, s9, s15
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s10, exec_lo
	v_cmpx_lt_i32_e64 s11, v0
	s_cbranch_execz .LBB0_5
; %bb.2:                                ;   in Loop: Header=BB0_1 Depth=1
	s_load_b32 s0, s[6:7], 0x0
	s_load_b32 s14, s[4:5], 0x0
	s_load_b32 s15, s[8:9], 0x0
	s_waitcnt vmcnt(1) lgkmcnt(0)
	v_dual_subrev_f32 v5, s0, v2 :: v_dual_subrev_f32 v6, s14, v1
	s_waitcnt vmcnt(0)
	v_subrev_f32_e32 v7, s15, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v5, v5, v5
	v_fmac_f32_e32 v5, v6, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v5, v7, v7
	v_mul_f32_e32 v6, 0x4f800000, v5
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v5, v5, v6, vcc_lo
	v_sqrt_f32_e32 v6, v5
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v7, -1, v6
	v_add_nc_u32_e32 v8, 1, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v9, -v7, v6, v5
	v_fma_f32 v10, -v8, v6, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_f32_e64 s0, 0, v9
	v_cndmask_b32_e64 v6, v6, v7, s0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_lt_f32_e64 s0, 0, v10
	v_cndmask_b32_e64 v6, v6, v8, s0
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v7, 0x37800000, v6
	v_cndmask_b32_e32 v6, v6, v7, vcc_lo
	v_cmp_class_f32_e64 vcc_lo, v5, 0x260
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v6, v6, v5 :: v_dual_mov_b32 v5, 0x65
	v_cmpx_nle_f32_e32 0x4f32d05e, v6
	s_cbranch_execz .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_1 Depth=1
	v_cvt_f64_f32_e32 v[5:6], v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[5:6], 0x40590000, v[5:6]
	v_div_scale_f64 v[7:8], null, s[12:13], s[12:13], v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[9:10], v[7:8]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[11:12], -v[7:8], v[9:10], 1.0
	v_fma_f64 v[9:10], v[9:10], v[11:12], v[9:10]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[11:12], -v[7:8], v[9:10], 1.0
	v_fma_f64 v[9:10], v[9:10], v[11:12], v[9:10]
	v_div_scale_f64 v[11:12], vcc_lo, v[5:6], s[12:13], v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[13:14], v[11:12], v[9:10]
	v_fma_f64 v[7:8], -v[7:8], v[13:14], v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f64 v[7:8], v[7:8], v[9:10], v[13:14]
	v_div_fixup_f64 v[5:6], v[7:8], s[12:13], v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[5:6], v[5:6], 1.0
	v_cvt_i32_f64_e32 v5, v[5:6]
.LBB0_4:                                ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v5, v5, v4
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[5:6], 2, v[5:6]
	v_add_co_u32 v5, vcc_lo, s2, v5
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v6, vcc_lo, s3, v6, vcc_lo
	global_load_b32 v7, v[5:6], off
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v7, 1, v7
	global_store_b32 v[5:6], v7, off
.LBB0_5:                                ; %Flow
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
	s_add_i32 s0, s11, 1
	s_add_u32 s4, s4, 4
	s_addc_u32 s5, s5, 0
	s_add_u32 s6, s6, 4
	s_addc_u32 s7, s7, 0
	s_add_u32 s8, s8, 4
	s_addc_u32 s9, s9, 0
	s_cmp_ge_i32 s11, s1
	s_mov_b32 s11, s0
	s_cbranch_scc0 .LBB0_1
; %bb.6:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8distancePfS_S_iiPi
		.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 15
		.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	_Z8distancePfS_S_iiPi, .Lfunc_end0-_Z8distancePfS_S_iiPi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 712
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .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
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .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:           _Z8distancePfS_S_iiPi
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z8distancePfS_S_iiPi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
