// File: /workspace/github_c_makefile_star2_O0/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
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x20263(%rip), %rsi     # 0x28745
callq 0x22f0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x22e0
movq -0x18(%rbp), %rdi
callq 0x2030
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2070
movq -0x18(%rbp), %rcx
leaq -0x44(%rbp), %rdi
movl $0x24, %esi
movl $0x1, %edx
callq 0x23e0
movswl -0x30(%rbp), %eax
cmpl $0x1, %eax
je 0x8564
movswl -0x30(%rbp), %eax
cmpl $0x3, %eax
je 0x8564
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movswl -0x30(%rbp), %edx
leaq 0x20414(%rip), %rdi     # 0x28963
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rdi
callq 0x2260
jmp 0x8724
movswl -0x2e(%rbp), %eax
cmpl $0x2, %eax
jle 0x8595
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movswl -0x30(%rbp), %edx
leaq 0x2041d(%rip), %rdi     # 0x2899d
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rdi
callq 0x2260
jmp 0x8724
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2030
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
je 0x867b
movq -0x18(%rbp), %rcx
leaq -0x60(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x23e0
movsbl -0x60(%rbp), %eax
cmpl $0x64, %eax
jne 0x8664
movsbl -0x5f(%rbp), %eax
cmpl $0x61, %eax
jne 0x8664
movsbl -0x5e(%rbp), %eax
cmpl $0x74, %eax
jne 0x8664
movsbl -0x5d(%rbp), %eax
cmpl $0x61, %eax
jne 0x8664
cmpq $0x0, -0x58(%rbp)
jne 0x8624
movq -0x50(%rbp), %rdi
movslq -0x5c(%rbp), %rax
addq %rax, %rdi
callq 0x2350
movq %rax, -0x58(%rbp)
jmp 0x863c
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movslq -0x5c(%rbp), %rax
addq %rax, %rsi
callq 0x2450
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
addq -0x50(%rbp), %rdi
movslq -0x5c(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x1, %esi
callq 0x23e0
movslq -0x5c(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x8676
movq -0x18(%rbp), %rdi
movslq -0x5c(%rbp), %rsi
movl $0x1, %edx
callq 0x22e0
jmp 0x85a5
movq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xb8(%rbp)
movswl -0x2e(%rbp), %eax
movswl -0x22(%rbp), %ecx
imull %ecx, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0xb8(%rbp), %rax
movslq %ecx, %rcx
xorl %edx, %edx
divq %rcx
movq %rax, -0x70(%rbp)
movl -0x2c(%rbp), %eax
movw %ax, -0x64(%rbp)
movw -0x22(%rbp), %ax
movb %al, -0x62(%rbp)
movswl -0x2e(%rbp), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
setl %al
andb $0x1, %al
movb %al, -0x61(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x50(%rbp), %rax
addq $0x30, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
callq 0x7680
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)