// File: /workspace/github_c_makefile_star2_O2/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 0xcc42(%rip), %rsi      # 0x13885
callq 0x2250
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x2240
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
movq %r15, %rdi
callq 0x2070
leaq -0x9c(%rbp), %rbx
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x2340
movswl 0x14(%rbx), %edx
movl %edx, %eax
andl $0xfffd, %eax           # imm = 0xFFFD
cmpw $0x1, %ax
jne 0x6cad
movswl -0x86(%rbp), %eax
cmpl $0x2, %eax
jle 0x6cd5
movq 0x8(%r14), %rsi
leaq 0xce32(%rip), %rdi      # 0x13add
jmp 0x6cb8
movq 0x8(%r14), %rsi
leaq 0xcdeb(%rip), %rdi      # 0x13aa3
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f0
movl %eax, -0x34(%rbp)
movq %r14, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
leaq -0x78(%rbp), %r14
pushq $0x8
popq %r13
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x2030
cmpq %rax, %r12
je 0x6d8a
movq %r14, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2340
cmpb $0x64, -0x78(%rbp)
jne 0x6d46
cmpb $0x61, -0x77(%rbp)
jne 0x6d46
cmpb $0x74, -0x76(%rbp)
jne 0x6d46
cmpb $0x61, -0x75(%rbp)
jne 0x6d46
movq %r12, %r14
movslq -0x74(%rbp), %r12
leaq (%rbx,%r12), %r13
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x6d57
movq %r13, %rsi
callq 0x23b0
jmp 0x6d5f
movslq -0x74(%rbp), %rsi
movq %r15, %rdi
pushq $0x1
popq %rdx
callq 0x2240
jmp 0x6cee
movq %r13, %rdi
callq 0x22b0
movq %rax, -0x30(%rbp)
addq %rax, %rbx
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2340
movq %r13, %rbx
movq %r14, %r12
leaq -0x78(%rbp), %r14
pushq $0x8
popq %r13
jmp 0x6cee
movswl -0x7a(%rbp), %ecx
movl -0x34(%rbp), %edi
movl %edi, %eax
imull %ecx, %eax
pushq $0x8
popq %rsi
cltd
idivl %esi
movslq %eax, %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 0x61e5
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq