// File: /workspace/github_c_makefile_star2_O1/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 0x5d6f
movq 0x8(%rsi), %r12
movq 0x10(%rsi), %r15
movq 0x18(%rsi), %rdi
callq 0x1190
movl %eax, %ebp
leaq 0xe7e0(%rip), %rdi      # 0x143c3
xorl %r13d, %r13d
movq %r12, %rsi
xorl %eax, %eax
callq 0x1090
leaq 0x8(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq %r12, %rdi
movl $0x3, %r8d
callq 0x13f0
movq %rax, %r14
testq %rax, %rax
je 0x5d7e
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %edx
leaq 0xe7be(%rip), %rdi      # 0x143e6
xorl %eax, %eax
callq 0x1090
testl %ebp, %ebp
jle 0x5cac
xorl %r13d, %r13d
movq %r14, 0x10(%rsp)
movq %r14, %rbx
xorl %r12d, %r12d
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %ecx
movq %r15, %rdi
movq %rbx, %rsi
movl %r13d, %r8d
callq 0x5a65
movq %rax, %r14
testq %rax, %rax
je 0x5c84
testl %r13d, %r13d
je 0x5c6c
movq %rbx, %rdi
callq 0x1030
decl 0x8(%rsp)
xorl %ecx, %ecx
movq %r14, %rbx
testq %r14, %r14
je 0x5cbb
incl %r13d
cmpl %r13d, %ebp
jne 0x5c41
jmp 0x5cb6
movq 0x1336d(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
leaq 0xe765(%rip), %rsi      # 0x143fa
movl %r13d, %edx
xorl %eax, %eax
callq 0x10e0
movl $0x2, %ecx
movl $0x1, %r12d
jmp 0x5c75
movq %r14, %rbx
movl $0x3, %ecx
jmp 0x5cc3
movl $0x3, %ecx
movq 0x10(%rsp), %r14
movl %r12d, %r13d
cmpl $0x2, %ecx
je 0x5d35
xorl %eax, %eax
cmpl $0x3, %ecx
jne 0x5d5d
movq %r14, %r12
leaq 0xe739(%rip), %rdx      # 0x14416
leaq 0x20(%rsp), %r14
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x10a0
movl 0x8(%rsp), %ebp
movl 0xc(%rsp), %r15d
leaq 0xe639(%rip), %rdi      # 0x1433d
movl %ebp, %esi
movl %r15d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movl $0x3, %ecx
movq %rbx, %r8
movl $0x50, %r9d
callq 0x5318
testl %eax, %eax
je 0x5da3
movq %r12, %r14
testq %r14, %r14
je 0x5d42
movq %r14, %rdi
callq 0x1030
testq %rbx, %rbx
sete %al
cmpq %r14, %rbx
sete %cl
orb %al, %cl
jne 0x5d5a
movq %rbx, %rdi
callq 0x1030
movl %r13d, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rdi
callq 0x12cd
movl $0x1, %eax
jmp 0x5d5d
movq 0x13273(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
leaq 0xe642(%rip), %rsi      # 0x143d1
xorl %ebx, %ebx
movq %r12, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x1, %r13d
jmp 0x5d35
movq 0x1324e(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
leaq 0xe66d(%rip), %rsi      # 0x14421
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x5d32