// File: /workspace/github_c_makefile_star2_O0/FredTheDino[P]Fog/src/engine/unix_assets.cpp
// Function: load_sprite(AssetFile*, Asset::Header*)
void load_sprite(AssetFile *file, Asset::Header *main_header) {
    Asset::Header header_template;
    header_template.type = Asset::Type::SPRITE;
    header_template.asset_id = Asset::ASSET_ID_NO_ASSET;

    FILE *sprite_file = fopen(main_header->file_path, "rb");
    while (sprite_file) {
        char *line = nullptr;
        size_t line_n = 0;
        getline(&line, &line_n, sprite_file);

        if (feof(sprite_file))
            break;

        char *ptr = line;
        u32 i = 0;
        char *name = ptr + i;
        for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }
        char *res = ptr + i + 1;
        for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }
        char *num = ptr + i + 1;
        for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }
        char *points_str = ptr + i + 1;
        for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }

        // TODO(ed): Parse points, and stuff
#define CHECK_FOR_ERROR(ptr)                                  \
    do {                                                      \
        if (ptr == endptr) {                                  \
            ERR("Failed to parse number in sprite file '%s'", \
                main_header->file_path);                      \
            fclose(sprite_file);                              \
            return;                                           \
        }                                                     \
    } while (false);

        char *endptr;
        u32 num_points = strtol(num, &endptr, 10);
        CHECK_FOR_ERROR(num);
        u32 size = sizeof(Vec4) * num_points;
        Vec4 *points = (Vec4 *) malloc(size);
        for (u32 p = 0; p < num_points; p++) {
            char *endptr;
            Vec4 point;
            while (isspace(*points_str)) points_str++;
            points_str += 1; // (
            for (u32 coord = 0; coord < 4; coord++) {
                point._[coord] = strtod(points_str, &endptr);
                CHECK_FOR_ERROR(points_str);
                points_str = endptr;
            }
            points_str += 1; // )
            points[p] = point;
        }
#undef CHECK_FOR_ERROR

        u32 resource_hash = res[0] == '#' ? atoi(res + 1): Asset::asset_hash(res);

        Asset::Header header = header_template;
        header.file_path = name;
        header.file_path_length = strlen(name);
        header.hash = resource_hash;
        header.asset_size = sizeof(Asset::Data) + size;
        Sprite sprite = {
            resource_hash,
            num_points,
            points,
        };
        Asset::Data asset = {.sprite = sprite};
        add_asset_to_file(file, &header, &asset);
    }
    fclose(sprite_file);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x3, -0x48(%rbp)
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x20705(%rip), %rsi     # 0x28745
callq 0x22f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x84ac
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x50(%rbp), %rdx
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2610
movq -0x50(%rbp), %rdi
callq 0x2400
cmpl $0x0, %eax
je 0x8088
jmp 0x84ac
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x80d8
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movsbl (%rax,%rcx), %eax
cmpl $0x3a, %eax
jne 0x80cb
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movb $0x0, (%rax,%rcx)
jmp 0x80d8
jmp 0x80cd
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x80a5
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x811d
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movsbl (%rax,%rcx), %eax
cmpl $0x3a, %eax
jne 0x8110
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movb $0x0, (%rax,%rcx)
jmp 0x811d
jmp 0x8112
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x80ea
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x8165
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movsbl (%rax,%rcx), %eax
cmpl $0x3a, %eax
jne 0x8158
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movb $0x0, (%rax,%rcx)
jmp 0x8165
jmp 0x815a
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x8132
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x81ad
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movsbl (%rax,%rcx), %eax
cmpl $0x3a, %eax
jne 0x81a0
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
movb $0x0, (%rax,%rcx)
jmp 0x81ad
jmp 0x81a2
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x817a
movq -0x88(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0xa, %edx
callq 0x23a0
movl %eax, -0x9c(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x98(%rbp), %rax
jne 0x8227
movq 0x2adfe(%rip), %rax     # 0x32fe0
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
leaq 0x20734(%rip), %rdi     # 0x28928
leaq 0x20559(%rip), %rdx     # 0x28754
movl $0x109, %ecx            # imm = 0x109
leaq 0x20725(%rip), %r8      # 0x2892c
leaq 0x2072a(%rip), %r9      # 0x28938
movq %rax, (%rsp)
movb $0x0, %al
callq 0x27a0
movq -0x50(%rbp), %rdi
callq 0x2260
jmp 0x84b5
jmp 0x8229
movl -0x9c(%rbp), %eax
shlq $0x4, %rax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, %edi
callq 0x2350
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0x83b1
jmp 0x826b
movq -0x90(%rbp), %rax
movsbl (%rax), %edi
callq 0x2060
cmpl $0x0, %eax
je 0x8293
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x826b
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0xcc(%rbp)
cmpl $0x4, -0xcc(%rbp)
jae 0x8362
movq -0x90(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x21d0
cvtsd2ss %xmm0, %xmm0
movl -0xcc(%rbp), %eax
movss %xmm0, -0xc8(%rbp,%rax,4)
movq -0x90(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jne 0x833e
movq 0x2ace7(%rip), %rax     # 0x32fe0
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
leaq 0x2061d(%rip), %rdi     # 0x28928
leaq 0x20442(%rip), %rdx     # 0x28754
movl $0x113, %ecx            # imm = 0x113
leaq 0x2060e(%rip), %r8      # 0x2892c
leaq 0x20613(%rip), %r9      # 0x28938
movq %rax, (%rsp)
movb $0x0, %al
callq 0x27a0
movq -0x50(%rbp), %rdi
callq 0x2260
jmp 0x84b5
jmp 0x8340
movq -0xb8(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x82af
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0xa8(%rbp), %rax
movl -0xac(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0xc8(%rbp), %rcx
movq %rcx, (%rax)
movq -0xc0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x8257
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x83d5
movq -0x80(%rbp), %rdi
addq $0x1, %rdi
callq 0x2410
cltq
movq %rax, -0x158(%rbp)
jmp 0x83e5
movq -0x80(%rbp), %rdi
callq 0x5b80
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movl %eax, -0xd0(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x38, %edx
callq 0x2200
movq -0x78(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x78(%rbp), %rdi
callq 0x2110
movq %rax, -0xf8(%rbp)
movl -0xd0(%rbp), %eax
movq %rax, -0xe0(%rbp)
movl -0xa0(%rbp), %eax
addq $0x30, %rax
movl %eax, -0xd8(%rbp)
movl -0xd0(%rbp), %eax
movq %rax, -0x120(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0x118(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x150(%rbp), %rdx
callq 0x7680
jmp 0x8049
movq -0x50(%rbp), %rdi
callq 0x2260
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nop