// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x9078(%rip), %rsi      # 0xe13f
callq 0x3410
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x50e0
movb $0x0, -0x1(%rbp)
jmp 0x537e
movl $0x0, -0x1c(%rbp)
movw $0x0, -0x1e(%rbp)
movq -0x18(%rbp), %rcx
leaq 0x904a(%rip), %rdi      # 0xe142
movl $0x4, %esi
movl $0x1, %edx
callq 0x3610
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movq -0x18(%rbp), %rcx
leaq 0x9053(%rip), %rdi      # 0xe183
movl $0x4, %esi
movl $0x1, %edx
callq 0x3610
movq -0x18(%rbp), %rcx
leaq 0x8ffd(%rip), %rdi      # 0xe147
movl $0x4, %esi
movl $0x1, %edx
callq 0x3610
movl $0x10, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movw $0x1, -0x1e(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1e(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x3610
movw $0x1, -0x1e(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1e(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x3610
movl 0xe189(%rip), %eax      # 0x13340
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movl 0xe169(%rip), %eax      # 0x13340
imull 0xe15e(%rip), %eax      # 0x1333c
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movl 0xe136(%rip), %eax      # 0x1333c
movl $0x8, %ecx
cltd
idivl %ecx
movw %ax, -0x1e(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1e(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x3610
movl 0xe10d(%rip), %eax      # 0x1333c
movw %ax, -0x1e(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1e(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x3610
movq -0x18(%rbp), %rcx
leaq 0x8ef7(%rip), %rdi      # 0xe14c
movl $0x4, %esi
movl $0x1, %edx
callq 0x3610
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3030
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movb $0x1, 0xf413(%rip)      # 0x146a8
movl $0x0, 0xf3e1(%rip)      # 0x14680
xorps %xmm0, %xmm0
movss %xmm0, 0xf3da(%rip)     # 0x14684
movl $0x0, 0xf3d4(%rip)      # 0x14688
callq 0x4740
testb $0x1, 0xe284(%rip)      # 0x13544
je 0x52d6
movq -0x18(%rbp), %rdx
movl $0x100, %edi            # imm = 0x100
xorl %eax, %eax
movl %eax, %esi
callq 0x4760
jmp 0x52b9
movb $0x0, 0xf3cb(%rip)      # 0x146a8
movq -0x18(%rbp), %rdi
movl $0x4, %esi
xorl %edx, %edx
callq 0x33e0
movl $0x0, -0x1c(%rbp)
movl -0x24(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x28(%rbp)
movl 0xf37d(%rip), %eax      # 0x14680
imull 0xe032(%rip), %eax      # 0x1333c
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x28(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
xorl %edx, %edx
callq 0x33e0
movl 0xf338(%rip), %eax      # 0x14680
imull 0xdfed(%rip), %eax      # 0x1333c
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x3610
movq -0x18(%rbp), %rdi
callq 0x3340
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)