// File: /workspace/github_c_makefile_star2_O1/avik-das[P]seam-carver/seam-carver.c
// Function: run_iteration
unsigned char * run_iteration(
        const char *output_directory,
        const unsigned char *data,
        int w,
        int h,
        int iteration) {

    unsigned int *energy = NULL;
    struct seam_link *vertical_seam_links = NULL;
    int *minimal_vertical_seam = NULL;
    unsigned char *output_data = NULL;

    char output_filename[1024];

    energy = compute_energy(data, w, h);
    if (!energy) { goto cleanup; }

    if (iteration == 0) {
        snprintf(output_filename, 1024, "%s/img-energy.jpg", output_directory);
        if (write_energy(energy, w, h, output_filename)) {
            goto cleanup;
        }
    }

    vertical_seam_links = compute_vertical_seam_links(energy, w, h);
    if (!vertical_seam_links) { goto cleanup; }

    minimal_vertical_seam = get_minimal_seam(vertical_seam_links, w, h);

    snprintf(
            output_filename,
            1024,
            "%s/img-seam-%04d.jpg",
            output_directory,
            iteration);
    if (draw_vertical_seam(
                data,
                minimal_vertical_seam,
                w,
                h,
                output_filename)) {
        goto cleanup;
    }

    output_data =
        image_after_vertical_seam_removal(data, minimal_vertical_seam, w, h);

cleanup:
    if (energy) { free(energy); }
    if (vertical_seam_links) { free(vertical_seam_links); }
    if (minimal_vertical_seam) { free(minimal_vertical_seam); }

    return output_data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %r8d, %r14d
movl %ecx, %ebp
movl %edx, %r15d
movq %rsi, %r13
movq %rdi, 0x18(%rsp)
movq %rsi, %rdi
movl %edx, %esi
movl %ecx, %edx
callq 0x5474
movq %rax, %rbx
testq %rax, %rax
je 0x5b2d
movl %r14d, 0xc(%rsp)
testl %r14d, %r14d
je 0x5b32
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x551f
testq %rax, %rax
je 0x5b2d
movq %rax, %r14
movq %rax, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x566d
movq %r13, 0x10(%rsp)
movq %rax, %r13
leaq 0xe8d1(%rip), %rdx      # 0x143ae
xorl %r12d, %r12d
leaq 0x20(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %r8d
xorl %eax, %eax
callq 0x10a0
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movl %r15d, %edx
movl %ebp, %ecx
leaq 0x20(%rsp), %r8
callq 0x5929
testl %eax, %eax
jne 0x5b73
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movl %r15d, %edx
movl %ebp, %ecx
callq 0x5703
movq %rax, %r12
jmp 0x5b73
xorl %r14d, %r14d
jmp 0x5b6d
leaq 0xe863(%rip), %rdx      # 0x1439c
xorl %r14d, %r14d
leaq 0x20(%rsp), %r12
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x10a0
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
movq %r12, %rcx
callq 0x57fa
testl %eax, %eax
je 0x5aac
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %rbx, %rbx
je 0x5b80
movq %rbx, %rdi
callq 0x1030
testq %r14, %r14
je 0x5b8d
movq %r14, %rdi
callq 0x1030
testq %r13, %r13
je 0x5b9a
movq %r13, %rdi
callq 0x1030
movq %r12, %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq