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

#define NUM_ELEMENTS 753411
#define EPSILON 0.000005
#define NUM_THREADS_PER_BLOCK 256
#define NUM_BLOCKS NUM_ELEMENTS/NUM_THREADS_PER_BLOCK+1

void functionSerial(float* d_in, float *d_out) {
  float x;
  for (int i = 0; i < NUM_ELEMENTS; i++) {
    x = d_in[i]; x = (x/(x-2.3));
    d_out[i] = x * x * x;
  }
}

__global__ void functionKernel(float* d_in, float *d_out) {
    const unsigned int lid = threadIdx.x; // local id inside a block
    const unsigned int gid = blockIdx.x*blockDim.x + lid; // global id
    float x;
    if (gid < NUM_ELEMENTS) {
      x = d_in[gid]; x = (x/(x-2.3));
      d_out[gid] = x * x * x;
    }
}

bool equal(float f0, float f1) {
    return fabs(f0 - f1) < EPSILON; 
}

int timeval_subtract(struct timeval* result, struct timeval* t2,struct timeval* t1) {
    unsigned int resolution=1000000;
    long int diff = (t2->tv_usec + resolution * t2->tv_sec) -
                    (t1->tv_usec + resolution * t1->tv_sec);
    result->tv_sec = diff / resolution;
    result->tv_usec = diff % resolution;
    return (diff<0);
}

int main(int argc, char** argv) {
    unsigned long int elapsed_gpu, elapsed_cpu, elapsed_gpu_memcpy;
    struct timeval t_start, t_end, t_diff;
    unsigned int mem_size = NUM_ELEMENTS*sizeof(float);

    // allocate host memory
    float* h_in = (float*) malloc(mem_size);
    float* h_out_gpu = (float*) malloc(mem_size);
    float* h_out_cpu = (float*) malloc(mem_size);

    // initialize the memory
    for(unsigned int i=0; i<NUM_ELEMENTS; ++i){
        h_in[i] = (float)i+1.0;
    }
    // allocate device memory
    float* d_in;
    float* d_out;
    cudaMalloc((void**)&d_in, mem_size);
    cudaMalloc((void**)&d_out, mem_size);

    // copy host memory to device
    gettimeofday(&t_start, NULL);
    cudaMemcpy(d_in, h_in, mem_size, cudaMemcpyHostToDevice);
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_gpu_memcpy = t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // execute the kernel on GPU
    gettimeofday(&t_start, NULL);
    functionKernel<<<NUM_BLOCKS, NUM_THREADS_PER_BLOCK>>>(d_in, d_out);
    cudaThreadSynchronize();
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_gpu = t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // copy result from ddevice to host 
    gettimeofday(&t_start, NULL);
    cudaMemcpy(h_out_gpu, d_out, mem_size, cudaMemcpyDeviceToHost);
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_gpu_memcpy += t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // compute the function on CPU
    gettimeofday(&t_start, NULL);
    functionSerial(h_in, h_out_cpu); 
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_cpu = t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // check if result is valid
    bool valid = true;
    for(unsigned int i=0; i<NUM_ELEMENTS; ++i)
      valid = valid && equal(h_out_gpu[i], h_out_cpu[i]);

    // report back
    if (valid)
      printf("VALID\n");
    else
      printf("INVALID\n");

    printf("GPU took %d microseconds (%.2fms)\n",elapsed_gpu_memcpy+elapsed_gpu,(elapsed_gpu_memcpy+elapsed_gpu)/1000.0);
    printf("-- of which  %d microseconds (%.2fms) was spent copying memory\n",elapsed_gpu_memcpy,elapsed_gpu_memcpy/1000.0);
    printf("-- of which  %d microseconds (%.2fms) was spent in the kernel\n",elapsed_gpu,elapsed_gpu/1000.0);
    printf("CPU took a total of  %d microseconds (%.2fms)\n",elapsed_cpu,elapsed_cpu/1000.0);

    // clean-up memory
    free(h_in);
    free(h_out_gpu);
    free(h_out_cpu);
    cudaFree(d_in);
    cudaFree(d_out);
}

	.file	"tmpxft_0036a0b8_00000000-6_task4.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
	.globl	_Z14functionSerialPfS_
	.type	_Z14functionSerialPfS_, @function
_Z14functionSerialPfS_:
.LFB2027:
	.cfi_startproc
	endbr64
	movsd	.LC0(%rip), %xmm2
	xorl	%eax, %eax
.L3:
	cvtss2sd	(%rdi,%rax), %xmm0
	movaps	%xmm0, %xmm1
	subsd	%xmm2, %xmm1
	divsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movaps	%xmm0, %xmm1
	mulss	%xmm0, %xmm1
	mulss	%xmm0, %xmm1
	movss	%xmm1, (%rsi,%rax)
	addq	$4, %rax
	cmpq	$3013644, %rax
	jne	.L3
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z14functionSerialPfS_, .-_Z14functionSerialPfS_
	.globl	_Z5equalff
	.type	_Z5equalff, @function
_Z5equalff:
.LFB2028:
	.cfi_startproc
	endbr64
	subss	%xmm1, %xmm0
	movsd	.LC2(%rip), %xmm1
	andps	.LC1(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm0, %xmm1
	seta	%al
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z5equalff, .-_Z5equalff
	.globl	_Z16timeval_subtractP7timevalS0_S0_
	.type	_Z16timeval_subtractP7timevalS0_S0_, @function
_Z16timeval_subtractP7timevalS0_S0_:
.LFB2029:
	.cfi_startproc
	endbr64
	imulq	$1000000, (%rsi), %rcx
	addq	8(%rsi), %rcx
	movl	$1000000, %esi
	imulq	$1000000, (%rdx), %rax
	addq	8(%rdx), %rax
	subq	%rax, %rcx
	movq	%rcx, %rax
	shrq	$63, %rcx
	cqto
	idivq	%rsi
	movq	%rax, (%rdi)
	movq	%rcx, %rax
	movq	%rdx, 8(%rdi)
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z16timeval_subtractP7timevalS0_S0_, .-_Z16timeval_subtractP7timevalS0_S0_
	.globl	_Z36__device_stub__Z14functionKernelPfS_PfS_
	.type	_Z36__device_stub__Z14functionKernelPfS_PfS_, @function
_Z36__device_stub__Z14functionKernelPfS_PfS_:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%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)
	movq	%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	.L8
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z14functionKernelPfS_(%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
.L8:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z36__device_stub__Z14functionKernelPfS_PfS_, .-_Z36__device_stub__Z14functionKernelPfS_PfS_
	.globl	_Z14functionKernelPfS_
	.type	_Z14functionKernelPfS_, @function
_Z14functionKernelPfS_:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z14functionKernelPfS_PfS_
	.cfi_endproc
.LFE2056:
	.size	_Z14functionKernelPfS_, .-_Z14functionKernelPfS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC6:
	.string	"VALID\n"
.LC7:
	.string	"INVALID\n"
.LC9:
	.string	"GPU took %d microseconds (%.2fms)\n"
.LC10:
	.string	"-- of which  %d microseconds (%.2fms) was spent copying memory\n"
.LC11:
	.string	"-- of which  %d microseconds (%.2fms) was spent in the kernel\n"
.LC12:
	.string	"CPU took a total of  %d microseconds (%.2fms)\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
	movl	$3013644, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$3013644, %edi
	movq	%rax, %r13
	call	malloc@PLT
	movl	$3013644, %edi
	movq	%rax, %r15
	call	malloc@PLT
	movss	.LC3(%rip), %xmm1
	movq	%rax, %r14
	xorl	%eax, %eax
.L14:
	cvtsi2ssl	%eax, %xmm0
	addss	%xmm1, %xmm0
	movss	%xmm0, 0(%r13,%rax,4)
	incq	%rax
	cmpq	$753411, %rax
	jne	.L14
	leaq	16(%rsp), %rdi
	movl	$3013644, %esi
	leaq	56(%rsp), %r12
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$3013644, %esi
	call	cudaMalloc@PLT
	xorl	%esi, %esi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%r13, %rsi
	movl	$3013644, %edx
	call	cudaMemcpy@PLT
	leaq	72(%rsp), %rax
	xorl	%esi, %esi
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	gettimeofday@PLT
	leaq	88(%rsp), %rax
	movq	(%rsp), %rsi
	movq	%r12, %rdx
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	_Z16timeval_subtractP7timevalS0_S0_
	cvtsi2sdq	88(%rsp), %xmm0
	mulsd	.LC4(%rip), %xmm0
	cvtsi2sdq	96(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	comisd	.LC5(%rip), %xmm0
	jnb	.L15
	cvttsd2siq	%xmm0, %rbp
	jmp	.L16
.L15:
	subsd	.LC5(%rip), %xmm0
	cvttsd2siq	%xmm0, %rbp
	btcq	$63, %rbp
.L16:
	xorl	%esi, %esi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	movl	$16777217, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$33554455, %edi
	salq	$8, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	salq	$7, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L17
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	call	_Z36__device_stub__Z14functionKernelPfS_PfS_
.L17:
	call	cudaThreadSynchronize@PLT
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	(%rsp), %rsi
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	call	_Z16timeval_subtractP7timevalS0_S0_
	cvtsi2sdq	88(%rsp), %xmm0
	mulsd	.LC4(%rip), %xmm0
	cvtsi2sdq	96(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	comisd	.LC5(%rip), %xmm0
	jnb	.L18
	cvttsd2siq	%xmm0, %rbx
	jmp	.L19
.L18:
	subsd	.LC5(%rip), %xmm0
	cvttsd2siq	%xmm0, %rbx
	btcq	$63, %rbx
.L19:
	xorl	%esi, %esi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%r15, %rdi
	movl	$3013644, %edx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	(%rsp), %rsi
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	call	_Z16timeval_subtractP7timevalS0_S0_
	cvtsi2sdq	88(%rsp), %xmm0
	mulsd	.LC4(%rip), %xmm0
	cvtsi2sdq	96(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	testq	%rbp, %rbp
	js	.L20
	cvtsi2sdq	%rbp, %xmm1
	jmp	.L21
.L20:
	movq	%rbp, %rax
	andl	$1, %ebp
	shrq	%rax
	orq	%rbp, %rax
	cvtsi2sdq	%rax, %xmm1
	addsd	%xmm1, %xmm1
.L21:
	addsd	%xmm1, %xmm0
	comisd	.LC5(%rip), %xmm0
	jnb	.L22
	cvttsd2siq	%xmm0, %rbp
	jmp	.L23
.L22:
	subsd	.LC5(%rip), %xmm0
	cvttsd2siq	%xmm0, %rbp
	btcq	$63, %rbp
.L23:
	xorl	%esi, %esi
	movq	%r12, %rdi
	call	gettimeofday@PLT
	movq	%r14, %rsi
	movq	%r13, %rdi
	call	_Z14functionSerialPfS_
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	(%rsp), %rsi
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	call	_Z16timeval_subtractP7timevalS0_S0_
	cvtsi2sdq	88(%rsp), %xmm0
	mulsd	.LC4(%rip), %xmm0
	cvtsi2sdq	96(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	comisd	.LC5(%rip), %xmm0
	jnb	.L24
	cvttsd2siq	%xmm0, %r12
	jmp	.L25
.L24:
	subsd	.LC5(%rip), %xmm0
	cvttsd2siq	%xmm0, %r12
	btcq	$63, %r12
.L25:
	movsd	.LC2(%rip), %xmm2
	xorl	%eax, %eax
	movb	$1, %dl
	movss	.LC1(%rip), %xmm1
.L27:
	testb	%dl, %dl
	je	.L26
	movss	(%r15,%rax), %xmm0
	subss	(%r14,%rax), %xmm0
	andps	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm0, %xmm2
	seta	%dl
.L26:
	addq	$4, %rax
	cmpq	$3013644, %rax
	jne	.L27
	leaq	.LC6(%rip), %rsi
	testb	%dl, %dl
	jne	.L45
	leaq	.LC7(%rip), %rsi
.L45:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rdx
	addq	%rbp, %rdx
	js	.L30
	cvtsi2sdq	%rdx, %xmm0
	jmp	.L31
.L30:
	movq	%rdx, %rax
	movq	%rdx, %rcx
	shrq	%rax
	andl	$1, %ecx
	orq	%rcx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L31:
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	divsd	.LC8(%rip), %xmm0
	call	__printf_chk@PLT
	testq	%rbp, %rbp
	js	.L32
	cvtsi2sdq	%rbp, %xmm0
	jmp	.L33
.L32:
	movq	%rbp, %rax
	movq	%rbp, %rdx
	shrq	%rax
	andl	$1, %edx
	orq	%rdx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L33:
	movq	%rbp, %rdx
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	divsd	.LC8(%rip), %xmm0
	call	__printf_chk@PLT
	testq	%rbx, %rbx
	js	.L34
	cvtsi2sdq	%rbx, %xmm0
	jmp	.L35
.L34:
	movq	%rbx, %rax
	movq	%rbx, %rdx
	shrq	%rax
	andl	$1, %edx
	orq	%rdx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L35:
	movq	%rbx, %rdx
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	divsd	.LC8(%rip), %xmm0
	call	__printf_chk@PLT
	testq	%r12, %r12
	js	.L36
	cvtsi2sdq	%r12, %xmm0
	jmp	.L37
.L36:
	movq	%r12, %rax
	movq	%r12, %rdx
	shrq	%rax
	andl	$1, %edx
	orq	%rdx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L37:
	movq	%r12, %rdx
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	divsd	.LC8(%rip), %xmm0
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%r15, %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L38
	call	__stack_chk_fail@PLT
.L38:
	addq	$120, %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
.LC13:
	.string	"_Z14functionKernelPfS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14functionKernelPfS_(%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
.LFE2058:
	.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
.LC0:
	.long	1717986918
	.long	1073899110
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC1:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8
	.align 8
.LC2:
	.long	-1998362383
	.long	1054144693
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC3:
	.long	1065353216
	.section	.rodata.cst8
	.align 8
.LC4:
	.long	0
	.long	1093567616
	.align 8
.LC5:
	.long	0
	.long	1138753536
	.align 8
.LC8:
	.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include <time.h>
#include <hip/hip_runtime.h>

#define NUM_ELEMENTS 753411
#define EPSILON 0.000005
#define NUM_THREADS_PER_BLOCK 256
#define NUM_BLOCKS NUM_ELEMENTS/NUM_THREADS_PER_BLOCK+1

void functionSerial(float* d_in, float *d_out) {
  float x;
  for (int i = 0; i < NUM_ELEMENTS; i++) {
    x = d_in[i]; x = (x/(x-2.3));
    d_out[i] = x * x * x;
  }
}

__global__ void functionKernel(float* d_in, float *d_out) {
    const unsigned int lid = threadIdx.x; // local id inside a block
    const unsigned int gid = blockIdx.x*blockDim.x + lid; // global id
    float x;
    if (gid < NUM_ELEMENTS) {
      x = d_in[gid]; x = (x/(x-2.3));
      d_out[gid] = x * x * x;
    }
}

bool equal(float f0, float f1) {
    return fabs(f0 - f1) < EPSILON; 
}

int timeval_subtract(struct timeval* result, struct timeval* t2,struct timeval* t1) {
    unsigned int resolution=1000000;
    long int diff = (t2->tv_usec + resolution * t2->tv_sec) -
                    (t1->tv_usec + resolution * t1->tv_sec);
    result->tv_sec = diff / resolution;
    result->tv_usec = diff % resolution;
    return (diff<0);
}

int main(int argc, char** argv) {
    unsigned long int elapsed_gpu, elapsed_cpu, elapsed_gpu_memcpy;
    struct timeval t_start, t_end, t_diff;
    unsigned int mem_size = NUM_ELEMENTS*sizeof(float);

    // allocate host memory
    float* h_in = (float*) malloc(mem_size);
    float* h_out_gpu = (float*) malloc(mem_size);
    float* h_out_cpu = (float*) malloc(mem_size);

    // initialize the memory
    for(unsigned int i=0; i<NUM_ELEMENTS; ++i){
        h_in[i] = (float)i+1.0;
    }
    // allocate device memory
    float* d_in;
    float* d_out;
    hipMalloc((void**)&d_in, mem_size);
    hipMalloc((void**)&d_out, mem_size);

    // copy host memory to device
    gettimeofday(&t_start, NULL);
    hipMemcpy(d_in, h_in, mem_size, hipMemcpyHostToDevice);
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_gpu_memcpy = t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // execute the kernel on GPU
    gettimeofday(&t_start, NULL);
    functionKernel<<<NUM_BLOCKS, NUM_THREADS_PER_BLOCK>>>(d_in, d_out);
    hipDeviceSynchronize();
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_gpu = t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // copy result from ddevice to host 
    gettimeofday(&t_start, NULL);
    hipMemcpy(h_out_gpu, d_out, mem_size, hipMemcpyDeviceToHost);
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_gpu_memcpy += t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // compute the function on CPU
    gettimeofday(&t_start, NULL);
    functionSerial(h_in, h_out_cpu); 
    gettimeofday(&t_end, NULL);
    timeval_subtract(&t_diff, &t_end, &t_start);
    elapsed_cpu = t_diff.tv_sec*1e6+t_diff.tv_usec;    

    // check if result is valid
    bool valid = true;
    for(unsigned int i=0; i<NUM_ELEMENTS; ++i)
      valid = valid && equal(h_out_gpu[i], h_out_cpu[i]);

    // report back
    if (valid)
      printf("VALID\n");
    else
      printf("INVALID\n");

    printf("GPU took %d microseconds (%.2fms)\n",elapsed_gpu_memcpy+elapsed_gpu,(elapsed_gpu_memcpy+elapsed_gpu)/1000.0);
    printf("-- of which  %d microseconds (%.2fms) was spent copying memory\n",elapsed_gpu_memcpy,elapsed_gpu_memcpy/1000.0);
    printf("-- of which  %d microseconds (%.2fms) was spent in the kernel\n",elapsed_gpu,elapsed_gpu/1000.0);
    printf("CPU took a total of  %d microseconds (%.2fms)\n",elapsed_cpu,elapsed_cpu/1000.0);

    // clean-up memory
    free(h_in);
    free(h_out_gpu);
    free(h_out_cpu);
    hipFree(d_in);
    hipFree(d_out);
}

	.text
	.file	"task4.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z14functionSerialPfS_
.LCPI0_0:
	.quad	0xc002666666666666              # double -2.2999999999999998
	.text
	.globl	_Z14functionSerialPfS_
	.type	_Z14functionSerialPfS_,@function
_Z14functionSerialPfS_:                 # @_Z14functionSerialPfS_
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
	movsd	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm1, %xmm1
	cvtss2sd	(%rdi,%rax,4), %xmm1
	movaps	%xmm1, %xmm2
	addsd	%xmm0, %xmm2
	divsd	%xmm2, %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movaps	%xmm1, %xmm2
	mulss	%xmm1, %xmm2
	mulss	%xmm1, %xmm2
	movss	%xmm2, (%rsi,%rax,4)
	incq	%rax
	cmpq	$753411, %rax                   # imm = 0xB7F03
	jne	.LBB0_1
# %bb.2:
	retq
.Lfunc_end0:
	.size	_Z14functionSerialPfS_, .Lfunc_end0-_Z14functionSerialPfS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z29__device_stub__functionKernelPfS_ # -- Begin function _Z29__device_stub__functionKernelPfS_
	.type	_Z29__device_stub__functionKernelPfS_,@function
_Z29__device_stub__functionKernelPfS_:  # @_Z29__device_stub__functionKernelPfS_
	.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	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z14functionKernelPfS_, %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	_Z29__device_stub__functionKernelPfS_, .Lfunc_end1-_Z29__device_stub__functionKernelPfS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z5equalff
.LCPI2_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI2_1:
	.quad	0x3ed4f8b588e368f1              # double 5.0000000000000004E-6
	.text
	.globl	_Z5equalff
	.type	_Z5equalff,@function
_Z5equalff:                             # @_Z5equalff
	.cfi_startproc
# %bb.0:
	subss	%xmm1, %xmm0
	andps	.LCPI2_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movsd	.LCPI2_1(%rip), %xmm1           # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	seta	%al
	retq
.Lfunc_end2:
	.size	_Z5equalff, .Lfunc_end2-_Z5equalff
	.cfi_endproc
                                        # -- End function
	.globl	_Z16timeval_subtractP7timevalS0_S0_ # -- Begin function _Z16timeval_subtractP7timevalS0_S0_
	.type	_Z16timeval_subtractP7timevalS0_S0_,@function
_Z16timeval_subtractP7timevalS0_S0_:    # @_Z16timeval_subtractP7timevalS0_S0_
	.cfi_startproc
# %bb.0:
	movq	(%rsi), %rax
	movq	8(%rsi), %rcx
	subq	(%rdx), %rax
	imulq	$1000000, %rax, %rax            # imm = 0xF4240
	subq	8(%rdx), %rcx
	addq	%rax, %rcx
	movabsq	$4835703278458516699, %rdx      # imm = 0x431BDE82D7B634DB
	movq	%rcx, %rax
	imulq	%rdx
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$18, %rdx
	addq	%rax, %rdx
	movq	%rdx, (%rdi)
	imulq	$1000000, %rdx, %rax            # imm = 0xF4240
	movq	%rcx, %rdx
	subq	%rax, %rdx
	movq	%rdx, 8(%rdi)
	shrq	$63, %rcx
	movl	%ecx, %eax
	retq
.Lfunc_end3:
	.size	_Z16timeval_subtractP7timevalS0_S0_, .Lfunc_end3-_Z16timeval_subtractP7timevalS0_S0_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x3ff0000000000000              # double 1
.LCPI4_1:
	.quad	0xc002666666666666              # double -2.2999999999999998
.LCPI4_3:
	.quad	0x3ed4f8b588e368f1              # double 5.0000000000000004E-6
.LCPI4_4:
	.quad	0x412e848000000000              # double 1.0E+6
.LCPI4_5:
	.quad	0x43e0000000000000              # double 9.2233720368547758E+18
.LCPI4_8:
	.quad	0x408f400000000000              # double 1000
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI4_2:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
.LCPI4_6:
	.long	1127219200                      # 0x43300000
	.long	1160773632                      # 0x45300000
	.long	0                               # 0x0
	.long	0                               # 0x0
.LCPI4_7:
	.quad	0x4330000000000000              # double 4503599627370496
	.quad	0x4530000000000000              # double 1.9342813113834067E+25
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$3013644, %edi                  # imm = 0x2DFC0C
	callq	malloc
	movq	%rax, %rbx
	movl	$3013644, %edi                  # imm = 0x2DFC0C
	callq	malloc
	movq	%rax, %r14
	movl	$3013644, %edi                  # imm = 0x2DFC0C
	callq	malloc
	movq	%rax, %r15
	xorl	%eax, %eax
	movsd	.LCPI4_0(%rip), %xmm0           # xmm0 = mem[0],zero
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	movl	%eax, %ecx
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rcx, %xmm1
	addsd	%xmm0, %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movss	%xmm1, (%rbx,%rax,4)
	incq	%rax
	cmpq	$753411, %rax                   # imm = 0xB7F03
	jne	.LBB4_1
# %bb.2:
	leaq	16(%rsp), %r12
	movl	$3013644, %esi                  # imm = 0x2DFC0C
	movq	%r12, %rdi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$3013644, %esi                  # imm = 0x2DFC0C
	callq	hipMalloc
	leaq	136(%rsp), %r13
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rdi
	movl	$3013644, %edx                  # imm = 0x2DFC0C
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	152(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	movq	%rax, 120(%rsp)                 # 8-byte Spill
	movq	8(%r12), %rax
	movq	%rax, 128(%rsp)                 # 8-byte Spill
	movq	(%r13), %rax
	movq	%rax, 112(%rsp)                 # 8-byte Spill
	movq	8(%r13), %rax
	movq	%rax, 104(%rsp)                 # 8-byte Spill
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movabsq	$4294967552, %rdx               # imm = 0x100000100
	leaq	2688(%rdx), %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_4
# %bb.3:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	callq	_Z29__device_stub__functionKernelPfS_
.LBB4_4:
	callq	hipDeviceSynchronize
	xorl	%ebp, %ebp
	leaq	152(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	movq	8(%r12), %rax
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	leaq	136(%rsp), %r13
	movq	(%r13), %rax
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movq	8(%r13), %rax
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%rsp), %rsi
	movl	$3013644, %edx                  # imm = 0x2DFC0C
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	movq	8(%r12), %rax
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	movq	(%r13), %rax
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	movq	8(%r13), %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movsd	.LCPI4_1(%rip), %xmm0           # xmm0 = mem[0],zero
.LBB4_5:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm1, %xmm1
	cvtss2sd	(%rbx,%rbp,4), %xmm1
	movaps	%xmm1, %xmm2
	addsd	%xmm0, %xmm2
	divsd	%xmm2, %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movaps	%xmm1, %xmm2
	mulss	%xmm1, %xmm2
	mulss	%xmm1, %xmm2
	movss	%xmm2, (%r15,%rbp,4)
	incq	%rbp
	cmpq	$753411, %rbp                   # imm = 0xB7F03
	jne	.LBB4_5
# %bb.6:                                # %_Z14functionSerialPfS_.exit
	xorl	%ebp, %ebp
	leaq	152(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %r13
	movq	8(%r12), %r12
	movq	136(%rsp), %rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movq	144(%rsp), %rax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movb	$1, %al
	movaps	.LCPI4_2(%rip), %xmm1           # xmm1 = [NaN,NaN,NaN,NaN]
	movsd	.LCPI4_3(%rip), %xmm0           # xmm0 = mem[0],zero
.LBB4_7:                                # %.backedge
                                        # =>This Inner Loop Header: Depth=1
	testb	$1, %al
	je	.LBB4_10
# %bb.8:                                #   in Loop: Header=BB4_7 Depth=1
	movss	(%r14,%rbp,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	subss	(%r15,%rbp,4), %xmm2
	andps	%xmm1, %xmm2
	cvtss2sd	%xmm2, %xmm2
	ucomisd	%xmm2, %xmm0
	seta	%al
	incq	%rbp
	cmpq	$753411, %rbp                   # imm = 0xB7F03
	jne	.LBB4_7
	jmp	.LBB4_9
.LBB4_10:                               # %.thread
                                        #   in Loop: Header=BB4_7 Depth=1
	incq	%rbp
	xorl	%eax, %eax
	cmpq	$753411, %rbp                   # imm = 0xB7F03
	jne	.LBB4_7
	jmp	.LBB4_11
.LBB4_9:
	movl	$.Lstr.1, %edi
	ucomisd	%xmm2, %xmm0
	ja	.LBB4_12
.LBB4_11:                               # %.thread71
	movl	$.Lstr, %edi
.LBB4_12:
	callq	puts@PLT
	subq	24(%rsp), %r12                  # 8-byte Folded Reload
	subq	32(%rsp), %r13                  # 8-byte Folded Reload
	imulq	$1000000, %r13, %rdi            # imm = 0xF4240
	addq	%r12, %rdi
	movabsq	$4835703278458516699, %r12      # imm = 0x431BDE82D7B634DB
	movq	%rdi, %rax
	imulq	%r12
	movq	%rdx, %r9
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$18, %r9
	addq	%rax, %r9
	movq	64(%rsp), %rax                  # 8-byte Reload
	subq	40(%rsp), %rax                  # 8-byte Folded Reload
	imulq	$1000000, %r9, %rcx             # imm = 0xF4240
	movq	56(%rsp), %rdx                  # 8-byte Reload
	subq	48(%rsp), %rdx                  # 8-byte Folded Reload
	imulq	$1000000, %rdx, %r8             # imm = 0xF4240
	addq	%rax, %r8
	movq	%r8, %rax
	imulq	%r12
	movq	%rdx, %r10
	subq	%rcx, %rdi
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$18, %r10
	addq	%rax, %r10
	imulq	$1000000, %r10, %rax            # imm = 0xF4240
	subq	%rax, %r8
	movq	96(%rsp), %rax                  # 8-byte Reload
	subq	72(%rsp), %rax                  # 8-byte Folded Reload
	movq	88(%rsp), %rcx                  # 8-byte Reload
	subq	80(%rsp), %rcx                  # 8-byte Folded Reload
	imulq	$1000000, %rcx, %rcx            # imm = 0xF4240
	addq	%rax, %rcx
	movq	%rcx, %rax
	imulq	%r12
	movq	%rdx, %rsi
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$18, %rsi
	addq	%rax, %rsi
	movq	128(%rsp), %rax                 # 8-byte Reload
	subq	104(%rsp), %rax                 # 8-byte Folded Reload
	movq	120(%rsp), %rdx                 # 8-byte Reload
	subq	112(%rsp), %rdx                 # 8-byte Folded Reload
	imulq	$1000000, %rdx, %r11            # imm = 0xF4240
	addq	%rax, %r11
	movq	%r11, %rax
	imulq	%r12
	imulq	$1000000, %rsi, %rax            # imm = 0xF4240
	subq	%rax, %rcx
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$18, %rdx
	addq	%rax, %rdx
	cvtsi2sd	%r9, %xmm1
	imulq	$1000000, %rdx, %r9             # imm = 0xF4240
	movsd	.LCPI4_4(%rip), %xmm0           # xmm0 = mem[0],zero
	cvtsi2sd	%rdi, %xmm2
	mulsd	%xmm0, %xmm1
	addsd	%xmm1, %xmm2
	cvttsd2si	%xmm2, %rax
	subq	%r9, %r11
	movq	%rax, %rdi
	sarq	$63, %rdi
	movsd	.LCPI4_5(%rip), %xmm1           # xmm1 = mem[0],zero
	subsd	%xmm1, %xmm2
	cvttsd2si	%xmm2, %r12
	andq	%rdi, %r12
	cvtsi2sd	%r10, %xmm3
	xorps	%xmm2, %xmm2
	cvtsi2sd	%r8, %xmm2
	cvtsi2sd	%rdx, %xmm4
	cvtsi2sd	%r11, %xmm5
	mulsd	%xmm0, %xmm4
	addsd	%xmm4, %xmm5
	cvttsd2si	%xmm5, %rdx
	movq	%rdx, %rdi
	subsd	%xmm1, %xmm5
	cvttsd2si	%xmm5, %r8
	sarq	$63, %rdi
	andq	%rdi, %r8
	mulsd	%xmm0, %xmm3
	addsd	%xmm3, %xmm2
	orq	%rdx, %r8
	movq	%r8, %xmm3
	movdqa	.LCPI4_6(%rip), %xmm4           # xmm4 = [1127219200,1160773632,0,0]
	punpckldq	%xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
	movdqa	%xmm4, %xmm5
	movapd	.LCPI4_7(%rip), %xmm4           # xmm4 = [4.503599627370496E+15,1.9342813113834067E+25]
	subpd	%xmm4, %xmm3
	movapd	%xmm4, %xmm6
	movapd	%xmm3, %xmm4
	unpckhpd	%xmm3, %xmm4                    # xmm4 = xmm4[1],xmm3[1]
	addsd	%xmm3, %xmm4
	addsd	%xmm2, %xmm4
	cvttsd2si	%xmm4, %rdx
	orq	%rax, %r12
	movq	%rdx, %rax
	sarq	$63, %rax
	subsd	%xmm1, %xmm4
	cvttsd2si	%xmm4, %rbp
	andq	%rax, %rbp
	orq	%rdx, %rbp
	xorps	%xmm2, %xmm2
	cvtsi2sd	%rsi, %xmm2
	xorps	%xmm3, %xmm3
	cvtsi2sd	%rcx, %xmm3
	mulsd	%xmm0, %xmm2
	addsd	%xmm2, %xmm3
	cvttsd2si	%xmm3, %rax
	movq	%rax, %rcx
	subsd	%xmm1, %xmm3
	cvttsd2si	%xmm3, %r13
	sarq	$63, %rcx
	andq	%rcx, %r13
	orq	%rax, %r13
	movq	%rbp, %rsi
	addq	%r13, %rsi
	movq	%rsi, %xmm1
	punpckldq	%xmm5, %xmm1            # xmm1 = xmm1[0],xmm5[0],xmm1[1],xmm5[1]
	subpd	%xmm6, %xmm1
	movapd	%xmm1, %xmm0
	unpckhpd	%xmm1, %xmm0                    # xmm0 = xmm0[1],xmm1[1]
	addsd	%xmm1, %xmm0
	divsd	.LCPI4_8(%rip), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	movq	%rbp, %xmm1
	punpckldq	.LCPI4_6(%rip), %xmm1   # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
	subpd	.LCPI4_7(%rip), %xmm1
	movapd	%xmm1, %xmm0
	unpckhpd	%xmm1, %xmm0                    # xmm0 = xmm0[1],xmm1[1]
	addsd	%xmm1, %xmm0
	divsd	.LCPI4_8(%rip), %xmm0
	movl	$.L.str.3, %edi
	movq	%rbp, %rsi
	movb	$1, %al
	callq	printf
	movq	%r13, %xmm1
	punpckldq	.LCPI4_6(%rip), %xmm1   # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
	subpd	.LCPI4_7(%rip), %xmm1
	movapd	%xmm1, %xmm0
	unpckhpd	%xmm1, %xmm0                    # xmm0 = xmm0[1],xmm1[1]
	addsd	%xmm1, %xmm0
	divsd	.LCPI4_8(%rip), %xmm0
	movl	$.L.str.4, %edi
	movq	%r13, %rsi
	movb	$1, %al
	callq	printf
	movq	%r12, %xmm1
	punpckldq	.LCPI4_6(%rip), %xmm1   # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
	subpd	.LCPI4_7(%rip), %xmm1
	movapd	%xmm1, %xmm0
	unpckhpd	%xmm1, %xmm0                    # xmm0 = xmm0[1],xmm1[1]
	addsd	%xmm1, %xmm0
	divsd	.LCPI4_8(%rip), %xmm0
	movl	$.L.str.5, %edi
	movq	%r12, %rsi
	movb	$1, %al
	callq	printf
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$168, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.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	$_Z14functionKernelPfS_, %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	_Z14functionKernelPfS_,@object  # @_Z14functionKernelPfS_
	.section	.rodata,"a",@progbits
	.globl	_Z14functionKernelPfS_
	.p2align	3, 0x0
_Z14functionKernelPfS_:
	.quad	_Z29__device_stub__functionKernelPfS_
	.size	_Z14functionKernelPfS_, 8

	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"GPU took %d microseconds (%.2fms)\n"
	.size	.L.str.2, 35

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"-- of which  %d microseconds (%.2fms) was spent copying memory\n"
	.size	.L.str.3, 64

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"-- of which  %d microseconds (%.2fms) was spent in the kernel\n"
	.size	.L.str.4, 63

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"CPU took a total of  %d microseconds (%.2fms)\n"
	.size	.L.str.5, 47

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14functionKernelPfS_"
	.size	.L__unnamed_1, 23

	.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	"INVALID"
	.size	.Lstr, 8

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"VALID"
	.size	.Lstr.1, 6

	.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 _Z29__device_stub__functionKernelPfS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14functionKernelPfS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14functionKernelPfS_  ; -- Begin function _Z14functionKernelPfS_
	.globl	_Z14functionKernelPfS_
	.p2align	8
	.type	_Z14functionKernelPfS_,@function
_Z14functionKernelPfS_:                 ; @_Z14functionKernelPfS_
; %bb.0:
	s_load_b32 s2, s[0:1], 0x1c
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e32 0xb7f03, v1
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mov_b32_e32 v2, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v1, vcc_lo
	s_mov_b32 s0, 0x66666666
	s_mov_b32 s1, 0xc0026666
	global_load_b32 v2, v[2:3], off
	s_waitcnt vmcnt(0)
	v_cvt_f64_f32_e32 v[2:3], v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f64 v[4:5], v[2:3], s[0:1]
	v_div_scale_f64 v[6:7], null, v[4:5], v[4:5], v[2:3]
	v_div_scale_f64 v[12:13], vcc_lo, v[2:3], v[4:5], v[2:3]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[8:9], v[6:7]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[10:11], -v[6:7], v[8:9], 1.0
	v_fma_f64 v[8:9], v[8:9], v[10:11], v[8:9]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[10:11], -v[6:7], v[8:9], 1.0
	v_fma_f64 v[8:9], v[8:9], v[10:11], v[8:9]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[10:11], v[12:13], v[8:9]
	v_fma_f64 v[6:7], -v[6:7], v[10:11], v[12:13]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f64 v[6:7], v[6:7], v[8:9], v[10:11]
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	v_div_fixup_f64 v[2:3], v[6:7], v[4:5], v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_f64_e32 v2, v[2:3]
	v_mul_f32_e32 v3, v2, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_mul_f32_e32 v2, v3, v2
	global_store_b32 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14functionKernelPfS_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.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 14
		.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	_Z14functionKernelPfS_, .Lfunc_end0-_Z14functionKernelPfS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 292
; NumSgprs: 18
; NumVgprs: 14
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 14
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

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

	.end_amdgpu_metadata
