// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
xorl %edi, %edi
callq 0x10890
movq %rax, %rcx
leaq 0x54b80(%rip), %rax     # 0x852d0
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rdi
leaq 0x14a01(%rip), %rsi     # 0x45160
callq 0x2520
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x307b2
movq 0x3886a(%rip), %rax     # 0x68fe0
movq (%rax), %rsi
movq -0x10(%rbp), %rax
leaq 0x154eb(%rip), %rdi     # 0x45c6f
leaq 0x15e1b(%rip), %rdx     # 0x465a6
movl $0x58, %ecx
leaq 0x1c888(%rip), %r8      # 0x4d01f
leaq 0x15ed8(%rip), %r9      # 0x46676
movq %rax, (%rsp)
movb $0x0, %al
callq 0xb100
movb $0x0, -0x1(%rbp)
jmp 0x30c71
movq -0x18(%rbp), %rdi
leaq 0x54b13(%rip), %rsi     # 0x852d0
movl $0x1, %edx
callq 0x423b0
leaq 0x54b02(%rip), %rax     # 0x852d0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
leaq 0x54af0(%rip), %rax     # 0x852d0
movq %rcx, 0x38(%rax)
leaq 0x54ae5(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x42490
movq %rax, %rcx
leaq 0x54ace(%rip), %rax     # 0x852d0
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rdi
leaq 0x54abf(%rip), %rax     # 0x852d0
movq 0x28(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x425c0
leaq 0x54aab(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
leaq 0x54aa0(%rip), %rax     # 0x852d0
movq 0x10(%rax), %rsi
callq 0x426a0
movq %rax, %rcx
leaq 0x54a8d(%rip), %rax     # 0x852d0
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rdi
leaq 0x54a7e(%rip), %rax     # 0x852d0
movq 0x20(%rax), %rsi
leaq 0x54a73(%rip), %rax     # 0x852d0
movq 0x10(%rax), %rdx
callq 0x425c0
leaq 0x54a63(%rip), %rax     # 0x852d0
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x308b4
movq -0x28(%rbp), %rcx
leaq 0x54a3e(%rip), %rax     # 0x852d0
movq 0x28(%rax), %rax
imulq $0x38, -0x30(%rbp), %rdx
addq %rdx, %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x3087d
leaq 0x54a15(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
leaq 0x54a0a(%rip), %rax     # 0x852d0
movq (%rax), %rsi
callq 0x427d0
movq %rax, %rcx
leaq 0x549f8(%rip), %rax     # 0x852d0
movq %rcx, 0x30(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x30c6d
leaq 0x549d7(%rip), %rax     # 0x852d0
movq 0x28(%rax), %rcx
movq -0x38(%rbp), %rdx
imulq $0x38, %rdx, %rdx
movq 0x30(%rcx,%rdx), %rsi
movq %rsi, -0x40(%rbp)
movups (%rcx,%rdx), %xmm0
movups 0x10(%rcx,%rdx), %xmm1
movups 0x20(%rcx,%rdx), %xmm2
movaps %xmm2, -0x50(%rbp)
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x2510
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl $0x1, %edx
callq 0x42900
movl -0x70(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x4, %eax
ja 0x30c22
movq -0xc0(%rbp), %rax
leaq 0x14afb(%rip), %rcx     # 0x45480
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x78(%rbp), %rdi
callq 0x429e0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl %eax, %edi
callq 0x42a00
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rax
movq (%rax), %rsi
movq -0x80(%rbp), %rdx
callq 0x42a60
movq -0x78(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x98(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x90(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movzwl 0x12(%rax), %edi
leaq -0x98(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x2e400
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0x42b40
jmp 0x30c5a
leaq 0x548a3(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x42b60
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x42c90
movq -0x78(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x30aa9
leaq 0x5485c(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x42d70
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x42c90
jmp 0x30c5a
leaq 0x5481b(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
movq -0x78(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %esi
callq 0x42ea0
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rax
movq (%rax), %rsi
movq -0x78(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0x42a60
jmp 0x30c5a
movq -0x18(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x2510
movl -0x40(%rbp), %eax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl %eax, %edi
callq 0x42fd0
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x43030
movq -0x48(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x2e450
movq -0xa8(%rbp), %rdi
callq 0x43110
jmp 0x30c5a
movq -0x18(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x30, %rsi
xorl %edx, %edx
callq 0x2510
movq -0x78(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq 0x54756(%rip), %rax     # 0x852d0
movq 0x40(%rax), %rdi
movq -0xb0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %esi
callq 0x43130
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdi
movq -0xb0(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0xb0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x43260
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x30c20
leaq 0x546f6(%rip), %rax     # 0x852d0
movq 0x28(%rax), %rax
movl -0xb4(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x28(%rax), %rax
movq -0xb0(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x30c0d
movl -0xb4(%rbp), %eax
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movq %rcx, (%rax)
jmp 0x30c0f
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x30bc7
jmp 0x30c5a
movq 0x383b7(%rip), %rax     # 0x68fe0
movq (%rax), %rsi
movl -0x70(%rbp), %eax
leaq 0x15039(%rip), %rdi     # 0x45c6f
leaq 0x15969(%rip), %rdx     # 0x465a6
movl $0x9f, %ecx
leaq 0x1c3d6(%rip), %r8      # 0x4d01f
leaq 0x15a49(%rip), %r9      # 0x46699
movl %eax, (%rsp)
movb $0x0, %al
callq 0xb100
jmp 0x30c5c
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x308e4
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)