/* This function writes the transformed space (dm,t) out to file in binary format.
 */

#include <stdio.h>
#include <stdlib.h>

void write_output(int i, int t_processed, int ndms, size_t gpu_memory, float *output_buffer, size_t gpu_outputsize, float *dm_low, float *dm_high)
{

	FILE *fp_out;
	char filename[200];
	/*
	 sprintf(filename, "binary-dm_%.2f-%.2f.dat", dm_low[i], dm_high[i]);
	 if ((fp_out=fopen(filename, "wb")) == NULL) {
	 fprintf(stderr, "Error opening output file!\n");
	 exit(0);
	 }

	 fwrite(output_buffer, sizeof(float), gpu_outputsize, fp_out);
	 */
	sprintf(filename, "dm_%.2f-%.2f.dat", dm_low[i], dm_high[i]);
	if (( fp_out = fopen(filename, "w") ) == NULL)
	{
		fprintf(stderr, "Error opening output file!\n");
		exit(0);
	}

	for (int k = 0; k < ndms; k++)
	{
		for (int l = 0; l < t_processed; l++)
		{
			fprintf(fp_out, "%f ", output_buffer[k * t_processed + l]);
		}
		fprintf(fp_out, "\n");
	}
}

	.file	"tmpxft_002ad6e3_00000000-6_host_write_file.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"dm_%.2f-%.2f.dat"
.LC1:
	.string	"w"
.LC2:
	.string	"Error opening output file!\n"
.LC3:
	.string	"%f "
.LC4:
	.string	"\n"
	.text
	.globl	_Z12write_outputiiimPfmS_S_
	.type	_Z12write_outputiiimPfmS_S_, @function
_Z12write_outputiiimPfmS_S_:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movslq	%edi, %rdi
	movl	%edx, %r15d
	leaq	.LC0(%rip), %rcx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	$200, %edx
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r8, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%esi, %ebp
	movl	$2, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$232, %rsp
	.cfi_def_cfa_offset 288
	movq	%fs:40, %rax
	movq	%rax, 216(%rsp)
	xorl	%eax, %eax
	movq	288(%rsp), %rax
	leaq	16(%rsp), %rbx
	cvtss2sd	(%rax,%rdi,4), %xmm0
	movq	296(%rsp), %rax
	cvtss2sd	(%rax,%rdi,4), %xmm1
	movq	%rbx, %rdi
	movb	$2, %al
	call	__sprintf_chk@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rbx, %rdi
	call	fopen@PLT
	testq	%rax, %rax
	je	.L3
	movq	%rax, %rbx
	xorl	%r13d, %r13d
	xorl	%r14d, %r14d
	jmp	.L4
.L3:
	movq	stderr(%rip), %rdi
	leaq	.LC2(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L11:
	leaq	.LC4(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	incl	%r14d
	movl	$2, %esi
	addl	%ebp, %r13d
	call	__fprintf_chk@PLT
.L4:
	cmpl	%r15d, %r14d
	jge	.L2
	movslq	%r13d, %rax
	xorl	%ecx, %ecx
	leaq	(%r12,%rax,4), %r8
.L7:
	cmpl	%ecx, %ebp
	jle	.L11
	leaq	.LC3(%rip), %rdx
	movq	%rbx, %rdi
	movb	$1, %al
	movq	%rcx, (%rsp)
	movl	$2, %esi
	cvtss2sd	(%r8,%rcx,4), %xmm0
	movq	%r8, 8(%rsp)
	call	__fprintf_chk@PLT
	movq	(%rsp), %rcx
	movq	8(%rsp), %r8
	incq	%rcx
	jmp	.L7
.L2:
	movq	216(%rsp), %rax
	subq	%fs:40, %rax
	je	.L8
	call	__stack_chk_fail@PLT
.L8:
	addq	$232, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE2027:
	.size	_Z12write_outputiiimPfmS_S_, .-_Z12write_outputiiimPfmS_S_
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.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
.LFE2053:
	.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:

/* This function writes the transformed space (dm,t) out to file in binary format.
 */


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>

void write_output(int i, int t_processed, int ndms, size_t gpu_memory, float *output_buffer, size_t gpu_outputsize, float *dm_low, float *dm_high)
{

	FILE *fp_out;
	char filename[200];
	/*
	 sprintf(filename, "binary-dm_%.2f-%.2f.dat", dm_low[i], dm_high[i]);
	 if ((fp_out=fopen(filename, "wb")) == NULL) {
	 fprintf(stderr, "Error opening output file!\n");
	 exit(0);
	 }

	 fwrite(output_buffer, sizeof(float), gpu_outputsize, fp_out);
	 */
	sprintf(filename, "dm_%.2f-%.2f.dat", dm_low[i], dm_high[i]);
	if (( fp_out = fopen(filename, "w") ) == NULL)
	{
		fprintf(stderr, "Error opening output file!\n");
		exit(0);
	}

	for (int k = 0; k < ndms; k++)
	{
		for (int l = 0; l < t_processed; l++)
		{
			fprintf(fp_out, "%f ", output_buffer[k * t_processed + l]);
		}
		fprintf(fp_out, "\n");
	}
}

	.text
	.file	"host_write_file.hip"
	.globl	_Z12write_outputiiimPfmS_S_     # -- Begin function _Z12write_outputiiimPfmS_S_
	.type	_Z12write_outputiiimPfmS_S_,@function
_Z12write_outputiiimPfmS_S_:            # @_Z12write_outputiiimPfmS_S_
	.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
	subq	$232, %rsp
	.cfi_def_cfa_offset 288
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r8, 16(%rsp)                   # 8-byte Spill
	movl	%edx, %r15d
	movl	%esi, %ebp
	movq	296(%rsp), %rax
	movq	288(%rsp), %rcx
	movslq	%edi, %rdx
	cvtss2sd	(%rcx,%rdx,4), %xmm0
	cvtss2sd	(%rax,%rdx,4), %xmm1
	leaq	32(%rsp), %r14
	movl	$.L.str, %esi
	movq	%r14, %rdi
	movb	$2, %al
	callq	sprintf
	movl	$.L.str.1, %esi
	movq	%r14, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_8
# %bb.1:                                # %.preheader18
	testl	%r15d, %r15d
	jle	.LBB0_7
# %bb.2:                                # %.preheader.lr.ph
	movq	%rax, %r14
	movl	%r15d, %eax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movl	%ebp, %r12d
	xorl	%r13d, %r13d
	xorl	%ebx, %ebx
	movl	%ebp, 12(%rsp)                  # 4-byte Spill
.LBB0_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_5 Depth 2
	testl	%ebp, %ebp
	jle	.LBB0_6
# %bb.4:                                # %.lr.ph
                                        #   in Loop: Header=BB0_3 Depth=1
	movl	%r13d, %eax
	movq	16(%rsp), %rcx                  # 8-byte Reload
	leaq	(%rcx,%rax,4), %r15
	xorl	%ebp, %ebp
.LBB0_5:                                #   Parent Loop BB0_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r15,%rbp,4), %xmm0
	movl	$.L.str.3, %esi
	movq	%r14, %rdi
	movb	$1, %al
	callq	fprintf
	incq	%rbp
	cmpq	%rbp, %r12
	jne	.LBB0_5
.LBB0_6:                                # %._crit_edge
                                        #   in Loop: Header=BB0_3 Depth=1
	movl	$10, %edi
	movq	%r14, %rsi
	callq	fputc@PLT
	incq	%rbx
	movl	12(%rsp), %ebp                  # 4-byte Reload
	addl	%ebp, %r13d
	cmpq	24(%rsp), %rbx                  # 8-byte Folded Reload
	jne	.LBB0_3
.LBB0_7:                                # %._crit_edge21
	addq	$232, %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
	retq
.LBB0_8:
	.cfi_def_cfa_offset 288
	movq	stderr(%rip), %rcx
	movl	$.L.str.2, %edi
	movl	$27, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z12write_outputiiimPfmS_S_, .Lfunc_end0-_Z12write_outputiiimPfmS_S_
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"dm_%.2f-%.2f.dat"
	.size	.L.str, 17

	.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	"Error opening output file!\n"
	.size	.L.str.2, 28

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%f "
	.size	.L.str.3, 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 __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
