#include <stdio.h>

#define CHECK(call)                                                            \
{                                                                              \
    const cudaError_t error = call;                                            \
    if (error != cudaSuccess)                                                  \
    {                                                                          \
        fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__);                 \
        fprintf(stderr, "code: %d, reason: %s\n", error,                       \
                cudaGetErrorString(error));                                    \
        exit(EXIT_FAILURE);                                                               \
    }                                                                          \
}

struct GpuTimer
{
    cudaEvent_t start;
    cudaEvent_t stop;

    GpuTimer()
    {
        cudaEventCreate(&start);
        cudaEventCreate(&stop);
    }

    ~GpuTimer()
    {
        cudaEventDestroy(start);
        cudaEventDestroy(stop);
    }

    void Start()
    {
        cudaEventRecord(start, 0);
    }

    void Stop()
    {
        cudaEventRecord(stop, 0);
    }

    float Elapsed()
    {
        float elapsed;
        cudaEventSynchronize(stop);
        cudaEventElapsedTime(&elapsed, start, stop);
        return elapsed;
    }
};

int reduceByHost(int * in, int n)
{    
    int s = in[0];
    for (int i = 1; i < n; i++)
        s += in[i];
    return s;
}

// Reduce within each block
// Choose the best kernel you have implemented in "bt3.cu"
__global__ void reduceBlksByDevice(int * in, int * out, int n)
{
    // TODO
    
}


// Reduce fully by device
int reduceByDevice(int * in, int n, dim3 blockSize)
{
    // TODO
    
    return 0;
}

// Reduce by device and host
int reduceByDeviceHost(int * in, int n, dim3 blockSize)
{
    // Allocate device memories
    int *d_in, *d_out;
    int bytes = n * sizeof(int);
    CHECK(cudaMalloc(&d_in, bytes));
    dim3 gridSize((n - 1) / (2 * blockSize.x) + 1);
    CHECK(cudaMalloc(&d_out, gridSize.x * sizeof(int)));
    
    // Copy data to device memories
    CHECK(cudaMemcpy(d_in, in, bytes, cudaMemcpyHostToDevice));

    // Invoke kernel function
    reduceBlksByDevice<<<gridSize, blockSize>>>(d_in, d_out, n);
    cudaDeviceSynchronize();
    CHECK(cudaGetLastError());

    // Copy temporary result from device memories
    int * out = (int *)malloc(gridSize.x * sizeof(int));
    CHECK(cudaMemcpy(out, d_out, gridSize.x * sizeof(int), cudaMemcpyDeviceToHost));

    // (Host) Do the remaining work
    int final_sum = 0;
    for (int i = 0; i < gridSize.x; i++) 
        final_sum += out[i];

    // Free device memories
    CHECK(cudaFree(d_in));
    CHECK(cudaFree(d_out));

    return final_sum;  
}

int main(int argc, char ** argv)
{
    // Print out device info
    cudaDeviceProp devProv;
    CHECK(cudaGetDeviceProperties(&devProv, 0));
    printf("**********GPU info**********\n");
    printf("Name: ...\n"); // TODO
    printf("Compute capability: ...\n"); // TODO
    printf("Num SMs: ...\n"); // TODO
    printf("Max num threads per SM: ...\n"); // TODO
    printf("Max num warps per SM: ...\n"); // TODO
    printf("****************************\n\n");

    // Set up input size
    int n = (1 << 24) + 1;
    printf("Input size: %d\n", n);

    // Set up block size
    dim3 blockSize(256); // Default
    if (argc == 2) // Get block size from cmd argument
        blockSize.x = atoi(argv[1]);

    // Set up input data
    size_t bytes = n * sizeof(int);
    int * in = (int *) malloc(bytes);
    for (int i = 0; i < n; i++)
    {
        // Generate a random integer in [0, 255]
        in[i] = (int)(rand() & 0xFF);
    }

    // Reduce by host
    int host_sum = reduceByHost(in, n);
    
    // Reduce by device-host
    printf("\nreduceByDeviceHost ...\n");
    GpuTimer timer;
    timer.Start();
    int devhost_sum = reduceByDeviceHost(in, n, blockSize);
    timer.Stop();
    printf("Time of reduceByDeviceHost: %.3f ms\n", timer.Elapsed());
    if (devhost_sum != host_sum)
        fprintf(stderr, "Error: reduceByDeviceHost is incorrect!\n");

    // Reduce by device
    printf("\nreduceByDevice ...\n");
    timer.Start();
    int dev_sum = reduceByDevice(in, n, blockSize);
    timer.Stop();
    printf("Time of reduceByDevice    : %.3f ms\n", timer.Elapsed());
    if (dev_sum != host_sum)
        fprintf(stderr, "Error: reduceByDevice is incorrect!\n");

    // Free memories
    free(in);
    
    return EXIT_SUCCESS;
}
	.file	"tmpxft_003b754b_00000000-6_bt3_p2.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2042:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2042:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.text._ZN8GpuTimer7ElapsedEv,"axG",@progbits,_ZN8GpuTimer7ElapsedEv,comdat
	.align 2
	.weak	_ZN8GpuTimer7ElapsedEv
	.type	_ZN8GpuTimer7ElapsedEv, @function
_ZN8GpuTimer7ElapsedEv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	8(%rdi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	cudaEventSynchronize@PLT
	movq	8(%rbx), %rdx
	movq	(%rbx), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movss	4(%rsp), %xmm0
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	_ZN8GpuTimer7ElapsedEv, .-_ZN8GpuTimer7ElapsedEv
	.text
	.globl	_Z12reduceByHostPii
	.type	_Z12reduceByHostPii, @function
_Z12reduceByHostPii:
.LFB2036:
	.cfi_startproc
	endbr64
	movl	(%rdi), %edx
	xorl	%eax, %eax
.L7:
	incq	%rax
	cmpl	%eax, %esi
	jle	.L9
	addl	(%rdi,%rax,4), %edx
	jmp	.L7
.L9:
	movl	%edx, %eax
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z12reduceByHostPii, .-_Z12reduceByHostPii
	.globl	_Z14reduceByDevicePii4dim3
	.type	_Z14reduceByDevicePii4dim3, @function
_Z14reduceByDevicePii4dim3:
.LFB2037:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2037:
	.size	_Z14reduceByDevicePii4dim3, .-_Z14reduceByDevicePii4dim3
	.globl	_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i
	.type	_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i, @function
_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i:
.LFB2064:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%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	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%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	.L11
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z18reduceBlksByDevicePiS_i(%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
.L11:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2064:
	.size	_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i, .-_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i
	.globl	_Z18reduceBlksByDevicePiS_i
	.type	_Z18reduceBlksByDevicePiS_i, @function
_Z18reduceBlksByDevicePiS_i:
.LFB2065:
	.cfi_startproc
	endbr64
	jmp	_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i
	.cfi_endproc
.LFE2065:
	.size	_Z18reduceBlksByDevicePiS_i, .-_Z18reduceBlksByDevicePiS_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/TruongNgocTai/TH_03/master/bt3_p2.cu"
.LC1:
	.string	"Error: %s:%d, "
.LC2:
	.string	"code: %d, reason: %s\n"
	.text
	.globl	_Z18reduceByDeviceHostPii4dim3
	.type	_Z18reduceByDeviceHostPii4dim3, @function
_Z18reduceByDeviceHostPii4dim3:
.LFB2038:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	leal	0(,%rsi,4), %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movslq	%r14d, %r14
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	movq	%r14, %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdx, (%rsp)
	leaq	24(%rsp), %rdi
	movl	%ecx, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L17
	movl	%eax, %ebx
	movl	$82, %r8d
	jmp	.L33
.L17:
	movl	(%rsp), %ebx
	leal	-1(%r12), %eax
	xorl	%edx, %edx
	leaq	32(%rsp), %rdi
	movl	$1, 48(%rsp)
	leal	(%rbx,%rbx), %ecx
	movl	$1, 52(%rsp)
	divl	%ecx
	leal	1(%rax), %ebp
	movq	%rbp, %rbx
	salq	$2, %rbp
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %r13d
	testl	%eax, %eax
	je	.L18
	movl	$84, %r8d
.L32:
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%r13d, %edi
	call	cudaGetErrorString@PLT
	movl	%r13d, %ecx
	movq	%rax, %r8
.L31:
	movq	stderr(%rip), %rdi
	leaq	.LC2(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L18:
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%r14, %rdx
	movq	%r15, %rsi
	call	cudaMemcpy@PLT
	movl	$87, %r8d
	movl	%eax, %r13d
	testl	%eax, %eax
	jne	.L32
	movl	%ebx, 44(%rsp)
	movl	8(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	(%rsp), %rdx
	movq	44(%rsp), %rdi
	movl	52(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L20
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	%r12d, %edx
	call	_Z41__device_stub__Z18reduceBlksByDevicePiS_iPiS_i
.L20:
	call	cudaDeviceSynchronize@PLT
	call	cudaGetLastError@PLT
	movl	$92, %r8d
	movl	%eax, %r12d
	testl	%eax, %eax
	jne	.L34
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	32(%rsp), %rsi
	movq	%rbp, %rdx
	movl	$2, %ecx
	movq	%rax, %rdi
	movq	%rax, %r13
	xorl	%ebp, %ebp
	call	cudaMemcpy@PLT
	xorl	%edx, %edx
	movl	%eax, %r12d
	testl	%eax, %eax
	je	.L22
	movl	$96, %r8d
.L34:
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%r12d, %edi
	call	cudaGetErrorString@PLT
	movl	%r12d, %ecx
	movq	%rax, %r8
	jmp	.L31
.L22:
	addl	0(%r13,%rdx,4), %ebp
	incq	%rdx
	cmpl	%ebx, %edx
	jb	.L22
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movl	$104, %r8d
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L33
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L16
	movl	$105, %r8d
.L33:
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebx, %edi
	call	cudaGetErrorString@PLT
	movl	%ebx, %ecx
	movq	%rax, %r8
	jmp	.L31
.L16:
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	movl	%ebp, %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
.LFE2038:
	.size	_Z18reduceByDeviceHostPii4dim3, .-_Z18reduceByDeviceHostPii4dim3
	.section	.rodata.str1.1
.LC3:
	.string	"**********GPU info**********\n"
.LC4:
	.string	"Name: ...\n"
.LC5:
	.string	"Compute capability: ...\n"
.LC6:
	.string	"Num SMs: ...\n"
.LC7:
	.string	"Max num threads per SM: ...\n"
.LC8:
	.string	"Max num warps per SM: ...\n"
.LC9:
	.string	"****************************\n\n"
.LC10:
	.string	"Input size: %d\n"
.LC11:
	.string	"\nreduceByDeviceHost ...\n"
.LC12:
	.string	"Time of reduceByDeviceHost: %.3f ms\n"
.LC13:
	.string	"\nreduceByDevice ...\n"
.LC14:
	.string	"Error: reduceByDeviceHost is incorrect!\n"
.LC15:
	.string	"Time of reduceByDevice    : %.3f ms\n"
.LC16:
	.string	"Error: reduceByDevice is incorrect!\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2039:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2039
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%edi, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rsi, %rbp
	xorl	%esi, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$1080, %rsp
	.cfi_def_cfa_offset 1120
	movq	%fs:40, %rax
	movq	%rax, 1064(%rsp)
	xorl	%eax, %eax
	leaq	32(%rsp), %rdi
.LEHB0:
	call	cudaGetDeviceProperties_v2@PLT
	testl	%eax, %eax
	je	.L36
	movq	stderr(%rip), %rdi
	movl	%eax, %ebx
	xorl	%eax, %eax
	movl	$114, %r8d
	leaq	.LC0(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebx, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	%ebx, %ecx
	leaq	.LC2(%rip), %rdx
	movq	%rax, %r8
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L36:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movl	$256, %r12d
	call	__printf_chk@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$16777217, %edx
	leaq	.LC10(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	cmpl	$2, %r13d
	jne	.L37
	movq	8(%rbp), %rdi
	call	atoi@PLT
	movl	%eax, %r12d
.L37:
	movl	$67108868, %edi
	xorl	%ebp, %ebp
	call	malloc@PLT
	movq	%rax, %rbx
.L38:
	call	rand@PLT
	movzbl	%al, %eax
	movl	%eax, (%rbx,%rbp,4)
	incq	%rbp
	cmpq	$16777217, %rbp
	jne	.L38
	movl	$16777217, %esi
	movq	%rbx, %rdi
	leaq	16(%rsp), %r13
	call	_Z12reduceByHostPii
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	movl	%eax, %ebp
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	cudaEventCreate@PLT
	leaq	24(%rsp), %rdi
	call	cudaEventCreate@PLT
.LEHE0:
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
.LEHB1:
	call	cudaEventRecord@PLT
	movl	%r12d, 4(%rsp)
	movl	$16777217, %esi
	movq	%rbx, %rdi
	movabsq	$4294967297, %rax
	movq	%rax, 8(%rsp)
	movl	12(%rsp), %ecx
	movq	4(%rsp), %rdx
	call	_Z18reduceByDeviceHostPii4dim3
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	movl	%eax, %r12d
	call	cudaEventRecord@PLT
	movq	%r13, %rdi
	call	_ZN8GpuTimer7ElapsedEv
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	cmpl	%r12d, %ebp
	jne	.L39
.L41:
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L53
.L39:
	movq	stderr(%rip), %rdi
	leaq	.LC14(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L41
.L53:
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	%r13, %rdi
	call	_ZN8GpuTimer7ElapsedEv
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
.LEHE1:
	testl	%ebp, %ebp
	jne	.L42
.L43:
	movq	%rbx, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	24(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	1064(%rsp), %rax
	subq	%fs:40, %rax
	je	.L46
	jmp	.L51
.L42:
	movq	stderr(%rip), %rdi
	leaq	.LC16(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
.LEHB2:
	call	__fprintf_chk@PLT
.LEHE2:
	jmp	.L43
.L48:
	endbr64
	movq	%rax, %rbx
.L44:
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	24(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	1064(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L51
	movq	%rbx, %rdi
.LEHB3:
	call	_Unwind_Resume@PLT
.LEHE3:
.L51:
	call	__stack_chk_fail@PLT
.L46:
	addq	$1080, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2039:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2039:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2039-.LLSDACSB2039
.LLSDACSB2039:
	.uleb128 .LEHB0-.LFB2039
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB2039
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L48-.LFB2039
	.uleb128 0
	.uleb128 .LEHB2-.LFB2039
	.uleb128 .LEHE2-.LEHB2
	.uleb128 .L48-.LFB2039
	.uleb128 0
	.uleb128 .LEHB3-.LFB2039
	.uleb128 .LEHE3-.LEHB3
	.uleb128 0
	.uleb128 0
.LLSDACSE2039:
	.section	.text.startup
	.size	main, .-main
	.section	.rodata.str1.1
.LC17:
	.string	"_Z18reduceBlksByDevicePiS_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2067:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC17(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z18reduceBlksByDevicePiS_i(%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
.LFE2067:
	.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
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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>

#define CHECK(call)                                                            \
{                                                                              \
    const hipError_t error = call;                                            \
    if (error != hipSuccess)                                                  \
    {                                                                          \
        fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__);                 \
        fprintf(stderr, "code: %d, reason: %s\n", error,                       \
                hipGetErrorString(error));                                    \
        exit(EXIT_FAILURE);                                                               \
    }                                                                          \
}

struct GpuTimer
{
    hipEvent_t start;
    hipEvent_t stop;

    GpuTimer()
    {
        hipEventCreate(&start);
        hipEventCreate(&stop);
    }

    ~GpuTimer()
    {
        hipEventDestroy(start);
        hipEventDestroy(stop);
    }

    void Start()
    {
        hipEventRecord(start, 0);
    }

    void Stop()
    {
        hipEventRecord(stop, 0);
    }

    float Elapsed()
    {
        float elapsed;
        hipEventSynchronize(stop);
        hipEventElapsedTime(&elapsed, start, stop);
        return elapsed;
    }
};

int reduceByHost(int * in, int n)
{    
    int s = in[0];
    for (int i = 1; i < n; i++)
        s += in[i];
    return s;
}

// Reduce within each block
// Choose the best kernel you have implemented in "bt3.cu"
__global__ void reduceBlksByDevice(int * in, int * out, int n)
{
    // TODO
    
}


// Reduce fully by device
int reduceByDevice(int * in, int n, dim3 blockSize)
{
    // TODO
    
    return 0;
}

// Reduce by device and host
int reduceByDeviceHost(int * in, int n, dim3 blockSize)
{
    // Allocate device memories
    int *d_in, *d_out;
    int bytes = n * sizeof(int);
    CHECK(hipMalloc(&d_in, bytes));
    dim3 gridSize((n - 1) / (2 * blockSize.x) + 1);
    CHECK(hipMalloc(&d_out, gridSize.x * sizeof(int)));
    
    // Copy data to device memories
    CHECK(hipMemcpy(d_in, in, bytes, hipMemcpyHostToDevice));

    // Invoke kernel function
    reduceBlksByDevice<<<gridSize, blockSize>>>(d_in, d_out, n);
    hipDeviceSynchronize();
    CHECK(hipGetLastError());

    // Copy temporary result from device memories
    int * out = (int *)malloc(gridSize.x * sizeof(int));
    CHECK(hipMemcpy(out, d_out, gridSize.x * sizeof(int), hipMemcpyDeviceToHost));

    // (Host) Do the remaining work
    int final_sum = 0;
    for (int i = 0; i < gridSize.x; i++) 
        final_sum += out[i];

    // Free device memories
    CHECK(hipFree(d_in));
    CHECK(hipFree(d_out));

    return final_sum;  
}

int main(int argc, char ** argv)
{
    // Print out device info
    hipDeviceProp_t devProv;
    CHECK(hipGetDeviceProperties(&devProv, 0));
    printf("**********GPU info**********\n");
    printf("Name: ...\n"); // TODO
    printf("Compute capability: ...\n"); // TODO
    printf("Num SMs: ...\n"); // TODO
    printf("Max num threads per SM: ...\n"); // TODO
    printf("Max num warps per SM: ...\n"); // TODO
    printf("****************************\n\n");

    // Set up input size
    int n = (1 << 24) + 1;
    printf("Input size: %d\n", n);

    // Set up block size
    dim3 blockSize(256); // Default
    if (argc == 2) // Get block size from cmd argument
        blockSize.x = atoi(argv[1]);

    // Set up input data
    size_t bytes = n * sizeof(int);
    int * in = (int *) malloc(bytes);
    for (int i = 0; i < n; i++)
    {
        // Generate a random integer in [0, 255]
        in[i] = (int)(rand() & 0xFF);
    }

    // Reduce by host
    int host_sum = reduceByHost(in, n);
    
    // Reduce by device-host
    printf("\nreduceByDeviceHost ...\n");
    GpuTimer timer;
    timer.Start();
    int devhost_sum = reduceByDeviceHost(in, n, blockSize);
    timer.Stop();
    printf("Time of reduceByDeviceHost: %.3f ms\n", timer.Elapsed());
    if (devhost_sum != host_sum)
        fprintf(stderr, "Error: reduceByDeviceHost is incorrect!\n");

    // Reduce by device
    printf("\nreduceByDevice ...\n");
    timer.Start();
    int dev_sum = reduceByDevice(in, n, blockSize);
    timer.Stop();
    printf("Time of reduceByDevice    : %.3f ms\n", timer.Elapsed());
    if (dev_sum != host_sum)
        fprintf(stderr, "Error: reduceByDevice is incorrect!\n");

    // Free memories
    free(in);
    
    return EXIT_SUCCESS;
}
	.text
	.file	"bt3_p2.hip"
	.globl	_Z12reduceByHostPii             # -- Begin function _Z12reduceByHostPii
	.type	_Z12reduceByHostPii,@function
_Z12reduceByHostPii:                    # @_Z12reduceByHostPii
	.cfi_startproc
# %bb.0:
	movl	(%rdi), %eax
	cmpl	$2, %esi
	jl	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %ecx
	movl	$1, %edx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	addl	(%rdi,%rdx,4), %eax
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z12reduceByHostPii, .Lfunc_end0-_Z12reduceByHostPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z33__device_stub__reduceBlksByDevicePiS_i # -- Begin function _Z33__device_stub__reduceBlksByDevicePiS_i
	.type	_Z33__device_stub__reduceBlksByDevicePiS_i,@function
_Z33__device_stub__reduceBlksByDevicePiS_i: # @_Z33__device_stub__reduceBlksByDevicePiS_i
	.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	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%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	$_Z18reduceBlksByDevicePiS_i, %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_end1:
	.size	_Z33__device_stub__reduceBlksByDevicePiS_i, .Lfunc_end1-_Z33__device_stub__reduceBlksByDevicePiS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z14reduceByDevicePii4dim3      # -- Begin function _Z14reduceByDevicePii4dim3
	.type	_Z14reduceByDevicePii4dim3,@function
_Z14reduceByDevicePii4dim3:             # @_Z14reduceByDevicePii4dim3
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
	retq
.Lfunc_end2:
	.size	_Z14reduceByDevicePii4dim3, .Lfunc_end2-_Z14reduceByDevicePii4dim3
	.cfi_endproc
                                        # -- End function
	.globl	_Z18reduceByDeviceHostPii4dim3  # -- Begin function _Z18reduceByDeviceHostPii4dim3
	.type	_Z18reduceByDeviceHostPii4dim3,@function
_Z18reduceByDeviceHostPii4dim3:         # @_Z18reduceByDeviceHostPii4dim3
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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	%ecx, 28(%rsp)                  # 4-byte Spill
	movq	%rdx, %r12
	movl	%esi, %r14d
	movq	%rdi, 32(%rsp)                  # 8-byte Spill
	leal	(,%r14,4), %eax
	movslq	%eax, %rbp
	leaq	16(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_1
# %bb.4:
	leal	-1(%r14), %eax
	leal	(%r12,%r12), %ecx
	xorl	%edx, %edx
	divl	%ecx
	movl	%eax, %ebx
	incl	%ebx
	leaq	(,%rbx,4), %r15
	leaq	8(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_5
# %bb.6:
	movq	16(%rsp), %rdi
	movq	32(%rsp), %rsi                  # 8-byte Reload
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_7
# %bb.9:
	movq	%rbx, %rdi
	btsq	$32, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	28(%rsp), %ecx                  # 4-byte Reload
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_11
# %bb.10:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	%r14d, %edx
	callq	_Z33__device_stub__reduceBlksByDevicePiS_i
.LBB3_11:
	callq	hipDeviceSynchronize
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB3_12
# %bb.13:
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	8(%rsp), %rsi
	movq	%rax, %rdi
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_23
# %bb.14:                               # %.preheader
	testq	%rbx, %rbx
	je	.LBB3_15
# %bb.18:                               # %.lr.ph.preheader
	xorl	%eax, %eax
	xorl	%ebp, %ebp
.LBB3_19:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	addl	(%r14,%rax,4), %ebp
	incq	%rax
	cmpq	%rbx, %rax
	jb	.LBB3_19
	jmp	.LBB3_16
.LBB3_15:
	xorl	%ebp, %ebp
.LBB3_16:                               # %._crit_edge
	movq	16(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB3_17
# %bb.20:
	movq	8(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB3_21
# %bb.22:
	movl	%ebp, %eax
	addq	$40, %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
.LBB3_1:
	.cfi_def_cfa_offset 96
	movl	%eax, %ebx
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$84, %ecx
	jmp	.LBB3_2
.LBB3_5:
	movl	%eax, %r13d
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$86, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
	movl	%r13d, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	movl	%r13d, %edx
	jmp	.LBB3_3
.LBB3_7:
	movl	%eax, %ebp
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$89, %ecx
	jmp	.LBB3_8
.LBB3_12:
	movl	%eax, %ebp
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$94, %ecx
	jmp	.LBB3_8
.LBB3_23:
	movl	%eax, %ebp
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$98, %ecx
.LBB3_8:
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
	movl	%ebp, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	movl	%ebp, %edx
	jmp	.LBB3_3
.LBB3_17:
	movl	%eax, %ebx
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$106, %ecx
	jmp	.LBB3_2
.LBB3_21:
	movl	%eax, %ebx
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$107, %ecx
.LBB3_2:
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %r14
	movl	%ebx, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %esi
	movq	%r14, %rdi
	movl	%ebx, %edx
.LBB3_3:
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z18reduceByDeviceHostPii4dim3, .Lfunc_end3-_Z18reduceByDeviceHostPii4dim3
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$1504, %rsp                     # imm = 0x5E0
	.cfi_def_cfa_offset 1552
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	leaq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	testl	%eax, %eax
	jne	.LBB4_24
# %bb.1:
	movabsq	$4294967296, %r14               # imm = 0x100000000
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	movl	$.Lstr.5, %edi
	callq	puts@PLT
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	movl	$.L.str.10, %edi
	movl	$16777217, %esi                 # imm = 0x1000001
	xorl	%eax, %eax
	callq	printf
	cmpl	$2, %ebp
	jne	.LBB4_2
# %bb.3:
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, %eax
	orq	%rax, %r14
	jmp	.LBB4_4
.LBB4_2:
	orq	$256, %r14                      # imm = 0x100
.LBB4_4:
	movl	$67108868, %edi                 # imm = 0x4000004
	callq	malloc
	movq	%rax, %rbx
	xorl	%r15d, %r15d
.LBB4_5:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	movzbl	%al, %eax
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpq	$16777217, %r15                 # imm = 0x1000001
	jne	.LBB4_5
# %bb.6:
	movl	(%rbx), %r12d
	movl	$1, %eax
.LBB4_7:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	addl	(%rbx,%rax,4), %r12d
	incq	%rax
	cmpq	$16777217, %rax                 # imm = 0x1000001
	jne	.LBB4_7
# %bb.8:                                # %_Z12reduceByHostPii.exit
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	movq	-8(%r15), %rdi
.Ltmp0:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp1:
# %bb.9:                                # %_ZN8GpuTimer5StartEv.exit
.Ltmp3:
	movq	%rbx, %rdi
	movl	$16777217, %esi                 # imm = 0x1000001
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	_Z18reduceByDeviceHostPii4dim3
.Ltmp4:
# %bb.10:
	movl	%eax, %ebp
	movq	24(%rsp), %rdi
.Ltmp5:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp6:
# %bb.11:                               # %_ZN8GpuTimer4StopEv.exit
	movq	24(%rsp), %rdi
.Ltmp7:
	callq	hipEventSynchronize
.Ltmp8:
# %bb.12:                               # %.noexc
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdx
.Ltmp9:
	leaq	12(%rsp), %rdi
	callq	hipEventElapsedTime
.Ltmp10:
# %bb.13:
	cvtss2sd	12(%rsp), %xmm0
	movl	$.L.str.12, %edi
	movb	$1, %al
	callq	printf
	cmpl	%r12d, %ebp
	jne	.LBB4_14
.LBB4_15:
	movl	$.Lstr.8, %edi
	callq	puts@PLT
	movq	16(%rsp), %rdi
.Ltmp11:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp12:
# %bb.16:                               # %_ZN8GpuTimer5StartEv.exit50
	movq	24(%rsp), %rdi
.Ltmp14:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp15:
# %bb.17:                               # %_ZN8GpuTimer4StopEv.exit52
	movq	24(%rsp), %rdi
.Ltmp16:
	callq	hipEventSynchronize
.Ltmp17:
# %bb.18:                               # %.noexc53
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdx
.Ltmp18:
	leaq	12(%rsp), %rdi
	callq	hipEventElapsedTime
.Ltmp19:
# %bb.19:
	xorps	%xmm0, %xmm0
	cvtss2sd	12(%rsp), %xmm0
	movl	$.L.str.15, %edi
	movb	$1, %al
	callq	printf
	testl	%r12d, %r12d
	jne	.LBB4_20
.LBB4_21:
	movq	%rbx, %rdi
	callq	free
	leaq	16(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	xorl	%eax, %eax
	addq	$1504, %rsp                     # imm = 0x5E0
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB4_14:
	.cfi_def_cfa_offset 1552
	movq	stderr(%rip), %rcx
	movl	$.L.str.13, %edi
	movl	$40, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB4_15
.LBB4_20:
	movq	stderr(%rip), %rcx
	movl	$.L.str.16, %edi
	movl	$36, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB4_21
.LBB4_24:
	movl	%eax, %r14d
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	movl	$.L.str.1, %edx
	movl	$116, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
	movl	%r14d, %edi
	callq	hipGetErrorString
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	movl	%r14d, %edx
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.LBB4_22:
.Ltmp2:
	jmp	.LBB4_23
.LBB4_26:
.Ltmp20:
	jmp	.LBB4_23
.LBB4_25:
.Ltmp13:
.LBB4_23:
	movq	%rax, %rbx
	leaq	16(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table4:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp1-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp1
	.uleb128 .Ltmp2-.Lfunc_begin0           #     jumps to .Ltmp2
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp3-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp12-.Ltmp3                 #   Call between .Ltmp3 and .Ltmp12
	.uleb128 .Ltmp13-.Lfunc_begin0          #     jumps to .Ltmp13
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp14-.Lfunc_begin0          # >> Call Site 4 <<
	.uleb128 .Ltmp19-.Ltmp14                #   Call between .Ltmp14 and .Ltmp19
	.uleb128 .Ltmp20-.Lfunc_begin0          #     jumps to .Ltmp20
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp19-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Lfunc_end4-.Ltmp19            #   Call between .Ltmp19 and .Lfunc_end4
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text._ZN8GpuTimerD2Ev,"axG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.weak	_ZN8GpuTimerD2Ev                # -- Begin function _ZN8GpuTimerD2Ev
	.p2align	1, 0x90
	.type	_ZN8GpuTimerD2Ev,@function
_ZN8GpuTimerD2Ev:                       # @_ZN8GpuTimerD2Ev
.Lfunc_begin1:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception1
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
.Ltmp21:
	callq	hipEventDestroy
.Ltmp22:
# %bb.1:
	movq	8(%rbx), %rdi
.Ltmp23:
	callq	hipEventDestroy
.Ltmp24:
# %bb.2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB5_3:
	.cfi_def_cfa_offset 16
.Ltmp25:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end5:
	.size	_ZN8GpuTimerD2Ev, .Lfunc_end5-_ZN8GpuTimerD2Ev
	.cfi_endproc
	.section	.gcc_except_table._ZN8GpuTimerD2Ev,"aG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.p2align	2, 0x0
GCC_except_table5:
.Lexception1:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end1-.Lcst_begin1
.Lcst_begin1:
	.uleb128 .Ltmp21-.Lfunc_begin1          # >> Call Site 1 <<
	.uleb128 .Ltmp24-.Ltmp21                #   Call between .Ltmp21 and .Ltmp24
	.uleb128 .Ltmp25-.Lfunc_begin1          #     jumps to .Ltmp25
	.byte	1                               #   On action: 1
.Lcst_end1:
	.byte	1                               # >> Action Record 1 <<
                                        #   Catch TypeInfo 1
	.byte	0                               #   No further actions
	.p2align	2, 0x0
                                        # >> Catch TypeInfos <<
	.long	0                               # TypeInfo 1
.Lttbase0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text.__clang_call_terminate,"axG",@progbits,__clang_call_terminate,comdat
	.hidden	__clang_call_terminate          # -- Begin function __clang_call_terminate
	.weak	__clang_call_terminate
	.type	__clang_call_terminate,@function
__clang_call_terminate:                 # @__clang_call_terminate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__cxa_begin_catch
	callq	_ZSt9terminatev
.Lfunc_end6:
	.size	__clang_call_terminate, .Lfunc_end6-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.text
	.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	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z18reduceBlksByDevicePiS_i, %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_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__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	.LBB8_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
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z18reduceBlksByDevicePiS_i,@object # @_Z18reduceBlksByDevicePiS_i
	.section	.rodata,"a",@progbits
	.globl	_Z18reduceBlksByDevicePiS_i
	.p2align	3, 0x0
_Z18reduceBlksByDevicePiS_i:
	.quad	_Z33__device_stub__reduceBlksByDevicePiS_i
	.size	_Z18reduceBlksByDevicePiS_i, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error: %s:%d, "
	.size	.L.str, 15

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/TruongNgocTai/TH_03/master/bt3_p2.hip"
	.size	.L.str.1, 100

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"code: %d, reason: %s\n"
	.size	.L.str.2, 22

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Input size: %d\n"
	.size	.L.str.10, 16

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Time of reduceByDeviceHost: %.3f ms\n"
	.size	.L.str.12, 37

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Error: reduceByDeviceHost is incorrect!\n"
	.size	.L.str.13, 41

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Time of reduceByDevice    : %.3f ms\n"
	.size	.L.str.15, 37

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Error: reduceByDevice is incorrect!\n"
	.size	.L.str.16, 37

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z18reduceBlksByDevicePiS_i"
	.size	.L__unnamed_1, 28

	.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	"**********GPU info**********"
	.size	.Lstr, 29

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Name: ..."
	.size	.Lstr.1, 10

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Compute capability: ..."
	.size	.Lstr.2, 24

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Num SMs: ..."
	.size	.Lstr.3, 13

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"Max num threads per SM: ..."
	.size	.Lstr.4, 28

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Max num warps per SM: ..."
	.size	.Lstr.5, 26

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"****************************\n"
	.size	.Lstr.6, 30

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"\nreduceByDeviceHost ..."
	.size	.Lstr.7, 24

	.type	.Lstr.8,@object                 # @str.8
.Lstr.8:
	.asciz	"\nreduceByDevice ..."
	.size	.Lstr.8, 20

	.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 _Z33__device_stub__reduceBlksByDevicePiS_i
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z18reduceBlksByDevicePiS_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z18reduceBlksByDevicePiS_i ; -- Begin function _Z18reduceBlksByDevicePiS_i
	.globl	_Z18reduceBlksByDevicePiS_i
	.p2align	8
	.type	_Z18reduceBlksByDevicePiS_i,@function
_Z18reduceBlksByDevicePiS_i:            ; @_Z18reduceBlksByDevicePiS_i
; %bb.0:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z18reduceBlksByDevicePiS_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 20
		.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 1
		.amdhsa_next_free_sgpr 1
		.amdhsa_reserve_vcc 0
		.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	_Z18reduceBlksByDevicePiS_i, .Lfunc_end0-_Z18reduceBlksByDevicePiS_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 4
; NumSgprs: 0
; NumVgprs: 0
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 0
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 1
; NumVGPRsForWavesPerEU: 1
; 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:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 20
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z18reduceBlksByDevicePiS_i
    .private_segment_fixed_size: 0
    .sgpr_count:     0
    .sgpr_spill_count: 0
    .symbol:         _Z18reduceBlksByDevicePiS_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     0
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
