#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>

const long BOX_SIZE = 23000; /* size of the data box on one dimension */
#define BLOCK_SIZE 512

#define EXITERROR() error_at_line(errno, errno, __FILE__, __LINE__, "pid %llu", (long long unsigned)getpid())

const char *argv0;

typedef struct hist_entry {
    //float min;
    //float max;
    unsigned long long d_cnt;   /* need a long long type as the count might be huge */
} bucket;

struct debuginfo {
    int idx;
    int ran;
    int i;
    int j;
    float dist;
    int which_bucket;
};


bucket * histogram;  /* list of all buckets in the histogram   */
unsigned long long num_points; /* total number of data points            */
int num_buckets;  /* total number of buckets in the histogram */
double resolution;  /* value of w                             */
double4 *h_points;


inline __device__ double dist3(double4 a, double4 b) {
    return norm3d(a.x - b.x, a.y - b.y, a.z - b.z);
}


__global__
void PDH_kernel(bucket *g_bins, size_t n_bins, double4 *g_points, size_t n_points, double res) {
    __shared__ double4 s_block[BLOCK_SIZE]; // R for inter-block, L for intra-block

    unsigned int t = threadIdx.x;
    unsigned int b = blockIdx.x;
    unsigned int B = blockDim.x;
    unsigned int M = gridDim.x;

    // Is our anchor point past the end of g_points?
    if (b*B + t >= n_points)
        return;

    // Anchor point in L
    double4 l_point = g_points[b*B + t];

    // Inter-block pairs, with block-level load balancing
    for (unsigned int i=1; i<=M/2; i++) {
        // Load balancing edge case
        if (M%2 == 0 && i >= M/2 && b >= M/2)
            continue;

        // Get R block
        unsigned int r = (b + i) % M;
        __syncthreads();
        if (r*B + t < n_points)
            s_block[t] = g_points[r*B + t];
        __syncthreads();

        // inter-block pairs
        for (unsigned int j=0; j<B; j++) {
            if (r*B + j < n_points) {
                double d = dist3(l_point, s_block[j]);
                unsigned long long bin = (unsigned long long)(d / res);
                if (bin <= n_bins)
                    atomicAdd(&g_bins[bin].d_cnt, 1);
            }
        }
    }

    // Get L block
    __syncthreads();
    if (b*B + t < n_points)
        s_block[t] = g_points[b*B + t];
    __syncthreads();

    // intra-block pairs
    for (unsigned int i=t+1; i<B && b*B+i<n_points; i++) {
        double d = dist3(s_block[t], s_block[i]);
        unsigned long long bin = (unsigned long long)(d / res);
        if (bin <= n_bins)
            atomicAdd(&g_bins[bin].d_cnt, 1);
    }
}

void PDH_gpu() {
    unsigned long num_threads = num_points*(num_points - 1)/2;

    // allocate histogram
    bucket *d_buckets;
    cudaMalloc(&d_buckets, sizeof(*histogram) * num_buckets);
    cudaMemset(d_buckets, 0, sizeof(*histogram) * num_buckets);

    // Copy points to device
    double4 *d_points;
    cudaMalloc(&d_points, sizeof(*h_points) * num_points);
    cudaMemcpy(d_points, h_points, sizeof(*h_points) * num_points, cudaMemcpyHostToDevice);

    PDH_kernel<<<(num_points + BLOCK_SIZE - 1)/BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE>>>(
        d_buckets, num_buckets, d_points, num_points, resolution);

    cudaError_t err = cudaGetLastError();
    if (err != cudaSuccess) {
        printf("CUDA ERROR: %s\n", cudaGetErrorString(err));
        puts("This is probably due to a too-large block count");
    }

    // Copy histogram from device and cleanup
    cudaFree(d_points);
    cudaMemcpy(histogram, d_buckets, sizeof(*histogram) * num_buckets, cudaMemcpyDeviceToHost);
    cudaFree(d_buckets);
}


/*
 print the counts in all buckets of the histogram
*/
void output_histogram() {
    int i;
    unsigned long long total_cnt = 0;
    for(i=0; i<num_buckets; i++) {
        if (i%5 == 0) /* we print 5 buckets in a row */
            printf("\n%02d: ", i);
        printf("%15lld ", histogram[i].d_cnt);
        total_cnt += histogram[i].d_cnt;
        /* we also want to make sure the total distance count is correct */
        if (i == num_buckets - 1) {
            printf("\n          Total: %lld", total_cnt);
            printf("\n Expected total: %lld \n", num_points*(num_points - 1)/2);
        } else {
            printf("| ");
        }
    }
}

void usage(FILE *f, int ret) {
    fprintf(f, "USAGE: %s <SAMPLES> <RESOLUTION>\n", argv0);
    exit(ret);
}

int main(int argc, char **argv) {
    argv0 = argv[0];

    if (argc < 3)
        usage(stderr, 1);

    if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
        usage(stdout, 0);

    errno = 0;
    num_points = strtoull(argv[1], NULL, 10);
    if (errno != 0)
        EXITERROR();

    errno = 0;
    resolution = strtof(argv[2], NULL);
    if (errno != 0)
        EXITERROR();


    num_buckets = (int)(BOX_SIZE * 1.732 / resolution) + 1;
    histogram = (bucket *)malloc(sizeof(bucket)*num_buckets);

    h_points = (double4 *)malloc(sizeof(double4)*num_points);


    srand(1);
    /* generate data following a uniform distribution */
    for(size_t i = 0; i < num_points; i++) {
        h_points[i].x = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        h_points[i].y = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        h_points[i].z = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
    }

    // GPU implementation
    puts("\nRunning Reg-SHM version...");
    memset(histogram, 0, sizeof(*histogram) * num_buckets);

    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start, 0);

    PDH_gpu();

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    float elapsed;
    cudaEventElapsedTime(&elapsed, start, stop);
    printf("Running time for Reg-SHM version: %.6f sec\n", elapsed/1000.0);
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    output_histogram();


    return 0;
}

	.file	"tmpxft_0029aa8b_00000000-6_proj2.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2049:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2049:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n%02d: "
.LC1:
	.string	"%15lld "
.LC2:
	.string	"\n          Total: %lld"
.LC3:
	.string	"\n Expected total: %lld \n"
.LC4:
	.string	"| "
	.text
	.globl	_Z16output_histogramv
	.type	_Z16output_histogramv, @function
_Z16output_histogramv:
.LFB2044:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	leaq	.LC0(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L3:
	cmpl	%ebx, num_buckets(%rip)
	movl	%ebx, %r13d
	jle	.L10
	movl	%r13d, %eax
	movl	$5, %ecx
	cltd
	idivl	%ecx
	testl	%edx, %edx
	jne	.L4
	movl	%r13d, %edx
	movq	%r12, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L4:
	movq	histogram(%rip), %rax
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movq	(%rax,%rbx,8), %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	histogram(%rip), %rax
	addq	(%rax,%rbx,8), %rbp
	movl	num_buckets(%rip), %eax
	decl	%eax
	cmpl	%r13d, %eax
	jne	.L5
	movq	%rbp, %rdx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	num_points(%rip), %rax
	movl	$2, %edi
	leaq	.LC3(%rip), %rsi
	leaq	-1(%rax), %rdx
	imulq	%rax, %rdx
	xorl	%eax, %eax
	shrq	%rdx
	call	__printf_chk@PLT
	jmp	.L6
.L5:
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L6:
	incq	%rbx
	jmp	.L3
.L10:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2044:
	.size	_Z16output_histogramv, .-_Z16output_histogramv
	.section	.rodata.str1.1
.LC5:
	.string	"USAGE: %s <SAMPLES> <RESOLUTION>\n"
	.text
	.globl	_Z5usageP8_IO_FILEi
	.type	_Z5usageP8_IO_FILEi, @function
_Z5usageP8_IO_FILEi:
.LFB2045:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	argv0(%rip), %rcx
	movl	%esi, %ebx
	xorl	%eax, %eax
	leaq	.LC5(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebx, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2045:
	.size	_Z5usageP8_IO_FILEi, .-_Z5usageP8_IO_FILEi
	.globl	_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md
	.type	_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md, @function
_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md:
.LFB2071:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movsd	%xmm0, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z10PDH_kernelP10hist_entrymP7double4md(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L13:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2071:
	.size	_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md, .-_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md
	.globl	_Z10PDH_kernelP10hist_entrymP7double4md
	.type	_Z10PDH_kernelP10hist_entrymP7double4md, @function
_Z10PDH_kernelP10hist_entrymP7double4md:
.LFB2072:
	.cfi_startproc
	endbr64
	jmp	_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md
	.cfi_endproc
.LFE2072:
	.size	_Z10PDH_kernelP10hist_entrymP7double4md, .-_Z10PDH_kernelP10hist_entrymP7double4md
	.section	.rodata.str1.1
.LC6:
	.string	"CUDA ERROR: %s\n"
.LC7:
	.string	"This is probably due to a too-large block count"
	.text
	.globl	_Z7PDH_gpuv
	.type	_Z7PDH_gpuv, @function
_Z7PDH_gpuv:
.LFB2043:
	.cfi_startproc
	endbr64
	subq	$56, %rsp
	.cfi_def_cfa_offset 64
	movslq	num_buckets(%rip), %rsi
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	salq	$3, %rsi
	call	cudaMalloc@PLT
	movslq	num_buckets(%rip), %rdx
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	salq	$3, %rdx
	call	cudaMemset@PLT
	movq	num_points(%rip), %rsi
	leaq	8(%rsp), %rdi
	salq	$5, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	num_points(%rip), %rdx
	movq	h_points(%rip), %rsi
	salq	$5, %rdx
	call	cudaMemcpy@PLT
	movq	num_points(%rip), %rax
	movl	$8388609, %edx
	xorl	%r9d, %r9d
	salq	$9, %rdx
	movl	$512, %r8d
	movl	$1, %ecx
	addq	$511, %rax
	shrq	$9, %rax
	movl	%eax, 16(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 20(%rsp)
	movq	16(%rsp), %rdi
	movl	24(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	movslq	num_buckets(%rip), %rsi
	movsd	resolution(%rip), %xmm0
	movq	num_points(%rip), %rcx
	movq	8(%rsp), %rdx
	movq	(%rsp), %rdi
	call	_Z53__device_stub__Z10PDH_kernelP10hist_entrymP7double4mdP10hist_entrymP7double4md
.L19:
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L20
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC6(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rdi
	call	puts@PLT
.L20:
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movslq	num_buckets(%rip), %rdx
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	histogram(%rip), %rdi
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$56, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2043:
	.size	_Z7PDH_gpuv, .-_Z7PDH_gpuv
	.section	.rodata.str1.1
.LC8:
	.string	"--help"
.LC9:
	.string	"-h"
.LC10:
	.string	"pid %llu"
.LC11:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/KevOrr/Classes/master/gpgpu/project2/proj2.cu"
.LC15:
	.string	"\nRunning Reg-SHM version..."
.LC17:
	.string	"Running time for Reg-SHM version: %.6f sec\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2046:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	movq	(%rsi), %rax
	movq	%rax, argv0(%rip)
	cmpl	$2, %edi
	jg	.L27
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L43
	movq	stderr(%rip), %rdi
	movl	$1, %esi
	jmp	.L42
.L27:
	movq	8(%rsi), %r12
	movq	%rsi, %rbp
	leaq	.LC8(%rip), %rsi
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	je	.L29
	leaq	.LC9(%rip), %rsi
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L30
.L29:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L43
	movq	stdout(%rip), %rdi
	xorl	%esi, %esi
.L42:
	call	_Z5usageP8_IO_FILEi
.L30:
	call	__errno_location@PLT
	xorl	%edx, %edx
	xorl	%esi, %esi
	movq	%r12, %rdi
	movl	%edx, (%rax)
	movq	%rax, %rbx
	movl	$10, %edx
	call	__isoc23_strtoull@PLT
	cmpl	$0, (%rbx)
	movq	%rax, num_points(%rip)
	je	.L32
	call	getpid@PLT
	movl	(%rbx), %edi
	leaq	.LC10(%rip), %r8
	movl	$166, %ecx
	movslq	%eax, %r9
	leaq	.LC11(%rip), %rdx
	xorl	%eax, %eax
	movl	%edi, %esi
	call	error_at_line@PLT
.L32:
	xorl	%eax, %eax
	movq	16(%rbp), %rdi
	xorl	%esi, %esi
	movl	%eax, (%rbx)
	call	strtof@PLT
	cmpl	$0, (%rbx)
	cvtss2sd	%xmm0, %xmm0
	movsd	%xmm0, resolution(%rip)
	je	.L33
	call	getpid@PLT
	movl	(%rbx), %edi
	leaq	.LC10(%rip), %r8
	movl	$171, %ecx
	movslq	%eax, %r9
	leaq	.LC11(%rip), %rdx
	xorl	%eax, %eax
	movl	%edi, %esi
	call	error_at_line@PLT
.L33:
	movsd	.LC12(%rip), %xmm0
	divsd	resolution(%rip), %xmm0
	cvttsd2sil	%xmm0, %edi
	xorl	%ebx, %ebx
	incl	%edi
	movl	%edi, num_buckets(%rip)
	movslq	%edi, %rdi
	salq	$3, %rdi
	call	malloc@PLT
	movq	num_points(%rip), %rdi
	movq	%rax, histogram(%rip)
	salq	$5, %rdi
	call	malloc@PLT
	movl	$1, %edi
	movq	%rax, h_points(%rip)
	call	srand@PLT
.L34:
	cmpq	num_points(%rip), %rbx
	jnb	.L44
	call	rand@PLT
	movq	%rbx, %rbp
	incq	%rbx
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC13(%rip), %xmm0
	mulsd	.LC14(%rip), %xmm0
	salq	$5, %rbp
	movq	h_points(%rip), %rax
	movsd	%xmm0, (%rax,%rbp)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC13(%rip), %xmm0
	mulsd	.LC14(%rip), %xmm0
	movq	h_points(%rip), %rax
	movsd	%xmm0, 8(%rax,%rbp)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC13(%rip), %xmm0
	mulsd	.LC14(%rip), %xmm0
	movq	h_points(%rip), %rax
	movsd	%xmm0, 16(%rax,%rbp)
	jmp	.L34
.L44:
	leaq	.LC15(%rip), %rdi
	call	puts@PLT
	movslq	num_buckets(%rip), %rcx
	movq	histogram(%rip), %rdi
	xorl	%eax, %eax
	salq	$3, %rcx
	rep stosb
	leaq	8(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	call	_Z7PDH_gpuv
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	4(%rsp), %xmm0
	divsd	.LC16(%rip), %xmm0
	call	__printf_chk@PLT
	movq	8(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	call	_Z16output_histogramv
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
.L43:
	call	__stack_chk_fail@PLT
.L36:
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2046:
	.size	main, .-main
	.section	.rodata.str1.1
.LC18:
	.string	"_Z10PDH_kernelP10hist_entrymP7double4md"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2074:
	.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	.LC18(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10PDH_kernelP10hist_entrymP7double4md(%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
.LFE2074:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	h_points
	.bss
	.align 8
	.type	h_points, @object
	.size	h_points, 8
h_points:
	.zero	8
	.globl	resolution
	.align 8
	.type	resolution, @object
	.size	resolution, 8
resolution:
	.zero	8
	.globl	num_buckets
	.align 4
	.type	num_buckets, @object
	.size	num_buckets, 4
num_buckets:
	.zero	4
	.globl	num_points
	.align 8
	.type	num_points, @object
	.size	num_points, 8
num_points:
	.zero	8
	.globl	histogram
	.align 8
	.type	histogram, @object
	.size	histogram, 8
histogram:
	.zero	8
	.globl	argv0
	.align 8
	.type	argv0, @object
	.size	argv0, 8
argv0:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC12:
	.long	0
	.long	1088648064
	.align 8
.LC13:
	.long	-4194304
	.long	1105199103
	.align 8
.LC14:
	.long	0
	.long	1087796736
	.align 8
.LC16:
	.long	0
	.long	1083129856
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>

const long BOX_SIZE = 23000; /* size of the data box on one dimension */
#define BLOCK_SIZE 512

#define EXITERROR() error_at_line(errno, errno, __FILE__, __LINE__, "pid %llu", (long long unsigned)getpid())

const char *argv0;

typedef struct hist_entry {
    //float min;
    //float max;
    unsigned long long d_cnt;   /* need a long long type as the count might be huge */
} bucket;

struct debuginfo {
    int idx;
    int ran;
    int i;
    int j;
    float dist;
    int which_bucket;
};


bucket * histogram;  /* list of all buckets in the histogram   */
unsigned long long num_points; /* total number of data points            */
int num_buckets;  /* total number of buckets in the histogram */
double resolution;  /* value of w                             */
double4 *h_points;


inline __device__ double dist3(double4 a, double4 b) {
    return norm3d(a.x - b.x, a.y - b.y, a.z - b.z);
}


__global__
void PDH_kernel(bucket *g_bins, size_t n_bins, double4 *g_points, size_t n_points, double res) {
    __shared__ double4 s_block[BLOCK_SIZE]; // R for inter-block, L for intra-block

    unsigned int t = threadIdx.x;
    unsigned int b = blockIdx.x;
    unsigned int B = blockDim.x;
    unsigned int M = gridDim.x;

    // Is our anchor point past the end of g_points?
    if (b*B + t >= n_points)
        return;

    // Anchor point in L
    double4 l_point = g_points[b*B + t];

    // Inter-block pairs, with block-level load balancing
    for (unsigned int i=1; i<=M/2; i++) {
        // Load balancing edge case
        if (M%2 == 0 && i >= M/2 && b >= M/2)
            continue;

        // Get R block
        unsigned int r = (b + i) % M;
        __syncthreads();
        if (r*B + t < n_points)
            s_block[t] = g_points[r*B + t];
        __syncthreads();

        // inter-block pairs
        for (unsigned int j=0; j<B; j++) {
            if (r*B + j < n_points) {
                double d = dist3(l_point, s_block[j]);
                unsigned long long bin = (unsigned long long)(d / res);
                if (bin <= n_bins)
                    atomicAdd(&g_bins[bin].d_cnt, 1);
            }
        }
    }

    // Get L block
    __syncthreads();
    if (b*B + t < n_points)
        s_block[t] = g_points[b*B + t];
    __syncthreads();

    // intra-block pairs
    for (unsigned int i=t+1; i<B && b*B+i<n_points; i++) {
        double d = dist3(s_block[t], s_block[i]);
        unsigned long long bin = (unsigned long long)(d / res);
        if (bin <= n_bins)
            atomicAdd(&g_bins[bin].d_cnt, 1);
    }
}

void PDH_gpu() {
    unsigned long num_threads = num_points*(num_points - 1)/2;

    // allocate histogram
    bucket *d_buckets;
    hipMalloc(&d_buckets, sizeof(*histogram) * num_buckets);
    hipMemset(d_buckets, 0, sizeof(*histogram) * num_buckets);

    // Copy points to device
    double4 *d_points;
    hipMalloc(&d_points, sizeof(*h_points) * num_points);
    hipMemcpy(d_points, h_points, sizeof(*h_points) * num_points, hipMemcpyHostToDevice);

    PDH_kernel<<<(num_points + BLOCK_SIZE - 1)/BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE>>>(
        d_buckets, num_buckets, d_points, num_points, resolution);

    hipError_t err = hipGetLastError();
    if (err != hipSuccess) {
        printf("CUDA ERROR: %s\n", hipGetErrorString(err));
        puts("This is probably due to a too-large block count");
    }

    // Copy histogram from device and cleanup
    hipFree(d_points);
    hipMemcpy(histogram, d_buckets, sizeof(*histogram) * num_buckets, hipMemcpyDeviceToHost);
    hipFree(d_buckets);
}


/*
 print the counts in all buckets of the histogram
*/
void output_histogram() {
    int i;
    unsigned long long total_cnt = 0;
    for(i=0; i<num_buckets; i++) {
        if (i%5 == 0) /* we print 5 buckets in a row */
            printf("\n%02d: ", i);
        printf("%15lld ", histogram[i].d_cnt);
        total_cnt += histogram[i].d_cnt;
        /* we also want to make sure the total distance count is correct */
        if (i == num_buckets - 1) {
            printf("\n          Total: %lld", total_cnt);
            printf("\n Expected total: %lld \n", num_points*(num_points - 1)/2);
        } else {
            printf("| ");
        }
    }
}

void usage(FILE *f, int ret) {
    fprintf(f, "USAGE: %s <SAMPLES> <RESOLUTION>\n", argv0);
    exit(ret);
}

int main(int argc, char **argv) {
    argv0 = argv[0];

    if (argc < 3)
        usage(stderr, 1);

    if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
        usage(stdout, 0);

    errno = 0;
    num_points = strtoull(argv[1], NULL, 10);
    if (errno != 0)
        EXITERROR();

    errno = 0;
    resolution = strtof(argv[2], NULL);
    if (errno != 0)
        EXITERROR();


    num_buckets = (int)(BOX_SIZE * 1.732 / resolution) + 1;
    histogram = (bucket *)malloc(sizeof(bucket)*num_buckets);

    h_points = (double4 *)malloc(sizeof(double4)*num_points);


    srand(1);
    /* generate data following a uniform distribution */
    for(size_t i = 0; i < num_points; i++) {
        h_points[i].x = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        h_points[i].y = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        h_points[i].z = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
    }

    // GPU implementation
    puts("\nRunning Reg-SHM version...");
    memset(histogram, 0, sizeof(*histogram) * num_buckets);

    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
    hipEventRecord(start, 0);

    PDH_gpu();

    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    float elapsed;
    hipEventElapsedTime(&elapsed, start, stop);
    printf("Running time for Reg-SHM version: %.6f sec\n", elapsed/1000.0);
    hipEventDestroy(start);
    hipEventDestroy(stop);

    output_histogram();


    return 0;
}

	.text
	.file	"proj2.hip"
	.globl	_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd # -- Begin function _Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.type	_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd,@function
_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd: # @_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.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	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movsd	%xmm0, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd, %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	_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd, .Lfunc_end0-_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.cfi_endproc
                                        # -- End function
	.globl	_Z7PDH_gpuv                     # -- Begin function _Z7PDH_gpuv
	.type	_Z7PDH_gpuv,@function
_Z7PDH_gpuv:                            # @_Z7PDH_gpuv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movslq	num_buckets(%rip), %rsi
	shlq	$3, %rsi
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movq	num_points(%rip), %rsi
	shlq	$5, %rsi
	leaq	8(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movq	h_points(%rip), %rsi
	movq	num_points(%rip), %rdx
	shlq	$5, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$511, %eax                      # imm = 0x1FF
	addq	num_points(%rip), %rax
	shrq	$9, %rax
	movl	%eax, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$512, %rdx                      # imm = 0x200
	movl	$512, %r8d                      # imm = 0x200
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	(%rsp), %rdi
	movslq	num_buckets(%rip), %rsi
	movq	8(%rsp), %rdx
	movq	num_points(%rip), %rcx
	movsd	resolution(%rip), %xmm0         # xmm0 = mem[0],zero
	callq	_Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
.LBB1_2:
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB1_4
# %bb.3:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.1, %edi
	callq	puts
.LBB1_4:
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	histogram(%rip), %rdi
	movq	(%rsp), %rsi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	callq	hipFree
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z7PDH_gpuv, .Lfunc_end1-_Z7PDH_gpuv
	.cfi_endproc
                                        # -- End function
	.globl	_Z16output_histogramv           # -- Begin function _Z16output_histogramv
	.type	_Z16output_histogramv,@function
_Z16output_histogramv:                  # @_Z16output_histogramv
	.cfi_startproc
# %bb.0:
	cmpl	$0, num_buckets(%rip)
	jle	.LBB2_9
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$3435973837, %r15d              # imm = 0xCCCCCCCD
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ebx, %eax
	imulq	%r15, %rax
	shrq	$34, %rax
	leal	(%rax,%rax,4), %eax
	cmpl	%eax, %ebx
	jne	.LBB2_4
# %bb.3:                                #   in Loop: Header=BB2_2 Depth=1
	movl	$.L.str.2, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
.LBB2_4:                                #   in Loop: Header=BB2_2 Depth=1
	movq	histogram(%rip), %rax
	movq	(%rax,%rbx,8), %rsi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movq	histogram(%rip), %rax
	addq	(%rax,%rbx,8), %r14
	movl	num_buckets(%rip), %eax
	decl	%eax
	cmpq	%rax, %rbx
	jne	.LBB2_6
# %bb.5:                                #   in Loop: Header=BB2_2 Depth=1
	movl	$.L.str.4, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	num_points(%rip), %rax
	leaq	-1(%rax), %rsi
	imulq	%rax, %rsi
	shrq	%rsi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB2_7
.LBB2_6:                                #   in Loop: Header=BB2_2 Depth=1
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
.LBB2_7:                                #   in Loop: Header=BB2_2 Depth=1
	incq	%rbx
	movslq	num_buckets(%rip), %rax
	cmpq	%rax, %rbx
	jl	.LBB2_2
# %bb.8:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB2_9:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z16output_histogramv, .Lfunc_end2-_Z16output_histogramv
	.cfi_endproc
                                        # -- End function
	.globl	_Z5usageP8_IO_FILEi             # -- Begin function _Z5usageP8_IO_FILEi
	.type	_Z5usageP8_IO_FILEi,@function
_Z5usageP8_IO_FILEi:                    # @_Z5usageP8_IO_FILEi
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	movq	argv0(%rip), %rdx
	movl	$.L.str.7, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebx, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z5usageP8_IO_FILEi, .Lfunc_end3-_Z5usageP8_IO_FILEi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x40e3738000000000              # double 39836
.LCPI4_1:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI4_2:
	.quad	0x40d6760000000000              # double 23000
.LCPI4_3:
	.quad	0x408f400000000000              # double 1000
	.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	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	(%rsi), %r14
	movq	%r14, argv0(%rip)
	cmpl	$2, %edi
	jle	.LBB4_11
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %r15
	movl	$.L.str.8, %esi
	movq	%r15, %rdi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB4_12
# %bb.2:
	movl	$.L.str.9, %esi
	movq	%r15, %rdi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB4_12
# %bb.3:
	callq	__errno_location
	movq	%rax, %r14
	movl	$0, (%rax)
	movq	%r15, %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtoull
	movq	%rax, num_points(%rip)
	movl	(%r14), %ebp
	testl	%ebp, %ebp
	jne	.LBB4_4
.LBB4_5:
	movl	$0, (%r14)
	movq	16(%rbx), %rdi
	xorl	%esi, %esi
	callq	strtof
	cvtss2sd	%xmm0, %xmm0
	movsd	%xmm0, resolution(%rip)
	movl	(%r14), %ebx
	testl	%ebx, %ebx
	jne	.LBB4_6
.LBB4_7:
	movsd	.LCPI4_0(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	cvttsd2si	%xmm1, %eax
	leal	1(%rax), %ecx
	movl	%ecx, num_buckets(%rip)
	cltq
	leaq	8(,%rax,8), %rdi
	callq	malloc
	movq	%rax, histogram(%rip)
	movq	num_points(%rip), %rdi
	shlq	$5, %rdi
	callq	malloc
	movq	%rax, h_points(%rip)
	movl	$1, %edi
	callq	srand
	cmpq	$0, num_points(%rip)
	je	.LBB4_10
# %bb.8:                                # %.lr.ph.preheader
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB4_9:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movsd	.LCPI4_1(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	.LCPI4_2(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	movq	h_points(%rip), %rax
	movsd	%xmm0, (%rax,%rbx)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI4_1(%rip), %xmm0
	mulsd	.LCPI4_2(%rip), %xmm0
	movq	h_points(%rip), %rax
	movsd	%xmm0, 8(%rax,%rbx)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI4_1(%rip), %xmm0
	mulsd	.LCPI4_2(%rip), %xmm0
	movq	h_points(%rip), %rax
	movsd	%xmm0, 16(%rax,%rbx)
	incq	%r14
	addq	$32, %rbx
	cmpq	num_points(%rip), %r14
	jb	.LBB4_9
.LBB4_10:                               # %._crit_edge
	movl	$.L.str.12, %edi
	callq	puts
	movq	histogram(%rip), %rdi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	xorl	%esi, %esi
	callq	memset@PLT
	leaq	16(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	callq	_Z7PDH_gpuv
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r14), %rdi
	callq	hipEventSynchronize
	movq	(%rbx), %rsi
	movq	(%r14), %rdx
	leaq	4(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r15), %xmm0
	divsd	.LCPI4_3(%rip), %xmm0
	movl	$.L.str.13, %edi
	movb	$1, %al
	callq	printf
	movq	(%rbx), %rdi
	callq	hipEventDestroy
	movq	(%r14), %rdi
	callq	hipEventDestroy
	callq	_Z16output_histogramv
	xorl	%eax, %eax
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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_4:
	.cfi_def_cfa_offset 64
	callq	getpid
	movslq	%eax, %r9
	movl	$.L.str.10, %edx
	movl	$.L.str.11, %r8d
	movl	%ebp, %edi
	movl	%ebp, %esi
	movl	$168, %ecx
	xorl	%eax, %eax
	callq	error_at_line
	jmp	.LBB4_5
.LBB4_6:
	callq	getpid
	movslq	%eax, %r9
	movl	$.L.str.10, %edx
	movl	$.L.str.11, %r8d
	movl	%ebx, %edi
	movl	%ebx, %esi
	movl	$173, %ecx
	xorl	%eax, %eax
	callq	error_at_line
	movsd	resolution(%rip), %xmm0         # xmm0 = mem[0],zero
	jmp	.LBB4_7
.LBB4_12:
	movq	stdout(%rip), %rdi
	movl	$.L.str.7, %esi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	fprintf
	xorl	%edi, %edi
	callq	exit
.LBB4_11:
	movq	stderr(%rip), %rdi
	movl	$.L.str.7, %esi
	movq	%r14, %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	$_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd, %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	argv0,@object                   # @argv0
	.bss
	.globl	argv0
	.p2align	3, 0x0
argv0:
	.quad	0
	.size	argv0, 8

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

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

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

	.type	resolution,@object              # @resolution
	.globl	resolution
	.p2align	3, 0x0
resolution:
	.quad	0x0000000000000000              # double 0
	.size	resolution, 8

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

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CUDA ERROR: %s\n"
	.size	.L.str, 16

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"This is probably due to a too-large block count"
	.size	.L.str.1, 48

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%15lld "
	.size	.L.str.3, 8

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\n Expected total: %lld \n"
	.size	.L.str.5, 25

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

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"USAGE: %s <SAMPLES> <RESOLUTION>\n"
	.size	.L.str.7, 34

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"--help"
	.size	.L.str.8, 7

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"-h"
	.size	.L.str.9, 3

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/KevOrr/Classes/master/gpgpu/project2/proj2.hip"
	.size	.L.str.10, 109

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"pid %llu"
	.size	.L.str.11, 9

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"\nRunning Reg-SHM version..."
	.size	.L.str.12, 28

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Running time for Reg-SHM version: %.6f sec\n"
	.size	.L.str.13, 44

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd"
	.size	.L__unnamed_1, 56

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

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

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z25__device_stub__PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd ; -- Begin function _Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.globl	_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
	.p2align	8
	.type	_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd,@function
_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd: ; @_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
; %bb.0:
	s_clause 0x1
	s_load_b32 s12, s[0:1], 0x34
	s_load_b256 s[4:11], s[0:1], 0x0
	s_add_u32 s2, s0, 40
	s_addc_u32 s3, s1, 0
	v_mov_b32_e32 v2, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s14, s12, 0xffff
	s_mov_b32 s12, exec_lo
	s_mul_i32 s16, s15, s14
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v1, s16, v0
	v_cmpx_gt_u64_e64 s[10:11], v[1:2]
	s_cbranch_execz .LBB0_19
; %bb.1:
	s_load_b32 s17, s[2:3], 0x0
	s_load_b64 s[2:3], s[0:1], 0x20
	v_lshlrev_b64 v[1:2], 5, v[1:2]
	v_lshlrev_b32_e32 v13, 5, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v7, vcc_lo, s8, v1
	v_add_co_ci_u32_e32 v8, vcc_lo, s9, v2, vcc_lo
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_u32 s17, 2
	s_cbranch_scc1 .LBB0_14
; %bb.2:                                ; %.lr.ph103
	s_clause 0x1
	global_load_b128 v[1:4], v[7:8], off offset:16
	global_load_b128 v[3:6], v[7:8], off
	v_cvt_f32_u32_e32 v9, s17
	s_lshr_b32 s1, s17, 1
	s_bitcmp1_b32 s17, 0
	s_mov_b32 s21, 1
	s_cselect_b32 s18, -1, 0
	v_rcp_iflag_f32_e32 v9, v9
	s_cmp_lt_u32 s15, s1
	s_mov_b32 s13, 0
	s_cselect_b32 s19, -1, 0
	s_cmp_lg_u32 s14, 0
	v_mov_b32_e32 v10, 0
	s_cselect_b32 s20, -1, 0
	s_sub_i32 s12, 0, s17
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v9, 0x4f7ffffe, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v9, v9
	v_readfirstlane_b32 s0, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s12, s12, s0
	s_mul_hi_u32 s12, s0, s12
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s22, s0, s12
.LBB0_3:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_8 Depth 2
	s_cmp_lt_u32 s21, s1
	s_cselect_b32 s0, -1, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s0, s18, s0
	s_or_b32 s0, s19, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccnz .LBB0_13
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	s_add_i32 s0, s21, s15
	s_waitcnt vmcnt(0)
	s_mul_hi_u32 s12, s0, s22
	s_waitcnt_vscnt null, 0x0
	s_barrier
	s_mul_i32 s12, s12, s17
	buffer_gl0_inv
	s_sub_i32 s0, s0, s12
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_sub_i32 s12, s0, s17
	s_cmp_ge_u32 s0, s17
	s_cselect_b32 s0, s12, s0
	s_sub_i32 s12, s0, s17
	s_cmp_ge_u32 s0, s17
	s_cselect_b32 s0, s12, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_mul_i32 s12, s0, s14
	s_mov_b32 s0, exec_lo
	v_add_nc_u32_e32 v9, s12, v0
	v_cmpx_gt_u64_e64 s[10:11], v[9:10]
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_3 Depth=1
	v_lshlrev_b64 v[11:12], 5, v[9:10]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v11, vcc_lo, s8, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s9, v12, vcc_lo
	s_clause 0x1
	global_load_b128 v[14:17], v[11:12], off offset:16
	global_load_b128 v[18:21], v[11:12], off
	s_waitcnt vmcnt(1)
	ds_store_b128 v13, v[14:17] offset:16
	s_waitcnt vmcnt(0)
	ds_store_b128 v13, v[18:21]
.LBB0_6:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 vcc_lo, exec_lo, s20
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_vccnz .LBB0_13
; %bb.7:                                ; %.lr.ph.preheader
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_mov_b32 s23, 0
	s_mov_b32 s24, s14
.LBB0_8:                                ; %.lr.ph
                                        ;   Parent Loop BB0_3 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_cmp_ge_u64_e64 s0, s[12:13], s[10:11]
	s_delay_alu instid0(VALU_DEP_1)
	s_and_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccnz .LBB0_12
; %bb.9:                                ;   in Loop: Header=BB0_8 Depth=2
	v_mov_b32_e32 v9, s23
	ds_load_b128 v[14:17], v9
	ds_load_b128 v[18:21], v9 offset:16
	s_waitcnt lgkmcnt(1)
	v_add_f64 v[11:12], v[3:4], -v[14:15]
	v_add_f64 v[14:15], v[5:6], -v[16:17]
	s_waitcnt lgkmcnt(0)
	v_add_f64 v[16:17], v[1:2], -v[18:19]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_class_f64_e64 s25, v[11:12], 0x204
	v_max_f64 v[18:19], |v[11:12]|, |v[14:15]|
	v_min_f64 v[22:23], |v[11:12]|, |v[14:15]|
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_4) | instid1(VALU_DEP_4)
	v_cmp_u_f64_e64 s0, v[16:17], v[16:17]
	v_cmp_class_f64_e64 s26, v[14:15], 0x204
	v_cmp_class_f64_e64 s27, v[16:17], 0x204
	v_max_f64 v[20:21], v[18:19], |v[16:17]|
	v_min_f64 v[18:19], v[18:19], |v[16:17]|
	s_or_b32 s25, s25, s26
	s_delay_alu instid0(VALU_DEP_3) | instid1(SALU_CYCLE_1)
	s_or_b32 s25, s27, s25
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_frexp_exp_i32_f64_e32 v9, v[20:21]
	v_min_f64 v[24:25], v[22:23], v[18:19]
	v_max_f64 v[18:19], v[22:23], v[18:19]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v26, 0, v9
	v_ldexp_f64 v[22:23], v[24:25], v26
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_ldexp_f64 v[18:19], v[18:19], v26
	v_ldexp_f64 v[20:21], v[20:21], v26
	v_mul_f64 v[22:23], v[22:23], v[22:23]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[18:19], v[18:19], v[18:19], v[22:23]
	v_fma_f64 v[18:19], v[20:21], v[20:21], v[18:19]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_rsq_f64_e32 v[20:21], v[18:19]
	v_cmp_eq_f64_e32 vcc_lo, 0, v[18:19]
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[22:23], v[18:19], v[20:21]
	v_mul_f64 v[20:21], v[20:21], 0.5
	v_fma_f64 v[24:25], -v[20:21], v[22:23], 0.5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[22:23], v[22:23], v[24:25], v[22:23]
	v_fma_f64 v[20:21], v[20:21], v[24:25], v[20:21]
	v_fma_f64 v[24:25], -v[22:23], v[22:23], v[18:19]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[20:21], v[24:25], v[20:21], v[22:23]
	v_dual_cndmask_b32 v18, v20, v18 :: v_dual_cndmask_b32 v19, v21, v19
	v_cmp_u_f64_e32 vcc_lo, v[11:12], v[14:15]
	s_delay_alu instid0(VALU_DEP_2)
	v_ldexp_f64 v[18:19], v[18:19], v9
	s_or_b32 s0, vcc_lo, s0
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_cndmask_b32_e64 v9, v19, 0x7ff80000, s0
	s_or_b32 s0, s25, s0
	s_delay_alu instid0(VALU_DEP_2) | instid1(SALU_CYCLE_1)
	v_cndmask_b32_e64 v11, v18, 0, s0
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v12, v9, 0x7ff00000, s25
	v_div_scale_f64 v[14:15], null, s[2:3], s[2:3], v[11:12]
	v_div_scale_f64 v[20:21], vcc_lo, v[11:12], s[2:3], v[11:12]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[16:17], v[14:15]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[18:19], -v[14:15], v[16:17], 1.0
	v_fma_f64 v[16:17], v[16:17], v[18:19], v[16:17]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[18:19], -v[14:15], v[16:17], 1.0
	v_fma_f64 v[16:17], v[16:17], v[18:19], v[16:17]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[18:19], v[20:21], v[16:17]
	v_fma_f64 v[14:15], -v[14:15], v[18:19], v[20:21]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f64 v[14:15], v[14:15], v[16:17], v[18:19]
	v_div_fixup_f64 v[11:12], v[14:15], s[2:3], v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_trunc_f64_e32 v[11:12], v[11:12]
	v_ldexp_f64 v[14:15], v[11:12], 0xffffffe0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_floor_f64_e32 v[14:15], v[14:15]
	v_fma_f64 v[11:12], 0xc1f00000, v[14:15], v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cvt_u32_f64_e32 v11, v[11:12]
	v_cvt_u32_f64_e32 v12, v[14:15]
	v_cmpx_ge_u64_e64 s[6:7], v[11:12]
	s_cbranch_execz .LBB0_11
; %bb.10:                               ;   in Loop: Header=BB0_8 Depth=2
	v_lshlrev_b64 v[11:12], 3, v[11:12]
	v_mov_b32_e32 v14, 1
	v_mov_b32_e32 v15, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v11, vcc_lo, s4, v11
	v_add_co_ci_u32_e32 v12, vcc_lo, s5, v12, vcc_lo
	global_atomic_add_u64 v[11:12], v[14:15], off
.LBB0_11:                               ; %Flow134
                                        ;   in Loop: Header=BB0_8 Depth=2
	s_or_b32 exec_lo, exec_lo, s0
.LBB0_12:                               ;   in Loop: Header=BB0_8 Depth=2
	s_add_i32 s24, s24, -1
	s_add_i32 s12, s12, 1
	s_add_i32 s23, s23, 32
	s_cmp_lg_u32 s24, 0
	s_cbranch_scc1 .LBB0_8
.LBB0_13:                               ; %.loopexit
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_add_i32 s0, s21, 1
	s_cmp_eq_u32 s21, s1
	s_mov_b32 s21, s0
	s_cbranch_scc0 .LBB0_3
.LBB0_14:                               ; %._crit_edge
	s_waitcnt vmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_clause 0x1
	global_load_b128 v[1:4], v[7:8], off offset:16
	global_load_b128 v[14:17], v[7:8], off
	v_add_nc_u32_e32 v5, 1, v0
	v_mov_b32_e32 v9, 0
	s_waitcnt vmcnt(1)
	ds_store_b128 v13, v[1:4] offset:16
	s_waitcnt vmcnt(0)
	ds_store_b128 v13, v[14:17]
	v_add_nc_u32_e32 v8, s16, v5
	v_cmp_gt_u32_e64 s0, s14, v5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmp_gt_u64_e32 vcc_lo, s[10:11], v[8:9]
	s_and_b32 s0, s0, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 exec_lo, exec_lo, s0
	s_cbranch_execz .LBB0_19
; %bb.15:                               ; %.lr.ph108
	ds_load_b128 v[1:4], v13
	ds_load_b128 v[5:8], v13 offset:16
	v_add_nc_u32_e32 v10, 2, v0
	v_lshl_add_u32 v0, v0, 5, 32
	s_mov_b32 s1, 0
.LBB0_16:                               ; =>This Inner Loop Header: Depth=1
	ds_load_b128 v[11:14], v0
	ds_load_b128 v[15:18], v0 offset:16
	s_waitcnt lgkmcnt(1)
	v_add_f64 v[7:8], v[1:2], -v[11:12]
	v_add_f64 v[11:12], v[3:4], -v[13:14]
	s_waitcnt lgkmcnt(0)
	v_add_f64 v[13:14], v[5:6], -v[15:16]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_class_f64_e64 s8, v[7:8], 0x204
	v_max_f64 v[15:16], |v[7:8]|, |v[11:12]|
	v_min_f64 v[19:20], |v[7:8]|, |v[11:12]|
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_4) | instid1(VALU_DEP_4)
	v_cmp_u_f64_e64 s0, v[13:14], v[13:14]
	v_cmp_class_f64_e64 s9, v[11:12], 0x204
	v_cmp_class_f64_e64 s12, v[13:14], 0x204
	v_max_f64 v[17:18], v[15:16], |v[13:14]|
	v_min_f64 v[15:16], v[15:16], |v[13:14]|
	s_or_b32 s8, s8, s9
	s_delay_alu instid0(VALU_DEP_3) | instid1(SALU_CYCLE_1)
	s_or_b32 s8, s12, s8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_frexp_exp_i32_f64_e32 v23, v[17:18]
	v_min_f64 v[21:22], v[19:20], v[15:16]
	v_max_f64 v[15:16], v[19:20], v[15:16]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v24, 0, v23
	v_ldexp_f64 v[19:20], v[21:22], v24
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_ldexp_f64 v[15:16], v[15:16], v24
	v_ldexp_f64 v[17:18], v[17:18], v24
	v_mul_f64 v[19:20], v[19:20], v[19:20]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[15:16], v[15:16], v[15:16], v[19:20]
	v_fma_f64 v[15:16], v[17:18], v[17:18], v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_rsq_f64_e32 v[17:18], v[15:16]
	v_cmp_eq_f64_e32 vcc_lo, 0, v[15:16]
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[19:20], v[15:16], v[17:18]
	v_mul_f64 v[17:18], v[17:18], 0.5
	v_fma_f64 v[21:22], -v[17:18], v[19:20], 0.5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[19:20], v[19:20], v[21:22], v[19:20]
	v_fma_f64 v[17:18], v[17:18], v[21:22], v[17:18]
	v_fma_f64 v[21:22], -v[19:20], v[19:20], v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[17:18], v[21:22], v[17:18], v[19:20]
	v_dual_cndmask_b32 v16, v18, v16 :: v_dual_cndmask_b32 v15, v17, v15
	v_cmp_u_f64_e32 vcc_lo, v[7:8], v[11:12]
	s_delay_alu instid0(VALU_DEP_2)
	v_ldexp_f64 v[15:16], v[15:16], v23
	s_or_b32 s0, vcc_lo, s0
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_cndmask_b32_e64 v7, v16, 0x7ff80000, s0
	s_or_b32 s0, s8, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v8, v7, 0x7ff00000, s8
	v_cndmask_b32_e64 v7, v15, 0, s0
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_scale_f64 v[11:12], null, s[2:3], s[2:3], v[7:8]
	v_div_scale_f64 v[17:18], vcc_lo, v[7:8], s[2:3], v[7:8]
	v_rcp_f64_e32 v[13:14], v[11:12]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[15:16], -v[11:12], v[13:14], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], v[13:14], v[15:16], v[13:14]
	v_fma_f64 v[15:16], -v[11:12], v[13:14], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[13:14], v[13:14], v[15:16], v[13:14]
	v_mul_f64 v[15:16], v[17:18], v[13:14]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[11:12], -v[11:12], v[15:16], v[17:18]
	v_div_fmas_f64 v[11:12], v[11:12], v[13:14], v[15:16]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fixup_f64 v[7:8], v[11:12], s[2:3], v[7:8]
	v_trunc_f64_e32 v[7:8], v[7:8]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ldexp_f64 v[11:12], v[7:8], 0xffffffe0
	v_floor_f64_e32 v[11:12], v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[7:8], 0xc1f00000, v[11:12], v[7:8]
	v_cvt_u32_f64_e32 v7, v[7:8]
	v_cvt_u32_f64_e32 v8, v[11:12]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_ge_u64_e64 s[6:7], v[7:8]
	s_cbranch_execz .LBB0_18
; %bb.17:                               ;   in Loop: Header=BB0_16 Depth=1
	v_lshlrev_b64 v[7:8], 3, v[7:8]
	v_mov_b32_e32 v11, 1
	v_mov_b32_e32 v12, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v7, vcc_lo, s4, v7
	v_add_co_ci_u32_e32 v8, vcc_lo, s5, v8, vcc_lo
	global_atomic_add_u64 v[7:8], v[11:12], off
.LBB0_18:                               ;   in Loop: Header=BB0_16 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v8, s16, v10
	v_cmp_le_u32_e64 s0, s14, v10
	v_add_nc_u32_e32 v10, 1, v10
	v_add_nc_u32_e32 v0, 32, v0
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cmp_le_u64_e32 vcc_lo, s[10:11], v[8:9]
	s_or_b32 s0, s0, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s0, exec_lo, s0
	s_or_b32 s1, s0, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s1
	s_cbranch_execnz .LBB0_16
.LBB0_19:                               ; %.critedge
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
		.amdhsa_group_segment_fixed_size 16384
		.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 27
		.amdhsa_next_free_sgpr 28
		.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	_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd, .Lfunc_end0-_Z10PDH_kernelP10hist_entrymP15HIP_vector_typeIdLj4EEmd
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1964
; NumSgprs: 30
; NumVgprs: 27
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 16384 bytes/workgroup (compile time only)
; SGPRBlocks: 3
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 30
; NumVGPRsForWavesPerEU: 27
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

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

	.end_amdgpu_metadata
