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

	const size_t expected_maxval = 255;
	const char *locale = "";
	uint8_t *bytedata;
	int *img;
	int *error;
	unsigned int width;
	unsigned int height;
	unsigned 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 %u %u %u", &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, (int)expected_maxval);
	}

	bytedata = malloc(width * height);

	if(fread(bytedata, 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");
	}

	img = malloc(width * height * sizeof(int));
	error = malloc(width * height * sizeof(int));

	for(size_t z = 0; z < width * height; z++) {
		img[z] = bytedata[z];
		error[z] = 0;
	}

	for(int i = 0; i < 5; i++)
	dither(img, error, width, height);

	braileencode(fpout, img, width, height);

	free(bytedata);
	free(error);
	free(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0xff, -0x18(%rbp)
leaq 0x8f0(%rip), %rax       # 0x202f
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
xorl %edi, %edi
callq 0x10c0
cmpq $0x0, %rax
jne 0x177a
movq 0x289d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x8a3(%rip), %rsi       # 0x200c
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10f0
movq -0x8(%rbp), %rdi
leaq 0x8ab(%rip), %rsi       # 0x2030
leaq -0x3c(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x44(%rbp), %r8
movb $0x0, %al
callq 0x1040
cmpl $0x3, %eax
je 0x17bf
movq 0x2854(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x88e(%rip), %rsi       # 0x203c
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10f0
movq -0x8(%rbp), %rdi
callq 0x1080
movl %eax, -0x48(%rbp)
cmpl $-0x1, -0x48(%rbp)
jne 0x17e7
leaq 0x880(%rip), %rdi       # 0x2058
callq 0x10d0
movl $0x1, %edi
callq 0x10f0
callq 0x1100
movq (%rax), %rax
movslq -0x48(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
jne 0x1823
movq 0x27f0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x84e(%rip), %rsi       # 0x2060
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10f0
movl -0x44(%rbp), %eax
cmpq $0xff, %rax
je 0x184e
movq 0x27c3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x44(%rbp), %edx
leaq 0x861(%rip), %rsi       # 0x20a3
movl $0xff, %ecx
movb $0x0, %al
callq 0x1090
movl -0x3c(%rbp), %eax
imull -0x40(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
callq 0x10b0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
movl -0x40(%rbp), %eax
movl %eax, %edx
movq -0x8(%rbp), %rcx
callq 0x1050
movl -0x40(%rbp), %ecx
cmpq %rcx, %rax
je 0x1897
leaq 0x846(%rip), %rdi       # 0x20ce
callq 0x10d0
movl $0x1, %edi
callq 0x10f0
movq -0x8(%rbp), %rdi
callq 0x1080
cmpl $-0x1, %eax
jne 0x18b3
movq -0x8(%rbp), %rdi
callq 0x10a0
cmpl $0x0, %eax
jne 0x18cb
movq 0x273e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x812(%rip), %rsi       # 0x20d6
movb $0x0, %al
callq 0x1090
movl -0x3c(%rbp), %eax
imull -0x40(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
imull -0x40(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0x40(%rbp), %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x1949
movq -0x28(%rbp), %rax
movq -0x50(%rbp), %rcx
movzbl (%rax,%rcx), %edx
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1903
movl $0x0, -0x54(%rbp)
cmpl $0x5, -0x54(%rbp)
jge 0x1978
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x3c(%rbp), %eax
movl %eax, %edx
movl -0x40(%rbp), %eax
movl %eax, %ecx
callq 0x1270
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1950
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x3c(%rbp), %eax
movl %eax, %edx
movl -0x40(%rbp), %eax
movl %eax, %ecx
callq 0x1510
movq -0x28(%rbp), %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
addq $0x60, %rsp
popq %rbp
retq