// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rdi, 0x18(%rsp)
pxor %xmm0, %xmm0
movq %rsp, %rdi
movdqa %xmm0, (%rdi)
movq 0x2e07(%rip), %rax      # 0x3fd8
movq (%rax), %rcx
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
callq 0x1030
cmpq $0x10, %rax
jne 0x147a
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, (%rsp)
jne 0x148f
movl 0x8(%rsp), %ebp
movl 0xc(%rsp), %ebx
movl %ebp, %r15d
bswapl %r15d
movl %ebx, %r12d
bswapl %r12d
movl %r12d, %r13d
imull %r15d, %r13d
pushq $0x8
popq %rsi
movq %r13, %rdi
callq 0x1050
testq %rax, %rax
je 0x1445
movq %rax, %r14
movq 0x2da2(%rip), %rax      # 0x3fd8
movq (%rax), %rcx
pushq $0x8
popq %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0x1030
cmpq %r13, %rax
jne 0x14a4
movl %r15d, %eax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
cmpl %r12d, %ecx
je 0x1299
movq 0x10(%rsp), %rdx
movl %eax, %esi
subq $0x1, %rdx
jb 0x1292
movl %esi, %edi
movq (%r14,%rdi,8), %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%r14,%rdi,8)
incl %esi
jmp 0x1268
incl %ecx
addl %r15d, %eax
jmp 0x125c
movq 0x2d30(%rip), %r13      # 0x3fd0
movq (%r13), %rsi
leaq 0xe1e(%rip), %rdi       # 0x20c9
callq 0x1040
movq %rsp, %rdi
movl %ebp, (%rdi)
movq (%r13), %rcx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1472
movq %rsp, %rdi
movl %ebx, (%rdi)
movq (%r13), %rcx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1472
pxor %xmm4, %xmm4
xorl %ebp, %ebp
cmpl %r12d, %ebp
je 0x1434
movl %ebp, %eax
imull %r15d, %eax
movq %rax, 0x20(%rsp)
pushq $-0x1
popq %r13
xorl %ebx, %ebx
cmpq 0x10(%rsp), %rbx
je 0x142d
movq 0x20(%rsp), %rax
addl %ebx, %eax
movzwl 0x6(%r14,%rax,8), %eax
pxor %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
pushq $-0x1
popq %r8
cmpq $0x2, %r8
je 0x13d7
leaq 0x1(%r8), %rsi
addl %ebp, %r8d
cmpl %r12d, %r8d
movl %ebp, %edi
cmovbl %r8d, %edi
testl %r8d, %r8d
cmovsl %ebp, %edi
imull %r15d, %edi
movl %r13d, %r8d
xorl %r9d, %r9d
cmpq $0x24, %r9
je 0x13cb
movss (%rdx,%r9), %xmm1
cmpl %r15d, %r8d
movl %ebx, %r10d
cmovbl %r8d, %r10d
testl %r8d, %r8d
cmovsl %ebx, %r10d
addl %edi, %r10d
punpcklwd %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1],xmm0[2],xmm4[2],xmm0[3],xmm4[3]
cvtdq2ps %xmm0, %xmm0
movd (%r14,%r10,8), %xmm2
punpcklwd %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1],xmm2[2],xmm4[2],xmm2[3],xmm4[3]
cvtdq2ps %xmm2, %xmm2
movzwl 0x4(%r14,%r10,8), %r10d
cvtsi2ss %r10d, %xmm3
mulss %xmm1, %xmm3
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm2, %xmm1
addps %xmm0, %xmm1
cvttps2dq %xmm1, %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movzwl %cx, %ecx
cvtsi2ss %ecx, %xmm1
addss %xmm1, %xmm3
cvttss2si %xmm3, %ecx
addq $0xc, %r9
incl %r8d
jmp 0x135d
addq $0x4, %rdx
movq %rsi, %r8
jmp 0x1333
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movd %xmm0, (%rsp)
rolw $0x8, %cx
movw %cx, 0x4(%rsp)
rolw $0x8, %ax
movw %ax, 0x6(%rsp)
movq 0x2bc9(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
movq %rsp, %rdi
pushq $0x2
popq %rsi
pushq $0x4
popq %rdx
callq 0x10a0
cmpq $0x4, %rax
jne 0x1456
incq %rbx
incl %r13d
pxor %xmm4, %xmm4
jmp 0x130c
incl %ebp
jmp 0x12f2
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc18(%rip), %rdi       # 0x2064
callq 0x1080
pushq $0x1
popq %rax
jmp 0x1436
movq 0x2b9b(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc90(%rip), %rdi       # 0x20f7
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
pushq $0x1
popq %rdi
callq 0x1090
movq 0x2b77(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc2b(%rip), %rdi       # 0x20b6
pushq $0x12
jmp 0x1469
movq 0x2b62(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc32(%rip), %rdi       # 0x20d2
pushq $0x14
jmp 0x1469
movq 0x2b4d(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc32(%rip), %rdi       # 0x20e7
pushq $0xf
jmp 0x1469