#include <iostream>
#include <sstream>
#include <stdio.h>
#include <cuda.h>

/**
 * \brief writes CUDA array to a text file
 * \details copies the cuda array to a local buffer, writes to buffer to a new file, then frees the local memory
 * @param[in] array_in   - device pointer to array to write
 * @param[in] N          - number of elements to write
 * @param[in] filename   - name for the file
 */ 
void write_to_file(unsigned* array_in , unsigned N , std::string filename){

	FILE* f  = fopen(filename.c_str(),"w");
	unsigned * hostdata = new unsigned [N];
	cudaMemcpy(hostdata,array_in,N*sizeof(unsigned),cudaMemcpyDeviceToHost);

	for(unsigned k = 0;  k<N ;k++){
		fprintf(f,"%u\n",hostdata[k]);
	}

	delete hostdata;
	fclose(f);

}


	.file	"tmpxft_0034eb12_00000000-6_write_to_file.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3696:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3696:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"w"
.LC1:
	.string	"%u\n"
	.text
	.globl	_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.type	_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, @function
_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE:
.LFB3693:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movl	%esi, %ebx
	movq	(%rdx), %rdi
	leaq	.LC0(%rip), %rsi
	leaq	0(,%rbx,4), %r14
	call	fopen@PLT
	movq	%r14, %rdi
	movq	%rax, %r13
	call	_Znam@PLT
	movq	%r14, %rdx
	movq	%r12, %rsi
	movl	$2, %ecx
	movq	%rax, %rdi
	movq	%rax, %rbp
	leaq	.LC1(%rip), %r14
	xorl	%r12d, %r12d
	call	cudaMemcpy@PLT
.L3:
	cmpq	%r12, %rbx
	je	.L7
	movl	0(%rbp,%r12,4), %ecx
	movq	%r14, %rdx
	movl	$2, %esi
	movq	%r13, %rdi
	xorl	%eax, %eax
	incq	%r12
	call	__fprintf_chk@PLT
	jmp	.L3
.L7:
	movq	%rbp, %rdi
	movl	$4, %esi
	call	_ZdlPvm@PLT
	popq	%rbx
	.cfi_def_cfa_offset 40
	movq	%r13, %rdi
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE3693:
	.size	_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .-_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3719:
	.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
.LFE3719:
	.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 <iostream>
#include <sstream>
#include <stdio.h>
#include <hip/hip_runtime.h>

/**
 * \brief writes CUDA array to a text file
 * \details copies the cuda array to a local buffer, writes to buffer to a new file, then frees the local memory
 * @param[in] array_in   - device pointer to array to write
 * @param[in] N          - number of elements to write
 * @param[in] filename   - name for the file
 */ 
void write_to_file(unsigned* array_in , unsigned N , std::string filename){

	FILE* f  = fopen(filename.c_str(),"w");
	unsigned * hostdata = new unsigned [N];
	hipMemcpy(hostdata,array_in,N*sizeof(unsigned),hipMemcpyDeviceToHost);

	for(unsigned k = 0;  k<N ;k++){
		fprintf(f,"%u\n",hostdata[k]);
	}

	delete hostdata;
	fclose(f);

}


	.text
	.file	"write_to_file.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE # -- Begin function _Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.type	_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE,@function
_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE: # @_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	%esi, %ebp
	movq	%rdi, %r15
	movq	(%rdx), %rdi
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	movl	%ebp, %r13d
	leaq	(,%r13,4), %r12
	movq	%r12, %rdi
	callq	_Znam
	movq	%rax, %r14
	movq	%rax, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%ebp, %ebp
	je	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%r15d, %r15d
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r14,%r15,4), %edx
	movl	$.L.str.1, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	fprintf
	incq	%r15
	cmpq	%r15, %r13
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	movq	%r14, %rdi
	callq	_ZdlPv
	movq	%rbx, %rdi
	addq	$8, %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
	jmp	fclose                          # TAILCALL
.Lfunc_end0:
	.size	_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, .Lfunc_end0-_Z13write_to_filePjjNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"w"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%u\n"
	.size	.L.str.1, 4

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