// File: /workspace/github_c_makefile_star2_O3/FredTheDino[P]Fog/src/engine/unix_assets.cpp
// Function: load_sound(AssetFile*, Asset::Header*)
void load_sound(AssetFile *file, Asset::Header *header) {
    FILE *wav_file = fopen(header->file_path, "rb");
    fseek(wav_file, 0, SEEK_END);
    long end = ftell(wav_file);
    rewind(wav_file);

    WAVHeader wav_header;
    fread((WAVHeader *) &wav_header, sizeof(wav_header), 1, wav_file);
    if (wav_header.format != 1 && wav_header.format != 3) {
        printf("Failed to load \"%s\", only accepts uncompressed data (%d)\n",
               header->file_path, wav_header.format);
        fclose(wav_file);
        return;
    }

    if (wav_header.channels > 2) {
        printf("Failed to load \"%s\", only supports 1 or 2 channels (%d)\n",
               header->file_path, wav_header.format);
        fclose(wav_file);
        return;
    }

    u64 size = 0;
    u8 *data = nullptr;
    while (end != ftell(wav_file)) {
        WAVChunk chunk;
        fread(&chunk, sizeof(WAVChunk), 1, wav_file);
        if (chunk.type[0] == 'd' && chunk.type[1] == 'a' &&
            chunk.type[2] == 't' && chunk.type[3] == 'a') {
            if (!data)
                data = (u8 *) malloc(size + chunk.size);
            else
                data = (u8 *) realloc(data, size + chunk.size);
            fread((void *) (data + size), 1, chunk.size, wav_file);
            size += chunk.size;
        } else {
            fseek(wav_file, chunk.size, SEEK_CUR);
        }
    }

    Sound sound;
    sound.data = data;
    sound.size = size;
    sound.num_samples = size / (wav_header.channels * wav_header.bitdepth / 8);
    sound.sample_rate = wav_header.sample_rate;
    sound.bits_per_sample = wav_header.bitdepth;
    sound.is_stereo = 1 < wav_header.channels;

    Asset::Data asset = {.sound = sound};
    header->asset_size = sizeof(Asset::Data) + size;
    add_asset_to_file(file, header, &asset);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r14
movq %rdi, -0x48(%rbp)
movq 0x8(%rsi), %rdi
leaq 0x12030(%rip), %rsi     # 0x198a5
callq 0x2220
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2210
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
movq %r15, %rdi
callq 0x2070
leaq -0x9c(%rbp), %rbx
movl $0x24, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x22f0
movswl 0x14(%rbx), %edx
movl %edx, %eax
andl $0xfffd, %eax           # imm = 0xFFFD
cmpw $0x1, %ax
jne 0x78e5
movswl -0x86(%rbp), %eax
cmpl $0x2, %eax
jle 0x790d
movq 0x8(%r14), %rsi
leaq 0x1221a(%rip), %rdi     # 0x19afd
jmp 0x78f0
movq 0x8(%r14), %rsi
leaq 0x121d3(%rip), %rdi     # 0x19ac3
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21c0
movl %eax, -0x34(%rbp)
movq %r14, -0x40(%rbp)
movq %r15, %rdi
callq 0x2030
movq $0x0, -0x30(%rbp)
movl $0x0, %ebx
cmpq %rax, %r12
je 0x79db
xorl %ebx, %ebx
leaq -0x78(%rbp), %r14
movq $0x0, -0x30(%rbp)
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x22f0
cmpb $0x64, -0x78(%rbp)
jne 0x798b
cmpb $0x61, -0x77(%rbp)
jne 0x798b
cmpb $0x74, -0x76(%rbp)
jne 0x798b
cmpb $0x61, -0x75(%rbp)
jne 0x798b
movq %r12, %r14
movslq -0x74(%rbp), %r12
leaq (%rbx,%r12), %r13
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x799e
movq %r13, %rsi
callq 0x2360
jmp 0x79a6
movslq -0x74(%rbp), %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0x2210
jmp 0x79ca
movq %r13, %rdi
callq 0x2270
movq %rax, -0x30(%rbp)
addq %rax, %rbx
movl $0x1, %esi
movq %rbx, %rdi
movq %r12, %rdx
movq %r15, %rcx
callq 0x22f0
movq %r13, %rbx
movq %r14, %r12
leaq -0x78(%rbp), %r14
movq %r15, %rdi
callq 0x2030
cmpq %rax, %r12
jne 0x7940
movswl -0x7a(%rbp), %ecx
movl -0x34(%rbp), %edi
movl %edi, %eax
imull %ecx, %eax
leal 0x7(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x3, %edx
movslq %edx, %rsi
movq %rbx, %rax
xorl %edx, %edx
divq %rsi
movzwl -0x84(%rbp), %esi
cmpw $0x2, %di
leaq -0x78(%rbp), %rdx
movq -0x30(%rbp), %rdi
movq %rdi, (%rdx)
movq %rax, 0x8(%rdx)
movl %ebx, 0x10(%rdx)
movw %si, 0x14(%rdx)
movb %cl, 0x16(%rdx)
sete 0x17(%rdx)
leal 0x30(%rbx), %eax
movq -0x40(%rbp), %rsi
movl %eax, 0x30(%rsi)
movq -0x48(%rbp), %rdi
callq 0x6bba
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq