#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cuda.h>

#include <cuda_runtime.h>
#include <cuda.h>
#include <device_launch_parameters.h>

#define LIST_SIZE 100000
extern "C" __device__ long long instCountList[LIST_SIZE];
extern "C" __device__ unsigned long long record_flag;

void bambooLogRecordOff(){

    long long local_record = 0;

    cudaMemcpyToSymbol(record_flag, &local_record, sizeof(long long), 0, cudaMemcpyHostToDevice);
}

void bambooLogKernelBegin(long long i) {

    i = 1;

    cudaMemcpyToSymbol(record_flag, &i, sizeof(long long), 0, cudaMemcpyHostToDevice);
}

void bambooLogKernelEnd() {

#ifdef KERNELTRACE
    cudaDeviceSynchronize();
#endif
    
    long long resultArray[LIST_SIZE] = {0};
    cudaMemcpyFromSymbol(resultArray, instCountList, LIST_SIZE * sizeof(long long), 0, cudaMemcpyDeviceToHost);    
    
    FILE *profileFile = fopen("instCountResult.txt", "w");
    for(long long i=0; i<LIST_SIZE; i++){
        if(resultArray[i] != 0){
            fprintf(profileFile, "%lld: %lld\n", i, resultArray[i]);
        }
    }
    fclose(profileFile);
}

	.file	"tmpxft_0035a9a7_00000000-6_record_data.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z18bambooLogRecordOffv
	.type	_Z18bambooLogRecordOffv, @function
_Z18bambooLogRecordOffv:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	xorl	%ecx, %ecx
	movl	$1, %r8d
	movl	$8, %edx
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rsi
	xorl	%eax, %eax
	leaq	record_flag(%rip), %rdi
	movq	%rax, (%rsp)
	call	cudaMemcpyToSymbol@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z18bambooLogRecordOffv, .-_Z18bambooLogRecordOffv
	.globl	_Z20bambooLogKernelBeginx
	.type	_Z20bambooLogKernelBeginx, @function
_Z20bambooLogKernelBeginx:
.LFB2028:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, %r8d
	xorl	%ecx, %ecx
	movl	$8, %edx
	leaq	8(%rsp), %rsi
	leaq	record_flag(%rip), %rdi
	movq	$1, 8(%rsp)
	call	cudaMemcpyToSymbol@PLT
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z20bambooLogKernelBeginx, .-_Z20bambooLogKernelBeginx
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"w"
.LC1:
	.string	"instCountResult.txt"
.LC2:
	.string	"%lld: %lld\n"
	.text
	.globl	_Z18bambooLogKernelEndv
	.type	_Z18bambooLogKernelEndv, @function
_Z18bambooLogKernelEndv:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leaq	-798720(%rsp), %r11
	.cfi_def_cfa 11, 798760
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$1304, %rsp
	.cfi_def_cfa_offset 800064
	movl	$200000, %ecx
	movl	$2, %r8d
	xorl	%ebx, %ebx
	movl	$800000, %edx
	leaq	instCountList(%rip), %rsi
	leaq	.LC2(%rip), %r13
	movq	%fs:40, %rax
	movq	%rax, 800008(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	leaq	8(%rsp), %r12
	rep stosl
	movq	%r12, %rdi
	call	cudaMemcpyFromSymbol@PLT
	leaq	.LC0(%rip), %rsi
	leaq	.LC1(%rip), %rdi
	call	fopen@PLT
	movq	%rax, %rbp
.L10:
	movq	(%r12,%rbx,8), %r8
	testq	%r8, %r8
	je	.L9
	movq	%rbx, %rcx
	movq	%r13, %rdx
	movl	$2, %esi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L9:
	incq	%rbx
	cmpq	$100000, %rbx
	jne	.L10
	movq	%rbp, %rdi
	call	fclose@PLT
	movq	800008(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$800024, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z18bambooLogKernelEndv, .-_Z18bambooLogKernelEndv
	.section	.rodata.str1.1
.LC3:
	.string	"instCountList"
.LC4:
	.string	"record_flag"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC3(%rip), %rdx
	movl	$800000, %r9d
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	movq	%rax, %rdi
	movl	$1, %r8d
	leaq	instCountList(%rip), %rsi
	movq	%rax, %rbx
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC4(%rip), %rdx
	movq	%rbx, %rdi
	movl	$8, %r9d
	movq	%rdx, %rcx
	movl	$1, %r8d
	leaq	record_flag(%rip), %rsi
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2055:
	.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <hip/hip_runtime.h>





#define LIST_SIZE 100000
extern "C" __device__ long long instCountList[LIST_SIZE];
extern "C" __device__ unsigned long long record_flag;

void bambooLogRecordOff(){

    long long local_record = 0;

    hipMemcpyToSymbol(HIP_SYMBOL(record_flag), &local_record, sizeof(long long), 0, hipMemcpyHostToDevice);
}

void bambooLogKernelBegin(long long i) {

    i = 1;

    hipMemcpyToSymbol(HIP_SYMBOL(record_flag), &i, sizeof(long long), 0, hipMemcpyHostToDevice);
}

void bambooLogKernelEnd() {

#ifdef KERNELTRACE
    hipDeviceSynchronize();
#endif
    
    long long resultArray[LIST_SIZE] = {0};
    hipMemcpyFromSymbol(resultArray, HIP_SYMBOL(instCountList), LIST_SIZE * sizeof(long long), 0, hipMemcpyDeviceToHost);    
    
    FILE *profileFile = fopen("instCountResult.txt", "w");
    for(long long i=0; i<LIST_SIZE; i++){
        if(resultArray[i] != 0){
            fprintf(profileFile, "%lld: %lld\n", i, resultArray[i]);
        }
    }
    fclose(profileFile);
}

	.text
	.file	"record_data.hip"
	.globl	_Z18bambooLogRecordOffv         # -- Begin function _Z18bambooLogRecordOffv
	.type	_Z18bambooLogRecordOffv,@function
_Z18bambooLogRecordOffv:                # @_Z18bambooLogRecordOffv
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsp, %rsi
	movq	$0, (%rsi)
	movl	$record_flag, %edi
	movl	$8, %edx
	xorl	%ecx, %ecx
	movl	$1, %r8d
	callq	hipMemcpyToSymbol
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z18bambooLogRecordOffv, .Lfunc_end0-_Z18bambooLogRecordOffv
	.cfi_endproc
                                        # -- End function
	.globl	_Z20bambooLogKernelBeginx       # -- Begin function _Z20bambooLogKernelBeginx
	.type	_Z20bambooLogKernelBeginx,@function
_Z20bambooLogKernelBeginx:              # @_Z20bambooLogKernelBeginx
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsp, %rsi
	movq	$1, (%rsi)
	movl	$record_flag, %edi
	movl	$8, %edx
	xorl	%ecx, %ecx
	movl	$1, %r8d
	callq	hipMemcpyToSymbol
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z20bambooLogKernelBeginx, .Lfunc_end1-_Z20bambooLogKernelBeginx
	.cfi_endproc
                                        # -- End function
	.globl	_Z18bambooLogKernelEndv         # -- Begin function _Z18bambooLogKernelEndv
	.type	_Z18bambooLogKernelEndv,@function
_Z18bambooLogKernelEndv:                # @_Z18bambooLogKernelEndv
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$800008, %rsp                   # imm = 0xC3508
	.cfi_def_cfa_offset 800032
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rsp, %rbx
	movl	$800000, %edx                   # imm = 0xC3500
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$instCountList, %esi
	movl	$800000, %edx                   # imm = 0xC3500
	movq	%rbx, %rdi
	xorl	%ecx, %ecx
	movl	$2, %r8d
	callq	hipMemcpyFromSymbol
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	xorl	%r14d, %r14d
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	movq	(%rsp,%r14,8), %rcx
	testq	%rcx, %rcx
	je	.LBB2_3
# %bb.2:                                #   in Loop: Header=BB2_1 Depth=1
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB2_3:                                #   in Loop: Header=BB2_1 Depth=1
	incq	%r14
	cmpq	$100000, %r14                   # imm = 0x186A0
	jne	.LBB2_1
# %bb.4:
	movq	%rbx, %rdi
	callq	fclose
	addq	$800008, %rsp                   # imm = 0xC3508
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z18bambooLogKernelEndv, .Lfunc_end2-_Z18bambooLogKernelEndv
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"instCountResult.txt"
	.size	.L.str, 20

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"w"
	.size	.L.str.1, 2

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%lld: %lld\n"
	.size	.L.str.2, 12

	.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 record_flag
	.addrsig_sym instCountList
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.protected	record_flag             ; @record_flag
	.type	record_flag,@object
	.section	.bss,"aw",@nobits
	.globl	record_flag
	.p2align	3, 0x0
record_flag:
	.quad	0                               ; 0x0
	.size	record_flag, 8

	.protected	instCountList           ; @instCountList
	.type	instCountList,@object
	.globl	instCountList
	.p2align	4, 0x0
instCountList:
	.zero	800000
	.size	instCountList, 800000

	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.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 record_flag
	.addrsig_sym instCountList
	.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
