// File: /workspace/github_cpp_makefile_star2_O1/tjohnman[P]sfxr-sdl2/build/../src/main.cpp
// Function: ExportWAV(char const*)
bool ExportWAV(const char* filename)
{
	FILE* foutput=fopen(filename, "wb");
	if(!foutput)
		return false;
	// write wav header
	unsigned int dword=0;
	unsigned short word=0;
	fwrite("RIFF", 4, 1, foutput); // "RIFF"
	dword=0;
	fwrite(&dword, 1, 4, foutput); // remaining file size
	fwrite("WAVE", 4, 1, foutput); // "WAVE"

	fwrite("fmt ", 4, 1, foutput); // "fmt "
	dword=16;
	fwrite(&dword, 1, 4, foutput); // chunk size
	word=1;
	fwrite(&word, 1, 2, foutput); // compression code
	word=1;
	fwrite(&word, 1, 2, foutput); // channels
	dword=wav_freq;
	fwrite(&dword, 1, 4, foutput); // sample rate
	dword=wav_freq*wav_bits/8;
	fwrite(&dword, 1, 4, foutput); // bytes/sec
	word=wav_bits/8;
	fwrite(&word, 1, 2, foutput); // block align
	word=wav_bits;
	fwrite(&word, 1, 2, foutput); // bits per sample

	fwrite("data", 4, 1, foutput); // "data"
	dword=0;
	int foutstream_datasize=ftell(foutput);
	fwrite(&dword, 1, 4, foutput); // chunk size

	// write sample data
	mute_stream=true;
	file_sampleswritten=0;
	filesample=0.0f;
	fileacc=0;
	PlaySample();
	while(playing_sample)
		SynthSample(256, NULL, foutput);
	mute_stream=false;

	// seek back to header and write size info
	fseek(foutput, 4, SEEK_SET);
	dword=0;
	dword=foutstream_datasize-4+file_sampleswritten*wav_bits/8;
	fwrite(&dword, 1, 4, foutput); // remaining file size
	fseek(foutput, foutstream_datasize, SEEK_SET);
	dword=file_sampleswritten*wav_bits/8;
	fwrite(&dword, 1, 4, foutput); // chunk size (data)
	fclose(foutput);
	
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
leaq 0x67c6(%rip), %rsi      # 0xa15f
callq 0x2310
movq %rax, %rbx
testq %rax, %rax
je 0x3c25
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
movl %r12d, (%r14)
leaq 0xe(%rsp), %r15
movw $0x0, (%r15)
leaq 0x679b(%rip), %rdi      # 0xa162
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x24e0
movl %r12d, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
leaq 0x67ab(%rip), %rdi      # 0xa1a3
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x24e0
leaq 0x6756(%rip), %rdi      # 0xa167
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x24e0
movl $0x10, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movw $0x1, %bp
movw %bp, (%r15)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x24e0
movw %bp, (%r15)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x24e0
movl 0x981d(%rip), %eax      # 0xd298
movl %eax, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movl 0x97fb(%rip), %eax      # 0xd294
imull 0x97f8(%rip), %eax      # 0xd298
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movl %ecx, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movl 0x97cb(%rip), %eax      # 0xd294
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
shrl $0x3, %ecx
movw %cx, (%r15)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x24e0
movzwl 0x97a0(%rip), %eax      # 0xd294
movw %ax, (%r15)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x24e0
leaq 0x6658(%rip), %rdi      # 0xa16c
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x24e0
movl %r12d, (%r14)
movq %rbx, %rdi
callq 0x2030
movq %rax, %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movb $0x1, %bpl
movb %bpl, 0xaab5(%rip)      # 0xe608
movl %r12d, 0xaa86(%rip)     # 0xe5e0
movl %r12d, 0xaa83(%rip)     # 0xe5e4
movl %r12d, 0xaa80(%rip)     # 0xe5e8
xorl %edi, %edi
callq 0x2e5c
movb %bpl, 0x992e(%rip)      # 0xd4a4
movl $0x100, %edi            # imm = 0x100
xorl %esi, %esi
movq %rbx, %rdx
callq 0x32c4
cmpb $0x0, 0x9918(%rip)      # 0xd4a4
jne 0x3b76
movb $0x0, 0xaa73(%rip)      # 0xe608
movl $0x4, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x22e0
movl 0x96ea(%rip), %eax      # 0xd294
imull 0xaa2f(%rip), %eax      # 0xe5e0
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
leal (%r15,%rcx), %eax
addl $-0x4, %eax
leaq 0x8(%rsp), %r14
movl %eax, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movslq %r15d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x22e0
movl 0x96a1(%rip), %eax      # 0xd294
imull 0xa9e6(%rip), %eax      # 0xe5e0
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movl %ecx, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movq %rbx, %rdi
callq 0x2280
testq %rbx, %rbx
setne %al
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq