// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x6a67
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6890
movl $0x1, -0x4(%rbp)
jmp 0x6c2f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x11a0
movl %eax, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rsi
leaq 0x1b33f(%rip), %rdi     # 0x21df0
movb $0x0, %al
callq 0x1090
movq -0x18(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq -0x44(%rbp), %rcx
movl $0x3, %r8d
callq 0x1320
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x6b06
movq 0x20513(%rip), %rax     # 0x26ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1b30b(%rip), %rsi     # 0x21dfe
movb $0x0, %al
callq 0x1100
movl $0x1, -0x28(%rbp)
jmp 0x6bff
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
leaq 0x1b300(%rip), %rdi     # 0x21e13
movb $0x0, %al
callq 0x1090
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x6ba5
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x3c(%rbp), %edx
movl -0x40(%rbp), %ecx
movl -0x48(%rbp), %r8d
callq 0x68c0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x6b7a
movq 0x2049e(%rip), %rax     # 0x26ff8
movq (%rax), %rdi
movl -0x48(%rbp), %edx
leaq 0x1b2c0(%rip), %rsi     # 0x21e27
movb $0x0, %al
callq 0x1100
movl $0x1, -0x28(%rbp)
jmp 0x6bff
cmpl $0x0, -0x48(%rbp)
jle 0x6b89
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6b29
leaq -0x450(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x400, %esi            # imm = 0x400
leaq 0x1b287(%rip), %rdx     # 0x21e43
movb $0x0, %al
callq 0x10a0
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
leaq -0x450(%rbp), %rcx
callq 0x6830
cmpl $0x0, %eax
jne 0x6bfd
movq 0x20413(%rip), %rax     # 0x26ff8
movq (%rax), %rdi
leaq -0x450(%rbp), %rdx
leaq 0x1b258(%rip), %rsi     # 0x21e4e
movb $0x0, %al
callq 0x1100
jmp 0x6bff
cmpq $0x0, -0x30(%rbp)
je 0x6c0f
movq -0x30(%rbp), %rdi
callq 0x12e0
cmpq $0x0, -0x38(%rbp)
je 0x6c29
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x6c29
movq -0x38(%rbp), %rdi
callq 0x1030
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
nopl (%rax,%rax)