// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x49a2(%rip), %rsi      # 0x915f
callq 0x3330
movq %rax, %rbx
testq %rax, %rax
je 0x49ff
leaq 0x498d(%rip), %rdi      # 0x9162
pushq $0x4
popq %r14
pushq $0x1
popq %r15
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3510
movq %rsp, %r12
andl $0x0, (%r12)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3510
leaq 0x4998(%rip), %rdi      # 0x91a3
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3510
leaq 0x4947(%rip), %rdi      # 0x9167
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3510
movl $0x10, (%r12)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3510
movw $0x1, %r14w
leaq 0x6(%rsp), %r13
movw %r14w, (%r13)
pushq $0x2
popq %rbp
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x3510
movw %r14w, (%r13)
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x3510
movl 0x8a3a(%rip), %eax      # 0xd2c0
movl %eax, (%r12)
movq %r12, %rdi
movq %r15, %rsi
pushq $0x4
popq %rdx
movq %rbx, %rcx
callq 0x3510
movl 0x8a1b(%rip), %eax      # 0xd2bc
imull 0x8a18(%rip), %eax      # 0xd2c0
pushq $0x8
popq %r14
cltd
idivl %r14d
movl %eax, (%r12)
movq %r12, %rdi
movq %r15, %rsi
pushq $0x4
popq %rdx
movq %rdx, %rbp
movq %rbx, %rcx
callq 0x3510
movl 0x89ee(%rip), %eax      # 0xd2bc
cltd
idivl %r14d
movw %ax, (%r13)
movq %r13, %rdi
movq %r15, %rsi
pushq $0x2
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3510
movzwl 0x89c9(%rip), %eax      # 0xd2bc
movw %ax, (%r13)
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3510
leaq 0x485c(%rip), %rdi      # 0x916c
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3510
andl $0x0, (%r12)
movq %rbx, %rdi
callq 0x3030
movq %rax, %r13
movq %r12, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x3510
andl $0x0, 0x9cca(%rip)      # 0xe610
andl $0x0, 0x9cc7(%rip)      # 0xe614
movb $0x1, 0x9ce4(%rip)      # 0xe638
andl $0x0, 0x9cbd(%rip)      # 0xe618
callq 0x4185
cmpb $0x1, 0x8b5d(%rip)      # 0xd4c4
jne 0x497a
movl $0x100, %edi            # imm = 0x100
xorl %esi, %esi
movq %rbx, %rdx
callq 0x4196
jmp 0x4960
movb $0x0, 0x9cb7(%rip)      # 0xe638
pushq $0x4
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3300
movl 0x8924(%rip), %eax      # 0xd2bc
imull 0x9c71(%rip), %eax      # 0xe610
cltd
pushq $0x8
popq %rbp
idivl %ebp
addl %r13d, %eax
addl $-0x4, %eax
movq %rsp, %r15
movl %eax, (%r15)
pushq $0x1
popq %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3510
movslq %r13d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3300
movl 0x88e3(%rip), %eax      # 0xd2bc
imull 0x9c30(%rip), %eax      # 0xe610
cltd
idivl %ebp
movl %eax, (%r15)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3510
movq %rbx, %rdi
callq 0x32b0
testq %rbx, %rbx
setne %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq