// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
andq $0x0, (%rdx)
movq %rsi, %r15
andl $0x0, 0x8(%rdx)
movq %rdi, %r14
andw $0x0, 0xc(%rdx)
callq 0x6fb6
testb %al, %al
je 0x6aaa
leaq 0x38(%rsp), %rdi
pushq $0x8
popq %rdx
movq %r15, %rsi
callq 0x34f0
leaq 0xb0(%rsp), %rdi
callq 0x30c0
testb %al, %al
je 0x6ad4
leaq 0x38(%rsp), %rdi
leaq 0xc(%rsp), %rsi
pushq $0x2c
popq %rdx
callq 0x33e0
leaq 0x14(%rsp), %rdx
leaq 0xc(%rsp), %rsi
callq 0x6b82
testb %al, %al
je 0x6ad4
cmpw $0x1, 0x20(%rsp)
jne 0x6aae
movzwl 0x22(%rsp), %eax
cmpb $0x0, 0x1200(%r14)
je 0x6af9
cmpw 0x1208(%r14), %ax
jne 0x6ad4
movl 0x24(%rsp), %eax
cmpl 0x120c(%r14), %eax
jne 0x6ad4
movzwl 0x2e(%rsp), %eax
cmpw 0x1210(%r14), %ax
jne 0x6ad4
jmp 0x6b21
xorl %ebx, %ebx
jmp 0x6aea
movq 0xb503(%rip), %rdi      # 0x11fb8
movq %r15, %rsi
callq 0x32a0
leaq 0x5afe(%rip), %rsi      # 0xc5c2
movq %rax, %rdi
callq 0x3310
movq %rax, %rdi
callq 0x3180
leaq 0x38(%rsp), %rdi
callq 0x3090
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(%r14)
movl 0x24(%rsp), %eax
movl %eax, 0x120c(%r14)
movzwl 0x2e(%rsp), %eax
movw %ax, 0x1210(%r14)
movb $0x1, 0x1200(%r14)
movl 0x34(%rsp), %r15d
movl %r15d, 0x8(%rbx)
movw %ax, 0xc(%rbx)
movq %r15, %rdi
callq 0x3030
movq %rax, (%rbx)
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x33e0
movl 0x8(%rbx), %eax
cmpl 0x1204(%r14), %eax
jbe 0x6b5c
movl %eax, 0x1204(%r14)
movb $0x1, %bl
leaq 0x38(%rsp), %rdi
callq 0x3090
jmp 0x6ae0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3570