// File: /workspace/github_cpp_makefile_star2_O1/ipatix[P]wav2agb/converter.cpp
// Function: convert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, cmp_type)
void convert(const std::string& wav_file_str, const std::string& s_file_str,
        const std::string& sym, cmp_type ct)
{
    wav_file wf(wav_file_str);

    std::ofstream fout(s_file_str, std::ios::out);
    if (!fout.is_open()) {
        perror("ofstream");
        throw std::runtime_error("unable to open output file");
    }

    // check command line overrides
    if (wav_loop_start_override) {
        wf.loopStart = std::min(wav_loop_start, wf.loopEnd);
        wf.loopEnabled = true;
    }
    if (wav_loop_end_override) {
        wf.loopEnd = std::min(wav_loop_end, wf.loopEnd);
    }
    if (wav_tune_override) {
        wf.tuning = wav_tune;
    }
    if (wav_key_override) {
        wf.midiKey = wav_key;
    }
    if (wav_rate_override) {
        wf.sampleRate = wav_rate;
    }

    agb_out(fout, "    .section .rodata\n");
    agb_out(fout, "    .global %s\n", sym.c_str());
    agb_out(fout, "    .align  2\n\n%s:\n\n", sym.c_str());

    uint8_t fmt;
    if (ct == cmp_type::none)
        fmt = 0;
    else if (ct == cmp_type::dpcm)
        fmt = 1;
    else
        throw std::runtime_error("convert: invalid compression type");

    agb_out(fout, "    .byte   0x%X, 0x0, 0x0, 0x%X\n", fmt, wf.loopEnabled ? 0x40 : 0x0);
    double pitch;
    if (wf.midiKey == 60 && wf.tuning == 0.0)
        pitch = wf.sampleRate;
    else
        pitch = wf.sampleRate * pow(2.0, (60.0 - wf.midiKey) / 12.0 + wf.tuning / 1200.0);
    agb_out(fout, "    .word   0x%08X  @ Mid-C ~%f\n",
            static_cast<uint32_t>(pitch * 1024.0),
            pitch);
    agb_out(fout, "    .word   %u, %u\n", wf.loopStart, wf.loopEnd);

    if (ct == cmp_type::none)
        convert_uncompressed(wf, fout);
    else if (ct == cmp_type::dpcm)
        convert_dpcm(wf, fout);
    else
        throw std::runtime_error("convert: invalid compression type");

    agb_out(fout, "\n\n    .end\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6b8, %rsp            # imm = 0x6B8
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rsi
leaq 0x40(%rsp), %rdi
callq 0x3ef0
leaq 0x2b0(%rsp), %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x23f0
leaq 0x320(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x2d0d
cmpb $0x1, 0x6a3f(%rip)      # 0x925c
jne 0x2840
movl 0x298(%rsp), %eax
movl 0x6a2c(%rip), %ecx      # 0x9258
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x294(%rsp)
movb $0x1, 0x29c(%rsp)
cmpb $0x1, 0x6a1d(%rip)      # 0x9264
jne 0x2862
movl 0x298(%rsp), %eax
movl 0x6a0a(%rip), %ecx      # 0x9260
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x298(%rsp)
cmpb $0x1, 0x6a07(%rip)      # 0x9270
jne 0x287c
movsd 0x69f5(%rip), %xmm0     # 0x9268
movsd %xmm0, 0x2a0(%rsp)
cmpb $0x1, 0x69ef(%rip)      # 0x9272
jne 0x2892
movb 0x69e6(%rip), %al       # 0x9271
movb %al, 0x2a8(%rsp)
cmpb $0x1, 0x69df(%rip)      # 0x9278
jne 0x28a8
movl 0x69d3(%rip), %eax      # 0x9274
movl %eax, 0x2ac(%rsp)
leaq 0x37c5(%rip), %rsi      # 0x6074
leaq 0x2b0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e42
movq (%r14), %rdx
leaq 0x37c2(%rip), %rsi      # 0x608a
leaq 0x2b0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e42
movq (%r14), %rdx
leaq 0x37b9(%rip), %rsi      # 0x609a
xorl %ebp, %ebp
leaq 0x2b0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e42
testl %ebx, %ebx
je 0x2904
movl $0x1, %ebp
cmpl $0x1, %ebx
jne 0x2d6f
movzbl 0x29c(%rsp), %ecx
shll $0x6, %ecx
leaq 0x37bb(%rip), %rsi      # 0x60d1
leaq 0x2b0(%rsp), %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2e42
movb 0x2a8(%rsp), %al
movsd 0x2a0(%rsp), %xmm0
movl 0x2ac(%rsp), %ecx
cvtsi2sd %rcx, %xmm2
cmpb $0x3c, %al
jne 0x2953
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2953
jnp 0x2992
movzbl %al, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm2, 0x20(%rsp)
movsd 0x369d(%rip), %xmm2     # 0x6008
subsd %xmm1, %xmm2
divsd 0x3699(%rip), %xmm2     # 0x6010
divsd 0x3699(%rip), %xmm0     # 0x6018
addsd %xmm2, %xmm0
callq 0x2200
movsd 0x20(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x3686(%rip), %xmm0     # 0x6020
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rdx
leaq 0x3749(%rip), %rsi      # 0x60f3
leaq 0x2b0(%rsp), %rdi
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x2e42
movl 0x294(%rsp), %edx
movl 0x298(%rsp), %ecx
leaq 0x3742(%rip), %rsi      # 0x6114
leaq 0x2b0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e42
testl %ebx, %ebx
je 0x2bd5
cmpl $0x1, %ebx
jne 0x2da3
leaq 0x1c(%rsp), %rax
movl $0x0, (%rax)
xorl %r14d, %r14d
leaq 0x10(%rsp), %rbp
xorl %ebx, %ebx
movl $0x40, %ecx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
leaq 0x4b0(%rsp), %rdx
callq 0x4fba
leaq 0x40(%r14), %rcx
movl 0x298(%rsp), %eax
cmpq %rax, %rcx
jbe 0x2a4e
subq %r14, %rax
cmpq $0x40, %rax
jae 0x2d50
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x4b0(%rsp,%rax,8)
movq %rcx, 0x38(%rsp)
movsd 0x4b0(%rsp), %xmm0
mulsd 0x35c4(%rip), %xmm0     # 0x6028
callq 0x2380
movsd 0x35bf(%rip), %xmm1     # 0x6030
maxsd %xmm0, %xmm1
movsd 0x35bb(%rip), %xmm0     # 0x6038
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movsbl %al, %ecx
movl 0x294(%rsp), %eax
cmpq %rax, %r14
movl %ecx, %r14d
sete %al
andb 0x29c(%rsp), %al
movb %al, 0xf(%rsp)
leaq 0x2b0(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
movl %ecx, %edx
xorl %ecx, %ecx
callq 0x2efb
movl %ebx, 0x30(%rsp)
xorl %ebx, %ebx
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movl $0x3e, %eax
leaq 0x4c0(%rsp), %rdx
movl %r14d, 0x2c(%rsp)
movq %rax, %r12
incq %rax
movq 0x6738(%rip), %rcx      # 0x9220
cmpq %rcx, %rax
cmovbq %rax, %rcx
movq %rdx, %r13
addq $-0x8, %rdx
leaq 0x34(%rsp), %rdi
movq %rbp, %rsi
movl %r14d, %r8d
callq 0x2f3e
movq 0x10(%rsp), %rax
movq 0x672e(%rip), %rcx      # 0x9240
movsbl (%rcx,%rax), %ebp
andl $0xf, %eax
andl $0xf0, %ebx
orl %eax, %ebx
leaq 0x2b0(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
movl %ebx, %edx
movl $0x1, %ecx
callq 0x2efb
movq 0x20(%rsp), %rax
cmpq $0x3f, %rax
jae 0x2ba5
addl %ebp, %r14d
movq 0x66d1(%rip), %rcx      # 0x9220
cmpq %rcx, %r12
cmovbq %r12, %rcx
leaq 0x34(%rsp), %rdi
leaq 0x10(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
movl %r14d, %r8d
movq %rax, %r15
callq 0x2f3e
movq %r13, %rdx
movq 0x10(%rsp), %rax
movq 0x66c0(%rip), %rcx      # 0x9240
movsbl (%rcx,%rax), %ecx
movl %eax, %ebx
movq %r12, %rax
shll $0x4, %ebx
addl %ecx, %r14d
addq $0x2, %r15
movq %r15, 0x20(%rsp)
addq $-0x2, %rax
addq $0x10, %rdx
jmp 0x2adb
cmpb $0x0, 0xf(%rsp)
movl 0x30(%rsp), %ebx
cmovnel 0x2c(%rsp), %ebx
movl 0x298(%rsp), %eax
movq 0x38(%rsp), %rcx
movq %rcx, %r14
cmpq %rax, %rcx
leaq 0x10(%rsp), %rbp
jbe 0x2a07
jmp 0x2ca3
movl $0x0, 0x10(%rsp)
cmpl $0x0, 0x298(%rsp)
je 0x2c8b
xorl %ebx, %ebx
leaq 0x10(%rsp), %r12
xorl %r13d, %r13d
movl $0x1, %ecx
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
leaq 0x4b0(%rsp), %rdx
callq 0x4fba
movsd 0x4b0(%rsp), %xmm0
mulsd 0x3408(%rip), %xmm0     # 0x6028
callq 0x2380
movsd 0x3403(%rip), %xmm1     # 0x6030
maxsd %xmm0, %xmm1
movsd 0x33ff(%rip), %xmm0     # 0x6038
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movsbl %al, %ebp
movb 0x29c(%rsp), %r14b
movl 0x294(%rsp), %r15d
leaq 0x2b0(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x2efb
cmpq %r15, %r13
cmovnel %ebx, %ebp
testb %r14b, %r14b
cmovel %ebx, %ebp
incq %r13
movl 0x298(%rsp), %eax
movl %ebp, %ebx
cmpq %rax, %r13
jb 0x2bf5
jmp 0x2c8d
xorl %ebp, %ebp
leaq 0x2b0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x2efb
leaq 0x347e(%rip), %rsi      # 0x6128
leaq 0x2b0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e42
movq 0x6308(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x2b8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x2b0(%rsp,%rcx)
callq 0x21d0
leaq 0x3a8(%rsp), %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
callq 0x4f84
addq $0x6b8, %rsp            # imm = 0x6B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x333c(%rip), %rdi      # 0x6050
callq 0x21c0
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq 0x332c(%rip), %rsi      # 0x6059
movq %rax, %rdi
callq 0x20f0
movq 0x62bc(%rip), %rsi      # 0x8ff8
movq 0x628d(%rip), %rdx      # 0x8fd0
movq %r14, %rdi
callq 0x23a0
jmp 0x2dd5
leaq 0x3411(%rip), %rdi      # 0x6168
leaq 0x3429(%rip), %rsi      # 0x6187
leaq 0x3430(%rip), %rcx      # 0x6195
movl $0x87, %edx
callq 0x2170
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq 0x332c(%rip), %rsi      # 0x60af
movq %rax, %rdi
callq 0x20f0
movq 0x6266(%rip), %rsi      # 0x8ff8
movq 0x6237(%rip), %rdx      # 0x8fd0
movq %r14, %rdi
callq 0x23a0
jmp 0x2dd5
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq 0x32f8(%rip), %rsi      # 0x60af
movq %rax, %rdi
callq 0x20f0
movq 0x6232(%rip), %rsi      # 0x8ff8
movq 0x6203(%rip), %rdx      # 0x8fd0
movq %r14, %rdi
callq 0x23a0
jmp 0x2de8
jmp 0x2de8
movq %rax, %rbx
jmp 0x2e30
jmp 0x2df5
jmp 0x2df5
jmp 0x2df5
jmp 0x2df5
jmp 0x2df5
movq %rax, %rbx
movq %r14, %rdi
callq 0x2190
jmp 0x2df8
movq %rax, %rbx
movq 0x61c9(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x2b8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x2b0(%rsp,%rcx)
callq 0x21d0
leaq 0x3a8(%rsp), %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
callq 0x4f84
movq %rbx, %rdi
callq 0x23c0