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


class cuStopwatch{
    
    public:
        cuStopwatch();
        ~cuStopwatch();
        void start();
        float stop();

    private:
        float elapsedTime;
        bool started;
        cudaEvent_t startTime;
        cudaEvent_t endTime;
};

cuStopwatch::cuStopwatch(){
    started = false;
    elapsedTime = 0;
    cudaError_t res = cudaEventCreate(&startTime);
    if (res != 0)
        printf("Return code when recording startTime : %d\n", res);

    res = cudaEventCreate(&endTime);
    if (res != 0)
        printf("Return code when recording endTime : %d\n", res);
}

cuStopwatch::~cuStopwatch(){
    cudaEventDestroy(startTime);
    cudaEventDestroy(endTime);
}

void cuStopwatch::start(){
    // todo: start the stopwatch, and ignore double start
    if (started) {
        return;
    }
    cudaError_t res = cudaEventRecord(startTime);
    if (res != 0)
        printf("Return code when recording startTime : %d\n", res);
    started = true;
}

float cuStopwatch::stop(){
    // todo: stop the stopwatch and return elapsed time, ignore invalid stops (e.g. stop when not yet started or double stop)
    if (! started) {
        return 0;
    }
    cudaError_t res = cudaEventRecord(endTime);
    if (res != 0)
        printf("Return code when recording endTime : %d\n", res);

    cudaEventSynchronize(endTime);
    
    res = cudaEventElapsedTime(&elapsedTime, startTime, endTime);
    if (res != 0)
        printf("Return code when computing elapsed time : %d\n", res);
    
    started = false;
    return elapsedTime;
}


	.file	"tmpxft_0032966d_00000000-6_cuStopwatch.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2037:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2037:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Return code when recording startTime : %d\n"
.LC2:
	.string	"Return code when recording endTime : %d\n"
	.text
	.align 2
	.globl	_ZN11cuStopwatchC2Ev
	.type	_ZN11cuStopwatchC2Ev, @function
_ZN11cuStopwatchC2Ev:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	addq	$8, %rdi
	movb	$0, -4(%rdi)
	movl	$0x00000000, -8(%rdi)
	call	cudaEventCreate@PLT
	testl	%eax, %eax
	je	.L3
	movl	%eax, %edx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L3:
	leaq	16(%rbx), %rdi
	call	cudaEventCreate@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L2
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_remember_state
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
.L2:
	.cfi_restore_state
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_ZN11cuStopwatchC2Ev, .-_ZN11cuStopwatchC2Ev
	.globl	_ZN11cuStopwatchC1Ev
	.set	_ZN11cuStopwatchC1Ev,_ZN11cuStopwatchC2Ev
	.align 2
	.globl	_ZN11cuStopwatchD2Ev
	.type	_ZN11cuStopwatchD2Ev, @function
_ZN11cuStopwatchD2Ev:
.LFB2031:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2031
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	8(%rdi), %rdi
	call	cudaEventDestroy@PLT
	movq	16(%rbx), %rdi
	call	cudaEventDestroy@PLT
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2031:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2031-.LLSDACSB2031
.LLSDACSB2031:
.LLSDACSE2031:
	.text
	.size	_ZN11cuStopwatchD2Ev, .-_ZN11cuStopwatchD2Ev
	.globl	_ZN11cuStopwatchD1Ev
	.set	_ZN11cuStopwatchD1Ev,_ZN11cuStopwatchD2Ev
	.align 2
	.globl	_ZN11cuStopwatch5startEv
	.type	_ZN11cuStopwatch5startEv, @function
_ZN11cuStopwatch5startEv:
.LFB2033:
	.cfi_startproc
	endbr64
	cmpb	$0, 4(%rdi)
	jne	.L19
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	8(%rdi), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L14
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L14:
	movb	$1, 4(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
.L19:
	.cfi_restore 3
	ret
	.cfi_endproc
.LFE2033:
	.size	_ZN11cuStopwatch5startEv, .-_ZN11cuStopwatch5startEv
	.section	.rodata.str1.1
.LC3:
	.string	"Return code when computing elapsed time : %d\n"
	.text
	.align 2
	.globl	_ZN11cuStopwatch4stopEv
	.type	_ZN11cuStopwatch4stopEv, @function
_ZN11cuStopwatch4stopEv:
.LFB2034:
	.cfi_startproc
	endbr64
	cmpb	$0, 4(%rdi)
	je	.L33
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L24
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L24:
	movq	16(%rbx), %rdi
	call	cudaEventSynchronize@PLT
	movq	16(%rbx), %rdx
	movq	8(%rbx), %rsi
	movq	%rbx, %rdi
	call	cudaEventElapsedTime@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L25
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L25:
	movss	(%rbx), %xmm0
	movb	$0, 4(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
.L33:
	.cfi_restore 3
	xorps	%xmm0, %xmm0
	ret
	.cfi_endproc
.LFE2034:
	.size	_ZN11cuStopwatch4stopEv, .-_ZN11cuStopwatch4stopEv
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2060:
	.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
.LFE2060:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.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>


class cuStopwatch{
    
    public:
        cuStopwatch();
        ~cuStopwatch();
        void start();
        float stop();

    private:
        float elapsedTime;
        bool started;
        hipEvent_t startTime;
        hipEvent_t endTime;
};

cuStopwatch::cuStopwatch(){
    started = false;
    elapsedTime = 0;
    hipError_t res = hipEventCreate(&startTime);
    if (res != 0)
        printf("Return code when recording startTime : %d\n", res);

    res = hipEventCreate(&endTime);
    if (res != 0)
        printf("Return code when recording endTime : %d\n", res);
}

cuStopwatch::~cuStopwatch(){
    hipEventDestroy(startTime);
    hipEventDestroy(endTime);
}

void cuStopwatch::start(){
    // todo: start the stopwatch, and ignore double start
    if (started) {
        return;
    }
    hipError_t res = hipEventRecord(startTime);
    if (res != 0)
        printf("Return code when recording startTime : %d\n", res);
    started = true;
}

float cuStopwatch::stop(){
    // todo: stop the stopwatch and return elapsed time, ignore invalid stops (e.g. stop when not yet started or double stop)
    if (! started) {
        return 0;
    }
    hipError_t res = hipEventRecord(endTime);
    if (res != 0)
        printf("Return code when recording endTime : %d\n", res);

    hipEventSynchronize(endTime);
    
    res = hipEventElapsedTime(&elapsedTime, startTime, endTime);
    if (res != 0)
        printf("Return code when computing elapsed time : %d\n", res);
    
    started = false;
    return elapsedTime;
}


	.text
	.file	"cuStopwatch.hip"
	.globl	_ZN11cuStopwatchC2Ev            # -- Begin function _ZN11cuStopwatchC2Ev
	.p2align	1, 0x90
	.type	_ZN11cuStopwatchC2Ev,@function
_ZN11cuStopwatchC2Ev:                   # @_ZN11cuStopwatchC2Ev
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movb	$0, 4(%rdi)
	movl	$0, (%rdi)
	addq	$8, %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	je	.LBB0_2
# %bb.1:
	movl	$.L.str, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB0_2:
	addq	$16, %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	je	.LBB0_3
# %bb.4:
	movl	$.L.str.1, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.LBB0_3:
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_ZN11cuStopwatchC2Ev, .Lfunc_end0-_ZN11cuStopwatchC2Ev
	.cfi_endproc
                                        # -- End function
	.globl	_ZN11cuStopwatchD2Ev            # -- Begin function _ZN11cuStopwatchD2Ev
	.p2align	1, 0x90
	.type	_ZN11cuStopwatchD2Ev,@function
_ZN11cuStopwatchD2Ev:                   # @_ZN11cuStopwatchD2Ev
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	8(%rdi), %rdi
.Ltmp0:
	callq	hipEventDestroy
.Ltmp1:
# %bb.1:
	movq	16(%rbx), %rdi
.Ltmp2:
	callq	hipEventDestroy
.Ltmp3:
# %bb.2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB1_3:
	.cfi_def_cfa_offset 16
.Ltmp4:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end1:
	.size	_ZN11cuStopwatchD2Ev, .Lfunc_end1-_ZN11cuStopwatchD2Ev
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table1:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 1 <<
	.uleb128 .Ltmp3-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp3
	.uleb128 .Ltmp4-.Lfunc_begin0           #     jumps to .Ltmp4
	.byte	1                               #   On action: 1
.Lcst_end0:
	.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_end2:
	.size	__clang_call_terminate, .Lfunc_end2-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.text
	.globl	_ZN11cuStopwatch5startEv        # -- Begin function _ZN11cuStopwatch5startEv
	.p2align	1, 0x90
	.type	_ZN11cuStopwatch5startEv,@function
_ZN11cuStopwatch5startEv:               # @_ZN11cuStopwatch5startEv
	.cfi_startproc
# %bb.0:
	cmpb	$0, 4(%rdi)
	je	.LBB3_1
# %bb.4:
	retq
.LBB3_1:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	8(%rdi), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	je	.LBB3_3
# %bb.2:
	movl	$.L.str, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB3_3:
	movb	$1, 4(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	retq
.Lfunc_end3:
	.size	_ZN11cuStopwatch5startEv, .Lfunc_end3-_ZN11cuStopwatch5startEv
	.cfi_endproc
                                        # -- End function
	.globl	_ZN11cuStopwatch4stopEv         # -- Begin function _ZN11cuStopwatch4stopEv
	.p2align	1, 0x90
	.type	_ZN11cuStopwatch4stopEv,@function
_ZN11cuStopwatch4stopEv:                # @_ZN11cuStopwatch4stopEv
	.cfi_startproc
# %bb.0:
	cmpb	$0, 4(%rdi)
	je	.LBB4_1
# %bb.2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	je	.LBB4_4
# %bb.3:
	movl	$.L.str.1, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB4_4:
	movq	16(%rbx), %rdi
	callq	hipEventSynchronize
	movq	8(%rbx), %rsi
	movq	16(%rbx), %rdx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	testl	%eax, %eax
	je	.LBB4_6
# %bb.5:
	movl	$.L.str.2, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB4_6:
	movb	$0, 4(%rbx)
	movss	(%rbx), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	popq	%rbx
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	retq
.LBB4_1:
	xorps	%xmm0, %xmm0
	retq
.Lfunc_end4:
	.size	_ZN11cuStopwatch4stopEv, .Lfunc_end4-_ZN11cuStopwatch4stopEv
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Return code when recording startTime : %d\n"
	.size	.L.str, 43

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Return code when recording endTime : %d\n"
	.size	.L.str.1, 41

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Return code when computing elapsed time : %d\n"
	.size	.L.str.2, 46

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

	.globl	_ZN11cuStopwatchC1Ev
	.type	_ZN11cuStopwatchC1Ev,@function
.set _ZN11cuStopwatchC1Ev, _ZN11cuStopwatchC2Ev
	.globl	_ZN11cuStopwatchD1Ev
	.type	_ZN11cuStopwatchD1Ev,@function
.set _ZN11cuStopwatchD1Ev, _ZN11cuStopwatchD2Ev
	.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 __gxx_personality_v0
	.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
