// File: /workspace/github_cpp_makefile_star2_O3/fgheng[P]keysound/src/Audio.cc
// Function: Audio::read_wav(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, WAV_DATA&)
bool Audio::read_wav(const std::string &file, WAV_DATA& wav_data) {
    wav_data.data = nullptr;
    wav_data.len = 0;
    wav_data.bits_per_sample = 0;

    if (!file_exists(file)) return false;

    WAVE_HEADER header;
    std::ifstream f(file);
    if (f.is_open()) {
        f.read((char *)&header, sizeof(WAVE_HEADER));

        // 不是wav文件		Not a wav file
        if (!tag_is_right(header.riff_id, header.riff_type)) goto failed;

        if (header.fmt_audio_format == 1) {
            if (!init_property) {
                channels = header.fmt_channels;
                sample_rate = header.fmt_sample_rate;
                bits_per_sample = header.fmt_bits_per_sample;
                init_property = true;
            } else if (header.fmt_channels != channels
                    || header.fmt_sample_rate != sample_rate
                    || header.fmt_bits_per_sample != bits_per_sample) {
                goto failed;
            }
        } else {
            std::cout << file << " has not pcm tag" << std::endl;
            goto failed;
        }

        wav_data.len = header.data_size;
        wav_data.bits_per_sample =  header.fmt_bits_per_sample;
        wav_data.data = new uint8_t[wav_data.len];

        f.read((char *)wav_data.data, wav_data.len);

        // 更新最大值		Update the maximum size
        if (wav_data.len > max_len) max_len = wav_data.len;

        f.close();
        return true;
    } // else goto failed;

failed:
    f.close();
    return false;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x240, %rsp            # imm = 0x240
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq $0x0, (%rdx)
movl $0x0, 0x8(%rdx)
movw $0x0, 0xc(%rdx)
movq (%rsi), %rdi
leaq 0x38(%rsp), %rsi
callq 0x32f0
testl %eax, %eax
je 0x7a3f
xorl %ebx, %ebx
jmp 0x7b3f
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x3550
leaq 0xb0(%rsp), %rdi
callq 0x30c0
testb %al, %al
je 0x7b29
leaq 0x38(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x2c, %edx
callq 0x3440
cmpl $0x46464952, 0xc(%rsp)  # imm = 0x46464952
jne 0x7b29
cmpl $0x45564157, 0x14(%rsp) # imm = 0x45564157
jne 0x7b29
cmpw $0x1, 0x20(%rsp)
jne 0x7ad8
movzwl 0x22(%rsp), %eax
cmpb $0x0, 0x1200(%rbx)
je 0x7b4e
cmpw 0x1208(%rbx), %ax
jne 0x7b29
movl 0x24(%rsp), %eax
cmpl 0x120c(%rbx), %eax
jne 0x7b29
movzwl 0x2e(%rsp), %eax
cmpw 0x1210(%rbx), %ax
jne 0x7b29
jmp 0x7b72
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0xb4da(%rip), %rdi      # 0x12fc0
callq 0x33d0
movq %rax, %rbx
leaq 0x69ba(%rip), %rsi      # 0xe4af
movl $0x10, %edx
movq %rax, %rdi
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
leaq 0x38(%rsp), %rdi
callq 0x30a0
xorl %ebx, %ebx
leaq 0x38(%rsp), %rdi
callq 0x30e0
movl %ebx, %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbx
popq %r14
popq %r15
retq
movw %ax, 0x1208(%rbx)
movl 0x24(%rsp), %eax
movl %eax, 0x120c(%rbx)
movzwl 0x2e(%rsp), %eax
movw %ax, 0x1210(%rbx)
movb $0x1, 0x1200(%rbx)
movl 0x34(%rsp), %r15d
movl %r15d, 0x8(%r14)
movw %ax, 0xc(%r14)
movq %r15, %rdi
callq 0x3030
movq %rax, (%r14)
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x3440
movl 0x8(%r14), %eax
cmpl 0x1204(%rbx), %eax
jbe 0x7bad
movl %eax, 0x1204(%rbx)
movb $0x1, %bl
leaq 0x38(%rsp), %rdi
callq 0x30a0
jmp 0x7b35
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x35e0
nop