#include <iostream>
#include <time.h>
#include <random>
#include "kernels.cuh"



int main()
{
	unsigned int n = 32;

	// variables instantiations
	int *h_x;
	int *d_x;
	int *h_root;
	int *d_root;
	int *h_child;
	int *d_child;

	// initiate memory allocation
	h_x = (int*)malloc(n*sizeof(int));
	h_root = (int*)malloc(sizeof(int));
	h_child = (int*)malloc(2*(n+1)*sizeof(int));
	cudaMalloc((void**)&d_root, sizeof(int));
	cudaMalloc((void**)&d_x, n*sizeof(int));
	cudaMalloc((void**)&d_child, 2*(n+1)*sizeof(int));
	cudaMemset(d_child, -1, 2*(n+1)*sizeof(int));


	// fill h_temp and h_x arrays
	for(unsigned int i = 0; i < n; i++){
		h_x[i] = i+1;
	}

	for(unsigned int i=0;i<n;i++){
		unsigned int j = random() % (n-i);
		int temp = h_x[i];
		h_x[i] = h_x[i+j];
		h_x[i+j] = temp;
	}
	*h_root = h_x[0];

	for(unsigned int i=0;i<n;i++){
		std::cout<<h_x[i]<<" ";
	}
	std::cout<<""<<std::endl;


	// copy data to device
	cudaMemcpy(d_root, h_root, sizeof(int), cudaMemcpyHostToDevice);
	cudaMemcpy(d_x, h_x, n*sizeof(int), cudaMemcpyHostToDevice);


	// kernel call
	dim3 gridSize = 8;
	dim3 blockSize = 8;
	build_binary_tree<<< gridSize, blockSize>>>(d_x, d_child, d_root, n);


	// copy from device back to host
	cudaMemcpy(h_child, d_child, 2*(n+1)*sizeof(int), cudaMemcpyDeviceToHost);


	// print tree
	for(unsigned int i = 0; i < 2*(n+1); i++){
		std::cout<<h_child[i]<<" ";
	}
	std::cout<<""<<std::endl;

	// free memory
	free(h_x);
	free(h_root);
	free(h_child);
	cudaFree(d_x);
	cudaFree(d_root);
	cudaFree(d_child);
}
	.file	"tmpxft_0027d788_00000000-6_main.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4722:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4722:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	" "
.LC1:
	.string	""
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB4719:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$128, %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	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$4, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movl	$264, %edi
	movq	%rax, %r13
	call	malloc@PLT
	leaq	16(%rsp), %rdi
	movl	$4, %esi
	movq	%rax, %r12
	call	cudaMalloc@PLT
	leaq	8(%rsp), %rdi
	movl	$128, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$264, %esi
	call	cudaMalloc@PLT
	movq	24(%rsp), %rdi
	movl	$264, %edx
	orl	$-1, %esi
	call	cudaMemset@PLT
	movl	$1, %eax
.L3:
	movl	%eax, -4(%rbx,%rax,4)
	incq	%rax
	cmpq	$33, %rax
	jne	.L3
	xorl	%ebp, %ebp
	movl	$32, %r14d
.L4:
	call	random@PLT
	movq	%r14, %rcx
	movl	(%rbx,%rbp,4), %esi
	subq	%rbp, %rcx
	cqto
	idivq	%rcx
	leal	(%rdx,%rbp), %eax
	leaq	(%rbx,%rax,4), %rax
	movl	(%rax), %edx
	movl	%edx, (%rbx,%rbp,4)
	incq	%rbp
	movl	%esi, (%rax)
	cmpq	$32, %rbp
	jne	.L4
	movl	(%rbx), %eax
	xorl	%r14d, %r14d
	movl	%eax, 0(%r13)
.L5:
	movl	(%rbx,%r14,4), %esi
	leaq	_ZSt4cout(%rip), %rdi
	incq	%r14
	leaq	.LC0(%rip), %r15
	call	_ZNSolsEi@PLT
	movq	%r15, %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cmpq	$32, %r14
	jne	.L5
	leaq	.LC1(%rip), %r14
	leaq	_ZSt4cout(%rip), %rdi
	movq	%r14, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%r13, %rsi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$128, %edx
	call	cudaMemcpy@PLT
	movl	$536870913, %edi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	salq	$3, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdi, %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L6
	movq	16(%rsp), %rdx
	movq	24(%rsp), %rsi
	movl	$32, %ecx
	movq	8(%rsp), %rdi
	call	_Z17build_binary_treePiS_S_j@PLT
.L6:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	xorl	%ebp, %ebp
	movl	$264, %edx
	call	cudaMemcpy@PLT
.L7:
	movl	(%r12,%rbp,4), %esi
	leaq	_ZSt4cout(%rip), %rdi
	incq	%rbp
	call	_ZNSolsEi@PLT
	movq	%r15, %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cmpq	$66, %rbp
	jne	.L7
	movq	%r14, %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L8
	call	__stack_chk_fail@PLT
.L8:
	addq	$72, %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
.LFE4719:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4745:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4745:
	.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
	.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 <iostream>
#include <time.h>
#include <random>
#include "kernels.cuh"



int main()
{
	unsigned int n = 32;

	// variables instantiations
	int *h_x;
	int *d_x;
	int *h_root;
	int *d_root;
	int *h_child;
	int *d_child;

	// initiate memory allocation
	h_x = (int*)malloc(n*sizeof(int));
	h_root = (int*)malloc(sizeof(int));
	h_child = (int*)malloc(2*(n+1)*sizeof(int));
	hipMalloc((void**)&d_root, sizeof(int));
	hipMalloc((void**)&d_x, n*sizeof(int));
	hipMalloc((void**)&d_child, 2*(n+1)*sizeof(int));
	hipMemset(d_child, -1, 2*(n+1)*sizeof(int));


	// fill h_temp and h_x arrays
	for(unsigned int i = 0; i < n; i++){
		h_x[i] = i+1;
	}

	for(unsigned int i=0;i<n;i++){
		unsigned int j = random() % (n-i);
		int temp = h_x[i];
		h_x[i] = h_x[i+j];
		h_x[i+j] = temp;
	}
	*h_root = h_x[0];

	for(unsigned int i=0;i<n;i++){
		std::cout<<h_x[i]<<" ";
	}
	std::cout<<""<<std::endl;


	// copy data to device
	hipMemcpy(d_root, h_root, sizeof(int), hipMemcpyHostToDevice);
	hipMemcpy(d_x, h_x, n*sizeof(int), hipMemcpyHostToDevice);


	// kernel call
	dim3 gridSize = 8;
	dim3 blockSize = 8;
	build_binary_tree<<< gridSize, blockSize>>>(d_x, d_child, d_root, n);


	// copy from device back to host
	hipMemcpy(h_child, d_child, 2*(n+1)*sizeof(int), hipMemcpyDeviceToHost);


	// print tree
	for(unsigned int i = 0; i < 2*(n+1); i++){
		std::cout<<h_child[i]<<" ";
	}
	std::cout<<""<<std::endl;

	// free memory
	free(h_x);
	free(h_root);
	free(h_child);
	hipFree(d_x);
	hipFree(d_root);
	hipFree(d_child);
}
	.text
	.file	"main.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$128, %edi
	callq	malloc
	movq	%rax, %rbx
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r14
	movl	$264, %edi                      # imm = 0x108
	callq	malloc
	movq	%rax, %r15
	leaq	16(%rsp), %rdi
	movl	$4, %esi
	callq	hipMalloc
	leaq	24(%rsp), %rdi
	movl	$128, %esi
	callq	hipMalloc
	leaq	8(%rsp), %r12
	movl	$264, %esi                      # imm = 0x108
	movq	%r12, %rdi
	callq	hipMalloc
	movq	(%r12), %rdi
	movl	$264, %edx                      # imm = 0x108
	movl	$-1, %esi
	callq	hipMemset
	xorl	%eax, %eax
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	leaq	1(%rax), %rcx
	movl	%ecx, (%rbx,%rax,4)
	movq	%rcx, %rax
	cmpq	$32, %rcx
	jne	.LBB0_1
# %bb.2:                                # %.preheader.preheader
	movl	$32, %r12d
	xorl	%r13d, %r13d
.LBB0_3:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	callq	random
	cqto
	idivq	%r12
	movl	(%rbx,%r13,4), %eax
	addl	%r13d, %edx
	movl	(%rbx,%rdx,4), %ecx
	movl	%ecx, (%rbx,%r13,4)
	movl	%eax, (%rbx,%rdx,4)
	incq	%r13
	decq	%r12
	jne	.LBB0_3
# %bb.4:
	movl	(%rbx), %eax
	movl	%eax, (%r14)
	xorl	%r12d, %r12d
.LBB0_5:                                # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r12,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSolsEi
	movl	$.L.str, %esi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	incq	%r12
	cmpq	$32, %r12
	jne	.LBB0_5
# %bb.6:
	movl	$_ZSt4cout, %r12d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	xorl	%edx, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	16(%rsp), %rdi
	movl	$4, %edx
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	movl	$128, %edx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$4294967304, %rdi               # imm = 0x100000008
	movl	$1, %esi
	movq	%rdi, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_8
# %bb.7:
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	16(%rsp), %rdx
	movl	$32, %ecx
	callq	_Z32__device_stub__build_binary_treePiS_S_j
.LBB0_8:
	movq	8(%rsp), %rsi
	movl	$264, %edx                      # imm = 0x108
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%r12d, %r12d
.LBB0_9:                                # =>This Inner Loop Header: Depth=1
	movl	(%r15,%r12,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSolsEi
	movl	$.L.str, %esi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	incq	%r12
	cmpq	$66, %r12
	jne	.LBB0_9
# %bb.10:
	movl	$_ZSt4cout, %r12d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	xorl	%edx, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	" "
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.zero	1
	.size	.L.str.1, 1

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

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

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
