#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>


#define COMMENT "Histogram_GPU"
#define RGB_COMPONENT_COLOR 255

#define HISTOGRAM_SIZE 64

typedef struct {
	unsigned char red, green, blue;
} PPMPixel;

typedef struct {
	int x, y;
	PPMPixel *data;
} PPMImage;

__global__ void histo_kernel(PPMPixel *pixel, int size, float *histo) {
    __shared__ float histo_private[HISTOGRAM_SIZE];

    int i = threadIdx.x + blockIdx.x * blockDim.x;
    int stride = blockDim.x * gridDim.x;

    if (threadIdx.x < HISTOGRAM_SIZE) {
        histo_private[threadIdx.x] = 0;
    }

    __syncthreads();

    while (i < size) {
        atomicAdd(&(histo_private[pixel[i].red*16 + pixel[i].green*4 + pixel[i].blue]), 1);

        i += stride;
    }

    __syncthreads();

    if (threadIdx.x < HISTOGRAM_SIZE) {
        atomicAdd(&(histo[threadIdx.x]), histo_private[threadIdx.x]);
    }

}

__global__ void histo_normalization(float *histo, int n) {
    histo[threadIdx.x] = histo[threadIdx.x]/n;
}

double rtclock()
{
    struct timezone Tzp;
    struct timeval Tp;
    int stat;
    stat = gettimeofday (&Tp, &Tzp);
    if (stat != 0) printf("Error return from gettimeofday: %d",stat);
    return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}


static PPMImage *readPPM(const char *filename) {
	char buff[16];
	PPMImage *img;
	FILE *fp;
	int c, rgb_comp_color;
	fp = fopen(filename, "rb");
	if (!fp) {
		fprintf(stderr, "Unable to open file '%s'\n", filename);
		exit(1);
	}

	if (!fgets(buff, sizeof(buff), fp)) {
		perror(filename);
		exit(1);
	}

	if (buff[0] != 'P' || buff[1] != '6') {
		fprintf(stderr, "Invalid image format (must be 'P6')\n");
		exit(1);
	}

	img = (PPMImage *) malloc(sizeof(PPMImage));
	if (!img) {
		fprintf(stderr, "Unable to allocate memory\n");
		exit(1);
	}

	c = getc(fp);
	while (c == '#') {
		while (getc(fp) != '\n')
			;
		c = getc(fp);
	}

	ungetc(c, fp);
	if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
		fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
		exit(1);
	}

	if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
		fprintf(stderr, "Invalid rgb component (error loading '%s')\n",
				filename);
		exit(1);
	}

	if (rgb_comp_color != RGB_COMPONENT_COLOR) {
		fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
		exit(1);
	}

	while (fgetc(fp) != '\n')
		;
	img->data = (PPMPixel*) malloc(img->x * img->y * sizeof(PPMPixel));

	if (!img) {
		fprintf(stderr, "Unable to allocate memory\n");
		exit(1);
	}

	if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
		fprintf(stderr, "Error loading image '%s'\n", filename);
		exit(1);
	}

	fclose(fp);
	return img;
}


void Histogram(PPMImage *image, float *h) {

    int i;
	int rows, cols;

	float n = image->y * image->x;

    // begin - cuda variables
    float *d_h;
    PPMPixel *d_pixel;

    cudaDeviceProp prop;

    cudaEvent_t p_1, p_2, p_3, p_4, p_5;
    float milliseconds_1 = 0;
    float milliseconds_2 = 0;
    float milliseconds_3 = 0;
    float milliseconds_4 = 0;
    // end - cuda variables

	cols = image->x;
	rows = image->y;

	for (i = 0; i < n; i++) {
		image->data[i].red = floor((image->data[i].red * 4) / 256);
		image->data[i].blue = floor((image->data[i].blue * 4) / 256);
		image->data[i].green = floor((image->data[i].green * 4) / 256);
	}

    // begin - cuda execution
    cudaEventCreate(&p_1);
    cudaEventCreate(&p_2);
    cudaEventCreate(&p_3);
    cudaEventCreate(&p_4);
    cudaEventCreate(&p_5);
    cudaGetDeviceProperties(&prop, 0);

    cudaEventRecord(p_1);

    cudaMalloc((void **)&d_pixel, sizeof(unsigned char)*3*rows*cols);
    cudaMalloc((void **)&d_h, sizeof(float)*HISTOGRAM_SIZE);

    cudaEventRecord(p_2);

    cudaMemcpy(d_pixel, image->data, sizeof(unsigned char)*3*rows*cols, cudaMemcpyHostToDevice);
    cudaMemset(d_h, 0,HISTOGRAM_SIZE*sizeof(float));

    cudaEventRecord(p_3);

    histo_kernel<<<2*prop.multiProcessorCount, HISTOGRAM_SIZE>>>(d_pixel, cols*rows, d_h);
    histo_normalization<<<1, 64>>>(d_h, cols*rows);

    cudaEventRecord(p_4);

    cudaMemcpy(h, d_h, sizeof(float)*HISTOGRAM_SIZE, cudaMemcpyDeviceToHost);

    cudaEventRecord(p_5);

    cudaFree(d_h);
    cudaFree(d_pixel);
    // end - cuda execution
    cudaEventSynchronize(p_4);

    cudaEventElapsedTime(&milliseconds_1, p_1, p_2);
    cudaEventElapsedTime(&milliseconds_2, p_2, p_3);
    cudaEventElapsedTime(&milliseconds_3, p_3, p_4);
    cudaEventElapsedTime(&milliseconds_4, p_4, p_5);
}

int main(int argc, char *argv[]) {

	if( argc != 2 ) {
		printf("Too many or no one arguments supplied.\n");
	}

	int i;
	char *filename = argv[1]; //Recebendo o arquivo!;
	
	//scanf("%s", filename);
	PPMImage *image = readPPM(filename);

	float *h = (float*)malloc(sizeof(float) * HISTOGRAM_SIZE);

	//Inicializar h
	for(i=0; i < HISTOGRAM_SIZE; i++) h[i] = 0.0;

	Histogram(image, h);

	for (i = 0; i < HISTOGRAM_SIZE; i++){
		printf("%0.3f ", h[i]);
	}
	printf("\n");
	//fprintf(stdout, "\n%0.6lfs\n", t_end - t_start);  
	free(h);
}

// NOTES(ciroceissler):
//
// arq1.ppm, 0.279246s, 0.318880ms, 8.368736 ms, 1.688352 ms, 0.017504ms, 10.393472ms, 26.8674414
// arq2.ppm, 0.559345s, 0.291424ms, 18.400768ms, 6.097952 ms, 0.015392ms, 24.805536ms, 22.5492003
// arq3.ppm, 2.127529s, 0.298560ms, 72.371140ms, 22.600864ms, 0.015072ms, 95.285636ms, 22.3279089

	.file	"tmpxft_0032de08_00000000-6_histogram.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Error return from gettimeofday: %d"
	.text
	.globl	_Z7rtclockv
	.type	_Z7rtclockv, @function
_Z7rtclockv:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rsi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	testl	%eax, %eax
	je	.L3
	movl	%eax, %edx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L3:
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC1(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z7rtclockv, .-_Z7rtclockv
	.globl	_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf
	.type	_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf, @function
_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L10
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z12histo_kernelP8PPMPixeliPf(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L10:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf, .-_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf
	.globl	_Z12histo_kernelP8PPMPixeliPf
	.type	_Z12histo_kernelP8PPMPixeliPf, @function
_Z12histo_kernelP8PPMPixeliPf:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf
	.cfi_endproc
.LFE2056:
	.size	_Z12histo_kernelP8PPMPixeliPf, .-_Z12histo_kernelP8PPMPixeliPf
	.globl	_Z40__device_stub__Z19histo_normalizationPfiPfi
	.type	_Z40__device_stub__Z19histo_normalizationPfiPfi, @function
_Z40__device_stub__Z19histo_normalizationPfiPfi:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movl	%esi, 4(%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z19histo_normalizationPfi(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L15:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z40__device_stub__Z19histo_normalizationPfiPfi, .-_Z40__device_stub__Z19histo_normalizationPfiPfi
	.globl	_Z19histo_normalizationPfi
	.type	_Z19histo_normalizationPfi, @function
_Z19histo_normalizationPfi:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z40__device_stub__Z19histo_normalizationPfiPfi
	.cfi_endproc
.LFE2058:
	.size	_Z19histo_normalizationPfi, .-_Z19histo_normalizationPfi
	.globl	_Z9HistogramP8PPMImagePf
	.type	_Z9HistogramP8PPMImagePf, @function
_Z9HistogramP8PPMImagePf:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	xorl	%edx, %edx
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$1136, %rsp
	.cfi_def_cfa_offset 1184
	movslq	4(%rdi), %rbx
	movslq	(%rdi), %r14
	movq	%fs:40, %rax
	movq	%rax, 1128(%rsp)
	xorl	%eax, %eax
	movl	$0x00000000, (%rsp)
	movl	%ebx, %r12d
	movl	$0x00000000, 4(%rsp)
	imull	%r14d, %r12d
	movl	$0x00000000, 8(%rsp)
	movl	$0x00000000, 12(%rsp)
	cvtsi2ssl	%r12d, %xmm1
.L21:
	cvtsi2ssl	%edx, %xmm0
	comiss	%xmm0, %xmm1
	jbe	.L27
	leaq	(%rdx,%rdx,2), %rax
	addq	8(%r13), %rax
	incq	%rdx
	shrb	$6, (%rax)
	shrb	$6, 2(%rax)
	shrb	$6, 1(%rax)
	jmp	.L21
.L27:
	imulq	%r14, %rbx
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	48(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	leaq	(%rbx,%rbx,2), %rbx
	call	cudaEventCreate@PLT
	leaq	64(%rsp), %rdi
	call	cudaEventCreate@PLT
	xorl	%esi, %esi
	leaq	96(%rsp), %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$256, %esi
	call	cudaMalloc@PLT
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	8(%r13), %rsi
	movq	24(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdi
	movl	$256, %edx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$67108865, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	484(%rsp), %eax
	salq	$6, %rdx
	movl	$1, %ecx
	movl	$1, 92(%rsp)
	movq	%rdx, 84(%rsp)
	addl	%eax, %eax
	movl	%eax, 72(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 76(%rsp)
	movq	72(%rsp), %rdi
	movl	80(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	movq	16(%rsp), %rdx
	movq	24(%rsp), %rdi
	movl	%r12d, %esi
	call	_Z43__device_stub__Z12histo_kernelP8PPMPixeliPfP8PPMPixeliPf
.L23:
	movl	$67108865, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	salq	$6, %rdx
	movl	$1, %esi
	movabsq	$4294967297, %rdi
	movl	$1, 92(%rsp)
	movq	%rdx, 84(%rsp)
	movq	%rdi, 72(%rsp)
	movl	$1, 80(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	movq	16(%rsp), %rdi
	movl	%r12d, %esi
	call	_Z40__device_stub__Z19histo_normalizationPfiPfi
.L24:
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	movl	$256, %edx
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movq	%rsp, %rdi
	call	cudaEventElapsedTime@PLT
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	8(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	64(%rsp), %rdx
	movq	56(%rsp), %rsi
	leaq	12(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	1128(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$1136, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z9HistogramP8PPMImagePf, .-_Z9HistogramP8PPMImagePf
	.section	.rodata.str1.1
.LC3:
	.string	"Too many or no one arguments supplied.\n"
.LC4:
	.string	"rb"
.LC5:
	.string	"Unable to open file '%s'\n"
.LC6:
	.string	"Invalid image format (must be 'P6')\n"
.LC7:
	.string	"Unable to allocate memory\n"
.LC8:
	.string	"%d %d"
.LC9:
	.string	"Invalid image size (error loading '%s')\n"
.LC10:
	.string	"%d"
.LC11:
	.string	"Invalid rgb component (error loading '%s')\n"
.LC12:
	.string	"'%s' does not have 8-bits components\n"
.LC13:
	.string	"Error loading image '%s'\n"
.LC14:
	.string	"%0.3f "
.LC15:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	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	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	je	.L29
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
.L29:
	movq	8(%rbx), %r12
	leaq	.LC4(%rip), %rsi
	movq	%r12, %rdi
	call	fopen@PLT
	movq	%r12, %rcx
	leaq	.LC5(%rip), %rdx
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.L55
	leaq	8(%rsp), %rdi
	movq	%rax, %rcx
	movl	$16, %edx
	movl	$16, %esi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	jne	.L31
	movq	%r12, %rdi
	call	perror@PLT
.L54:
	movl	$1, %edi
	call	exit@PLT
.L31:
	cmpb	$80, 8(%rsp)
	jne	.L32
	cmpb	$54, 9(%rsp)
	je	.L33
.L32:
	leaq	.LC6(%rip), %rdx
	jmp	.L56
.L33:
	movl	$16, %edi
	call	malloc@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L53
	leaq	.LC7(%rip), %rdx
.L56:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L54
.L36:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L36
.L53:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$35, %eax
	je	.L36
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	ungetc@PLT
	leaq	4(%rbp), %rcx
	movq	%rbp, %rdx
	movq	%rbx, %rdi
	leaq	.LC8(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r12, %rcx
	leaq	.LC9(%rip), %rdx
	cmpl	$2, %eax
	jne	.L55
	leaq	4(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rsi
	movq	%rbx, %rdi
	call	__isoc23_fscanf@PLT
	movq	%r12, %rcx
	leaq	.LC11(%rip), %rdx
	decl	%eax
	jne	.L55
	cmpl	$255, 4(%rsp)
	leaq	.LC12(%rip), %rdx
	jne	.L55
.L39:
	movq	%rbx, %rdi
	call	fgetc@PLT
	cmpl	$10, %eax
	jne	.L39
	movl	0(%rbp), %r14d
	movl	4(%rbp), %r15d
	movl	%r14d, %r13d
	imull	%r15d, %r13d
	movslq	%r13d, %r13
	leaq	0(%r13,%r13,2), %r13
	movq	%r13, %rdi
	call	malloc@PLT
	leal	(%r14,%r14,2), %edx
	movslq	%r15d, %rcx
	movq	%rbx, %r8
	movq	%rax, 8(%rbp)
	movslq	%edx, %rdx
	movq	%rax, %rdi
	movq	%r13, %rsi
	call	__fread_chk@PLT
	movq	%rax, %rdx
	movslq	4(%rbp), %rax
	cmpq	%rax, %rdx
	je	.L40
	movq	%r12, %rcx
	leaq	.LC13(%rip), %rdx
.L55:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L54
.L40:
	movq	%rbx, %rdi
	leaq	.LC14(%rip), %r12
	call	fclose@PLT
	movl	$256, %edi
	call	malloc@PLT
	movl	$64, %ecx
	movq	%rax, %rbx
	xorl	%eax, %eax
	movq	%rbx, %rdi
	movq	%rbx, %rsi
	rep stosl
	movq	%rbp, %rdi
	xorl	%ebp, %ebp
	call	_Z9HistogramP8PPMImagePf
.L41:
	cvtss2sd	(%rbx,%rbp,4), %xmm0
	movq	%r12, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	incq	%rbp
	cmpq	$64, %rbp
	jne	.L41
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rsi
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L42
	call	__stack_chk_fail@PLT
.L42:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC16:
	.string	"_Z19histo_normalizationPfi"
.LC17:
	.string	"_Z12histo_kernelP8PPMPixeliPf"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2060:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC16(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z19histo_normalizationPfi(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC17(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z12histo_kernelP8PPMPixeliPf(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2060:
	.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.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	-1598689907
	.long	1051772663
	.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 <sys/time.h>
#include <math.h>


#define COMMENT "Histogram_GPU"
#define RGB_COMPONENT_COLOR 255

#define HISTOGRAM_SIZE 64

typedef struct {
	unsigned char red, green, blue;
} PPMPixel;

typedef struct {
	int x, y;
	PPMPixel *data;
} PPMImage;

__global__ void histo_kernel(PPMPixel *pixel, int size, float *histo) {
    __shared__ float histo_private[HISTOGRAM_SIZE];

    int i = threadIdx.x + blockIdx.x * blockDim.x;
    int stride = blockDim.x * gridDim.x;

    if (threadIdx.x < HISTOGRAM_SIZE) {
        histo_private[threadIdx.x] = 0;
    }

    __syncthreads();

    while (i < size) {
        atomicAdd(&(histo_private[pixel[i].red*16 + pixel[i].green*4 + pixel[i].blue]), 1);

        i += stride;
    }

    __syncthreads();

    if (threadIdx.x < HISTOGRAM_SIZE) {
        atomicAdd(&(histo[threadIdx.x]), histo_private[threadIdx.x]);
    }

}

__global__ void histo_normalization(float *histo, int n) {
    histo[threadIdx.x] = histo[threadIdx.x]/n;
}

double rtclock()
{
    struct timezone Tzp;
    struct timeval Tp;
    int stat;
    stat = gettimeofday (&Tp, &Tzp);
    if (stat != 0) printf("Error return from gettimeofday: %d",stat);
    return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}


static PPMImage *readPPM(const char *filename) {
	char buff[16];
	PPMImage *img;
	FILE *fp;
	int c, rgb_comp_color;
	fp = fopen(filename, "rb");
	if (!fp) {
		fprintf(stderr, "Unable to open file '%s'\n", filename);
		exit(1);
	}

	if (!fgets(buff, sizeof(buff), fp)) {
		perror(filename);
		exit(1);
	}

	if (buff[0] != 'P' || buff[1] != '6') {
		fprintf(stderr, "Invalid image format (must be 'P6')\n");
		exit(1);
	}

	img = (PPMImage *) malloc(sizeof(PPMImage));
	if (!img) {
		fprintf(stderr, "Unable to allocate memory\n");
		exit(1);
	}

	c = getc(fp);
	while (c == '#') {
		while (getc(fp) != '\n')
			;
		c = getc(fp);
	}

	ungetc(c, fp);
	if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
		fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
		exit(1);
	}

	if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
		fprintf(stderr, "Invalid rgb component (error loading '%s')\n",
				filename);
		exit(1);
	}

	if (rgb_comp_color != RGB_COMPONENT_COLOR) {
		fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
		exit(1);
	}

	while (fgetc(fp) != '\n')
		;
	img->data = (PPMPixel*) malloc(img->x * img->y * sizeof(PPMPixel));

	if (!img) {
		fprintf(stderr, "Unable to allocate memory\n");
		exit(1);
	}

	if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
		fprintf(stderr, "Error loading image '%s'\n", filename);
		exit(1);
	}

	fclose(fp);
	return img;
}


void Histogram(PPMImage *image, float *h) {

    int i;
	int rows, cols;

	float n = image->y * image->x;

    // begin - cuda variables
    float *d_h;
    PPMPixel *d_pixel;

    hipDeviceProp_t prop;

    hipEvent_t p_1, p_2, p_3, p_4, p_5;
    float milliseconds_1 = 0;
    float milliseconds_2 = 0;
    float milliseconds_3 = 0;
    float milliseconds_4 = 0;
    // end - cuda variables

	cols = image->x;
	rows = image->y;

	for (i = 0; i < n; i++) {
		image->data[i].red = floor((image->data[i].red * 4) / 256);
		image->data[i].blue = floor((image->data[i].blue * 4) / 256);
		image->data[i].green = floor((image->data[i].green * 4) / 256);
	}

    // begin - cuda execution
    hipEventCreate(&p_1);
    hipEventCreate(&p_2);
    hipEventCreate(&p_3);
    hipEventCreate(&p_4);
    hipEventCreate(&p_5);
    hipGetDeviceProperties(&prop, 0);

    hipEventRecord(p_1);

    hipMalloc((void **)&d_pixel, sizeof(unsigned char)*3*rows*cols);
    hipMalloc((void **)&d_h, sizeof(float)*HISTOGRAM_SIZE);

    hipEventRecord(p_2);

    hipMemcpy(d_pixel, image->data, sizeof(unsigned char)*3*rows*cols, hipMemcpyHostToDevice);
    hipMemset(d_h, 0,HISTOGRAM_SIZE*sizeof(float));

    hipEventRecord(p_3);

    histo_kernel<<<2*prop.multiProcessorCount, HISTOGRAM_SIZE>>>(d_pixel, cols*rows, d_h);
    histo_normalization<<<1, 64>>>(d_h, cols*rows);

    hipEventRecord(p_4);

    hipMemcpy(h, d_h, sizeof(float)*HISTOGRAM_SIZE, hipMemcpyDeviceToHost);

    hipEventRecord(p_5);

    hipFree(d_h);
    hipFree(d_pixel);
    // end - cuda execution
    hipEventSynchronize(p_4);

    hipEventElapsedTime(&milliseconds_1, p_1, p_2);
    hipEventElapsedTime(&milliseconds_2, p_2, p_3);
    hipEventElapsedTime(&milliseconds_3, p_3, p_4);
    hipEventElapsedTime(&milliseconds_4, p_4, p_5);
}

int main(int argc, char *argv[]) {

	if( argc != 2 ) {
		printf("Too many or no one arguments supplied.\n");
	}

	int i;
	char *filename = argv[1]; //Recebendo o arquivo!;
	
	//scanf("%s", filename);
	PPMImage *image = readPPM(filename);

	float *h = (float*)malloc(sizeof(float) * HISTOGRAM_SIZE);

	//Inicializar h
	for(i=0; i < HISTOGRAM_SIZE; i++) h[i] = 0.0;

	Histogram(image, h);

	for (i = 0; i < HISTOGRAM_SIZE; i++){
		printf("%0.3f ", h[i]);
	}
	printf("\n");
	//fprintf(stdout, "\n%0.6lfs\n", t_end - t_start);  
	free(h);
}

// NOTES(ciroceissler):
//
// arq1.ppm, 0.279246s, 0.318880ms, 8.368736 ms, 1.688352 ms, 0.017504ms, 10.393472ms, 26.8674414
// arq2.ppm, 0.559345s, 0.291424ms, 18.400768ms, 6.097952 ms, 0.015392ms, 24.805536ms, 22.5492003
// arq3.ppm, 2.127529s, 0.298560ms, 72.371140ms, 22.600864ms, 0.015072ms, 95.285636ms, 22.3279089

	.text
	.file	"histogram.hip"
	.globl	_Z27__device_stub__histo_kernelP8PPMPixeliPf # -- Begin function _Z27__device_stub__histo_kernelP8PPMPixeliPf
	.type	_Z27__device_stub__histo_kernelP8PPMPixeliPf,@function
_Z27__device_stub__histo_kernelP8PPMPixeliPf: # @_Z27__device_stub__histo_kernelP8PPMPixeliPf
	.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	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	12(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z12histo_kernelP8PPMPixeliPf, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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	_Z27__device_stub__histo_kernelP8PPMPixeliPf, .Lfunc_end0-_Z27__device_stub__histo_kernelP8PPMPixeliPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z34__device_stub__histo_normalizationPfi # -- Begin function _Z34__device_stub__histo_normalizationPfi
	.type	_Z34__device_stub__histo_normalizationPfi,@function
_Z34__device_stub__histo_normalizationPfi: # @_Z34__device_stub__histo_normalizationPfi
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%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	$_Z19histo_normalizationPfi, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end1:
	.size	_Z34__device_stub__histo_normalizationPfi, .Lfunc_end1-_Z34__device_stub__histo_normalizationPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z7rtclockv
.LCPI2_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z7rtclockv
	.type	_Z7rtclockv,@function
_Z7rtclockv:                            # @_Z7rtclockv
	.cfi_startproc
# %bb.0:
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rsp, %rdi
	leaq	16(%rsp), %rsi
	callq	gettimeofday
	testl	%eax, %eax
	je	.LBB2_2
# %bb.1:
	movl	$.L.str, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB2_2:
	cvtsi2sdq	(%rsp), %xmm1
	cvtsi2sdq	8(%rsp), %xmm0
	mulsd	.LCPI2_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z7rtclockv, .Lfunc_end2-_Z7rtclockv
	.cfi_endproc
                                        # -- End function
	.globl	_Z9HistogramP8PPMImagePf        # -- Begin function _Z9HistogramP8PPMImagePf
	.type	_Z9HistogramP8PPMImagePf,@function
_Z9HistogramP8PPMImagePf:               # @_Z9HistogramP8PPMImagePf
	.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	$1560, %rsp                     # imm = 0x618
	.cfi_def_cfa_offset 1616
	.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, 72(%rsp)                  # 8-byte Spill
	movq	%rdi, %r15
	movslq	4(%rdi), %r14
	movslq	(%rdi), %r13
	movl	%r13d, %ebp
	imull	%r14d, %ebp
	xorl	%eax, %eax
	movl	%eax, 44(%rsp)
	movl	%eax, 40(%rsp)
	movl	%eax, 36(%rsp)
	movl	%eax, 32(%rsp)
	testl	%ebp, %ebp
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph
	cvtsi2ss	%ebp, %xmm0
	movq	8(%r15), %rax
	addq	$2, %rax
	movl	$1, %ecx
.LBB3_2:                                # =>This Inner Loop Header: Depth=1
	shrb	$6, -2(%rax)
	shrb	$6, (%rax)
	shrb	$6, -1(%rax)
	xorps	%xmm1, %xmm1
	cvtsi2ss	%ecx, %xmm1
	addq	$3, %rax
	incl	%ecx
	ucomiss	%xmm1, %xmm0
	ja	.LBB3_2
.LBB3_3:                                # %._crit_edge
	leaq	80(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	leaq	48(%rsp), %rdi
	callq	hipEventCreate
	leaq	88(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movq	(%r12), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	imulq	%r13, %r14
	leaq	(%r14,%r14,2), %r12
	leaq	64(%rsp), %r14
	movq	%r14, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %r13
	movl	$256, %esi                      # imm = 0x100
	movq	%r13, %rdi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movabsq	$4294967360, %rbx               # imm = 0x100000040
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r14), %rdi
	movq	8(%r15), %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movl	$256, %edx                      # imm = 0x100
	xorl	%esi, %esi
	callq	hipMemset
	leaq	24(%rsp), %rax
	movq	(%rax), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	leaq	88(%rsp), %rax
	movl	388(%rax), %eax
	addl	%eax, %eax
	leaq	(%rax,%rbx), %rdi
	addq	$-64, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_5
# %bb.4:
	movq	64(%rsp), %rdi
	movq	16(%rsp), %rdx
	movl	%ebp, %esi
	callq	_Z27__device_stub__histo_kernelP8PPMPixeliPf
.LBB3_5:
	leaq	-63(%rbx), %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_7
# %bb.6:
	movq	16(%rsp), %rdi
	movl	%ebp, %esi
	callq	_Z34__device_stub__histo_normalizationPfi
.LBB3_7:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rsi
	movl	$256, %edx                      # imm = 0x100
	movq	72(%rsp), %rdi                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	64(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	80(%rsp), %rsi
	movq	56(%rsp), %rdx
	leaq	44(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	56(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	40(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	36(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	8(%rsp), %rsi
	movq	48(%rsp), %rdx
	leaq	32(%rsp), %rdi
	callq	hipEventElapsedTime
	addq	$1560, %rsp                     # imm = 0x618
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z9HistogramP8PPMImagePf, .Lfunc_end3-_Z9HistogramP8PPMImagePf
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	cmpl	$2, %edi
	je	.LBB4_2
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB4_2:
	movq	8(%rbx), %rbx
	movl	$.L.str.4, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB4_18
# %bb.3:
	movq	%rax, %r14
	leaq	16(%rsp), %rdi
	movl	$16, %esi
	movq	%rax, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB4_19
# %bb.4:
	cmpb	$80, 16(%rsp)
	jne	.LBB4_20
# %bb.5:
	cmpb	$54, 17(%rsp)
	jne	.LBB4_20
# %bb.6:
	movl	$16, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB4_21
# %bb.7:
	movq	%rax, %r15
.LBB4_8:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_9 Depth 2
	movq	%r14, %rdi
	callq	getc
	cmpl	$35, %eax
	jne	.LBB4_10
.LBB4_9:                                # %.preheader44.i
                                        #   Parent Loop BB4_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r14, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB4_9
	jmp	.LBB4_8
.LBB4_10:                               # %._crit_edge.i
	movl	%eax, %edi
	movq	%r14, %rsi
	callq	ungetc
	movq	%r15, %rcx
	addq	$4, %rcx
	movl	$.L.str.8, %esi
	movq	%r14, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$2, %eax
	jne	.LBB4_24
# %bb.11:
	leaq	12(%rsp), %rdx
	movl	$.L.str.10, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB4_25
# %bb.12:
	cmpl	$255, 12(%rsp)
	jne	.LBB4_26
.LBB4_13:                               # %.preheader.i
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	fgetc
	cmpl	$10, %eax
	jne	.LBB4_13
# %bb.14:
	movslq	(%r15), %rax
	movslq	4(%r15), %r12
	leaq	(%rax,%rax,2), %r13
	movq	%r13, %rdi
	imulq	%r12, %rdi
	callq	malloc
	movq	%rax, 8(%r15)
	movq	%rax, %rdi
	movq	%r13, %rsi
	movq	%r12, %rdx
	movq	%r14, %rcx
	callq	fread
	movslq	4(%r15), %rcx
	cmpq	%rcx, %rax
	jne	.LBB4_27
# %bb.15:                               # %_ZL7readPPMPKc.exit
	movq	%r14, %rdi
	callq	fclose
	movl	$1, %edi
	movl	$256, %esi                      # imm = 0x100
	callq	calloc@PLT
	movq	%rax, %rbx
	movq	%r15, %rdi
	movq	%rax, %rsi
	callq	_Z9HistogramP8PPMImagePf
	xorl	%r14d, %r14d
.LBB4_16:                               # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r14,4), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	incq	%r14
	cmpq	$64, %r14
	jne	.LBB4_16
# %bb.17:
	movl	$10, %edi
	callq	putchar@PLT
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	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
.LBB4_18:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rdi
	movl	$.L.str.5, %esi
	jmp	.LBB4_28
.LBB4_19:
	movq	%rbx, %rdi
	callq	perror
	jmp	.LBB4_23
.LBB4_20:
	movq	stderr(%rip), %rcx
	movl	$.L.str.6, %edi
	movl	$36, %esi
	jmp	.LBB4_22
.LBB4_21:
	movq	stderr(%rip), %rcx
	movl	$.L.str.7, %edi
	movl	$26, %esi
.LBB4_22:
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB4_23
.LBB4_24:
	movq	stderr(%rip), %rdi
	movl	$.L.str.9, %esi
	jmp	.LBB4_28
.LBB4_25:
	movq	stderr(%rip), %rdi
	movl	$.L.str.11, %esi
	jmp	.LBB4_28
.LBB4_26:
	movq	stderr(%rip), %rdi
	movl	$.L.str.12, %esi
	jmp	.LBB4_28
.LBB4_27:
	movq	stderr(%rip), %rdi
	movl	$.L.str.13, %esi
.LBB4_28:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB4_23:
	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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z12histo_kernelP8PPMPixeliPf, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z19histo_normalizationPfi, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.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	_Z12histo_kernelP8PPMPixeliPf,@object # @_Z12histo_kernelP8PPMPixeliPf
	.section	.rodata,"a",@progbits
	.globl	_Z12histo_kernelP8PPMPixeliPf
	.p2align	3, 0x0
_Z12histo_kernelP8PPMPixeliPf:
	.quad	_Z27__device_stub__histo_kernelP8PPMPixeliPf
	.size	_Z12histo_kernelP8PPMPixeliPf, 8

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error return from gettimeofday: %d"
	.size	.L.str, 35

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%0.3f "
	.size	.L.str.2, 7

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Unable to open file '%s'\n"
	.size	.L.str.5, 26

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Invalid image format (must be 'P6')\n"
	.size	.L.str.6, 37

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Unable to allocate memory\n"
	.size	.L.str.7, 27

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%d %d"
	.size	.L.str.8, 6

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Invalid image size (error loading '%s')\n"
	.size	.L.str.9, 41

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

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Invalid rgb component (error loading '%s')\n"
	.size	.L.str.11, 44

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"'%s' does not have 8-bits components\n"
	.size	.L.str.12, 38

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Error loading image '%s'\n"
	.size	.L.str.13, 26

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z12histo_kernelP8PPMPixeliPf"
	.size	.L__unnamed_1, 30

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z19histo_normalizationPfi"
	.size	.L__unnamed_2, 27

	.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	"Too many or no one arguments supplied."
	.size	.Lstr, 39

	.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 _Z27__device_stub__histo_kernelP8PPMPixeliPf
	.addrsig_sym _Z34__device_stub__histo_normalizationPfi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z12histo_kernelP8PPMPixeliPf
	.addrsig_sym _Z19histo_normalizationPfi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z12histo_kernelP8PPMPixeliPf ; -- Begin function _Z12histo_kernelP8PPMPixeliPf
	.globl	_Z12histo_kernelP8PPMPixeliPf
	.p2align	8
	.type	_Z12histo_kernelP8PPMPixeliPf,@function
_Z12histo_kernelP8PPMPixeliPf:          ; @_Z12histo_kernelP8PPMPixeliPf
; %bb.0:
	s_load_b32 s2, s[0:1], 0x24
	v_cmp_gt_u32_e32 vcc_lo, 64, v0
	v_lshlrev_b32_e32 v4, 2, v0
	s_and_saveexec_b32 s3, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	v_mov_b32_e32 v1, 0
	ds_store_b32 v4, v1
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s3
	s_load_b32 s4, s[0:1], 0x8
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, 0xffff, s2
	s_mov_b32 s5, exec_lo
	v_mad_u64_u32 v[1:2], null, s15, s3, v[0:1]
	s_barrier
	buffer_gl0_inv
	v_cmpx_gt_i32_e64 s4, v1
	s_cbranch_execz .LBB0_5
; %bb.3:                                ; %.lr.ph.preheader
	s_clause 0x1
	s_load_b64 s[6:7], s[0:1], 0x0
	s_load_b32 s2, s[0:1], 0x18
	v_mov_b32_e32 v0, 1.0
	s_mov_b32 s9, 0
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[2:3], null, v1, 3, s[6:7]
	s_mul_i32 s6, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	s_mul_hi_i32 s7, s6, 3
	s_mul_i32 s8, s6, 3
	v_add_co_u32 v2, s2, v2, 1
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v3, s2, 0, v3, s2
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_clause 0x1
	global_load_u16 v5, v[2:3], off offset:-1
	global_load_u8 v6, v[2:3], off offset:1
	v_add_nc_u32_e32 v1, s6, v1
	v_add_co_u32 v2, s3, v2, s8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v3, s3, s7, v3, s3
	v_cmp_le_i32_e64 s2, s4, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	s_or_b32 s9, s2, s9
	s_waitcnt vmcnt(1)
	v_lshrrev_b32_e32 v7, 6, v5
	v_and_b32_e32 v5, 0xff, v5
	v_and_b32_e32 v7, 0x3fc, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v5, v5, 4, v7
	s_waitcnt vmcnt(0)
	v_add_lshl_u32 v5, v5, v6, 2
	ds_add_f32 v5, v0
	s_and_not1_b32 exec_lo, exec_lo, s9
	s_cbranch_execnz .LBB0_4
.LBB0_5:                                ; %Flow23
	s_or_b32 exec_lo, exec_lo, s5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_8
; %bb.6:
	s_load_b64 s[0:1], s[0:1], 0x10
	ds_load_b32 v5, v4
	s_waitcnt lgkmcnt(0)
	global_load_b32 v3, v4, s[0:1]
	v_add_co_u32 v0, s0, s0, v4
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v1, null, s1, 0, s0
	s_mov_b32 s0, 0
.LBB0_7:                                ; %atomicrmw.start
                                        ; =>This Inner Loop Header: Depth=1
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v2, v3, v5
	global_atomic_cmpswap_b32 v2, v[0:1], v[2:3], off glc
	s_waitcnt vmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v2, v3
	v_mov_b32_e32 v3, v2
	s_or_b32 s0, vcc_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s0
	s_cbranch_execnz .LBB0_7
.LBB0_8:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z12histo_kernelP8PPMPixeliPf
		.amdhsa_group_segment_fixed_size 256
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 8
		.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	_Z12histo_kernelP8PPMPixeliPf, .Lfunc_end0-_Z12histo_kernelP8PPMPixeliPf
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 424
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 256 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; 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
	.protected	_Z19histo_normalizationPfi ; -- Begin function _Z19histo_normalizationPfi
	.globl	_Z19histo_normalizationPfi
	.p2align	8
	.type	_Z19histo_normalizationPfi,@function
_Z19histo_normalizationPfi:             ; @_Z19histo_normalizationPfi
; %bb.0:
	s_load_b64 s[2:3], s[0:1], 0x0
	v_lshlrev_b32_e32 v0, 2, v0
	s_load_b32 s0, s[0:1], 0x8
	s_waitcnt lgkmcnt(0)
	global_load_b32 v1, v0, s[2:3]
	v_cvt_f32_i32_e32 v2, s0
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_scale_f32 v3, null, v2, v2, v1
	v_rcp_f32_e32 v4, v3
	s_waitcnt_depctr 0xfff
	v_fma_f32 v5, -v3, v4, 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v4, v5, v4
	v_div_scale_f32 v5, vcc_lo, v1, v2, v1
	v_mul_f32_e32 v6, v5, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v7, -v3, v6, v5
	v_fmac_f32_e32 v6, v7, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v3, -v3, v6, v5
	v_div_fmas_f32 v3, v3, v4, v6
	s_delay_alu instid0(VALU_DEP_1)
	v_div_fixup_f32 v1, v3, v2, v1
	global_store_b32 v0, v1, s[2:3]
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z19histo_normalizationPfi
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 12
		.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 8
		.amdhsa_next_free_sgpr 4
		.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_end1:
	.size	_Z19histo_normalizationPfi, .Lfunc_end1-_Z19histo_normalizationPfi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 156
; NumSgprs: 6
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 0
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 6
; NumVGPRsForWavesPerEU: 8
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

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

	.end_amdgpu_metadata
