// File: /workspace/github_cpp_makefile_star2_O2/lunasorcery[P]brailify/main.cpp
// Function: processFile(char const*)
void processFile(const char* filepath)
{
	int srcWidth, srcHeight, srcComp;
	rgba8888_t* srcData = (rgba8888_t*)stbi_load(filepath, &srcWidth, &srcHeight, &srcComp, 4);
	rgba8888_t* dstData = srcData;

	if (!srcData) {
		printf("Failed to load image %s.\n", filepath);
		return;
	}

	int dstWidth  = srcWidth;
	int dstHeight = srcHeight;

	if (resizeWidth != 0) {
		dstWidth = resizeWidth * 2;
		dstHeight = (srcHeight * dstWidth) / srcWidth;
		dstData = new rgba8888_t[dstWidth * dstHeight * 3];
		stbir_resize_uint8((uint8_t const*)srcData, srcWidth, srcHeight, 0, (uint8_t*)dstData, dstWidth, dstHeight, 0, 4);
	}

	char buffer[4];
	buffer[0] = 0xe2;
	buffer[3] = 0;
	for (int y = 0; y < dstHeight; y += 4) {
		for (int x = 0; x < dstWidth; x += 2) {
			const unsigned char masks[8] = { 0x01, 0x02, 0x04, 0x40, 0x08, 0x10, 0x20, 0x80 };
			
			uint8_t bits = 0;
			for (int i = 0; i < 8; ++i) {
				if (getPixel(dstData, dstWidth, dstHeight, x + i / 4, y + i % 4) >= .0f) {
					bits |= masks[i];
				}
			}

			buffer[1] = 0xa0 | (bits >> 6);
			buffer[2] = 0x80 | (bits & 0x3f);
			fputs(buffer, stdout);
		}
		fputs("\n", stdout);
	}

	if (dstData != srcData) {
		delete[] dstData;
	}
	stbi_image_free(srcData);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq -0x48(%rbp), %rsi
leaq -0x44(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
pushq $0x4
popq %r8
callq 0x12ef
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x4417
movl -0x48(%rbp), %r12d
movl -0x44(%rbp), %r15d
movl 0x11e53(%rip), %ebx     # 0x16208
testl %ebx, %ebx
je 0x442d
addl %ebx, %ebx
movl %ebx, %eax
imull %r15d, %eax
cltd
idivl %r12d
movl %eax, %r13d
imull %ebx, %eax
leal (%rax,%rax,2), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0x1040
andl $0x0, 0x8(%rsp)
movl %r13d, (%rsp)
movl $0x4, 0x10(%rsp)
movq -0x38(%rbp), %rdi
movl %r12d, %esi
movl %r15d, %edx
xorl %ecx, %ecx
movq %rax, -0x40(%rbp)
movq %rax, %r8
movl %ebx, %r9d
callq 0x2de7
movl %ebx, %r12d
movl %r13d, %r15d
jmp 0x4435
leaq 0xc8a0(%rip), %rdi      # 0x10cbe
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
jmp 0x450a
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq -0x2c(%rbp), %rax
movb $-0x1e, (%rax)
movb $0x0, 0x3(%rax)
xorl %ebx, %ebx
movl %r15d, -0x30(%rbp)
cmpl %r15d, %ebx
jge 0x44f0
xorl %r13d, %r13d
cmpl %r12d, %r13d
jge 0x44ce
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq $0x8, %r14
je 0x449e
movl %r14d, %ecx
shrl $0x2, %ecx
orl %r13d, %ecx
movl %r14d, %r8d
andl $0x3, %r8d
orl %ebx, %r8d
movq -0x40(%rbp), %rdi
movl %r12d, %esi
movl -0x30(%rbp), %edx
callq 0x410e
ucomiss 0xbb90(%rip), %xmm0     # 0x1001c
jb 0x4499
leaq 0xc7f3(%rip), %rax      # 0x10c88
orb (%r14,%rax), %r15b
incq %r14
jmp 0x445d
movl %r15d, %eax
shrb $0x6, %al
orb $-0x60, %al
movb %al, -0x2b(%rbp)
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, -0x2a(%rbp)
movq 0x11b04(%rip), %rax     # 0x15fc0
movq (%rax), %rsi
leaq -0x2c(%rbp), %rdi
callq 0x10e0
addl $0x2, %r13d
jmp 0x4452
movq 0x11aeb(%rip), %rax     # 0x15fc0
movq (%rax), %rsi
leaq 0xc7f7(%rip), %rdi      # 0x10cd6
callq 0x10e0
addl $0x4, %ebx
movl -0x30(%rbp), %r15d
jmp 0x4446
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %rdi
cmpq %rbx, %rdi
je 0x4502
callq 0x1120
movq %rbx, %rdi
callq 0x1100
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq