// File: /workspace/github_c_makefile_star2_O2/avik-das[P]seam-carver/seam-carver.c
// Function: main
int main(int argc, char **argv) {
    if (argc != 4) {
        show_usage(argv[0]);
        return 1;
    }

    const char *input_filename = argv[1];
    const char *output_directory = argv[2];
    int num_iterations = atoi(argv[3]);

    int result = 0;

    unsigned char *initial_img = NULL;
    unsigned char *data = NULL;

    printf("Reading '%s'\n", input_filename);

    int w, h, n;
    initial_img = stbi_load(input_filename, &w, &h, &n, 3);
    if (!initial_img) {
        fprintf(stderr, "Unable to read '%s'\n", input_filename);

        result = 1;
        goto cleanup;
    }

    printf("Loaded %dx%d image\n", w, h);

    data = initial_img;
    for (int i = 0; i < num_iterations; i++) {
        unsigned char *next_data =
            run_iteration(output_directory, data, w, h, i);

        if (!next_data) {
            fprintf(stderr, "Error running iteration %d\n", i);

            result = 1;
            goto cleanup;
        }

        if (i > 0) { free(data); }
        data = next_data;
        w--;
    }

    char resized_output_filename[1024];
    snprintf(resized_output_filename, 1024, "%s/img.jpg", output_directory);
    if (!draw_image(data, w, h, resized_output_filename)) {
        fprintf(
                stderr,
                "\033[1;31mUnable to write %s\033[0m\n",
                resized_output_filename);
    }

cleanup:
    if (initial_img) { stbi_image_free(initial_img); }
    if (data && data != initial_img) { free(data); }

    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
cmpl $0x4, %edi
jne 0x5abc
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsi), %rdi
callq 0x1180
movl %eax, %r12d
leaq 0xc98a(%rip), %rdi      # 0x123b3
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1080
leaq 0x4(%rsp), %rsi
movq %rsp, %rdx
leaq 0x1c(%rsp), %rcx
pushq $0x3
popq %r8
movq %rbx, %rdi
callq 0x12c0
testq %rax, %rax
je 0x5b2d
movq %rax, %r14
movl 0x4(%rsp), %ebx
movl (%rsp), %r13d
leaq 0xc96d(%rip), %rdi      # 0x123d6
movl %ebx, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1080
testl %r12d, %r12d
cmovlel %ebp, %r12d
movq %r14, 0x10(%rsp)
cmpl %ebp, %r12d
je 0x5ac9
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movl %r13d, %ecx
movl %ebp, %r8d
callq 0x58b4
testq %rax, %rax
je 0x5b4d
movq %rax, %r15
testl %ebp, %ebp
je 0x5ab3
movq %r14, %rdi
callq 0x1030
decl %ebx
incl %ebp
movq %r15, %r14
jmp 0x5a81
movq (%rsi), %rdi
callq 0x5899
pushq $0x1
popq %rbp
jmp 0x5b19
leaq 0xc936(%rip), %rdx      # 0x12406
xorl %ebp, %ebp
leaq 0x20(%rsp), %r15
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %edx
movq %r15, %rcx
callq 0x5858
testl %eax, %eax
je 0x5b6c
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1030
cmpq %rbx, %r14
je 0x5b19
movq %r14, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x114c4(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xc883(%rip), %rsi      # 0x123c1
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
jmp 0x5ac4
movq 0x114a4(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xc88c(%rip), %rsi      # 0x123ea
movl %ebp, %edx
xorl %eax, %eax
callq 0x10d0
pushq $0x1
popq %rbp
jmp 0x5aff
movq 0x11485(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xc894(%rip), %rsi      # 0x12411
xorl %ebp, %ebp
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
jmp 0x5aff