// File: /workspace/github_c_makefile_star2_O2/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 0x9a17
leaq 0x44d91(%rip), %r14     # 0x662a0
movq %rax, 0x40(%r14)
leaq 0x9c46(%rip), %rsi      # 0x2b160
movq %rbx, %rdi
callq 0x2520
testq %rax, %rax
je 0x217f4
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x29b9a
movq %r15, %rbx
movq (%r14), %r15
movq 0x40(%r14), %rdi
movq %r15, 0x38(%r14)
movq %r15, %rsi
callq 0x29c42
movq %rax, 0x28(%r14)
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x29d05
movq 0x10(%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x29dae
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rdx
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x29d05
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
addq $0x8, %rcx
movq %r15, %rdx
subq $0x1, %rdx
jb 0x215a6
addq %rax, (%rcx)
addq $0x38, %rcx
jmp 0x21597
movq (%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x29e74
movq %rax, 0x30(%r14)
xorl %esi, %esi
cmpq %r15, %rsi
je 0x217f0
movq 0x28(%r14), %rax
imulq $0x38, %rsi, %rcx
movl (%rax,%rcx), %r12d
movq %r14, %rdx
movq 0x20(%rax,%rcx), %r14
movq 0x28(%rax,%rcx), %rdi
movq %rdi, -0x40(%rbp)
movl 0x30(%rax,%rcx), %eax
movq %rax, -0x38(%rbp)
movq %rsi, -0x48(%rbp)
imulq $0x30, %rsi, %r13
addq 0x30(%rdx), %r13
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2510
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x29f37
leal -0x1(%r12), %eax
cmpl $0x4, %eax
ja 0x217a7
leaq 0x9e4e(%rip), %rcx      # 0x2b474
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 0x29fdf
movq %rax, (%r13)
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x2a013
movq 0x10(%r13), %rax
movq %rax, -0x50(%rbp)
movups (%r13), %xmm0
movaps %xmm0, -0x60(%rbp)
movzwl 0x12(%r13), %esi
leaq -0x60(%rbp), %rdi
callq 0x1f5f9
movq (%r13), %rdi
jmp 0x216bc
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2510
movq -0x38(%rbp), %r12
movl %r12d, %edi
callq 0x2a3b9
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x2a3ed
movq -0x40(%rbp), %rdi
movq %r14, %rsi
callq 0x1f6ff
movq %r14, %rdi
callq 0x2140
jmp 0x217dd
addq $0x30, %r14
xorl %ebx, %ebx
movq -0x30(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2510
leaq 0x44bbc(%rip), %r14     # 0x662a0
movq 0x40(%r14), %rdi
movl 0x8(%r13), %esi
callq 0x2a492
movq %rax, 0x10(%r13)
movl 0x8(%r13), %edx
movq %r12, %rdi
movq %rax, %rsi
callq 0x2a55b
movq 0x28(%r14), %rax
movl %ebx, %ebx
cmpq %rbx, %r15
jbe 0x217e4
imulq $0x38, %rbx, %rcx
movq 0x28(%rax,%rcx), %rcx
cmpq (%r13), %rcx
jne 0x21726
movq %rbx, (%r13)
incl %ebx
jmp 0x21708
leaq 0x44b6f(%rip), %r14     # 0x662a0
movq 0x40(%r14), %rdi
movl 0x10(%r13), %esi
callq 0x2a2f4
movq %rax, (%r13)
movl 0x10(%r13), %edx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2a013
jmp 0x217e4
leaq 0x44b42(%rip), %r14     # 0x662a0
movq 0x40(%r14), %rdi
movq 0x10(%r13), %rsi
callq 0x2a0b8
movq %rax, 0x20(%r13)
movq 0x10(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2a181
movq 0x18(%r13), %rsi
testq %rsi, %rsi
je 0x217e4
movq 0x40(%r14), %rdi
callq 0x2a22a
movq %rax, 0x28(%r13)
movq 0x18(%r13), %rdx
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2a181
jmp 0x217e4
movq 0x28822(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
movl %r12d, (%rsp)
leaq 0xa4bb(%rip), %rdi      # 0x2bc77
leaq 0xadeb(%rip), %rdx      # 0x2c5ae
movl $0x9f, %ecx
leaq 0x11733(%rip), %r8      # 0x32f02
leaq 0xaecb(%rip), %r9       # 0x2c6a1
xorl %eax, %eax
callq 0x683a
leaq 0x44abc(%rip), %r14     # 0x662a0
movq -0x48(%rbp), %rsi
incq %rsi
jmp 0x215b8
movb $0x1, %bl
jmp 0x2182a
movq 0x287d5(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
movq %rbx, (%rsp)
leaq 0xa46e(%rip), %rdi      # 0x2bc77
leaq 0xad9e(%rip), %rdx      # 0x2c5ae
leaq 0x116eb(%rip), %r8      # 0x32f02
leaq 0xae60(%rip), %r9       # 0x2c67e
xorl %ebx, %ebx
pushq $0x58
popq %rcx
xorl %eax, %eax
callq 0x683a
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq