// File: /workspace/github_c_makefile_star2_O3/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 0xacd1
leaq 0x47f40(%rip), %r14     # 0x6b298
movq %rax, 0x40(%r14)
leaq 0xcdfd(%rip), %rsi      # 0x30160
movq %rbx, %rdi
callq 0x2520
testq %rax, %rax
je 0x2364b
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2de47
movq (%r14), %rbx
movq 0x40(%r14), %rdi
movq %rbx, 0x38(%r14)
movq %rbx, %rsi
callq 0x2def6
movq %rax, 0x28(%r14)
movq %r12, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2e006
movq 0x10(%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x2e0b4
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rdx
movq %r12, %rdi
movq %rax, %rsi
callq 0x2e006
testq %rbx, %rbx
je 0x23685
movq %r12, -0x30(%rbp)
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
addq $0x8, %rcx
movq %rbx, %rdx
addq %rax, (%rcx)
addq $0x38, %rcx
decq %rdx
jne 0x233e8
movq (%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x2e1c2
movq %rax, 0x30(%r14)
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 0x2510
movl $0x1, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2e2d6
leal -0x1(%rbx), %eax
cmpl $0x4, %eax
ja 0x235fd
leaq 0xd025(%rip), %rcx      # 0x30494
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 0x2e38f
movq %rax, (%r13)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2e401
movq 0x10(%r13), %rax
movq %rax, -0x50(%rbp)
movups (%r13), %xmm0
movaps %xmm0, -0x60(%rbp)
movzwl 0x12(%r13), %esi
leaq -0x60(%rbp), %rdi
callq 0x20c75
movq (%r13), %rdi
jmp 0x23505
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2510
movq -0x40(%rbp), %r15
movl %r15d, %edi
callq 0x2e88b
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x2e8fd
movq -0x48(%rbp), %rdi
movq %r14, %rsi
callq 0x20d7d
movq %r14, %rdi
callq 0x2140
jmp 0x23632
addq $0x30, %r14
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2510
leaq 0x47d6d(%rip), %r14     # 0x6b298
movq 0x40(%r14), %rdi
movl 0x8(%r13), %esi
callq 0x2e9a8
movq %rax, 0x10(%r13)
movl 0x8(%r13), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eab9
movq 0x28(%r14), %rax
movq (%r13), %rcx
addq $0x28, %rax
xorl %edx, %edx
movq -0x38(%rbp), %rbx
cmpq %rcx, (%rax)
jne 0x23569
movq %rdx, (%r13)
movq %rdx, %rcx
incq %rdx
movl %edx, %esi
addq $0x38, %rax
cmpq %rsi, %rbx
ja 0x2355d
jmp 0x2363d
leaq 0x47d15(%rip), %r14     # 0x6b298
movq 0x40(%r14), %rdi
movl 0x10(%r13), %esi
callq 0x2e77e
movq %rax, (%r13)
movl 0x10(%r13), %edx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2e401
jmp 0x23639
leaq 0x47ce8(%rip), %r14     # 0x6b298
movq 0x40(%r14), %rdi
movq 0x10(%r13), %rsi
callq 0x2e4ac
movq %rax, 0x20(%r13)
movq 0x10(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2e5bd
movq 0x18(%r13), %rsi
testq %rsi, %rsi
movq -0x38(%rbp), %rbx
je 0x2363d
movq 0x40(%r14), %rdi
callq 0x2e66c
movq %rax, 0x28(%r13)
movq 0x18(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2e5bd
jmp 0x2363d
movq 0x2b9cc(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
movl %ebx, (%rsp)
leaq 0xd68e(%rip), %rdi      # 0x30c9f
leaq 0xdfbe(%rip), %rdx      # 0x315d6
movl $0x9f, %ecx
leaq 0x14906(%rip), %r8      # 0x37f2a
leaq 0xe09e(%rip), %r9       # 0x316c9
xorl %eax, %eax
callq 0x718c
leaq 0x47c5f(%rip), %r14     # 0x6b298
movq -0x38(%rbp), %rbx
incq %r12
cmpq %rbx, %r12
jne 0x2340b
jmp 0x23695
movq 0x2b97e(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
movq %rbx, (%rsp)
leaq 0xd63f(%rip), %rdi      # 0x30c9f
leaq 0xdf6f(%rip), %rdx      # 0x315d6
leaq 0x148bc(%rip), %r8      # 0x37f2a
leaq 0xe031(%rip), %r9       # 0x316a6
xorl %ebx, %ebx
movl $0x58, %ecx
xorl %eax, %eax
callq 0x718c
jmp 0x23697
movq (%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x2e1c2
movq %rax, 0x30(%r14)
movb $0x1, %bl
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq