// File: /workspace/github_c_makefile_star2_O1/FredTheDino[P]Fog/src/engine/asset/asset.cpp
// Function: Asset::load(char const*)
b8 load(const char *file_path) {
    system.arena = Util::request_arena();
    FILE *file = fopen(file_path, "rb");
    if (!file) {
        ERR("Failed to open resource file! (%s)", file_path);
        return false;
    }

    read_from_file<FileHeader>(file, &system.file_header);
    u64 num_assets = system.file_header.number_of_assets;
    system.num_assets = num_assets;

    system.headers = system.arena->push<Header>(num_assets);
    read_from_file<Header>(file, system.headers, num_assets);

    system.strings = system.arena->push<char>(
        system.file_header.size_of_strings);
    read_from_file<Header>(file, system.strings,
                                system.file_header.size_of_strings);

    char *read_head = system.strings;
    for (u64 asset = 0; asset < num_assets; asset++)
        system.headers[asset].file_path += (u64) read_head;

    system.assets = system.arena->push<Data>(
        system.file_header.number_of_assets);
    for (u64 asset = 0; asset < num_assets; asset++) {
        Header header = system.headers[asset];
        Data *asset_ptr = &system.assets[asset];
        fseek(file, header.offset, SEEK_SET);
        read_from_file<Data>(file, asset_ptr);
        switch (header.type) {
        case Type::TEXTURE: {
            u64 size = asset_ptr->image.size();
            asset_ptr->image.data = Util::push_memory<u8>(size);
            read_from_file<u8>(file, asset_ptr->image.data, size);
            Renderer::upload_texture(asset_ptr->image, asset_ptr->image.id);
            Util::pop_memory(asset_ptr->image.data);
        } break;
        case Type::FONT: {
            asset_ptr->font.glyphs =
                system.arena->push<Font::Glyph>(asset_ptr->font.num_glyphs);
            read_from_file<Font::Glyph>(file, asset_ptr->font.glyphs,
                                        asset_ptr->font.num_glyphs);
            if (asset_ptr->font.num_kernings) {
                asset_ptr->font.kernings =
                    system.arena->push<Font::Kerning>(asset_ptr->font.num_kernings);
                read_from_file<Font::Glyph>(file, asset_ptr->font.kernings,
                        asset_ptr->font.num_kernings);
            }
        } break;
        case Type::SOUND: {
            asset_ptr->sound.data = system.arena->push<u8>(asset_ptr->sound.size);
            read_from_file<u8>(file, asset_ptr->sound.data, asset_ptr->sound.size);
        } break;
        case Type::SHADER: {
            fseek(file, header.offset, SEEK_SET);
            u64 size = header.asset_size;
            char *src = Util::push_memory<char>(size);
            read_from_file<char>(file, src, size);
            Renderer::upload_shader(header.hash, src);
            Util::pop_memory(src);
        } break;
        case Type::SPRITE: {
            fseek(file, header.offset + sizeof(Data), SEEK_SET);
            Sprite *sprite = &asset_ptr->sprite;
            sprite->points = system.arena->push<Vec4>(sprite->num_points);
            read_from_file<Vec4>(file, sprite->points, sprite->num_points);
            for (u32 i = 0; i < num_assets; i++) {
                if (system.headers[i].hash == sprite->sprite_sheet) {
                    sprite->sprite_sheet = i;
                }
            }
        } break;
        default:
            ERR("UNKOWN ASSET TYPE %d", header.type);
            break;
        };
    }
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
xorl %edi, %edi
callq 0xaf5a
leaq 0x47c9f(%rip), %r14     # 0x6b2a8
movq %rax, 0x40(%r14)
leaq 0xcb4c(%rip), %rsi      # 0x30160
movq %rbx, %rdi
callq 0x2530
testq %rax, %rax
je 0x23907
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2e5c5
movq (%r14), %rbx
movq 0x40(%r14), %rdi
movq %rbx, 0x38(%r14)
movq %rbx, %rsi
callq 0x2e674
movq %rax, 0x28(%r14)
movq %r12, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2e774
movq 0x10(%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x2e822
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rdx
movq %r12, %rdi
movq %rax, %rsi
callq 0x2e774
testq %rbx, %rbx
je 0x2369d
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
addq $0x8, %rcx
movq %rbx, %rdx
addq %rax, (%rcx)
addq $0x38, %rcx
decq %rdx
jne 0x23691
movq (%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x2e922
movq %rax, 0x30(%r14)
movb $0x1, %r15b
testq %rbx, %rbx
je 0x23940
movq %r12, -0x30(%rbp)
xorl %r12d, %r12d
movq %rbx, -0x38(%rbp)
movq 0x28(%r14), %rax
imulq $0x38, %r12, %rcx
movl (%rax,%rcx), %ebx
movq %r14, %rdx
movq 0x20(%rax,%rcx), %r14
movq 0x28(%rax,%rcx), %rsi
movq %rsi, -0x48(%rbp)
movl 0x30(%rax,%rcx), %eax
movq %rax, -0x40(%rbp)
leaq (%r12,%r12,2), %r13
shlq $0x4, %r13
addq 0x30(%rdx), %r13
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2520
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2ea26
leal -0x1(%rbx), %eax
cmpl $0x4, %eax
ja 0x238b6
leaq 0xcd7c(%rip), %rcx      # 0x304a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0xc(%r13), %eax
imull 0x8(%r13), %eax
movzbl 0x10(%r13), %ebx
imull %eax, %ebx
movl %ebx, %edi
callq 0x2eadf
movq %rax, (%r13)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2eb51
movq 0x10(%r13), %rax
movq %rax, -0x50(%rbp)
movups (%r13), %xmm0
movaps %xmm0, -0x60(%rbp)
movzwl 0x12(%r13), %esi
leaq -0x60(%rbp), %rdi
callq 0x20f72
movq (%r13), %rdi
jmp 0x237be
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2520
movq -0x40(%rbp), %r15
movl %r15d, %edi
callq 0x2efb1
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x2f023
movq -0x48(%rbp), %rdi
movq %r14, %rsi
callq 0x2107a
movq %r14, %rdi
callq 0x2140
jmp 0x238eb
addq $0x30, %r14
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2520
leaq 0x47ac4(%rip), %r14     # 0x6b2a8
movq 0x40(%r14), %rdi
movl 0x8(%r13), %esi
callq 0x2f0ce
movq %rax, 0x10(%r13)
movl 0x8(%r13), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2f1d1
movq 0x28(%r14), %rax
movq (%r13), %rcx
addq $0x28, %rax
xorl %edx, %edx
movq -0x38(%rbp), %rbx
cmpq %rcx, (%rax)
jne 0x23822
movq %rdx, (%r13)
movq %rdx, %rcx
incq %rdx
movl %edx, %esi
addq $0x38, %rax
cmpq %rsi, %rbx
ja 0x23816
jmp 0x238f6
leaq 0x47a6c(%rip), %r14     # 0x6b2a8
movq 0x40(%r14), %rdi
movl 0x10(%r13), %esi
callq 0x2eeb2
movq %rax, (%r13)
movl 0x10(%r13), %edx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2eb51
jmp 0x238f2
leaq 0x47a3f(%rip), %r14     # 0x6b2a8
movq 0x40(%r14), %rdi
movq 0x10(%r13), %rsi
callq 0x2ebfc
movq %rax, 0x20(%r13)
movq 0x10(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2ecff
movq 0x18(%r13), %rsi
testq %rsi, %rsi
movq -0x38(%rbp), %rbx
je 0x238f6
movq 0x40(%r14), %rdi
callq 0x2edae
movq %rax, 0x28(%r13)
movq 0x18(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2ecff
jmp 0x238f6
movq 0x2b713(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
movl %ebx, (%rsp)
leaq 0xd3e5(%rip), %rdi      # 0x30caf
leaq 0xdd15(%rip), %rdx      # 0x315e6
movl $0x9f, %ecx
leaq 0x1465d(%rip), %r8      # 0x37f3a
leaq 0xddf5(%rip), %r9       # 0x316d9
xorl %eax, %eax
callq 0x711f
leaq 0x479b6(%rip), %r14     # 0x6b2a8
movq -0x38(%rbp), %rbx
incq %r12
cmpq %rbx, %r12
jne 0x236c4
movb $0x1, %r15b
jmp 0x23940
movq 0x2b6c2(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
movq %rbx, (%rsp)
leaq 0xd393(%rip), %rdi      # 0x30caf
leaq 0xdcc3(%rip), %rdx      # 0x315e6
leaq 0x14610(%rip), %r8      # 0x37f3a
leaq 0xdd85(%rip), %r9       # 0x316b6
xorl %r15d, %r15d
movl $0x58, %ecx
xorl %eax, %eax
callq 0x711f
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq