// File: /workspace/github_c_makefile_star2_O1/erik[P]gamut/convolute.c
// Function: convolve
int convolve(const float kernel[3][3])
{
    uint32_t width, height;
    pixel_t* image = NULL;

    read_header(&width, &height);

    if (!(image = calloc(width * height, sizeof(pixel_t)))) {
        perror("couldn't allocate memory");
        return 1;
    }

    read_image(image, width, height);
    write_header(width, height);

    for (uint32_t y = 0; y < height; ++y) {
        for (uint32_t x = 0; x < width; ++x) {
            pixel_t accumulator = {0, 0, 0, PIXEL(image, x, y).a};

            for (int ky = -1; ky < 2; ++ky) {
                for (int kx = -1; kx < 2; ++kx) {
                    pixel_t* current = NULL;
                    float mult = kernel[kx + 1][ky + 1];
                    int idx = x + kx,
                        idy = y + ky;

                    if (idx < 0 || (uint32_t) idx >= width) idx = x;
                    if (idy < 0 || (uint32_t) idy >= height) idy = y;

                    current = PIXEL(&image, idx, idy);

                    accumulator.r += current->r * mult;
                    accumulator.g += current->g * mult;
                    accumulator.b += current->b * mult;
                }
            }

            write_pixel(&accumulator);
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq 0x2d47(%rip), %rbp      # 0x3fd8
movq (%rbp), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x1030
cmpq $0x10, %rax
jne 0x1597
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x10(%rsp)
jne 0x159c
movl 0x18(%rsp), %r13d
movl 0x1c(%rsp), %ebx
movl %r13d, %r12d
bswapl %r12d
movl %ebx, %eax
bswapl %eax
movl %eax, 0x8(%rsp)
movl %eax, %r15d
imull %r12d, %r15d
movl $0x8, %esi
movq %r15, %rdi
callq 0x1050
testq %rax, %rax
je 0x1575
movq %rax, %r14
movq (%rbp), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %r15, %rdx
callq 0x1030
cmpq %r15, %rax
jne 0x15a1
testl %ebx, %ebx
je 0x135c
movl %r12d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl %ecx, %esi
movq %rax, %rdi
testl %r13d, %r13d
je 0x1351
movl %esi, %r8d
rolw $0x8, (%r14,%r8,8)
rolw $0x8, 0x2(%r14,%r8,8)
rolw $0x8, 0x4(%r14,%r8,8)
rolw $0x8, 0x6(%r14,%r8,8)
incl %esi
decq %rdi
jne 0x132c
incl %edx
addl %r12d, %ecx
cmpl 0x8(%rsp), %edx
jne 0x1322
movq 0x2c6d(%rip), %r15      # 0x3fd0
movq (%r15), %rsi
leaq 0xd5c(%rip), %rdi       # 0x20c9
callq 0x1040
leaq 0x10(%rsp), %rdi
movl %r13d, (%rdi)
movq (%r15), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x158d
leaq 0x10(%rsp), %rdi
movl %ebx, (%rdi)
movq (%r15), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x158d
testl %ebx, %ebx
je 0x1564
cmpl $0x1, %r12d
movl %r12d, %eax
adcl $0x0, %eax
movq %rax, 0x30(%rsp)
movl 0x8(%rsp), %eax
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, 0x28(%rsp)
xorl %r15d, %r15d
movl %r13d, 0x2c(%rsp)
testl %r13d, %r13d
je 0x1551
movl %r15d, %eax
imull %r12d, %eax
movq %rax, 0x40(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x40(%rsp), %rax
addl %r13d, %eax
movzwl 0x6(%r14,%rax,8), %eax
movw %ax, 0xe(%rsp)
movq $-0x1, %rcx
xorl %esi, %esi
movq 0x38(%rsp), %rdx
xorl %r8d, %r8d
xorl %edi, %edi
movq %rcx, %r10
incq %rcx
addl %r15d, %r10d
cmpl 0x8(%rsp), %r10d
movl %r15d, %r9d
cmovbl %r10d, %r9d
testl %r10d, %r10d
cmovsl %r15d, %r9d
imull %r12d, %r9d
xorl %r10d, %r10d
movl %ebp, %r11d
movss (%rdx,%r10), %xmm0
cmpl %r12d, %r11d
movl %r13d, %ebx
cmovbl %r11d, %ebx
testl %r11d, %r11d
cmovsl %r13d, %ebx
addl %r9d, %ebx
movzwl (%r14,%rbx,8), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movzwl %si, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm0, %xmm1
addss %xmm2, %xmm1
cvttss2si %xmm1, %esi
movzwl 0x2(%r14,%rbx,8), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movzwl %r8w, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm0, %xmm1
addss %xmm2, %xmm1
cvttss2si %xmm1, %r8d
movzwl 0x4(%r14,%rbx,8), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movzwl %di, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm0, %xmm1
addss %xmm2, %xmm1
cvttss2si %xmm1, %edi
incl %r11d
addq $0xc, %r10
cmpq $0x24, %r10
jne 0x144f
addq $0x4, %rdx
cmpq $0x2, %rcx
jne 0x1429
rolw $0x8, %si
movw %si, 0x10(%rsp)
rolw $0x8, %r8w
movw %r8w, 0x12(%rsp)
rolw $0x8, %di
movw %di, 0x14(%rsp)
movzwl 0xe(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x16(%rsp)
movq 0x2aac(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
movl $0x2, %esi
movl $0x4, %edx
leaq 0x10(%rsp), %rdi
callq 0x10a0
cmpq $0x4, %rax
jne 0x1588
incq %r13
incl %ebp
cmpq 0x30(%rsp), %r13
jne 0x1403
incl %r15d
cmpl 0x28(%rsp), %r15d
movl 0x2c(%rsp), %r13d
jne 0x13e6
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xae8(%rip), %rdi       # 0x2064
callq 0x1080
movl $0x1, %eax
jmp 0x1566
callq 0x112f
movl $0x1, %edi
callq 0x1090
callq 0x10c0
callq 0x10e5
callq 0x110a