// File: /workspace/github_cpp_makefile_star2_O3/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 0x3f14
leaq 0x2c0(%rsp), %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x23f0
leaq 0x330(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x2d8e
cmpb $0x1, 0x6a3f(%rip)      # 0x925c
jne 0x2840
movl 0x2a8(%rsp), %eax
movl 0x6a2c(%rip), %ecx      # 0x9258
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x2a4(%rsp)
movb $0x1, 0x2ac(%rsp)
cmpb $0x1, 0x6a1d(%rip)      # 0x9264
jne 0x2862
movl 0x2a8(%rsp), %eax
movl 0x6a0a(%rip), %ecx      # 0x9260
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x2a8(%rsp)
cmpb $0x1, 0x6a07(%rip)      # 0x9270
jne 0x287c
movsd 0x69f5(%rip), %xmm0     # 0x9268
movsd %xmm0, 0x2b0(%rsp)
cmpb $0x1, 0x69ef(%rip)      # 0x9272
jne 0x2892
movb 0x69e6(%rip), %al       # 0x9271
movb %al, 0x2b8(%rsp)
cmpb $0x1, 0x69df(%rip)      # 0x9278
jne 0x28a8
movl 0x69d3(%rip), %eax      # 0x9274
movl %eax, 0x2bc(%rsp)
leaq 0x37c5(%rip), %rsi      # 0x6074
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e8a
movq (%r14), %rdx
leaq 0x37c2(%rip), %rsi      # 0x608a
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e8a
movq (%r14), %rdx
leaq 0x37b9(%rip), %rsi      # 0x609a
xorl %ebp, %ebp
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e8a
testl %ebx, %ebx
je 0x2904
movl $0x1, %ebp
cmpl $0x1, %ebx
jne 0x2ded
movzbl 0x2ac(%rsp), %ecx
shll $0x6, %ecx
leaq 0x37bb(%rip), %rsi      # 0x60d1
leaq 0x2c0(%rsp), %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2e8a
movb 0x2b8(%rsp), %al
movsd 0x2b0(%rsp), %xmm1
movl 0x2bc(%rsp), %ecx
cvtsi2sd %rcx, %xmm2
cmpb $0x3c, %al
jne 0x2953
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x2953
jnp 0x2996
movzbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm2, 0x18(%rsp)
movsd 0x369d(%rip), %xmm2     # 0x6008
subsd %xmm0, %xmm2
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
divpd 0x36b5(%rip), %xmm1     # 0x6030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x2200
movsd 0x18(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x3672(%rip), %xmm0     # 0x6010
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rdx
leaq 0x3745(%rip), %rsi      # 0x60f3
leaq 0x2c0(%rsp), %rdi
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x2e8a
movl 0x2a4(%rsp), %edx
movl 0x2a8(%rsp), %ecx
leaq 0x373e(%rip), %rsi      # 0x6114
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e8a
testl %ebx, %ebx
je 0x2c2f
leaq 0x4c(%rsp), %r14
leaq 0x10(%rsp), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r12d, %r12d
movl $0x40, %ecx
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
leaq 0x4c0(%rsp), %rdx
callq 0x50a8
leaq 0x40(%rbx), %rcx
movl 0x2a8(%rsp), %eax
cmpq %rax, %rcx
jbe 0x2a45
subq %rbx, %rax
cmpq $0x40, %rax
jae 0x2dce
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x4c0(%rsp,%rax,8)
movq %rcx, 0x38(%rsp)
movsd 0x4c0(%rsp), %xmm0
mulsd 0x35bd(%rip), %xmm0     # 0x6018
callq 0x2380
movsd 0x35b8(%rip), %xmm1     # 0x6020
maxsd %xmm0, %xmm1
movsd 0x35b4(%rip), %xmm0     # 0x6028
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movsbl %al, %r13d
movl 0x2a4(%rsp), %eax
cmpq %rax, %rbx
sete %al
andb 0x2ac(%rsp), %al
movb %al, 0xf(%rsp)
testl %r12d, %r12d
leaq 0x36c0(%rip), %rsi      # 0x6162
leaq 0x369f(%rip), %rax      # 0x6148
cmoveq %rax, %rsi
leaq 0x2c0(%rsp), %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2e8a
movl %ebp, 0x28(%rsp)
incl %r12d
andl $0xf, %r12d
movq %r15, %rsi
movq 0x674c(%rip), %r15      # 0x9220
movq %r14, %rdi
xorl %r14d, %r14d
movl $0x1, %eax
movq %rax, 0x30(%rsp)
movq 0x6755(%rip), %rax      # 0x9240
movq %rax, 0x18(%rsp)
leaq 0x4d0(%rsp), %rbx
movl $0x3e, %eax
movl %r13d, 0x24(%rsp)
movl %r13d, %r8d
movq %rax, %rbp
incq %rax
cmpq %r15, %rax
cmovbq %rax, %r15
movq %rbx, 0x40(%rsp)
leaq -0x8(%rbx), %rdx
movq %rdi, %r13
movq %r15, %rcx
movl %r8d, %r15d
callq 0x2f43
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movsbl (%rcx,%rax), %ebx
andl $0xf, %eax
andl $0xf0, %r14d
orl %eax, %r14d
testl %r12d, %r12d
leaq 0x360b(%rip), %rsi      # 0x6159
leaq 0x35df(%rip), %rax      # 0x6134
cmoveq %rax, %rsi
leaq 0x2c0(%rsp), %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2e8a
incl %r12d
andl $0xf, %r12d
movq 0x30(%rsp), %rax
cmpq $0x3f, %rax
jae 0x2bfc
addl %ebx, %r15d
movl %r15d, %r14d
movq 0x6696(%rip), %r15      # 0x9220
cmpq %r15, %rbp
movq %r15, %rcx
cmovbq %rbp, %rcx
movq %r13, %rdi
movl %r12d, 0x2c(%rsp)
movq %rax, %r12
leaq 0x10(%rsp), %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdx
movl %r14d, %r8d
callq 0x2f43
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movl %r14d, %r8d
movq 0x10(%rsp), %rax
movq 0x6675(%rip), %rcx      # 0x9240
movq %rcx, 0x18(%rsp)
movsbl (%rcx,%rax), %ecx
movl %eax, %r14d
movq %rbp, %rax
shll $0x4, %r14d
addl %ecx, %r8d
addq $0x2, %r12
movq %r12, 0x30(%rsp)
movl 0x2c(%rsp), %r12d
addq $-0x2, %rax
addq $0x10, %rbx
jmp 0x2b05
cmpb $0x0, 0xf(%rsp)
movl 0x28(%rsp), %ebp
cmovnel 0x24(%rsp), %ebp
movl 0x2a8(%rsp), %eax
movq 0x38(%rsp), %rcx
movq %rcx, %rbx
cmpq %rax, %rcx
movq %r13, %r14
leaq 0x10(%rsp), %r15
jbe 0x29fe
jmp 0x2d24
cmpl $0x0, 0x2a8(%rsp)
je 0x2d0a
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
movl $0x1, %ecx
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
leaq 0x4c0(%rsp), %rdx
callq 0x50a8
movsd 0x4c0(%rsp), %xmm0
mulsd 0x33a8(%rip), %xmm0     # 0x6018
callq 0x2380
movsd 0x33a3(%rip), %xmm1     # 0x6020
maxsd %xmm0, %xmm1
movsd 0x339f(%rip), %xmm0     # 0x6028
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movsbl %al, %r14d
movb 0x2ac(%rsp), %r12b
movl 0x2a4(%rsp), %ebx
testl %r15d, %r15d
leaq 0x34b4(%rip), %rsi      # 0x6162
leaq 0x3493(%rip), %rax      # 0x6148
cmoveq %rax, %rsi
leaq 0x2c0(%rsp), %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2e8a
cmpq %rbx, %r13
cmovnel %ebp, %r14d
testb %r12b, %r12b
cmovnel %r14d, %ebp
incl %r15d
andl $0xf, %r15d
incq %r13
movl 0x2a8(%rsp), %eax
cmpq %rax, %r13
jb 0x2c45
testl %r15d, %r15d
leaq 0x3465(%rip), %rsi      # 0x6162
leaq 0x3444(%rip), %rax      # 0x6148
cmoveq %rax, %rsi
jmp 0x2d13
xorl %ebp, %ebp
leaq 0x3435(%rip), %rsi      # 0x6148
leaq 0x2c0(%rsp), %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2e8a
leaq 0x33fd(%rip), %rsi      # 0x6128
leaq 0x2c0(%rsp), %rdi
xorl %eax, %eax
callq 0x2e8a
movq 0x6287(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x2c8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x2c0(%rsp,%rcx)
callq 0x21d0
leaq 0x3b8(%rsp), %rdi
callq 0x20c0
leaq 0x50(%rsp), %rdi
callq 0x5072
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x32bb(%rip), %rdi      # 0x6050
callq 0x21c0
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq 0x32ab(%rip), %rsi      # 0x6059
movq %rax, %rdi
callq 0x20f0
movq 0x623b(%rip), %rsi      # 0x8ff8
movq 0x620c(%rip), %rdx      # 0x8fd0
movq %r14, %rdi
callq 0x23a0
jmp 0x2e1f
leaq 0x3393(%rip), %rdi      # 0x6168
leaq 0x33ab(%rip), %rsi      # 0x6187
leaq 0x33b2(%rip), %rcx      # 0x6195
movl $0x87, %edx
callq 0x2170
movl $0x10, %edi
callq 0x2120
movq %rax, %r14
leaq 0x32ae(%rip), %rsi      # 0x60af
movq %rax, %rdi
callq 0x20f0
movq 0x61e8(%rip), %rsi      # 0x8ff8
movq 0x61b9(%rip), %rdx      # 0x8fd0
movq %r14, %rdi
callq 0x23a0
jmp 0x2e30
movq %rax, %rbx
jmp 0x2e78
jmp 0x2e3d
jmp 0x2e3d
jmp 0x2e3d
jmp 0x2e3d
jmp 0x2e3d
movq %rax, %rbx
movq %r14, %rdi
callq 0x2190
jmp 0x2e40
movq %rax, %rbx
movq 0x6181(%rip), %rax      # 0x8fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x2c8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x2c0(%rsp,%rcx)
callq 0x21d0
leaq 0x3b8(%rsp), %rdi
callq 0x20c0
leaq 0x50(%rsp), %rdi
callq 0x5072
movq %rbx, %rdi
callq 0x23c0