// File: /workspace/github_cpp_makefile_star2_O3/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 0xa4c(%rip), %rsi       # 0x202f
xorl %edi, %edi
callq 0x10c0
testq %rax, %rax
je 0x1753
leaq 0xa36(%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 0x1758
movq %r14, %rdi
callq 0x1070
cmpl $-0x1, %eax
je 0x175d
movl %eax, %ebp
callq 0x1110
movq (%rax), %rax
movslq %ebp, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x1762
movl 0x14(%rsp), %edx
cmpl $0xff, %edx
jne 0x1731
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 0x1767
movq %r14, %rdi
callq 0x1070
cmpl $-0x1, %eax
jne 0x1727
movq %r14, %rdi
callq 0x10a0
testl %eax, %eax
je 0x1727
movl 0xc(%rsp), %r12d
imull 0x10(%rsp), %r12d
leaq (,%r12,4), %rdi
callq 0x10b0
movq %rax, %r14
testl %r12d, %r12d
je 0x16dc
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
movl %ecx, (%r14,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x16cb
movl $0x5, %ebp
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
movq %r14, %rdi
callq 0x12f3
decl %ebp
jne 0x16e1
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x148b
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
callq 0x116a
jmp 0x16a9
movq 0x28c0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x961(%rip), %rsi       # 0x20a3
movl $0xff, %ecx
xorl %eax, %eax
callq 0x1090
jmp 0x1655
callq 0x11c6
callq 0x1130
callq 0x11b1
callq 0x118c
callq 0x1155