// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %rbx
leaq -0x40(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
movl $0x4, %r8d
callq 0x12df
testq %rax, %rax
je 0x4985
movq %rax, %r14
movl -0x40(%rbp), %r12d
movl -0x3c(%rbp), %r15d
movl 0x178de(%rip), %ebx     # 0x1c1d8
testl %ebx, %ebx
movq %rax, -0x48(%rbp)
je 0x499b
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
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x1040
movq %rax, %r8
xorl %eax, %eax
movl $0x1, %r10d
movss 0x116c1(%rip), %xmm2    # 0x16008
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
xorl %ecx, %ecx
movq %r8, -0x38(%rbp)
movl %ebx, %r9d
movaps %xmm2, %xmm3
pushq %rax
pushq %r10
pushq %r10
pushq %rax
pushq %rax
pushq %rax
pushq %rax
pushq $-0x1
pushq $0x4
pushq $0x0
pushq %rax
pushq %r13
callq 0x3116
addq $0x60, %rsp
movl %ebx, %r12d
movl %r13d, %r15d
jmp 0x499f
leaq 0x12412(%rip), %rdi     # 0x16d9e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1050
jmp 0x4a88
movq %r14, -0x38(%rbp)
movb $-0x1e, -0x2c(%rbp)
movb $0x0, -0x29(%rbp)
testl %r15d, %r15d
jle 0x4a6e
movl $0x0, -0x30(%rbp)
movl %r15d, %ebx
testl %r12d, %r12d
jle 0x4a44
xorl %r13d, %r13d
movl %r12d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %r12d, %ecx
shrl $0x2, %ecx
orl %r13d, %ecx
movl %r12d, %r8d
andl $0x3, %r8d
orl -0x30(%rbp), %r8d
movq -0x38(%rbp), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x4621
ucomiss 0x11624(%rip), %xmm0    # 0x1601c
jb 0x4a05
leaq 0x12367(%rip), %rax     # 0x16d68
orb (%r12,%rax), %r14b
incq %r12
cmpq $0x8, %r12
jne 0x49cf
movl %r14d, %eax
shrb $0x6, %al
orb $-0x60, %al
movb %al, -0x2b(%rbp)
andb $0x3f, %r14b
orb $-0x80, %r14b
movb %r14b, -0x2a(%rbp)
movq 0x17594(%rip), %rax     # 0x1bfc0
movq (%rax), %rsi
leaq -0x2c(%rbp), %rdi
callq 0x10d0
addl $0x2, %r13d
movl %r15d, %r12d
cmpl %r15d, %r13d
jl 0x49c6
movq 0x17575(%rip), %rax     # 0x1bfc0
movq (%rax), %rsi
leaq 0x12361(%rip), %rdi     # 0x16db6
callq 0x10d0
movl -0x30(%rbp), %eax
addl $0x4, %eax
movl %ebx, %r15d
movl %eax, -0x30(%rbp)
cmpl %ebx, %eax
jl 0x49b7
movq -0x48(%rbp), %rbx
movq -0x38(%rbp), %rdi
cmpq %rbx, %rdi
je 0x4a80
callq 0x1110
movq %rbx, %rdi
callq 0x10f0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq