// File: /workspace/github_cpp_makefile_star2_O3/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 0x67ca(%rip), %rsi      # 0xa16f
callq 0x2310
movq %rax, %rbx
testq %rax, %rax
je 0x3c28
leaq 0x67b5(%rip), %rdi      # 0xa172
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x24e0
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
movl %r12d, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
leaq 0x67bd(%rip), %rdi      # 0xa1b3
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x24e0
leaq 0x6768(%rip), %rdi      # 0xa177
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
leaq 0xe(%rsp), %r15
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 0x981a(%rip), %eax      # 0xd298
movl %eax, (%r14)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x24e0
movl 0x97f8(%rip), %eax      # 0xd294
imull 0x97f5(%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 0x97c8(%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 0x979d(%rip), %eax      # 0xd294
movw %ax, (%r15)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x24e0
leaq 0x6665(%rip), %rdi      # 0xa17c
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, 0xaac2(%rip)      # 0xe618
movl %r12d, 0xaa93(%rip)     # 0xe5f0
movl %r12d, 0xaa90(%rip)     # 0xe5f4
movl %r12d, 0xaa8d(%rip)     # 0xe5f8
xorl %edi, %edi
callq 0x2e5c
movb %bpl, 0x992b(%rip)      # 0xd4a4
movl $0x100, %edi            # imm = 0x100
xorl %esi, %esi
movq %rbx, %rdx
callq 0x32b4
cmpb $0x0, 0x9915(%rip)      # 0xd4a4
jne 0x3b79
movb $0x0, 0xaa80(%rip)      # 0xe618
movl $0x4, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x22e0
movl 0x96e7(%rip), %eax      # 0xd294
imull 0xaa3c(%rip), %eax      # 0xe5f0
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 0x969e(%rip), %eax      # 0xd294
imull 0xa9f3(%rip), %eax      # 0xe5f0
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