// File: brianwu02[P]ImageProcessing/sharpen.cpp
// Function: sharpen(imageS*, imageS*, int, imageS*)
void sharpen(imageP I1, imageP I2, int fctr, imageP final_img) {
    /* I2 will be blurred image.
     * I1 will be the un-altered input image.
     * final will be final output image.
     */

    int i, total;
    unsigned char *original, *blurred, *fin_img;

    total = I1->width * I1->height;
    
    final_img->width = I1->width;
    final_img->height = I1->height;

    final_img->image = (unsigned char *) malloc(total);

    fin_img = final_img->image;
    original = I1->image;
    blurred = I2->image;

    if (final_img->image == NULL) {
        cerr << "not enough memory\n" << endl;
    }

    int w = I1->width;
    int h = I1->height;

    unsigned char difference[total];
    
    // subtract blurred image from original image to get difference.
    for (i=0; i<total; i++) {
        difference[i] = clip(fctr * (original[i] - blurred[i]));
    }

    for (i=0; i<total; i++) {
        fin_img[i] = clip(original[i] + difference[i]);
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
imull 0x4(%rcx), %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movslq -0x28(%rbp), %rdi
callq 0x1110
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1b27
movq 0x34e4(%rip), %rdi      # 0x4ff0
leaq 0x1527(%rip), %rsi      # 0x303a
callq 0x10a0
movq %rax, %rdi
movq 0x349e(%rip), %rsi      # 0x4fc0
callq 0x10b0
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x28(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x1bae
movl -0x14(%rbp), %edi
movq -0x30(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x38(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
subl %ecx, %eax
imull %eax, %edi
callq 0x1c00
movl %eax, %ecx
movq -0x60(%rbp), %rax
movb %cl, %dl
movslq -0x24(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1b67
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x1bf4
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movzbl (%rcx,%rdx), %edi
movslq -0x24(%rbp), %rcx
movzbl (%rax,%rcx), %eax
addl %eax, %edi
callq 0x1c00
movb %al, %dl
movq -0x40(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1bb5
movq -0x50(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq