// File: /workspace/github_c_makefile_star2_O2/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 0xcf35(%rip), %rsi      # 0x13885
callq 0x2250
movq %rax, %rbx
testq %rax, %rax
je 0x6bcd
movq %r14, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0xf0(%rbp), %r12
movq %rbx, -0x38(%rbp)
movq %r15, -0x68(%rbp)
andq $0x0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
movq %rbx, %rdx
callq 0x255c
movq %rbx, %rdi
callq 0x2360
testl %eax, %eax
jne 0x6bcd
movq -0x40(%rbp), %r13
xorl %ecx, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x69c5
cmpb $0x3a, (%r13,%rdx)
je 0x69bb
incl %ecx
jmp 0x69a8
movb $0x0, (%r13,%rdx)
movq -0x30(%rbp), %rax
leaq (%rdx,%r13), %rsi
movq %rsi, -0x60(%rbp)
leaq (%rdx,%r13), %rsi
incq %rsi
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x69f1
cmpb $0x3a, (%r13,%rdx)
je 0x69e7
incl %ecx
jmp 0x69d4
movb $0x0, (%r13,%rdx)
movq -0x30(%rbp), %rax
leaq (%rdx,%r13), %rbx
incq %rbx
movl %ecx, %r14d
cmpq %r14, %rax
jbe 0x6a16
cmpb $0x3a, (%r13,%r14)
je 0x6a0c
incl %ecx
jmp 0x69f8
movb $0x0, (%r13,%r14)
movq -0x30(%rbp), %rax
addq %r13, %r14
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x6a31
leal 0x1(%rdx), %ecx
cmpb $0x3a, (%r13,%rdx)
jne 0x6a19
movb $0x0, (%r13,%rdx)
movq %rsi, -0x70(%rbp)
movq %rbx, %rdi
leaq -0x88(%rbp), %rsi
pushq $0xa
popq %rdx
callq 0x2300
cmpq -0x88(%rbp), %rbx
je 0x6be7
movq %rax, %r15
shlq $0x4, %rax
movq %rax, -0x58(%rbp)
movl %eax, %edi
callq 0x22b0
movq %rax, -0x50(%rbp)
movl %r15d, %eax
xorl %ebx, %ebx
movq %r15, -0x80(%rbp)
movq %rax, -0x78(%rbp)
cmpq %rax, %rbx
je 0x6aeb
incq %r14
movsbl (%r14), %edi
callq 0x2060
incq %r14
testl %eax, %eax
jne 0x6a7f
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x6ac8
movq %r14, %rdi
movq %r12, %rsi
callq 0x2180
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xc0(%rbp,%r15,4)
movq -0xf0(%rbp), %rax
incq %r15
cmpq %rax, %r14
movq %rax, %r14
jne 0x6a92
jmp 0x6b89
movq %rbx, %rax
shlq $0x4, %rax
movups -0xc0(%rbp), %xmm0
movq -0x50(%rbp), %rcx
movups %xmm0, (%rcx,%rax)
incq %rbx
movq -0x80(%rbp), %r15
movq -0x78(%rbp), %rax
jmp 0x6a77
movq -0x70(%rbp), %rdi
cmpb $0x23, (%rdi)
jne 0x6b06
movq -0x60(%rbp), %rdi
addq $0x2, %rdi
callq 0x2370
movl %eax, %r14d
jmp 0x6b0e
callq 0x42e3
movq %rax, %r14
movq -0x38(%rbp), %rbx
movl $0x3, -0xc0(%rbp)
orl $-0x1, -0x8c(%rbp)
movq %r13, -0xb8(%rbp)
movq %r13, %rdi
callq 0x20e0
movq %rax, -0xb0(%rbp)
movl %r14d, %eax
movq %rax, -0x98(%rbp)
movq -0x58(%rbp), %rcx
addl $0x30, %ecx
movl %ecx, -0x90(%rbp)
movq %rax, -0xf0(%rbp)
movl %r15d, -0xe8(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x68(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movq %r12, %rdx
callq 0x61e5
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
jmp 0x697c
movq 0x13450(%rip), %rax     # 0x19fe0
movq (%rax), %rsi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0xcec2(%rip), %rdi      # 0x13a68
leaq 0xcce7(%rip), %rdx      # 0x13894
leaq 0xceb8(%rip), %r8       # 0x13a6c
leaq 0xcebd(%rip), %r9       # 0x13a78
movl $0x113, %ecx            # imm = 0x113
xorl %eax, %eax
callq 0x2682
movq -0x38(%rbp), %rdi
jmp 0x6bd0
movq %rbx, %rdi
callq 0x21f0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x133f2(%rip), %rax     # 0x19fe0
movq (%rax), %rsi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
leaq 0xce64(%rip), %rdi      # 0x13a68
leaq 0xcc89(%rip), %rdx      # 0x13894
leaq 0xce5a(%rip), %r8       # 0x13a6c
leaq 0xce5f(%rip), %r9       # 0x13a78
movl $0x109, %ecx            # imm = 0x109
jmp 0x6bc0