// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rsi), %rdi
leaq 0x123eb(%rip), %rsi     # 0x198a5
callq 0x2220
movq %rax, %rbx
testq %rax, %rax
je 0x74eb
xorl %eax, %eax
leaq -0x40(%rbp), %rdi
movq %rax, (%rdi)
leaq -0x30(%rbp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdx
callq 0x257c
movq %rbx, %rdi
callq 0x2310
testl %eax, %eax
je 0x74f3
movq %rbx, %rdi
jmp 0x7802
leaq -0x68(%rbp), %rsi
movq %rbx, -0x38(%rbp)
movq %r14, -0x58(%rbp)
movq %r15, -0x48(%rbp)
movq -0x40(%rbp), %r12
movq -0x30(%rbp), %rax
testq %rax, %rax
je 0x752a
xorl %edx, %edx
xorl %ecx, %ecx
cmpb $0x3a, (%r12,%rdx)
je 0x753e
incl %ecx
movq %rcx, %rdx
cmpq %rcx, %rax
ja 0x7514
movq %rcx, %rdx
jmp 0x7547
leaq 0x1(%r12), %rbx
movq %r12, %r14
movq %r12, -0x50(%rbp)
movq %rbx, %r13
jmp 0x75de
movb $0x0, (%r12,%rdx)
movq -0x30(%rbp), %rax
movl %ecx, %r8d
cmpq %r8, %rax
jbe 0x7568
cmpb $0x3a, (%r12,%r8)
je 0x756d
incl %ecx
movq %rcx, %r8
cmpq %rcx, %rax
ja 0x754f
movq %rcx, %r14
movq %rcx, %r8
jmp 0x7579
movq %r8, %r14
jmp 0x7579
movb $0x0, (%r12,%r8)
movq -0x30(%rbp), %rax
movl %ecx, %r14d
cmpq %r14, %rax
jbe 0x7597
cmpb $0x3a, (%r12,%r14)
je 0x759c
incl %ecx
movq %rcx, %r14
cmpq %rcx, %rax
ja 0x757e
movq %rcx, %rdi
movq %rcx, %r14
jmp 0x75a7
movq %r14, %rdi
jmp 0x75a7
movb $0x0, (%r12,%r14)
movq -0x30(%rbp), %rax
movl %ecx, %edi
leaq (%r12,%rdx), %r9
leaq (%r12,%rdx), %rbx
incq %rbx
leaq (%r12,%r8), %r13
incq %r13
addq %r12, %r14
cmpq %rdi, %rax
movq %r9, -0x50(%rbp)
jbe 0x75de
incl %ecx
cmpb $0x3a, (%r12,%rdi)
je 0x75d9
movl %ecx, %edi
incl %ecx
cmpq %rdi, %rax
ja 0x75c7
jmp 0x75de
movb $0x0, (%r12,%rdi)
movq %r13, %rdi
movl $0xa, %edx
callq 0x22b0
cmpq -0x68(%rbp), %r13
je 0x7819
movq %rax, %r15
shlq $0x4, %rax
movq %rax, -0x80(%rbp)
movl %eax, %edi
callq 0x2270
movq %rax, -0x60(%rbp)
testl %r15d, %r15d
je 0x76a1
movl %r15d, %eax
movq %rax, -0x70(%rbp)
xorl %edx, %edx
movq %r15, -0x78(%rbp)
movq %rdx, -0x88(%rbp)
incq %r14
movsbl (%r14), %edi
callq 0x2060
incq %r14
testl %eax, %eax
jne 0x762b
xorl %r15d, %r15d
movq %r14, %r13
movq %r14, %rdi
leaq -0xf0(%rbp), %rsi
callq 0x2160
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xc0(%rbp,%r15,4)
movq -0xf0(%rbp), %r14
cmpq %r14, %r13
je 0x77c0
incq %r15
cmpq $0x4, %r15
jne 0x763e
movq -0x88(%rbp), %rdx
movq %rdx, %rax
shlq $0x4, %rax
movups -0xc0(%rbp), %xmm0
movq -0x60(%rbp), %rcx
movups %xmm0, (%rcx,%rax)
incq %rdx
cmpq -0x70(%rbp), %rdx
movq -0x78(%rbp), %r15
jne 0x7621
movq %r15, %r13
movzbl (%rbx), %eax
testl %eax, %eax
je 0x76c6
cmpl $0x23, %eax
movq -0x48(%rbp), %r15
jne 0x76d2
movq -0x50(%rbp), %rdi
addq $0x2, %rdi
callq 0x2320
movl %eax, %r14d
jmp 0x7719
movl $0x14e7, %r14d          # imm = 0x14E7
movq -0x48(%rbp), %r15
jmp 0x7719
incq %rbx
movl $0x14e7, %r14d          # imm = 0x14E7
movsbq %al, %rax
incq %r14
imulq %rax, %r14
movb (%rbx), %al
incq %rbx
testb %al, %al
jne 0x76db
movq %r14, %rax
movabsq $0x1000000101, %rcx     # imm = 0x1000000101
mulq %rcx
movq %r14, %rax
subq %rdx, %rax
shrq %rax
addq %rdx, %rax
shrq $0x1b, %rax
movl %eax, %ecx
shll $0x1c, %ecx
subl %ecx, %eax
addl %eax, %r14d
movl $0x3, -0xc0(%rbp)
movl $0xffffffff, -0x8c(%rbp) # imm = 0xFFFFFFFF
movq %r12, -0xb8(%rbp)
movq %r12, %rdi
callq 0x20e0
movq %rax, -0xb0(%rbp)
movl %r14d, %eax
movq %rax, -0x98(%rbp)
movq -0x80(%rbp), %rcx
addl $0x30, %ecx
movl %ecx, -0x90(%rbp)
movq %rax, -0xf0(%rbp)
movl %r13d, -0xe8(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq %r15, %rdi
leaq -0xc0(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0x6bba
xorl %eax, %eax
movq %rax, -0x40(%rbp)
movq %rax, -0x30(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rdx
callq 0x257c
movq %rbx, %rdi
callq 0x2310
testl %eax, %eax
leaq -0x68(%rbp), %rsi
je 0x7503
jmp 0x74eb
movq 0x18819(%rip), %rax     # 0x1ffe0
movq (%rax), %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0x122ab(%rip), %rdi     # 0x19a88
leaq 0x120d0(%rip), %rdx     # 0x198b4
leaq 0x122a1(%rip), %r8      # 0x19a8c
leaq 0x122a6(%rip), %r9      # 0x19a98
movl $0x113, %ecx            # imm = 0x113
xorl %eax, %eax
callq 0x2683
movq -0x38(%rbp), %rdi
callq 0x21c0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x187c0(%rip), %rax     # 0x1ffe0
movq (%rax), %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0x12252(%rip), %rdi     # 0x19a88
leaq 0x12077(%rip), %rdx     # 0x198b4
leaq 0x12248(%rip), %r8      # 0x19a8c
leaq 0x1224d(%rip), %r9      # 0x19a98
movl $0x109, %ecx            # imm = 0x109
jmp 0x77f7