// File: /workspace/github_c_makefile_star2_O3/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)
pxor %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movdqa %xmm0, (%rdi)
movq 0x2d45(%rip), %rbp      # 0x3fd8
movq (%rbp), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x1030
cmpq $0x10, %rax
jne 0x1570
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x10(%rsp)
jne 0x1575
movl 0x18(%rsp), %r12d
movl 0x1c(%rsp), %ebx
movl %r12d, 0xc(%rsp)
bswapl %r12d
movl %ebx, %r13d
bswapl %r13d
movl %r13d, %r15d
imull %r12d, %r15d
movl $0x8, %esi
movq %r15, %rdi
callq 0x1050
testq %rax, %rax
je 0x154e
movq %rax, %r14
movq (%rbp), %rcx
movl $0x8, %esi
movq %rax, %rdi
movq %r15, %rdx
callq 0x1030
cmpq %r15, %rax
jne 0x157a
testl %ebx, %ebx
movl 0xc(%rsp), %ebp
je 0x1363
movl %r12d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl %ecx, %esi
movq %rax, %rdi
testl %ebp, %ebp
je 0x1359
movl %esi, %r8d
movq (%r14,%r8,8), %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%r14,%r8,8)
incl %esi
decq %rdi
jne 0x1331
incl %edx
addl %r12d, %ecx
cmpl %r13d, %edx
jne 0x1328
movq 0x2c66(%rip), %r15      # 0x3fd0
movq (%r15), %rsi
leaq 0xd55(%rip), %rdi       # 0x20c9
callq 0x1040
leaq 0x10(%rsp), %rdi
movl %ebp, (%rdi)
movq (%r15), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1566
leaq 0x10(%rsp), %rdi
movl %ebx, (%rdi)
movq (%r15), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1566
testl %ebx, %ebx
je 0x153d
cmpl $0x1, %r12d
movl %r12d, %eax
adcl $0x0, %eax
movq %rax, 0x30(%rsp)
cmpl $0x1, %r13d
movl %r13d, %eax
adcl $0x0, %eax
movl %eax, 0x2c(%rsp)
xorl %r15d, %r15d
pxor %xmm4, %xmm4
testl %ebp, %ebp
je 0x152b
movl %r15d, %eax
imull %r12d, %eax
movq %rax, 0x40(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq 0x40(%rsp), %rax
addl %ebx, %eax
movzwl 0x6(%r14,%rax,8), %eax
pxor %xmm0, %xmm0
movq $-0x1, %rdx
xorl %ecx, %ecx
movq 0x38(%rsp), %rsi
movq %rdx, %r8
incq %rdx
addl %r15d, %r8d
cmpl %r13d, %r8d
movl %r15d, %edi
cmovbl %r8d, %edi
testl %r8d, %r8d
cmovsl %r15d, %edi
imull %r12d, %edi
xorl %r8d, %r8d
movl %ebp, %r9d
cmpl %r12d, %r9d
movl %ebx, %r10d
cmovbl %r9d, %r10d
testl %r9d, %r9d
cmovsl %ebx, %r10d
movss (%rsi,%r8), %xmm1
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
xorps %xmm3, %xmm3
cvtsi2ss %r10d, %xmm3
mulss %xmm1, %xmm3
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
mulps %xmm2, %xmm1
addps %xmm0, %xmm1
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
cvttps2dq %xmm1, %xmm1
addss %xmm0, %xmm3
cvttss2si %xmm3, %ecx
pshuflw $0xe8, %xmm1, %xmm0     # xmm0 = xmm1[0,2,2,3,4,5,6,7]
incl %r9d
addq $0xc, %r8
cmpq $0x24, %r8
jne 0x1449
addq $0x4, %rsi
cmpq $0x2, %rdx
jne 0x1425
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movd %xmm0, 0x10(%rsp)
rolw $0x8, %cx
movw %cx, 0x14(%rsp)
rolw $0x8, %ax
movw %ax, 0x16(%rsp)
movq 0x2ad6(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
movl $0x2, %esi
movl $0x4, %edx
leaq 0x10(%rsp), %rdi
callq 0x10a0
cmpq $0x4, %rax
jne 0x1561
incq %rbx
incl %ebp
cmpq 0x30(%rsp), %rbx
pxor %xmm4, %xmm4
jne 0x1406
incl %r15d
cmpl 0x2c(%rsp), %r15d
movl 0xc(%rsp), %ebp
jne 0x13eb
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb0f(%rip), %rdi       # 0x2064
callq 0x1080
movl $0x1, %eax
jmp 0x153f
callq 0x112f
movl $0x1, %edi
callq 0x1090
callq 0x10c0
callq 0x10e5
callq 0x110a