// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xb48(%rip), %rsi       # 0x202f
xorl %edi, %edi
callq 0x10c0
testq %rax, %rax
je 0x1664
leaq 0xb32(%rip), %rsi       # 0x2030
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0x14(%rsp), %r8
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
cmpl $0x3, %eax
jne 0x1685
movq %r14, %rdi
callq 0x1070
cmpl $-0x1, %eax
je 0x169a
movl %eax, %ebp
callq 0x1110
movq (%rax), %rax
movslq %ebp, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x16a3
movl 0x14(%rsp), %edx
cmpl $0xff, %edx
jne 0x1642
movl 0x10(%rsp), %r12d
movl 0xc(%rsp), %r13d
movl %r13d, %edi
imull %r12d, %edi
callq 0x10b0
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x1050
movl 0xc(%rsp), %ecx
cmpq %rcx, %rax
jne 0x16c1
movq %r14, %rdi
callq 0x1070
cmpl $-0x1, %eax
jne 0x1621
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x1621
movl 0xc(%rsp), %r12d
imull 0x10(%rsp), %r12d
leaq (,%r12,4), %rdi
callq 0x10b0
movq %rax, %r14
xorl %eax, %eax
cmpq %rax, %r12
je 0x15dd
movzbl (%r15,%rax), %ecx
movl %ecx, (%r14,%rax,4)
incq %rax
jmp 0x15ca
pushq $0x5
popq %rbp
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
subl $0x1, %ebp
jb 0x15f7
movq %r14, %rdi
callq 0x1234
jmp 0x15e0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x139c
movq %r15, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x29d0(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xaa4(%rip), %rdi       # 0x20d6
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x15ad
movq 0x29af(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa50(%rip), %rsi       # 0x20a3
movl $0xff, %ecx
xorl %eax, %eax
callq 0x1090
jmp 0x1559
movq 0x298d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x997(%rip), %rsi       # 0x200c
leaq 0x9b3(%rip), %rdx       # 0x202f
xorl %eax, %eax
callq 0x1090
jmp 0x16cd
movq 0x296c(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x9a6(%rip), %rdi       # 0x203c
pushq $0x1b
jmp 0x16b6
leaq 0x9b7(%rip), %rdi       # 0x2058
jmp 0x16c8
movq 0x294e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x9ac(%rip), %rdi       # 0x2060
pushq $0x42
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
jmp 0x16cd
leaq 0xa06(%rip), %rdi       # 0x20ce
callq 0x10d0
pushq $0x1
popq %rdi
callq 0x10f0