#include <err.h>
#include <cuda.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>

#define BUFLEN (4096)

int byte_sum(uint8_t* bytes, int size)
{
    int i = 0;
    int sum = 0;
    for(; i< size; i++)
    {
        sum += bytes[i];
    }
    return sum;
}
void print_nonzeros(uint8_t* bytes, int size)
{
    int i = 0;
    for(; i< size; i++)
    {
        if (bytes[i] != 0)
            printf("%x", bytes[i]);
    }
    return;
}
    int
main(int argc, char *argv[])
{
    unsigned char host_buf[BUFLEN];
    unsigned char *gpu_buf;
    uint64_t num_bytes =0;

    while (cudaMalloc(&gpu_buf, BUFLEN) == cudaSuccess) {
        num_bytes+=BUFLEN;
        if (cudaMemcpy(host_buf, gpu_buf, BUFLEN, cudaMemcpyDeviceToHost))
            err(1, "cudaMemcpy failed\n");
    
        if (byte_sum(host_buf, BUFLEN) < 768)
        {
            continue;
        }
        write(fileno(stdout), host_buf, BUFLEN);
        printf("\n");
    }
    printf("%lld bytes copied\n",num_bytes);

    return 0;
}

	.file	"tmpxft_0032480a_00000000-6_main.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2045:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2045:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z8byte_sumPhi
	.type	_Z8byte_sumPhi, @function
_Z8byte_sumPhi:
.LFB2040:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorl	%edx, %edx
.L3:
	cmpl	%eax, %esi
	jle	.L6
	movzbl	(%rdi,%rax), %ecx
	incq	%rax
	addl	%ecx, %edx
	jmp	.L3
.L6:
	movl	%edx, %eax
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z8byte_sumPhi, .-_Z8byte_sumPhi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%x"
	.text
	.globl	_Z14print_nonzerosPhi
	.type	_Z14print_nonzerosPhi, @function
_Z14print_nonzerosPhi:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L8:
	cmpl	%ebx, %ebp
	jle	.L15
	movzbl	(%r12,%rbx), %edx
	testb	%dl, %dl
	je	.L9
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L9:
	incq	%rbx
	jmp	.L8
.L15:
	popq	%rax
	.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
.LFE2041:
	.size	_Z14print_nonzerosPhi, .-_Z14print_nonzerosPhi
	.section	.rodata.str1.1
.LC1:
	.string	"cudaMemcpy failed\n"
.LC2:
	.string	"\n"
.LC3:
	.string	"%lld bytes copied\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2042:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$4096, %rsp
	.cfi_def_cfa_offset 4128
	orq	$0, (%rsp)
	subq	$16, %rsp
	.cfi_def_cfa_offset 4144
	xorl	%ebx, %ebx
	leaq	.LC2(%rip), %r12
	movq	%fs:40, %rax
	movq	%rax, 4104(%rsp)
	xorl	%eax, %eax
.L17:
	movq	%rsp, %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	testl	%eax, %eax
	jne	.L25
	leaq	8(%rsp), %rbp
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movl	$4096, %edx
	movq	%rbp, %rdi
	addq	$4096, %rbx
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L18
	movq	4104(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L24
	leaq	.LC1(%rip), %rsi
	movl	$1, %edi
	xorl	%eax, %eax
	call	err@PLT
.L18:
	movl	$4096, %esi
	movq	%rbp, %rdi
	call	_Z8byte_sumPhi
	cmpl	$767, %eax
	jle	.L17
	movq	stdout(%rip), %rdi
	call	fileno@PLT
	movq	%rbp, %rsi
	movl	$4096, %edx
	movl	%eax, %edi
	call	write@PLT
	movq	%r12, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L17
.L25:
	xorl	%eax, %eax
	movq	%rbx, %rdx
	movl	$2, %edi
	leaq	.LC3(%rip), %rsi
	call	__printf_chk@PLT
	movq	4104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
.L24:
	call	__stack_chk_fail@PLT
.L22:
	addq	$4112, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2042:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2068:
	.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
.LFE2068:
	.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 <err.h>
#include <hip/hip_runtime.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>

#define BUFLEN (4096)

int byte_sum(uint8_t* bytes, int size)
{
    int i = 0;
    int sum = 0;
    for(; i< size; i++)
    {
        sum += bytes[i];
    }
    return sum;
}
void print_nonzeros(uint8_t* bytes, int size)
{
    int i = 0;
    for(; i< size; i++)
    {
        if (bytes[i] != 0)
            printf("%x", bytes[i]);
    }
    return;
}
    int
main(int argc, char *argv[])
{
    unsigned char host_buf[BUFLEN];
    unsigned char *gpu_buf;
    uint64_t num_bytes =0;

    while (hipMalloc(&gpu_buf, BUFLEN) == hipSuccess) {
        num_bytes+=BUFLEN;
        if (hipMemcpy(host_buf, gpu_buf, BUFLEN, hipMemcpyDeviceToHost))
            err(1, "hipMemcpy failed\n");
    
        if (byte_sum(host_buf, BUFLEN) < 768)
        {
            continue;
        }
        write(fileno(stdout), host_buf, BUFLEN);
        printf("\n");
    }
    printf("%lld bytes copied\n",num_bytes);

    return 0;
}

	.text
	.file	"main.hip"
	.globl	_Z8byte_sumPhi                  # -- Begin function _Z8byte_sumPhi
	.type	_Z8byte_sumPhi,@function
_Z8byte_sumPhi:                         # @_Z8byte_sumPhi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_1
# %bb.2:                                # %.lr.ph.preheader
	movl	%esi, %ecx
	xorl	%edx, %edx
	xorl	%eax, %eax
.LBB0_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%rdi,%rdx), %esi
	addl	%esi, %eax
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB0_3
# %bb.4:                                # %._crit_edge
	retq
.LBB0_1:
	xorl	%eax, %eax
	retq
.Lfunc_end0:
	.size	_Z8byte_sumPhi, .Lfunc_end0-_Z8byte_sumPhi
	.cfi_endproc
                                        # -- End function
	.globl	_Z14print_nonzerosPhi           # -- Begin function _Z14print_nonzerosPhi
	.type	_Z14print_nonzerosPhi,@function
_Z14print_nonzerosPhi:                  # @_Z14print_nonzerosPhi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB1_6
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%rbx,%r15), %esi
	testl	%esi, %esi
	je	.LBB1_4
# %bb.3:                                #   in Loop: Header=BB1_2 Depth=1
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
.LBB1_4:                                #   in Loop: Header=BB1_2 Depth=1
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB1_2
# %bb.5:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB1_6:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z14print_nonzerosPhi, .Lfunc_end1-_Z14print_nonzerosPhi
	.cfi_endproc
                                        # -- End function
	.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	%rbx
	.cfi_def_cfa_offset 32
	subq	$4112, %rsp                     # imm = 0x1010
	.cfi_def_cfa_offset 4144
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	8(%rsp), %rdi
	movl	$4096, %esi                     # imm = 0x1000
	callq	hipMalloc
	xorl	%ebx, %ebx
	testl	%eax, %eax
	jne	.LBB2_8
# %bb.1:                                # %.lr.ph.preheader
	leaq	16(%rsp), %r14
	leaq	8(%rsp), %r15
.LBB2_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_4 Depth 2
	movq	8(%rsp), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB2_9
# %bb.3:                                # %.lr.ph.i.preheader
                                        #   in Loop: Header=BB2_2 Depth=1
	addq	$4096, %rbx                     # imm = 0x1000
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.LBB2_4:                                # %.lr.ph.i
                                        #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movzbl	16(%rsp,%rax), %edx
	addl	%edx, %ecx
	incq	%rax
	cmpq	$4096, %rax                     # imm = 0x1000
	jne	.LBB2_4
# %bb.5:                                # %_Z8byte_sumPhi.exit
                                        #   in Loop: Header=BB2_2 Depth=1
	cmpl	$768, %ecx                      # imm = 0x300
	jb	.LBB2_7
# %bb.6:                                #   in Loop: Header=BB2_2 Depth=1
	movq	stdout(%rip), %rdi
	callq	fileno
	movl	$4096, %edx                     # imm = 0x1000
	movl	%eax, %edi
	movq	%r14, %rsi
	callq	write
	movl	$10, %edi
	callq	putchar@PLT
.LBB2_7:                                # %.backedge
                                        #   in Loop: Header=BB2_2 Depth=1
	movl	$4096, %esi                     # imm = 0x1000
	movq	%r15, %rdi
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB2_2
.LBB2_8:                                # %._crit_edge
	movl	$.L.str.3, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	xorl	%eax, %eax
	addq	$4112, %rsp                     # imm = 0x1010
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB2_9:
	.cfi_def_cfa_offset 4144
	movl	$.L.str.1, %esi
	movl	$1, %edi
	xorl	%eax, %eax
	callq	err
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%x"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"hipMemcpy failed\n"
	.size	.L.str.1, 18

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%lld bytes copied\n"
	.size	.L.str.3, 19

	.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 __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
