// File: /workspace/github_c_makefile_star2_O1/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 %esi, %r15d
movl %edx, %eax
movq %rdi, -0x38(%rbp)
imull %edi, %eax
movq %rax, -0x48(%rbp)
movl %eax, %edi
imull %esi, %edi
shlq $0x2, %rdi
callq 0x1140
movss -0x30(%rbp), %xmm1
movq %rax, %r13
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
ucomiss 0xcc5a(%rip), %xmm1     # 0x12038
jae 0x53f8
ucomiss 0xcc45(%rip), %xmm1     # 0x1202c
jae 0x540a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
mulsd 0xee10(%rip), %xmm0     # 0x14210
addsd 0xee10(%rip), %xmm0     # 0x14218
jmp 0x5445
mulsd 0xee0e(%rip), %xmm0     # 0x14220
addsd 0xee0e(%rip), %xmm0     # 0x14228
cvtsd2ss %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x542c
sqrtss %xmm0, %xmm0
jmp 0x5431
callq 0x1130
cvtss2sd %xmm0, %xmm0
mulsd 0xedf3(%rip), %xmm0     # 0x14230
addsd 0xedf3(%rip), %xmm0     # 0x14238
cvtsd2ss %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd 0xede8(%rip), %xmm4     # 0x14240
mulsd %xmm1, %xmm4
movsd 0xede4(%rip), %xmm0     # 0x14248
addsd %xmm4, %xmm0
movsd 0xede0(%rip), %xmm2     # 0x14250
mulsd %xmm1, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd 0xedd4(%rip), %xmm3     # 0x14258
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movsd 0xedc0(%rip), %xmm5     # 0x14260
mulsd %xmm1, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
movsd 0xedb4(%rip), %xmm4     # 0x14268
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 0xcb8c(%rip), %xmm2     # 0x12070
cvtsd2ss %xmm3, %xmm3
addss %xmm3, %xmm5
divss %xmm0, %xmm5
movss 0xcb10(%rip), %xmm4     # 0x12008
subss %xmm5, %xmm4
movzbl %r14b, %eax
testl %r15d, %r15d
je 0x5616
xorl %edi, %edi
leal 0xf(,%rax,4), %ecx
andl $-0x10, %ecx
movq %rcx, -0x50(%rbp)
xorl %ecx, %ecx
movl %ecx, -0x30(%rbp)
movq %rsp, -0x40(%rbp)
movq %rsp, %r8
movq -0x50(%rbp), %rcx
subq %rcx, %r8
movq %r8, %rsp
movq %rsp, %r9
subq %rcx, %r9
movq %r9, %rsp
movq %rsp, %r10
subq %rcx, %r10
movq %r10, %rsp
testb %r14b, %r14b
je 0x5570
xorl %ecx, %ecx
leal (%rdi,%rcx), %edx
movzbl (%rbx,%rdx), %edx
xorps %xmm5, %xmm5
cvtsi2ss %edx, %xmm5
movss %xmm5, (%r8,%rcx,4)
movss %xmm5, (%r9,%rcx,4)
movss %xmm5, (%r10,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x5548
cmpl $0x0, -0x38(%rbp)
je 0x5600
xorl %r11d, %r11d
movq %rdi, %rcx
testb %r14b, %r14b
je 0x55f4
xorl %edx, %edx
leal (%rcx,%rdx), %esi
movzbl (%rbx,%rsi), %r12d
xorps %xmm5, %xmm5
cvtsi2ss %r12d, %xmm5
movss (%r8,%rdx,4), %xmm6
movss (%r9,%rdx,4), %xmm7
movss (%r10,%rdx,4), %xmm8
movss %xmm7, (%r10,%rdx,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,%rsi,4)
movss %xmm6, (%r9,%rdx,4)
movss %xmm5, (%r8,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x5587
incl %r11d
addq %rax, %rcx
cmpl -0x38(%rbp), %r11d
jne 0x5580
movq -0x40(%rbp), %rsp
movl -0x30(%rbp), %ecx
incl %ecx
addq -0x48(%rbp), %rdi
cmpl %r15d, %ecx
jne 0x551b
movl %r15d, %ecx
subl $0x1, %ecx
movl %ecx, -0x50(%rbp)
jb 0x5749
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %edx
movl %edx, -0x60(%rbp)
movl %r15d, %r11d
imull %ecx, %r11d
decl %r11d
imull %r14d, %r11d
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 -0x50(%rbp), %ecx
movl %ecx, -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 0x56ad
xorl %esi, %esi
leal (%r11,%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 0x568b
movq %r11, %rsi
movl -0x60(%rbp), %r9d
cmpl $0x0, -0x38(%rbp)
je 0x5733
testb %r14b, %r14b
je 0x5727
xorl %r10d, %r10d
leal (%rsi,%r10), %r12d
movss (%rcx,%r10,4), %xmm5
movss (%rdx,%r10,4), %xmm6
movss (%rdi,%r10,4), %xmm7
movss %xmm6, (%rdi,%r10,4)
mulss %xmm2, %xmm6
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm6, %xmm8
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%r12,4), %xmm6
mulss %xmm4, %xmm6
addss %xmm7, %xmm6
movss %xmm6, (%r13,%r12,4)
movss %xmm5, (%rdx,%r10,4)
movss %xmm6, (%rcx,%r10,4)
incq %r10
cmpq %r10, %rax
jne 0x56c2
decl %r9d
addq %r8, %rsi
cmpl -0x38(%rbp), %r9d
jb 0x56ba
movq -0x40(%rbp), %rsp
movl -0x30(%rbp), %ecx
decl %ecx
addq -0x58(%rbp), %r11
cmpl %r15d, %ecx
jb 0x565e
cmpl $0x0, -0x38(%rbp)
je 0x596c
xorl %r8d, %r8d
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), %rdx
subq %rdx, %r9
movq %r9, %rsp
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, %rsp
movq %rsp, %r10
subq %rdx, %r10
movq %r10, %rsp
testb %r14b, %r14b
je 0x57b4
xorl %edx, %edx
leal (%r8,%rdx), %esi
movss (%r13,%rsi,4), %xmm5
movss %xmm5, (%r9,%rdx,4)
movss %xmm5, (%rcx,%rdx,4)
movss %xmm5, (%r10,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x5790
testl %r15d, %r15d
je 0x5835
xorl %r11d, %r11d
movq %r8, %rdx
testb %r14b, %r14b
je 0x5829
xorl %esi, %esi
leal (%rdx,%rsi), %r12d
movss (%r9,%rsi,4), %xmm5
movss (%rcx,%rsi,4), %xmm6
movss (%r10,%rsi,4), %xmm7
movss %xmm6, (%r10,%rsi,4)
mulss %xmm2, %xmm6
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm6, %xmm8
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%r12,4), %xmm6
mulss %xmm4, %xmm6
addss %xmm7, %xmm6
movss %xmm6, (%r13,%r12,4)
movss %xmm5, (%rcx,%rsi,4)
movss %xmm6, (%r9,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x57c6
incl %r11d
addq -0x48(%rbp), %rdx
cmpl %r15d, %r11d
jne 0x57bf
movq -0x30(%rbp), %rsp
incl %edi
addq %rax, %r8
cmpl -0x38(%rbp), %edi
jne 0x5766
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %edx
movl %r15d, %edi
imull %ecx, %edi
decl %edi
imull %r14d, %edi
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 0x58cc
xorl %ecx, %ecx
leal (%rdi,%rcx), %esi
movss (%r13,%rsi,4), %xmm5
movss %xmm5, (%r9,%rcx,4)
movss %xmm5, (%r10,%rcx,4)
movss %xmm5, (%r11,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x58a8
movq %rdi, %rsi
movl -0x50(%rbp), %ecx
testl %r15d, %r15d
je 0x5956
testb %r14b, %r14b
je 0x594b
xorl %r8d, %r8d
leal (%rsi,%r8), %r12d
movss (%r9,%r8,4), %xmm5
movss (%r10,%r8,4), %xmm6
movaps %xmm6, %xmm7
mulss %xmm2, %xmm7
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm7, %xmm8
movss (%r11,%r8,4), %xmm7
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%r12,4), %xmm8
mulss %xmm4, %xmm8
addss %xmm7, %xmm8
cvttss2si %xmm8, %edx
movb %dl, (%rbx,%r12)
movss %xmm6, (%r11,%r8,4)
movss %xmm5, (%r10,%r8,4)
movss %xmm8, (%r9,%r8,4)
incq %r8
cmpq %r8, %rax
jne 0x58df
decl %ecx
addq -0x48(%rbp), %rsi
cmpl %r15d, %ecx
jb 0x58d7
movq -0x40(%rbp), %rsp
movl -0x30(%rbp), %edx
decl %edx
addq -0x60(%rbp), %rdi
cmpl -0x38(%rbp), %edx
jb 0x587b
movq %r13, %rdi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030