// File: /workspace/github_c_makefile_star2_O3/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 0x5df3
movq 0x8(%rsi), %rbx
movq 0x10(%rsi), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsi), %rdi
callq 0x1180
movl %eax, %r13d
leaq 0xf7b3(%rip), %rdi      # 0x1547b
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1080
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq %rbx, %rdi
movl $0x3, %r8d
callq 0x13e0
testq %rax, %rax
je 0x5dfd
movq %rax, %rbp
movl (%rsp), %r15d
movl 0x4(%rsp), %r12d
leaq 0xf795(%rip), %rdi      # 0x1549e
movl %r15d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1080
testl %r13d, %r13d
movq %rbp, 0x10(%rsp)
jle 0x5d63
xorl %r14d, %r14d
movq %rbp, %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %r14d, %r8d
callq 0x5b5a
testq %rax, %rax
je 0x5e48
movq %rax, %rbp
testl %r14d, %r14d
je 0x5d55
movq %rbx, %rdi
callq 0x1030
decl %r15d
incl %r14d
movq %rbp, %rbx
cmpl %r14d, %r13d
jne 0x5d26
movl %r15d, (%rsp)
leaq 0xf760(%rip), %rdx      # 0x154ce
leaq 0x20(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x1090
leaq 0xf667(%rip), %rdi      # 0x153f5
movl %r15d, %esi
movl %r12d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1080
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
movl $0x3, %ecx
movq %rbp, %r8
movl $0x50, %r9d
callq 0x5412
testl %eax, %eax
je 0x5e20
movq %rbp, %rbx
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x1030
cmpq %r14, %rbx
je 0x5dde
movq %rbx, %rdi
callq 0x1030
movl %r15d, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rdi
callq 0x12bd
jmp 0x5e18
movq 0x141f4(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xf67b(%rip), %rsi      # 0x15489
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
movl $0x1, %r15d
jmp 0x5dde
movq 0x141d1(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xf6a8(%rip), %rsi      # 0x154d9
xorl %r15d, %r15d
leaq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
movq %rbp, %rbx
jmp 0x5dc4
movl %r15d, (%rsp)
movq 0x141a5(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xf655(%rip), %rsi      # 0x154b2
movl %r14d, %edx
xorl %eax, %eax
callq 0x10d0
movl $0x1, %r15d
jmp 0x5dc4