// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r14
movq %rdi, %r13
movq 0x8(%rsi), %rdi
leaq 0x12f04(%rip), %rsi     # 0x1a8a5
callq 0x2210
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2200
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
movq %r15, %rdi
callq 0x2070
leaq -0x8c(%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 0x7a0c
cmpw $0x2, -0x76(%rbp)
jle 0x7a34
movq 0x8(%r14), %rsi
leaq 0x130f3(%rip), %rdi     # 0x1aafd
jmp 0x7a17
movq 0x8(%r14), %rsi
leaq 0x130ac(%rip), %rdi     # 0x1aac3
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21b0
movq %r13, -0x30(%rbp)
movq %r14, -0x38(%rbp)
movq %r15, %rdi
callq 0x2030
xorl %r14d, %r14d
movl $0x0, %r13d
cmpq %rax, %r12
je 0x7af2
xorl %r13d, %r13d
leaq -0x68(%rbp), %rbx
xorl %r14d, %r14d
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x22f0
cmpb $0x64, -0x68(%rbp)
jne 0x7aa3
cmpb $0x61, -0x67(%rbp)
jne 0x7aa3
cmpb $0x74, -0x66(%rbp)
jne 0x7aa3
cmpb $0x61, -0x65(%rbp)
jne 0x7aa3
testq %r14, %r14
je 0x7ab6
movslq -0x64(%rbp), %rsi
addq %r13, %rsi
movq %r14, %rdi
callq 0x2360
jmp 0x7ac2
movslq -0x64(%rbp), %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0x2200
jmp 0x7ae1
movslq -0x64(%rbp), %rdi
addq %r13, %rdi
callq 0x2260
movq %rax, %r14
leaq (%rax,%r13), %rdi
movslq -0x64(%rbp), %rdx
movl $0x1, %esi
movq %r15, %rcx
callq 0x22f0
movslq -0x64(%rbp), %rax
addq %rax, %r13
movq %r15, %rdi
callq 0x2030
cmpq %rax, %r12
jne 0x7a60
movswl -0x76(%rbp), %ecx
movswl -0x6a(%rbp), %esi
movl %esi, %eax
imull %ecx, %eax
leal 0x7(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x3, %edx
movslq %edx, %rdi
movq %r13, %rax
xorl %edx, %edx
divq %rdi
cmpl $0x2, %ecx
movzwl -0x74(%rbp), %ecx
leaq -0x68(%rbp), %rdx
movq %r14, (%rdx)
movq %rax, 0x8(%rdx)
movl %r13d, 0x10(%rdx)
movw %cx, 0x14(%rdx)
movb %sil, 0x16(%rdx)
setge 0x17(%rdx)
leal 0x30(%r13), %eax
movq -0x38(%rbp), %rsi
movl %eax, 0x30(%rsi)
movq -0x30(%rbp), %rdi
callq 0x6c83
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq