// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movq %rdi, -0x68(%rbp)
movq %rsi, -0x60(%rbp)
movq 0x8(%rsi), %rdi
leaq 0x132f0(%rip), %rsi     # 0x1a8a5
callq 0x2210
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x7964
xorl %r14d, %r14d
leaq -0x48(%rbp), %r15
leaq -0x30(%rbp), %r12
movq %r14, -0x48(%rbp)
movq %r14, -0x30(%rbp)
movq %r15, %rdi
movq %r12, %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rdx
callq 0x258c
movq %rbx, %rdi
callq 0x2310
movl $0x3, %ecx
testl %eax, %eax
jne 0x7957
movq -0x48(%rbp), %r13
movq -0x30(%rbp), %rcx
testq %rcx, %rcx
je 0x7627
xorl %r15d, %r15d
xorl %eax, %eax
cmpb $0x3a, (%r13,%r15)
je 0x762e
incl %eax
movq %rax, %r15
cmpq %rax, %rcx
ja 0x7613
jmp 0x7634
xorl %eax, %eax
xorl %r15d, %r15d
jmp 0x7634
movb $0x0, (%r13,%r15)
movq -0x30(%rbp), %rdx
movl %eax, %ecx
cmpq %rcx, %rdx
jbe 0x7659
cmpb $0x3a, (%r13,%rcx)
je 0x7653
incl %eax
movq %rax, %rcx
cmpq %rax, %rdx
ja 0x763f
jmp 0x7659
movb $0x0, (%r13,%rcx)
movq -0x30(%rbp), %rdx
movl %eax, %r14d
cmpq %r14, %rdx
jbe 0x767f
cmpb $0x3a, (%r13,%r14)
je 0x7679
incl %eax
movq %rax, %r14
cmpq %rax, %rdx
ja 0x7665
jmp 0x767f
movb $0x0, (%r13,%r14)
movq -0x30(%rbp), %rdx
movl %eax, %eax
cmpq %rax, %rdx
jbe 0x76a6
leal 0x1(%rax), %esi
cmpb $0x3a, (%r13,%rax)
je 0x76a0
movl %esi, %eax
incl %esi
cmpq %rax, %rdx
ja 0x768d
jmp 0x76a6
movb $0x0, (%r13,%rax)
leaq (%rcx,%r13), %rbx
incq %rbx
movq %rbx, %rdi
leaq -0x78(%rbp), %rsi
movl $0xa, %edx
callq 0x22b0
cmpq -0x78(%rbp), %rbx
je 0x7837
movq %rax, %rcx
shlq $0x4, %rcx
movq %rcx, -0x40(%rbp)
movl %ecx, %edi
movq %rax, -0x50(%rbp)
callq 0x2260
movq -0x50(%rbp), %rcx
movq %rax, -0x58(%rbp)
testl %ecx, %ecx
je 0x7807
addq %r13, %r14
incq %r14
movl %ecx, %eax
movq %rax, -0x70(%rbp)
xorl %ebx, %ebx
movsbl (%r14), %edi
callq 0x2060
incq %r14
testl %eax, %eax
jne 0x76fc
movq %r14, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x2150
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xb0(%rbp)
movq -0xe0(%rbp), %rax
cmpq %rax, %r14
je 0x777c
xorl %r12d, %r12d
movq %rax, %r14
cmpq $0x3, %r12
je 0x77cd
movq %r14, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x2150
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xac(%rbp,%r12,4)
movq -0xe0(%rbp), %rax
incq %r12
cmpq %rax, %r14
jne 0x7736
decq %r12
cmpq $0x3, %r12
setae %r12b
jmp 0x777f
xorl %r12d, %r12d
movq 0x1985a(%rip), %rax     # 0x20fe0
movq (%rax), %rsi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0x132ec(%rip), %rdi     # 0x1aa88
leaq 0x13111(%rip), %rdx     # 0x1a8b4
movl $0x113, %ecx            # imm = 0x113
leaq 0x132dd(%rip), %r8      # 0x1aa8c
leaq 0x132e2(%rip), %r9      # 0x1aa98
xorl %eax, %eax
callq 0x2693
movq -0x38(%rbp), %rdi
callq 0x21b0
movl $0x1, %ecx
jmp 0x77d6
setae %r12b
movl $0x17, %ecx
testb %r12b, %r12b
je 0x77f6
incq %r14
movq %rbx, %rax
shlq $0x4, %rax
movups -0xb0(%rbp), %xmm0
movq -0x58(%rbp), %rcx
movups %xmm0, (%rcx,%rax)
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x780c
incq %rbx
cmpq -0x70(%rbp), %rbx
jne 0x76fc
movl $0x12, %ecx
cmpl $0x12, %ecx
jne 0x794c
leaq (%r15,%r13), %rdi
movzbl 0x1(%rdi), %eax
testl %eax, %eax
je 0x7888
cmpl $0x23, %eax
jne 0x7890
addq $0x2, %rdi
callq 0x2320
movl %eax, %r14d
jmp 0x78dc
movq 0x197a2(%rip), %rax     # 0x20fe0
movq (%rax), %rsi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0x13234(%rip), %rdi     # 0x1aa88
leaq 0x13059(%rip), %rdx     # 0x1a8b4
movl $0x109, %ecx            # imm = 0x109
leaq 0x13225(%rip), %r8      # 0x1aa8c
leaq 0x1322a(%rip), %r9      # 0x1aa98
xorl %eax, %eax
callq 0x2693
movq -0x38(%rbp), %rdi
callq 0x21b0
movl $0x1, %ecx
jmp 0x794c
movl $0x14e7, %r14d          # imm = 0x14E7
jmp 0x78dc
leaq (%r15,%r13), %rcx
addq $0x2, %rcx
movl $0x14e7, %r14d          # imm = 0x14E7
movsbq %al, %rax
incq %r14
imulq %rax, %r14
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x789e
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
movq -0x40(%rbp), %rbx
movl $0x3, -0xb0(%rbp)
movl $0xffffffff, -0x7c(%rbp) # imm = 0xFFFFFFFF
movq %r13, -0xa8(%rbp)
movq %r13, %rdi
callq 0x20e0
movq %rax, -0xa0(%rbp)
movl %r14d, %eax
movq %rax, -0x88(%rbp)
addl $0x30, %ebx
movl %ebx, -0x80(%rbp)
movq %rax, -0xe0(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0xd8(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x68(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x6c83
xorl %ecx, %ecx
xorl %r14d, %r14d
leaq -0x48(%rbp), %r15
leaq -0x30(%rbp), %r12
testl %ecx, %ecx
je 0x75d2
cmpl $0x3, %ecx
jne 0x796d
movq -0x38(%rbp), %rdi
callq 0x21b0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq