// File: aempirei[P]Chat-Art/pgmtobrailedux.c
// Function: dither
void dither(int *data, int *error, size_t width, size_t height) {

	int *front;

	size_t sz = width * height;

	front = malloc(sizeof(int) * sz);
	error = malloc(sizeof(int) * sz);

	memset(error, 0, sizeof(int) * sz);

	for(size_t z = 0; z < sz; z++)
		front[z] = ((data[z] >= mid) ? upper : lower) - data[z];

	for(size_t y = 0; y < height; y++) {
		for(size_t x = 0; x < width; x++) {

			int used = (x<width-1)+(y<height-1)+1;
			int e = BIT(front,x,y,width) / used;

			if(x < width - 1) 
				BIT(error,x+1,y,width) += e;
			if(y < height - 1) 
				BIT(error,x,y+1,width) += e;

			//	BIT(error,x,y,width) -= e * used;

		}
	}

	int max = lower;
	int min = upper;

	for(size_t z = 0; z < sz; z++) {
		data[z] += error[z];
		if(data[z] > max)
			max = data[z];
		if(data[z] < min)
			min = data[z];
	}

	for(size_t z = 0; z < sz; z++) {
		data[z] = ((upper - lower) * (data[z] - min) / (max - min)) + lower;
	}

	free(front);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
imulq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rdi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x1070
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x1320
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movl 0x2dab(%rip), %esi      # 0x4098
movl $0x1, %edx
movl $0xff, %eax
cmpl %esi, %ecx
cmovgel %eax, %edx
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
subl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x12d2
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x142b
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1418
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
setb %al
andb $0x1, %al
movzbl %al, %eax
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rdx
subq $0x1, %rdx
cmpq %rdx, %rcx
setb %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq -0x18(%rbp), %rdx
addq %rdx, %rcx
movl (%rax,%rcx,4), %eax
cltd
idivl -0x4c(%rbp)
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x13d3
movl -0x50(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
addq $0x1, %rcx
movq -0x40(%rbp), %rsi
imulq -0x18(%rbp), %rsi
addq %rsi, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x1405
movl -0x50(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
addq $0x1, %rsi
imulq -0x18(%rbp), %rsi
addq %rsi, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x1407
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x133e
jmp 0x141a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1328
movl $0x1, -0x54(%rbp)
movl $0xff, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x14b0
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x54(%rbp), %eax
jle 0x1482
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x58(%rbp), %eax
jge 0x14a0
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
jmp 0x14a2
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1441
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x14fd
movq -0x8(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
subl -0x58(%rbp), %eax
imull $0xfe, %eax, %eax
movl -0x54(%rbp), %ecx
subl -0x58(%rbp), %ecx
cltd
idivl %ecx
movl %eax, %edx
addl $0x1, %edx
movq -0x8(%rbp), %rax
movq -0x68(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x14b8
movq -0x28(%rbp), %rdi
callq 0x1030
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)