// File: /workspace/github_c_makefile_star2_O1/FredTheDino[P]Fog/src/rain/editor_main.cpp
// Function: setup(int, char**)
void setup(int argc, char **argv) {
    // Commandline parsing, sets globalstate
    for (s32 i = 1; i < argc; i++) {
        char *val = argv[i];
        if (*val != '-') {
            if (path) {
                printf("Multiple files given replacing '%s' with '%s'.", path, argv[i]);
            }
            path = argv[i];
        } else {
            // Parse flags
            val++;
            if (*val == '-') val++;
            switch (*val) {
                default:
                    printf("Unkown argument '%s', ignoring.", argv[i]);
            };
        }
    }



    global_editor.sprites = std::vector<EditableSprite>();
    global_editor.cursor = fog_V2(0.5, 0.5);
    global_editor.current_sprite = 0;
    b8 no_path_passed = true;
    if (path != nullptr) {
        // NOTE(ed): Code bellow is copied from the asset loader. Might need to be a
        // unified function.
        FILE *sprite_file = fopen(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) {                                  \
                    fprintf(stderr, "Failed to parse number in sprite file '%s'", \
                            path);                      \
                    fclose(sprite_file);                              \
                    return;                                           \
                }                                                     \
            } while (false);

            char *endptr;
            u32 num_points = strtol(num, &endptr, 10);
            CHECK_FOR_ERROR(num);
            std::vector<Vec2> points = std::vector<Vec2>(num_points);
            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] = fog_V2(point.z, point.w);
            }
#undef CHECK_FOR_ERROR

            u32 resource_hash = res[0] == '#' ? atoi(res + 1): fog_asset_asset_hash(res);
            // Reverse the hash
            for (u32 i = 0; i < fog_asset_num(); i++) {
                if (fog_asset_num() == resource_hash) {
                    resource_hash = i;
                }
            }

            EditableSprite sprite = {
                points,
                resource_hash,
            };
            memcpy(name, sprite.name, MIN(MAX_TEXT_LENGTH, strlen(name)));
            global_editor.sprites.push_back(sprite);
            no_path_passed = false;
            free(line);
        }
        if (sprite_file)
            fclose(sprite_file);
    } else {
        path = "default.spr";
    }

    if (no_path_passed) {
        create_new_sprite();

    }

    // Sprite editor
    fog_input_add(K(a), (u32) In::EDIT_PLACE, Player::P1);
    fog_input_add(K(s), (u32) In::EDIT_SELECT, Player::P1);
    fog_input_add(K(d), (u32) In::EDIT_REMOVE, Player::P1);
    fog_input_add(K(r), (u32) In::EDIT_RENAME, Player::P1);
    fog_input_add(K(u), (u32) In::EDIT_NEXT_SPRITE, Player::P1);
    fog_input_add(K(i), (u32) In::EDIT_PREV_SPRITE, Player::P1);
    fog_input_add(K(n), (u32) In::EDIT_NEXT_SPRITE_SHEET, Player::P1);
    fog_input_add(K(p), (u32) In::EDIT_PREV_SPRITE_SHEET, Player::P1);
    fog_input_add(K(j), (u32) In::EDIT_SNAP_SMALLER, Player::P1);
    fog_input_add(K(k), (u32) In::EDIT_SNAP_LARGER, Player::P1);
    fog_input_add(K(w), (u32) In::EDIT_SAVE, Player::P1);
    fog_input_add(K(c), (u32) In::EDIT_ADD_SPRITE, Player::P1);
    fog_input_add(K(r), (u32) In::EDIT_RENAME, Player::P1);
    fog_input_add(K(LSHIFT), (u32) In::TWEAK_STEP, Player::P1);
    fog_input_add(K(LCTRL), (u32) In::TWEAK_SMOOTH, Player::P1);

    fog_input_add(A(LEFTX, Player::P1), (u32) In::EDIT_MOVE_RIGHT_LEFT, Player::P1);
    fog_input_add(A(LEFTY, Player::P1), (u32) In::EDIT_MOVE_UP_DOWN, Player::P1);
    fog_input_add(A(RIGHTY, Player::P1), (u32) In::EDIT_ZOOM_IN_OUT, Player::P1);
    fog_input_add(B(B, Player::P1), (u32) In::EDIT_PLACE, Player::P1);
    fog_input_add(B(A, Player::P1), (u32) In::EDIT_SELECT, Player::P1);
    fog_input_add(B(Y, Player::P1), (u32) In::EDIT_REMOVE, Player::P1);
    fog_input_add(B(LEFTSHOULDER, Player::P1), (u32) In::TWEAK_STEP, Player::P1);
    fog_input_add(B(DPAD_DOWN, Player::P1), (u32) In::EDIT_SNAP_SMALLER, Player::P1);
    fog_input_add(B(DPAD_UP, Player::P1), (u32) In::EDIT_SNAP_LARGER, Player::P1);
    fog_input_add(A(TRIGGERRIGHT, Player::P1), (u32) In::EDIT_NEXT_SPRITE, Player::P1);
    fog_input_add(A(TRIGGERLEFT, Player::P1), (u32) In::EDIT_PREV_SPRITE, Player::P1);
    fog_input_add(B(DPAD_RIGHT, Player::P1), (u32) In::EDIT_NEXT_SPRITE_SHEET, Player::P1);
    fog_input_add(B(DPAD_LEFT, Player::P1), (u32) In::EDIT_PREV_SPRITE_SHEET, Player::P1);
    fog_input_add(B(START, Player::P1), (u32) In::EDIT_SAVE, Player::P1);
    fog_input_add(B(BACK, Player::P1), (u32) In::EDIT_ADD_SPRITE, Player::P1);

    fog_renderer_fetch_camera(0)->position = fog_V2(0.5, 0.5);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
cmpl $0x2, %edi
jl 0x2c3c
movq %rsi, %rbx
movl %edi, %r12d
movl $0x1, %r13d
leaq 0x2d54c(%rip), %r14     # 0x30140
leaq 0x2d516(%rip), %r15     # 0x30111
movq (%rbx,%r13,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x2c13
movq %r14, %rdi
movq %rdx, %rsi
xorl %eax, %eax
callq 0x2080
jmp 0x2c34
movq 0x4d1a6(%rip), %rsi     # 0x4fdc0
testq %rsi, %rsi
je 0x2c29
movq %r15, %rdi
xorl %eax, %eax
callq 0x2080
movq (%rbx,%r13,8), %rax
movq %rax, 0x4d18c(%rip)     # 0x4fdc0
incq %r13
cmpq %r13, %r12
jne 0x2bfb
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rbx
movaps %xmm0, (%rbx)
xorl %eax, %eax
movq %rax, 0x10(%rbx)
leaq -0xb0(%rbp), %rdi
movaps %xmm0, (%rdi)
movups 0x4d133(%rip), %xmm0    # 0x4fd90
movq 0x4d13c(%rip), %rcx     # 0x4fda0
movq %rax, 0x4d125(%rip)     # 0x4fd90
movq 0x8(%rbx), %rdx
movq %rdx, 0x4d122(%rip)     # 0x4fd98
movq %rax, 0x4d123(%rip)     # 0x4fda0
movaps (%rdi), %xmm1
movaps %xmm0, (%rdi)
movq %rcx, 0x10(%rdi)
movaps %xmm1, (%rbx)
callq 0x44f6
movq %rbx, %rdi
callq 0x44f6
movss 0x2d369(%rip), %xmm0    # 0x30008
movaps %xmm0, %xmm1
callq 0x2899d
movlps %xmm0, 0x4d0fa(%rip)    # 0x4fda8
movl $0x0, 0x4d0d0(%rip)     # 0x4fd88
movq 0x4d101(%rip), %rdi     # 0x4fdc0
testq %rdi, %rdi
je 0x3076
leaq 0x2d491(%rip), %rsi     # 0x30160
callq 0x2530
movq %rax, %r12
leaq -0x68(%rbp), %r14
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movq %r12, -0x58(%rbp)
testq %r12, %r12
je 0x3089
xorl %eax, %eax
movq %rax, -0x60(%rbp)
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r12, %rdx
callq 0x63ec
movq %r12, %rdi
callq 0x2040
movl $0x7, %r13d
testl %eax, %eax
jne 0x304b
movq -0x60(%rbp), %rsi
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x2d47
xorl %edi, %edi
xorl %eax, %eax
leaq -0xb0(%rbp), %r13
cmpb $0x3a, (%rsi,%rdi)
je 0x2d54
incl %eax
movq %rax, %rdi
cmpq %rax, %rcx
ja 0x2d35
jmp 0x2d58
xorl %eax, %eax
xorl %edi, %edi
leaq -0xb0(%rbp), %r13
jmp 0x2d58
movb $0x0, (%rsi,%rdi)
movq -0x38(%rbp), %rdx
movl %eax, %ecx
cmpq %rcx, %rdx
jbe 0x2d79
cmpb $0x3a, (%rsi,%rcx)
je 0x2d75
incl %eax
movq %rax, %rcx
cmpq %rax, %rdx
ja 0x2d63
jmp 0x2d79
movb $0x0, (%rsi,%rcx)
movq %rdi, -0xb8(%rbp)
movq %rsi, -0x50(%rbp)
movq -0x38(%rbp), %rdx
movl %eax, %ebx
cmpq %rbx, %rdx
jbe 0x2dad
movq -0x50(%rbp), %rsi
cmpb $0x3a, (%rsi,%rbx)
je 0x2da5
incl %eax
movq %rax, %rbx
cmpq %rax, %rdx
ja 0x2d8f
jmp 0x2dad
movq -0x50(%rbp), %rdx
movb $0x0, (%rdx,%rbx)
movq -0x38(%rbp), %rdx
movl %eax, %eax
cmpq %rax, %rdx
jbe 0x2dd8
leal 0x1(%rax), %esi
movq -0x50(%rbp), %rdi
cmpb $0x3a, (%rdi,%rax)
je 0x2dd0
movl %esi, %eax
incl %esi
cmpq %rax, %rdx
ja 0x2dbb
jmp 0x2dd8
movq -0x50(%rbp), %rdx
movb $0x0, (%rdx,%rax)
movq -0x50(%rbp), %rax
leaq (%rax,%rcx), %r12
incq %r12
movq %r12, %rdi
leaq -0xc8(%rbp), %rsi
movl $0xa, %edx
callq 0x2430
cmpq -0xc8(%rbp), %r12
je 0x3056
movq %rax, %r12
movq %rax, %rsi
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
andq %r15, %rsi
leaq -0x80(%rbp), %rdi
movq %r13, %rdx
callq 0x4406
movq %r12, %rax
andq %r15, %rax
je 0x2f33
movq %r12, %rcx
movq -0x50(%rbp), %rax
leaq (%rax,%rbx), %r12
incq %r12
movl %ecx, %eax
movq %rax, -0xc0(%rbp)
xorl %ebx, %ebx
movsbl (%r12), %edi
callq 0x2380
incq %r12
testl %eax, %eax
jne 0x2e44
movq %r12, %rdi
movq %r14, %rsi
callq 0x2550
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xb0(%rbp)
movq -0x68(%rbp), %rax
cmpq %rax, %r12
je 0x2f00
xorl %r15d, %r15d
movq %rax, %r12
cmpq $0x3, %r15
je 0x2eb7
movq %r12, %rdi
movq %r14, %rsi
callq 0x2550
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xac(%rbp,%r15,4)
movq -0x68(%rbp), %rax
incq %r15
cmpq %rax, %r12
jne 0x2e7c
decq %r15
cmpq $0x3, %r15
setae %r15b
jmp 0x2f03
setae %r15b
movl $0x1b, %r13d
testb %r15b, %r15b
je 0x2ee9
movss -0xa8(%rbp), %xmm0
movss -0xa4(%rbp), %xmm1
callq 0x2899d
incq %r12
movq -0x80(%rbp), %rax
movlps %xmm0, (%rax,%rbx,8)
xorl %r13d, %r13d
testl %r13d, %r13d
jne 0x2f39
incq %rbx
cmpq -0xc0(%rbp), %rbx
jne 0x2e44
jmp 0x2f33
xorl %r15d, %r15d
movq 0x4c0c6(%rip), %rax     # 0x4efd0
movq (%rax), %rdi
movq 0x4ceac(%rip), %rdx     # 0x4fdc0
leaq 0x2d248(%rip), %rsi     # 0x30163
xorl %eax, %eax
callq 0x2170
movq -0x58(%rbp), %rdi
callq 0x2270
movl $0x1, %r13d
jmp 0x2ec1
movl $0x16, %r13d
cmpl $0x16, %r13d
movq -0x58(%rbp), %r12
jne 0x3036
movq -0x50(%rbp), %rax
movq -0xb8(%rbp), %rcx
leaq (%rax,%rcx), %rdi
incq %rdi
cmpb $0x23, (%rdi)
jne 0x2f79
addq %rax, %rcx
addq $0x2, %rcx
movq %rcx, %rdi
callq 0x21e0
movl %eax, %r15d
leaq -0xb0(%rbp), %r13
jmp 0x2f88
callq 0x28578
leaq -0xb0(%rbp), %r13
movq %rax, %r15
xorl %ebx, %ebx
callq 0x28609
movl %ebx, %ecx
cmpq %rcx, %rax
jbe 0x2faf
callq 0x28609
movl %r15d, %edx
movl %ebx, %ecx
cmpq %rdx, %rax
je 0x2fa8
movl %r15d, %ecx
incl %ebx
movl %ecx, %r15d
jmp 0x2f8a
movq %r13, %rdi
leaq -0x80(%rbp), %rsi
callq 0x4552
movl %r15d, %eax
movq %rax, -0x98(%rbp)
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rbx
movups %xmm0, (%rbx)
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x2250
cmpq $0x10, %rax
movl $0x10, %ecx
cmovaeq %rcx, %rax
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2090
leaq 0x4cd90(%rip), %rdi     # 0x4fd90
movq %r13, %rsi
callq 0x4498
movq -0x60(%rbp), %rdi
callq 0x2140
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x302c
movq -0xa0(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
xorl %r13d, %r13d
movl $0x0, -0x2c(%rbp)
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x304b
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
testl %r13d, %r13d
je 0x2ce4
jmp 0x306a
movq -0x58(%rbp), %r12
movq %r12, %rdi
callq 0x2570
movl $0x1, %r13d
jmp 0x304b
cmpl $0x7, %r13d
jne 0x309d
movl -0x2c(%rbp), %r14d
jmp 0x308c
leaq 0x2d111(%rip), %rax     # 0x3018e
movq %rax, 0x4cd3c(%rip)     # 0x4fdc0
movb $0x1, %r14b
jmp 0x30ab
movb $0x1, %r14b
movb $0x1, %bl
testq %r12, %r12
je 0x30a3
movq %r12, %rdi
callq 0x2270
jmp 0x30a3
xorl %ebx, %ebx
movl -0x2c(%rbp), %r14d
testb %bl, %bl
je 0x3419
testb %r14b, %r14b
je 0x30b5
callq 0x2b17
movl $0x61, %edi
callq 0x293e0
movl %eax, %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x73, %edi
callq 0x293e0
movl %eax, %edi
movl $0x2, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x64, %edi
callq 0x293e0
movl %eax, %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x72, %edi
callq 0x293e0
movl %eax, %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x75, %edi
callq 0x293e0
movl %eax, %edi
movl $0x5, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x69, %edi
callq 0x293e0
movl %eax, %edi
movl $0x6, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x6e, %edi
callq 0x293e0
movl %eax, %edi
movl $0x7, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x70, %edi
callq 0x293e0
movl %eax, %edi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x6a, %edi
callq 0x293e0
movl %eax, %edi
movl $0x9, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x6b, %edi
callq 0x293e0
movl %eax, %edi
movl $0xa, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x77, %edi
callq 0x293e0
movl %eax, %edi
movl $0xb, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x63, %edi
callq 0x293e0
movl %eax, %edi
movl $0xc, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x72, %edi
callq 0x293e0
movl %eax, %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x400000e1, %edi       # imm = 0x400000E1
callq 0x293e0
movl %eax, %edi
movl $0x10, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x400000e0, %edi       # imm = 0x400000E0
callq 0x293e0
movl %eax, %edi
movl $0x11, %esi
movl $0x1, %edx
callq 0x2923d
xorl %edi, %edi
xorl %esi, %esi
callq 0x293ec
movl %eax, %edi
movl $0xd, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x1, %edi
xorl %esi, %esi
callq 0x293ec
movl %eax, %edi
movl $0xe, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x3, %edi
xorl %esi, %esi
callq 0x293ec
movl %eax, %edi
movl $0xf, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x1, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x2923d
xorl %edi, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x2, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x3, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x9, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x10, %esi
movl $0x1, %edx
callq 0x2923d
movl $0xc, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x9, %esi
movl $0x1, %edx
callq 0x2923d
movl $0xb, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0xa, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x5, %edi
xorl %esi, %esi
callq 0x293ec
movl %eax, %edi
movl $0x5, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x4, %edi
xorl %esi, %esi
callq 0x293ec
movl %eax, %edi
movl $0x6, %esi
movl $0x1, %edx
callq 0x2923d
movl $0xe, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x7, %esi
movl $0x1, %edx
callq 0x2923d
movl $0xd, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x6, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0xb, %esi
movl $0x1, %edx
callq 0x2923d
movl $0x4, %edi
xorl %esi, %esi
callq 0x2942c
movl %eax, %edi
movl $0xc, %esi
movl $0x1, %edx
callq 0x2923d
movss 0x2cc09(%rip), %xmm0    # 0x30008
movaps %xmm0, %xmm1
callq 0x2899d
movaps %xmm0, -0x50(%rbp)
xorl %edi, %edi
callq 0x2958a
movaps -0x50(%rbp), %xmm0
movlps %xmm0, (%rax)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3451
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x3454
movq -0xa0(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
jmp 0x3454
jmp 0x3451
jmp 0x3451
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x3469
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
movq %rbx, %rdi
callq 0x2310