// File: /workspace/github_c_makefile_star2_O0/avik-das[P]seam-carver/seam-carver.c
// Function: write_energy
int write_energy(
        const unsigned int *energy,
        int w,
        int h,
        const char *filename) {
    int result = 0;

    unsigned char *energy_normalized = malloc(w * h);
    if (!energy_normalized) {
        fprintf(stderr, "Unable to allocate memory (%d)\n", __LINE__);

        result = 1;
        goto cleanup;
    }

    int max_energy = 1;
    for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
        int i = y * w + x;
        max_energy = energy[i] > max_energy ? energy[i] : max_energy;
    }

    for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
        int i = y * w + x;
        energy_normalized[i] = (char) ((double) energy[i] / max_energy * 255);
    }

    printf("Writing to '%s'\n", filename);
    if (!stbi_write_jpg(filename, w, h, 1, energy_normalized, 80)) {
        fprintf(stderr, "Unable to write output (%d)\n", __LINE__);

        result = 1;
        goto cleanup;
    }

cleanup:
    if (energy_normalized) { free(energy_normalized); }

    return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0xc(%rbp), %eax
imull -0x10(%rbp), %eax
movslq %eax, %rdi
callq 0x1150
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x6550
movq 0x20aca(%rip), %rax     # 0x26ff8
movq (%rax), %rdi
leaq 0x1b7e4(%rip), %rsi     # 0x21d1c
movl $0xbf, %edx
movb $0x0, %al
callq 0x1100
movl $0x1, -0x1c(%rbp)
jmp 0x66a0
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x65c6
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x65b9
movl -0x30(%rbp), %eax
imull -0xc(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x2c(%rbp), %eax
jbe 0x65a2
movq -0x8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
jmp 0x65a8
movl -0x2c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x656d
jmp 0x65bb
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x655e
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x6643
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x6636
movl -0x3c(%rbp), %eax
movl -0xc(%rbp), %ecx
imull %ecx, %eax
movl -0x40(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x1b5fa(%rip), %xmm1    # 0x21c10
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x65dc
jmp 0x6638
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x65cd
movq -0x18(%rbp), %rsi
leaq 0x1b6ee(%rip), %rdi     # 0x21d3c
movb $0x0, %al
callq 0x1090
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x28(%rbp), %r8
movl $0x1, %ecx
movl $0x50, %r9d
callq 0x5d70
cmpl $0x0, %eax
jne 0x669e
movq 0x20979(%rip), %rax     # 0x26ff8
movq (%rax), %rdi
leaq 0x1b6c4(%rip), %rsi     # 0x21d4d
movl $0xd4, %edx
movb $0x0, %al
callq 0x1100
movl $0x1, -0x1c(%rbp)
jmp 0x66a0
jmp 0x66a0
cmpq $0x0, -0x28(%rbp)
je 0x66b0
movq -0x28(%rbp), %rdi
callq 0x1030
movl -0x1c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)