// File: /workspace/github_c_makefile_star2_O3/arkanis[P]iir_gauss_blur/./iir_gauss_blur.h
// Function: iir_gauss_blur
void iir_gauss_blur(unsigned int width, unsigned int height, unsigned char components, unsigned char* image, float sigma) {
	// Create IDX macro but push any previous definition (and restore it later) so we don't overwrite a macro the user has possibly defined before us
	#pragma push_macro("IDX")
	#define IDX(x, y, n) ((y)*width*components + (x)*components + n)
	
	// Allocate buffers
	float* buffer = (float*)malloc(width * height * components * sizeof(buffer[0]));
	
	// Calculate filter parameters for a specified sigma
	// Use Equation 11b to determine q, do nothing if sigma is to small (should have no effect) or negative (doesn't make sense)
	float q;
	if (sigma >= 2.5)
		q = 0.98711 * sigma - 0.96330;
	else if (sigma >= 0.5)
		q = 3.97156 - 4.14554 * sqrtf(1.0 - 0.26891 * sigma);
	else
		return;
	
	// Use equation 8c to determine b0, b1, b2 and b3
	float b0 = 1.57825 + 2.44413*q + 1.4281*q*q + 0.422205*q*q*q;
	float b1 = 2.44413*q + 2.85619*q*q + 1.26661*q*q*q;
	float b2 = -( 1.4281*q*q + 1.26661*q*q*q );
	float b3 = 0.422205*q*q*q;
	// Use equation 10 to determine B
	float B = 1.0 - (b1 + b2 + b3) / b0;
	
	// Horizontal forward pass (from paper: Implement the forward filter with equation 9a)
	// The data is loaded from the byte image but stored in the float buffer
	for(unsigned int y = 0; y < height; y++) {
		float prev1[components], prev2[components], prev3[components];
		for(unsigned char n = 0; n < components; n++) {
			prev1[n] = image[IDX(0, y, n)];
			prev2[n] = prev1[n];
			prev3[n] = prev2[n];
		}
		
		for(unsigned int x = 0; x < width; x++) {
			for(unsigned char n = 0; n < components; n++) {
				float val = B * image[IDX(x, y, n)] + (b1 * prev1[n] + b2 * prev2[n] + b3 * prev3[n]) / b0;
				buffer[IDX(x, y, n)] = val;
				prev3[n] = prev2[n];
				prev2[n] = prev1[n];
				prev1[n] = val;
			}
		}
	}
	
	// Horizontal backward pass (from paper: Implement the backward filter with equation 9b)
	for(unsigned int y = height-1; y < height; y--) {
		float prev1[components], prev2[components], prev3[components];
		for(unsigned char n = 0; n < components; n++) {
			prev1[n] = buffer[IDX(width-1, y, n)];
			prev2[n] = prev1[n];
			prev3[n] = prev2[n];
		}
		
		for(unsigned int x = width-1; x < width; x--) {
			for(unsigned char n = 0; n < components; n++) {
				float val = B * buffer[IDX(x, y, n)] + (b1 * prev1[n] + b2 * prev2[n] + b3 * prev3[n]) / b0;
				buffer[IDX(x, y, n)] = val;
				prev3[n] = prev2[n];
				prev2[n] = prev1[n];
				prev1[n] = val;
			}
		}
	}
	
	// Vertical forward pass (from paper: Implement the forward filter with equation 9a)
	for(unsigned int x = 0; x < width; x++) {
		float prev1[components], prev2[components], prev3[components];
		for(unsigned char n = 0; n < components; n++) {
			prev1[n] = buffer[IDX(x, 0, n)];
			prev2[n] = prev1[n];
			prev3[n] = prev2[n];
		}
		
		for(unsigned int y = 0; y < height; y++) {
			for(unsigned char n = 0; n < components; n++) {
				float val = B * buffer[IDX(x, y, n)] + (b1 * prev1[n] + b2 * prev2[n] + b3 * prev3[n]) / b0;
				buffer[IDX(x, y, n)] = val;
				prev3[n] = prev2[n];
				prev2[n] = prev1[n];
				prev1[n] = val;
			}
		}
	}
	
	// Vertical backward pass (from paper: Implement the backward filter with equation 9b)
	// Also write the result back into the byte image
	for(unsigned int x = width-1; x < width; x--) {
		float prev1[components], prev2[components], prev3[components];
		for(unsigned char n = 0; n < components; n++) {
			prev1[n] = buffer[IDX(x, height-1, n)];
			prev2[n] = prev1[n];
			prev3[n] = prev2[n];
		}
		
		for(unsigned int y = height-1; y < height; y--) {
			for(unsigned char n = 0; n < components; n++) {
				float val = B * buffer[IDX(x, y, n)] + (b1 * prev1[n] + b2 * prev2[n] + b3 * prev3[n]) / b0;
				image[IDX(x, y, n)] = val;
				prev3[n] = prev2[n];
				prev2[n] = prev1[n];
				prev1[n] = val;
			}
		}
	}
	
	// Free temporary buffers and restore any potential IDX macro
	free(buffer);
	#pragma pop_macro("IDX")
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movss %xmm0, -0x30(%rbp)
movq %rcx, %rbx
movl %edx, %r14d
movl %edx, %eax
movq %rdi, -0x38(%rbp)
imull %edi, %eax
movq %rax, -0x48(%rbp)
movl %eax, %edi
movq %rsi, %r12
imull %esi, %edi
shlq $0x2, %rdi
callq 0x1140
movss -0x30(%rbp), %xmm1
movq %rax, %r13
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
ucomiss 0xdb42(%rip), %xmm1     # 0x13038
jae 0x5510
ucomiss 0xdb2d(%rip), %xmm1     # 0x1302c
jae 0x5522
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
mulsd 0xfda0(%rip), %xmm0     # 0x152b8
addsd 0xfda0(%rip), %xmm0     # 0x152c0
jmp 0x555d
mulsd 0xfd9e(%rip), %xmm0     # 0x152c8
addsd 0xfd9e(%rip), %xmm0     # 0x152d0
cvtsd2ss %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x5544
sqrtss %xmm0, %xmm0
jmp 0x5549
callq 0x1130
cvtss2sd %xmm0, %xmm0
mulsd 0xfd83(%rip), %xmm0     # 0x152d8
addsd 0xfd83(%rip), %xmm0     # 0x152e0
cvtsd2ss %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd 0xfd78(%rip), %xmm4     # 0x152e8
mulsd %xmm1, %xmm4
movsd 0xfd74(%rip), %xmm0     # 0x152f0
addsd %xmm4, %xmm0
movsd 0xfd70(%rip), %xmm2     # 0x152f8
mulsd %xmm1, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd 0xfd64(%rip), %xmm3     # 0x15300
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movsd 0xfd50(%rip), %xmm5     # 0x15308
mulsd %xmm1, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
movsd 0xfd44(%rip), %xmm4     # 0x15310
mulsd %xmm1, %xmm4
mulsd %xmm1, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm4, %xmm5
xorps %xmm1, %xmm1
cvtsd2ss %xmm5, %xmm1
addsd %xmm2, %xmm4
xorps %xmm2, %xmm2
cvtsd2ss %xmm4, %xmm2
movaps %xmm1, %xmm5
subss %xmm2, %xmm5
xorps 0xda84(%rip), %xmm2     # 0x13080
cvtsd2ss %xmm3, %xmm3
addss %xmm3, %xmm5
divss %xmm0, %xmm5
movss 0xd9f8(%rip), %xmm4     # 0x13008
subss %xmm5, %xmm4
movzbl %r14b, %eax
testl %r12d, %r12d
je 0x5858
xorl %ecx, %ecx
leal 0xf(,%rax,4), %edx
andl $-0x10, %edx
movq %rdx, -0x50(%rbp)
xorl %edx, %edx
movl %edx, -0x30(%rbp)
movq %rsp, -0x40(%rbp)
movq %rsp, %r8
movq -0x50(%rbp), %rdx
subq %rdx, %r8
movq %r8, %rsp
movq %rsp, %r9
subq %rdx, %r9
movq %r9, %rsp
movq %rsp, %r10
subq %rdx, %r10
movq %r10, %rsp
testb %r14b, %r14b
je 0x5688
xorl %edx, %edx
leal (%rcx,%rdx), %edi
movzbl (%rbx,%rdi), %edi
xorps %xmm5, %xmm5
cvtsi2ss %edi, %xmm5
movss %xmm5, (%r8,%rdx,4)
movss %xmm5, (%r9,%rdx,4)
movss %xmm5, (%r10,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x5660
cmpl $0x0, -0x38(%rbp)
je 0x5717
xorl %r11d, %r11d
movq %rcx, %r15
testb %r14b, %r14b
je 0x570b
xorl %edi, %edi
leal (%r15,%rdi), %edx
movzbl (%rbx,%rdx), %esi
xorps %xmm5, %xmm5
cvtsi2ss %esi, %xmm5
movss (%r8,%rdi,4), %xmm6
movss (%r9,%rdi,4), %xmm7
movss (%r10,%rdi,4), %xmm8
movss %xmm7, (%r10,%rdi,4)
mulss %xmm2, %xmm7
movaps %xmm1, %xmm9
mulss %xmm6, %xmm9
addss %xmm7, %xmm9
mulss %xmm3, %xmm8
addss %xmm9, %xmm8
divss %xmm0, %xmm8
mulss %xmm4, %xmm5
addss %xmm8, %xmm5
movss %xmm5, (%r13,%rdx,4)
movss %xmm6, (%r9,%rdi,4)
movss %xmm5, (%r8,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x569f
incl %r11d
addq %rax, %r15
cmpl -0x38(%rbp), %r11d
jne 0x5698
movq -0x40(%rbp), %rsp
movl -0x30(%rbp), %edx
incl %edx
addq -0x48(%rbp), %rcx
cmpl %r12d, %edx
jne 0x5633
movq %r12, %r15
leal -0x1(%r12), %edx
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %esi
movl %esi, -0x60(%rbp)
imull %ecx, %r15d
decl %r15d
imull %r14d, %r15d
movq -0x48(%rbp), %rcx
negl %ecx
movq %rcx, -0x58(%rbp)
movl %r14d, %r8d
negl %r8d
leal 0xf(,%rax,4), %ecx
andl $-0x10, %ecx
movq %rcx, -0x68(%rbp)
movl %edx, -0x50(%rbp)
movl %edx, -0x30(%rbp)
movq %rsp, -0x40(%rbp)
movq %rsp, %rcx
movq -0x68(%rbp), %rsi
subq %rsi, %rcx
movq %rcx, %rsp
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, %rsp
movq %rsp, %rdi
subq %rsi, %rdi
movq %rdi, %rsp
testb %r14b, %r14b
je 0x57ba
xorl %esi, %esi
leal (%r15,%rsi), %r9d
movss (%r13,%r9,4), %xmm5
movss %xmm5, (%rcx,%rsi,4)
movss %xmm5, (%rdx,%rsi,4)
movss %xmm5, (%rdi,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x5798
movq %r15, %rsi
movl -0x60(%rbp), %r11d
cmpl $0x0, -0x38(%rbp)
je 0x5840
testb %r14b, %r14b
je 0x5834
xorl %r9d, %r9d
leal (%rsi,%r9), %r10d
movss (%rcx,%r9,4), %xmm5
movss (%rdx,%r9,4), %xmm6
movss (%rdi,%r9,4), %xmm7
movss %xmm6, (%rdi,%r9,4)
mulss %xmm2, %xmm6
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm6, %xmm8
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%r10,4), %xmm6
mulss %xmm4, %xmm6
addss %xmm7, %xmm6
movss %xmm6, (%r13,%r10,4)
movss %xmm5, (%rdx,%r9,4)
movss %xmm6, (%rcx,%r9,4)
incq %r9
cmpq %r9, %rax
jne 0x57cf
decl %r11d
addq %r8, %rsi
cmpl -0x38(%rbp), %r11d
jb 0x57c7
movq -0x40(%rbp), %rsp
movl -0x30(%rbp), %edx
decl %edx
addq -0x58(%rbp), %r15
cmpl %r12d, %edx
jb 0x576b
jmp 0x585f
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x38(%rbp)
je 0x5a83
xorl %edx, %edx
leal 0xf(,%rax,4), %ecx
andl $-0x10, %ecx
movq %rcx, -0x40(%rbp)
xorl %edi, %edi
movq %rsp, -0x30(%rbp)
movq %rsp, %r9
movq -0x40(%rbp), %rsi
subq %rsi, %r9
movq %r9, %rsp
movq %rsp, %rcx
subq %rsi, %rcx
movq %rcx, %rsp
movq %rsp, %r10
subq %rsi, %r10
movq %r10, %rsp
testb %r14b, %r14b
je 0x58c9
xorl %esi, %esi
leal (%rdx,%rsi), %r8d
movss (%r13,%r8,4), %xmm5
movss %xmm5, (%r9,%rsi,4)
movss %xmm5, (%rcx,%rsi,4)
movss %xmm5, (%r10,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x58a5
testl %r12d, %r12d
je 0x594d
xorl %r11d, %r11d
movq %rdx, %r15
testb %r14b, %r14b
je 0x5941
xorl %r8d, %r8d
leal (%r15,%r8), %esi
movss (%r9,%r8,4), %xmm5
movss (%rcx,%r8,4), %xmm6
movss (%r10,%r8,4), %xmm7
movss %xmm6, (%r10,%r8,4)
mulss %xmm2, %xmm6
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm6, %xmm8
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%rsi,4), %xmm6
mulss %xmm4, %xmm6
addss %xmm7, %xmm6
movss %xmm6, (%r13,%rsi,4)
movss %xmm5, (%rcx,%r8,4)
movss %xmm6, (%r9,%r8,4)
incq %r8
cmpq %r8, %rax
jne 0x58dc
incl %r11d
addq -0x48(%rbp), %r15
cmpl %r12d, %r11d
jne 0x58d4
movq -0x30(%rbp), %rsp
incl %edi
addq %rax, %rdx
cmpl -0x38(%rbp), %edi
jne 0x587b
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %edx
movl %r12d, %esi
imull %ecx, %esi
decl %esi
imull %r14d, %esi
movl %r14d, %ecx
negl %ecx
movq %rcx, -0x60(%rbp)
movq -0x48(%rbp), %rcx
negl %ecx
movq %rcx, -0x48(%rbp)
leal 0xf(,%rax,4), %ecx
andl $-0x10, %ecx
movq %rcx, -0x58(%rbp)
movl %edx, -0x30(%rbp)
movq %rsp, -0x40(%rbp)
movq %rsp, %r9
movq -0x58(%rbp), %rcx
subq %rcx, %r9
movq %r9, %rsp
movq %rsp, %r10
subq %rcx, %r10
movq %r10, %rsp
movq %rsp, %r11
subq %rcx, %r11
movq %r11, %rsp
testb %r14b, %r14b
je 0x59e4
xorl %ecx, %ecx
leal (%rsi,%rcx), %edi
movss (%r13,%rdi,4), %xmm5
movss %xmm5, (%r9,%rcx,4)
movss %xmm5, (%r10,%rcx,4)
movss %xmm5, (%r11,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x59c0
movq %rsi, %r15
movl -0x50(%rbp), %edi
testl %r12d, %r12d
je 0x5a6d
testb %r14b, %r14b
je 0x5a62
xorl %ecx, %ecx
leal (%r15,%rcx), %r8d
movss (%r9,%rcx,4), %xmm5
movss (%r10,%rcx,4), %xmm6
movaps %xmm6, %xmm7
mulss %xmm2, %xmm7
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm7, %xmm8
movss (%r11,%rcx,4), %xmm7
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%r8,4), %xmm8
mulss %xmm4, %xmm8
addss %xmm7, %xmm8
cvttss2si %xmm8, %edx
movb %dl, (%rbx,%r8)
movss %xmm6, (%r11,%rcx,4)
movss %xmm5, (%r10,%rcx,4)
movss %xmm8, (%r9,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x59f6
decl %edi
addq -0x48(%rbp), %r15
cmpl %r12d, %edi
jb 0x59ef
movq -0x40(%rbp), %rsp
movl -0x30(%rbp), %edx
decl %edx
addq -0x60(%rbp), %rsi
cmpl -0x38(%rbp), %edx
jb 0x5993
movq %r13, %rdi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030