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

	int *front;
	int *error;

	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] > 40) ? 128 : 32) - data[z];

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

			int d = BIT(front,x,y,width) / 6;

			BIT(error,x,y-1,width) += d;
			BIT(error,x-1,y,width) += d;
			BIT(error,x+1,y,width) += d;
			BIT(error,x,y+1,width) += d;

			BIT(error,x,y,width) -= d * 6;
		}
	}

	for(size_t z = 0; z < sz; z++) {
		int x = (int)data[z] + error[z];
		data[z] = (uint8_t)((x < 0) ? 0 : (x > 255) ? 255 : x);
	}

	free(front);
	free(error);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
imulq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rdi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x28(%rbp)
movq -0x28(%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 0x12ab
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
movl $0x20, %edx
movl $0x80, %eax
cmpl $0x28, %ecx
cmovgl %eax, %edx
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
subl %eax, %edx
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x125e
movq $0x1, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x13cf
movq $0x1, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x13bc
movq -0x20(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq -0x10(%rbp), %rdx
addq %rdx, %rcx
movl (%rax,%rcx,4), %eax
movl $0x6, %ecx
cltd
idivl %ecx
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %edx
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
subq $0x1, %rsi
imulq -0x10(%rbp), %rsi
addq %rsi, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %edx
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
movq -0x40(%rbp), %rsi
imulq -0x10(%rbp), %rsi
addq %rsi, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %edx
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
addq $0x1, %rcx
movq -0x40(%rbp), %rsi
imulq -0x10(%rbp), %rsi
addq %rsi, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %edx
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rsi
addq $0x1, %rsi
imulq -0x10(%rbp), %rsi
addq %rsi, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
imull $0x6, -0x4c(%rbp), %esi
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq -0x10(%rbp), %rdx
addq %rdx, %rcx
movl (%rax,%rcx,4), %edx
subl %esi, %edx
movl %edx, (%rax,%rcx,4)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x12d0
jmp 0x13be
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x12b3
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x1445
movq -0x8(%rbp), %rax
movq -0x58(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0x28(%rbp), %rcx
movq -0x58(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jge 0x1408
xorl %eax, %eax
movl %eax, -0x60(%rbp)
jmp 0x1427
cmpl $0xff, -0x5c(%rbp)
jle 0x141b
movl $0xff, %eax
movl %eax, -0x64(%rbp)
jmp 0x1421
movl -0x5c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x13d7
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x28(%rbp), %rdi
callq 0x1030
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)