// File: /workspace/github_cpp_makefile_star2_O2/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 $0x6c8, %rsp            # imm = 0x6C8
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rsi
leaq 0x50(%rsp), %rdi
callq 0x3c7e
leaq 0x2c0(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r15, %rsi
callq 0x23e0
leaq 0x330(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x2b90
cmpb $0x1, 0x7b95(%rip)      # 0xa25c
jne 0x26ea
movl 0x2a8(%rsp), %eax
movl 0x7b82(%rip), %ecx      # 0xa258
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x2a4(%rsp)
movb $0x1, 0x2ac(%rsp)
cmpb $0x1, 0x7b73(%rip)      # 0xa264
jne 0x270c
movl 0x2a8(%rsp), %eax
movl 0x7b60(%rip), %ecx      # 0xa260
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x2a8(%rsp)
cmpb $0x1, 0x7b5d(%rip)      # 0xa270
jne 0x2726
movsd 0x7b4b(%rip), %xmm0     # 0xa268
movsd %xmm0, 0x2b0(%rsp)
cmpb $0x1, 0x7b45(%rip)      # 0xa272
jne 0x273c
movb 0x7b3c(%rip), %al       # 0xa271
movb %al, 0x2b8(%rsp)
cmpb $0x1, 0x7b35(%rip)      # 0xa278
jne 0x2752
movl 0x7b29(%rip), %eax      # 0xa274
movl %eax, 0x2bc(%rsp)
leaq 0x391b(%rip), %rsi      # 0x6074
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2c5d
movq (%r14), %rdx
leaq 0x3918(%rip), %rsi      # 0x608a
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2c5d
movq (%r14), %rdx
leaq 0x390f(%rip), %rsi      # 0x609a
xorl %ebp, %ebp
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2c5d
testl %ebx, %ebx
je 0x27ac
cmpl $0x1, %ebx
jne 0x2bed
pushq $0x1
popq %rbp
movzbl 0x2ac(%rsp), %ecx
shll $0x6, %ecx
leaq 0x3913(%rip), %rsi      # 0x60d1
leaq 0x2c0(%rsp), %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2c5d
movb 0x2b8(%rsp), %al
movsd 0x2b0(%rsp), %xmm1
movl 0x2bc(%rsp), %ecx
cvtsi2sd %rcx, %xmm2
cmpb $0x3c, %al
jne 0x27fb
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x27fb
jnp 0x283b
movzbl %al, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm2, 0x18(%rsp)
movsd 0x37f8(%rip), %xmm2     # 0x6008
subsd %xmm0, %xmm2
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
divpd 0x3810(%rip), %xmm1     # 0x6030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x21e0
movsd 0x18(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x37cd(%rip), %xmm0     # 0x6010
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rdx
leaq 0x38a0(%rip), %rsi      # 0x60f3
leaq 0x2c0(%rsp), %rdi
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x2c5d
movl 0x2a4(%rsp), %edx
movl 0x2a8(%rsp), %ecx
leaq 0x3899(%rip), %rsi      # 0x6114
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2c5d
testl %ebx, %ebx
je 0x2a90
leaq 0x14(%rsp), %rax
andl $0x0, (%rax)
xorl %r14d, %r14d
leaq 0x4c(%rsp), %r12
leaq 0x8(%rsp), %rbp
xorl %ebx, %ebx
movl 0x2a8(%rsp), %eax
cmpq %rax, %rbx
ja 0x2b51
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
leaq 0x4c0(%rsp), %rdx
pushq $0x40
popq %rcx
callq 0x4946
leaq 0x40(%rbx), %rcx
movl 0x2a8(%rsp), %eax
cmpq %rax, %rcx
jbe 0x28fc
subq %rbx, %rax
cmpq $0x40, %rax
jae 0x2bce
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x4c0(%rsp,%rax,8)
movq %rcx, 0x38(%rsp)
movsd 0x4c0(%rsp), %xmm0
mulsd 0x3706(%rip), %xmm0     # 0x6018
callq 0x2370
movsd 0x3701(%rip), %xmm1     # 0x6020
maxsd %xmm0, %xmm1
movsd 0x36fd(%rip), %xmm0     # 0x6028
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movsbl %al, %ecx
movl 0x2a4(%rsp), %eax
cmpq %rax, %rbx
movl %ecx, %ebx
sete %al
andb 0x2ac(%rsp), %al
movb %al, 0x7(%rsp)
leaq 0x2c0(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movl %ecx, %edx
xorl %ecx, %ecx
callq 0x2d0b
movq %r14, 0x30(%rsp)
movq %r12, %rdi
movq 0x78ab(%rip), %rcx      # 0xa220
xorl %r13d, %r13d
movq 0x78c1(%rip), %r14      # 0xa240
movq %rbp, %rsi
leaq 0x4d0(%rsp), %r15
pushq $0x3e
popq %rbp
movl %ebx, 0x24(%rsp)
movl %ebx, %r8d
pushq $0x1
popq %rax
movq %rax, 0x28(%rsp)
movq %rbp, 0x18(%rsp)
leaq 0x1(%rbp), %rax
cmpq %rcx, %rax
cmovbq %rax, %rcx
movq %r15, 0x40(%rsp)
leaq -0x8(%r15), %rdx
movq %rdi, %r12
movq %rsi, %rbp
movl %r8d, %r15d
callq 0x2d4e
movq 0x8(%rsp), %rax
movsbl (%r14,%rax), %r14d
andl $0xf, %eax
andl $0xf0, %r13d
orl %eax, %r13d
leaq 0x2c0(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movl %r13d, %edx
pushq $0x1
popq %rcx
callq 0x2d0b
movq 0x28(%rsp), %rcx
cmpq $0x3f, %rcx
jae 0x2a76
addl %r14d, %r15d
movl %r15d, %r13d
movq 0x7816(%rip), %rbx      # 0xa220
movq 0x18(%rsp), %rax
cmpq %rbx, %rax
movq %rcx, %r14
movq %rbx, %rcx
cmovbq %rax, %rcx
movq %r12, %rdi
movq %rbp, %rsi
movq 0x40(%rsp), %r15
movq %r15, %rdx
movl %r13d, %r8d
callq 0x2d4e
movq %rbp, %rsi
movq 0x18(%rsp), %rbp
movq %r12, %rdi
movl %r13d, %r8d
movq 0x8(%rsp), %rax
movq 0x77f4(%rip), %rdx      # 0xa240
movsbl (%rdx,%rax), %ecx
movl %eax, %r13d
shll $0x4, %r13d
addl %ecx, %r8d
movq %rbx, %rcx
addq $0x2, %r14
movq %r14, 0x28(%rsp)
movq %rdx, %r14
addq $-0x2, %rbp
addq $0x10, %r15
jmp 0x299c
cmpb $0x0, 0x7(%rsp)
movq 0x30(%rsp), %r14
cmovnel 0x24(%rsp), %r14d
movq 0x38(%rsp), %rbx
jmp 0x28a9
leaq 0x8(%rsp), %rax
andl $0x0, (%rax)
xorl %r14d, %r14d
leaq 0x2c0(%rsp), %rbx
xorl %ebp, %ebp
movl 0x2a8(%rsp), %eax
cmpq %rax, %r14
jae 0x2b3b
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
leaq 0x4c0(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0x4946
movsd 0x4c0(%rsp), %xmm0
mulsd 0x353a(%rip), %xmm0     # 0x6018
callq 0x2370
movsd 0x3535(%rip), %xmm1     # 0x6020
maxsd %xmm0, %xmm1
movsd 0x3531(%rip), %xmm0     # 0x6028
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movsbl %al, %r15d
movb 0x2ac(%rsp), %r12b
movl 0x2a4(%rsp), %r13d
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x2d0b
cmpq %r13, %r14
cmovnel %ebp, %r15d
testb %r12b, %r12b
cmovnel %r15d, %ebp
incq %r14
jmp 0x2aa5
leaq 0x2c0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x2d0b
leaq 0x35d0(%rip), %rsi      # 0x6128
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2c5d
leaq 0x2c0(%rsp), %rdi
callq 0x22f0
leaq 0x50(%rsp), %rdi
callq 0x4920
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x34b9(%rip), %rdi      # 0x6050
callq 0x21b0
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %r14
leaq 0x34ab(%rip), %rsi      # 0x6059
movq %rax, %rdi
callq 0x20e0
movq 0x743b(%rip), %rsi      # 0x9ff8
movq 0x7404(%rip), %rdx      # 0x9fc8
movq %r14, %rdi
callq 0x2380
jmp 0x2c1d
leaq 0x3593(%rip), %rdi      # 0x6168
leaq 0x35ab(%rip), %rsi      # 0x6187
leaq 0x35b2(%rip), %rcx      # 0x6195
movl $0x87, %edx
callq 0x2160
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %r14
leaq 0x34b0(%rip), %rsi      # 0x60af
movq %rax, %rdi
callq 0x20e0
movq 0x73ea(%rip), %rsi      # 0x9ff8
movq 0x73b3(%rip), %rdx      # 0x9fc8
movq %r14, %rdi
callq 0x2380
jmp 0x2c2e
movq %rax, %rbx
jmp 0x2c4b
jmp 0x2c3b
jmp 0x2c3b
jmp 0x2c3b
jmp 0x2c3b
jmp 0x2c3b
movq %rax, %rbx
movq %r14, %rdi
callq 0x2180
jmp 0x2c3e
movq %rax, %rbx
leaq 0x2c0(%rsp), %rdi
callq 0x22f0
leaq 0x50(%rsp), %rdi
callq 0x4920
movq %rbx, %rdi
callq 0x23a0