// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movss %xmm0, -0x38(%rbp)
movq %rcx, %rbx
movl %esi, %r15d
movl %edi, %r14d
movq %rdx, -0x48(%rbp)
movl %edx, %edi
imull %r14d, %edi
movq %rdi, -0x40(%rbp)
imull %esi, %edi
shlq $0x2, %rdi
callq 0x1140
movss -0x38(%rbp), %xmm1
movq %rax, %r13
cvtss2sd %xmm1, %xmm0
ucomiss 0xae27(%rip), %xmm1     # 0x10038
jae 0x522b
ucomiss 0xae12(%rip), %xmm1     # 0x1002c
jae 0x523d
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
mulsd 0xcfbd(%rip), %xmm0     # 0x121f0
addsd 0xcfbd(%rip), %xmm0     # 0x121f8
jmp 0x5278
mulsd 0xcfbb(%rip), %xmm0     # 0x12200
addsd 0xcfbb(%rip), %xmm0     # 0x12208
cvtsd2ss %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x525f
sqrtss %xmm0, %xmm0
jmp 0x5264
callq 0x1130
cvtss2sd %xmm0, %xmm0
mulsd 0xcfa0(%rip), %xmm0     # 0x12210
addsd 0xcfa0(%rip), %xmm0     # 0x12218
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xcf98(%rip), %xmm4     # 0x12220
mulsd %xmm1, %xmm4
movsd 0xcf94(%rip), %xmm0     # 0x12228
addsd %xmm4, %xmm0
movsd 0xcf90(%rip), %xmm2     # 0x12230
mulsd %xmm1, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd 0xcf84(%rip), %xmm3     # 0x12238
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movsd 0xcf70(%rip), %xmm5     # 0x12240
mulsd %xmm1, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
movsd 0xcf64(%rip), %xmm4     # 0x12248
mulsd %xmm1, %xmm4
mulsd %xmm1, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm4, %xmm5
cvtsd2ss %xmm5, %xmm1
addsd %xmm2, %xmm4
cvtsd2ss %xmm4, %xmm2
movaps %xmm1, %xmm5
subss %xmm2, %xmm5
xorps 0xad62(%rip), %xmm2     # 0x10070
cvtsd2ss %xmm3, %xmm3
addss %xmm3, %xmm5
divss %xmm0, %xmm5
movss 0xace6(%rip), %xmm4     # 0x10008
subss %xmm5, %xmm4
movzbl -0x48(%rbp), %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl %r15d, %edx
je 0x5421
movl %edx, -0x38(%rbp)
movq %rsp, -0x30(%rbp)
leal 0xf(,%rax,4), %esi
andl $-0x10, %esi
movq %rsp, %rdi
subq %rsi, %rdi
movq %rdi, %rsp
movq %rsp, %r8
subq %rsi, %r8
movq %r8, %rsp
movq %rsp, %r9
subq %rsi, %r9
movq %r9, %rsp
xorl %esi, %esi
cmpq %rsi, %rax
je 0x538e
leal (%rcx,%rsi), %r10d
movzbl (%rbx,%r10), %r10d
cvtsi2ss %r10d, %xmm5
movss %xmm5, (%rdi,%rsi,4)
movss %xmm5, (%r8,%rsi,4)
movss %xmm5, (%r9,%rsi,4)
incq %rsi
jmp 0x5365
xorl %r10d, %r10d
movq %rcx, %r11
cmpl %r14d, %r10d
je 0x540f
xorl %r12d, %r12d
cmpq %r12, %rax
je 0x5407
leal (%r11,%r12), %esi
movzbl (%rbx,%rsi), %edx
cvtsi2ss %edx, %xmm5
movss (%rdi,%r12,4), %xmm6
movss (%r8,%r12,4), %xmm7
movss (%r9,%r12,4), %xmm8
movss %xmm7, (%r9,%r12,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, (%r8,%r12,4)
movss %xmm5, (%rdi,%r12,4)
incq %r12
jmp 0x539c
incl %r10d
addq %rax, %r11
jmp 0x5394
movq -0x30(%rbp), %rsp
movl -0x38(%rbp), %edx
incl %edx
addq -0x40(%rbp), %rcx
jmp 0x532e
leal -0x1(%r15), %edi
movl %r15d, %esi
imull %r14d, %esi
decl %esi
movq -0x48(%rbp), %rcx
imull %ecx, %esi
movq -0x40(%rbp), %rdx
negl %edx
movq %rdx, -0x38(%rbp)
negl %ecx
movq %rcx, -0x48(%rbp)
movq %rsi, %rdx
movl %edi, -0x54(%rbp)
cmpl %r15d, %edi
jae 0x5538
movl %edi, -0x30(%rbp)
movq %rsp, -0x50(%rbp)
leal 0xf(,%rax,4), %edi
andl $-0x10, %edi
movq %rsp, %r10
subq %rdi, %r10
movq %r10, %rsp
movq %rsp, %r11
subq %rdi, %r11
movq %r11, %rsp
movq %rsp, %rcx
subq %rdi, %rcx
movq %rcx, %rsp
xorl %edi, %edi
cmpq %rdi, %rax
je 0x54a8
leal (%rdx,%rdi), %r8d
movss (%r13,%r8,4), %xmm5
movss %xmm5, (%r10,%rdi,4)
movss %xmm5, (%r11,%rdi,4)
movss %xmm5, (%rcx,%rdi,4)
incq %rdi
jmp 0x5482
movq %rdx, %rdi
movl %r14d, %r12d
decl %r12d
cmpl %r14d, %r12d
jae 0x5526
xorl %r9d, %r9d
cmpq %r9, %rax
je 0x5520
leal (%rdi,%r9), %r8d
movss (%r10,%r9,4), %xmm5
movss (%r11,%r9,4), %xmm6
movss (%rcx,%r9,4), %xmm7
movss %xmm6, (%rcx,%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,%r8,4), %xmm6
mulss %xmm4, %xmm6
addss %xmm7, %xmm6
movss %xmm6, (%r13,%r8,4)
movss %xmm5, (%r11,%r9,4)
movss %xmm6, (%r10,%r9,4)
incq %r9
jmp 0x54b9
addq -0x48(%rbp), %rdi
jmp 0x54ae
movq -0x50(%rbp), %rsp
movl -0x30(%rbp), %edi
decl %edi
addq -0x38(%rbp), %rdx
jmp 0x544b
xorl %r8d, %r8d
xorl %ecx, %ecx
cmpl %r14d, %ecx
je 0x5626
movl %ecx, -0x30(%rbp)
movq %rsp, -0x50(%rbp)
leal 0xf(,%rax,4), %edx
andl $-0x10, %edx
movq %rsp, %r10
subq %rdx, %r10
movq %r10, %rsp
movq %rsp, %r11
subq %rdx, %r11
movq %r11, %rsp
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, %rsp
xorl %edx, %edx
cmpq %rdx, %rax
je 0x559a
leal (%r8,%rdx), %edi
movss (%r13,%rdi,4), %xmm5
movss %xmm5, (%r10,%rdx,4)
movss %xmm5, (%r11,%rdx,4)
movss %xmm5, (%rcx,%rdx,4)
incq %rdx
jmp 0x5574
xorl %r9d, %r9d
movq %r8, %rdi
cmpl %r15d, %r9d
je 0x5615
xorl %edx, %edx
cmpq %rdx, %rax
je 0x560c
leal (%rdi,%rdx), %r12d
movss (%r10,%rdx,4), %xmm5
movss (%r11,%rdx,4), %xmm6
movss (%rcx,%rdx,4), %xmm7
movss %xmm6, (%rcx,%rdx,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, (%r11,%rdx,4)
movss %xmm6, (%r10,%rdx,4)
incq %rdx
jmp 0x55a7
incl %r9d
addq -0x40(%rbp), %rdi
jmp 0x55a0
movq -0x50(%rbp), %rsp
movl -0x30(%rbp), %ecx
incl %ecx
addq %rax, %r8
jmp 0x553d
movl %r14d, %ecx
decl %ecx
cmpl %r14d, %ecx
jae 0x571e
movl %ecx, -0x40(%rbp)
movq %rsp, -0x30(%rbp)
leal 0xf(,%rax,4), %edx
andl $-0x10, %edx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, %rsp
movq %rsp, %r9
subq %rdx, %r9
movq %r9, %rsp
movq %rsp, %r10
subq %rdx, %r10
movq %r10, %rsp
xorl %edx, %edx
cmpq %rdx, %rax
je 0x5688
leal (%rsi,%rdx), %r11d
movss (%r13,%r11,4), %xmm5
movss %xmm5, (%rcx,%rdx,4)
movss %xmm5, (%r9,%rdx,4)
movss %xmm5, (%r10,%rdx,4)
incq %rdx
jmp 0x5662
movq %rsi, %rdx
movl -0x54(%rbp), %r11d
cmpl %r15d, %r11d
jae 0x570e
xorl %r12d, %r12d
cmpq %r12, %rax
je 0x5705
leal (%rdx,%r12), %r8d
movss (%rcx,%r12,4), %xmm5
movss (%r9,%r12,4), %xmm6
movaps %xmm6, %xmm7
mulss %xmm2, %xmm7
movaps %xmm1, %xmm8
mulss %xmm5, %xmm8
addss %xmm7, %xmm8
movss (%r10,%r12,4), %xmm7
mulss %xmm3, %xmm7
addss %xmm8, %xmm7
divss %xmm0, %xmm7
movss (%r13,%r8,4), %xmm8
mulss %xmm4, %xmm8
addss %xmm7, %xmm8
cvttss2si %xmm8, %edi
movb %dil, (%rbx,%r8)
movss %xmm6, (%r10,%r12,4)
movss %xmm5, (%r9,%r12,4)
movss %xmm8, (%rcx,%r12,4)
incq %r12
jmp 0x5697
decl %r11d
addq -0x38(%rbp), %rdx
jmp 0x568f
movq -0x30(%rbp), %rsp
addq -0x48(%rbp), %rsi
movl -0x40(%rbp), %ecx
jmp 0x5629
movq %r13, %rdi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030