// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xbbe(%rip), %rsi       # 0x2027
xorl %edi, %edi
callq 0x10c0
testq %rax, %rax
je 0x15b2
leaq 0xba8(%rip), %rsi       # 0x2028
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0x14(%rsp), %r8
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
cmpl $0x3, %eax
jne 0x15d3
movq %r14, %rdi
callq 0x1070
cmpl $-0x1, %eax
je 0x15e8
movl %eax, %ebp
callq 0x1100
movq (%rax), %rax
movslq %ebp, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x15f1
movl 0x14(%rsp), %edx
cmpl $0xff, %edx
jne 0x1590
movslq 0x10(%rsp), %r12
movslq 0xc(%rsp), %r13
movq %r13, %rdi
imulq %r12, %rdi
callq 0x10b0
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x1050
movslq 0xc(%rsp), %rcx
cmpq %rcx, %rax
jne 0x160f
movq %r14, %rdi
callq 0x1070
cmpl $-0x1, %eax
jne 0x1572
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x1572
pushq $0x10
popq %r14
movslq 0x10(%rsp), %rdx
movslq 0xc(%rsp), %rcx
subq $0x1, %r14
jb 0x1550
movq %r15, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x120c
jmp 0x1530
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1323
movq %r15, %rdi
callq 0x1030
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2a7f(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb4b(%rip), %rdi       # 0x20ce
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x152c
movq 0x2a61(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xafa(%rip), %rsi       # 0x209b
movl $0xff, %ecx
xorl %eax, %eax
callq 0x1090
jmp 0x14db
movq 0x2a3f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa41(%rip), %rsi       # 0x2004
leaq 0xa5d(%rip), %rdx       # 0x2027
xorl %eax, %eax
callq 0x1090
jmp 0x161b
movq 0x2a1e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa50(%rip), %rdi       # 0x2034
pushq $0x1b
jmp 0x1604
leaq 0xa61(%rip), %rdi       # 0x2050
jmp 0x1616
movq 0x2a00(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa56(%rip), %rdi       # 0x2058
pushq $0x42
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x161b
leaq 0xab0(%rip), %rdi       # 0x20c6
callq 0x10d0
pushq $0x1
popq %rdi
callq 0x10e0