// File: aempirei[P]Chat-Art/pgmtobraile.c
// Function: pgmtobraile
void pgmtobraile(FILE * fpin, FILE * fpout) {

	const size_t max_rad = 16;
	const int expected_maxval = 255;
	const char *locale = "";
	uint8_t *data;
	int width;
	int height;
	int maxval;
	int ch;
	
	if (setlocale(LC_CTYPE, locale) == NULL) {
		fprintf(stderr, "failed to set locale LC_CTYPE=\"%s\"\n", locale);
		exit(EXIT_FAILURE);
	}

	if(fscanf(fpin, "P5 %d %d %d", &width, &height, &maxval) != 3) {
		fprintf(stderr, "failed to parse pgm header\n");
		exit(EXIT_FAILURE);
	}

	ch = fgetc(fpin);
	if(ch == EOF) {
		perror("fgetc()");
		exit(EXIT_FAILURE);
	}

	if(!isspace(ch)) {
		fprintf(stderr, "unexpected character found after maxval, was expecting whitespace\n");
		exit(EXIT_FAILURE);
	}

	if(maxval != expected_maxval) {
		fprintf(stderr, "unexpected maxval of %d, was expecting %d\n", maxval, expected_maxval);
	}

	data = malloc(width * height);

	if(fread(data, width, height, fpin) != (size_t)height) {
		perror("fread()");
		exit(EXIT_FAILURE);
	}

	if(fgetc(fpin) != EOF || !feof(fpin)) {
		fprintf(stderr, "unexpected trailing data after pgm\n");
	}

	for(size_t rad = 0; rad < max_rad; rad++)
		dither(data, width, height);

	braileencode(fpout, data, width, height);

	free(data);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x10, -0x18(%rbp)
movl $0xff, -0x1c(%rbp)
leaq 0x991(%rip), %rax       # 0x2027
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
xorl %edi, %edi
callq 0x10c0
cmpq $0x0, %rax
jne 0x16d1
movq 0x2946(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
leaq 0x944(%rip), %rsi       # 0x2004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
movq -0x8(%rbp), %rdi
leaq 0x94c(%rip), %rsi       # 0x2028
leaq -0x34(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x3c(%rbp), %r8
movb $0x0, %al
callq 0x1040
cmpl $0x3, %eax
je 0x1716
movq 0x28fd(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x92f(%rip), %rsi       # 0x2034
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
movq -0x8(%rbp), %rdi
callq 0x1080
movl %eax, -0x40(%rbp)
cmpl $-0x1, -0x40(%rbp)
jne 0x173e
leaq 0x921(%rip), %rdi       # 0x2050
callq 0x10d0
movl $0x1, %edi
callq 0x10e0
callq 0x10f0
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
jne 0x177a
movq 0x2899(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x8ef(%rip), %rsi       # 0x2058
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
cmpl $0xff, -0x3c(%rbp)
je 0x17a3
movq 0x286e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x3c(%rbp), %edx
leaq 0x904(%rip), %rsi       # 0x209b
movl $0xff, %ecx
movb $0x0, %al
callq 0x1090
movl -0x34(%rbp), %eax
imull -0x38(%rbp), %eax
movslq %eax, %rdi
callq 0x10b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rsi
movslq -0x38(%rbp), %rdx
movq -0x8(%rbp), %rcx
callq 0x1050
movslq -0x38(%rbp), %rcx
cmpq %rcx, %rax
je 0x17ea
leaq 0x8eb(%rip), %rdi       # 0x20c6
callq 0x10d0
movl $0x1, %edi
callq 0x10e0
movq -0x8(%rbp), %rdi
callq 0x1080
cmpl $-0x1, %eax
jne 0x1806
movq -0x8(%rbp), %rdi
callq 0x10a0
cmpl $0x0, %eax
jne 0x181e
movq 0x27eb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x8b7(%rip), %rsi       # 0x20ce
movb $0x0, %al
callq 0x1090
movq $0x0, -0x48(%rbp)
cmpq $0x10, -0x48(%rbp)
jae 0x184c
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rsi
movslq -0x38(%rbp), %rdx
callq 0x1200
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1826
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x34(%rbp), %rdx
movslq -0x38(%rbp), %rcx
callq 0x1460
movq -0x30(%rbp), %rdi
callq 0x1030
addq $0x50, %rsp
popq %rbp
retq