// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
leaq -0x14(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1430
movl -0x14(%rbp), %eax
imull -0x18(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x1080
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x1248
leaq 0xe2d(%rip), %rdi       # 0x2064
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x141e
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x1500
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x15e0
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x1417
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x1407
movw $0x0, -0x30(%rbp)
movw $0x0, -0x2e(%rbp)
movw $0x0, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
imull -0x14(%rbp), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, %ecx
movw 0x6(%rax,%rcx,8), %ax
movw %ax, -0x2a(%rbp)
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
cmpl $0x2, -0x34(%rbp)
jge 0x13f0
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
cmpl $0x2, -0x38(%rbp)
jge 0x13e0
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x44(%rbp)
movl -0x28(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x24(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x48(%rbp)
jl 0x1324
movl -0x48(%rbp), %eax
cmpl -0x14(%rbp), %eax
jb 0x132a
movl -0x28(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x4c(%rbp)
jl 0x1338
movl -0x4c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jb 0x133e
movl -0x24(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl -0x14(%rbp), %edx
imull %edx, %ecx
movl -0x48(%rbp), %edx
addl %edx, %ecx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movzwl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss -0x44(%rbp), %xmm2
movzwl -0x30(%rbp), %eax
cvtsi2ss %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movw %ax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movzwl 0x2(%rax), %eax
cvtsi2ss %eax, %xmm0
movss -0x44(%rbp), %xmm2
movzwl -0x2e(%rbp), %eax
cvtsi2ss %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movw %ax, -0x2e(%rbp)
movq -0x40(%rbp), %rax
movzwl 0x4(%rax), %eax
cvtsi2ss %eax, %xmm0
movss -0x44(%rbp), %xmm2
movzwl -0x2c(%rbp), %eax
cvtsi2ss %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movw %ax, -0x2c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x12cb
jmp 0x13e2
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x12ba
leaq -0x30(%rbp), %rdi
callq 0x1680
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x127c
jmp 0x1409
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1269
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)