// File: mdpkh[P]retro6k/src/main.cpp
// Function: DrawText(char const*, int&, int, unsigned char, unsigned char, SDL_Surface*)
void DrawText(const char* text, int &x, const int y0, unsigned char fg, unsigned char bg, SDL_Surface* destsurf)
{
	if (destsurf == nullptr)
		return;
	if (destsurf->pixels == nullptr)
		return;
	if (y0 >= destsurf->h)
		return;
	unsigned char c[2] = { bg, fg };
	int mingy = -y0;
	if (mingy < 0)
		mingy = 0;
	int maxgy = destsurf->h - y0;
	if (maxgy > 16)
		maxgy = 16;
	while (*text)
	{
		int gw = hifont[(unsigned char)*text].w;
		unsigned short gbits[16];
		for (unsigned short* src = (unsigned short*)hifont[(unsigned char)*text].bmp, * dst = gbits; src < hifont[(unsigned char)*text].bmp + 16; ++src, ++dst)
			*dst = *src;
		unsigned char* rowstart = (unsigned char*)destsurf->pixels + (long long)destsurf->pitch * y0 + x;
		int mingx = -x;
		int maxgx = destsurf->w - x;
		if (mingx < gw)
		{
			for (int gy = mingy; gy < maxgy; ++gy)
			{
				for (int gx = 0; gx < gw; ++gx)
				{
					if (gx >= mingx && gx < maxgx)
						rowstart[gx] = c[gbits[gy] & 1];
					gbits[gy] >>= 1;
				}
				rowstart += destsurf->pitch;
			}
		}
		if (maxgx <= 0)
			break;
		x += gw;
		++text;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movb %cl, -0x15(%rbp)
movb %al, -0x16(%rbp)
movq %r9, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x8f38
jmp 0x9132
movq -0x20(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x8f48
jmp 0x9132
movl -0x14(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jl 0x8f59
jmp 0x9132
movb -0x16(%rbp), %al
movb %al, -0x22(%rbp)
movb -0x15(%rbp), %al
movb %al, -0x21(%rbp)
xorl %eax, %eax
subl -0x14(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0x8f7a
movl $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x10, -0x2c(%rbp)
jle 0x8f94
movl $0x10, -0x2c(%rbp)
jmp 0x8f96
movq -0x8(%rbp), %rax
cmpb $0x0, (%rax)
je 0x9132
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x17ffd(%rip), %rax     # 0x20fb0
imulq $0x22, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x17fe0(%rip), %rax     # 0x20fb0
imulq $0x22, %rcx, %rcx
addq %rcx, %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movzbl (%rcx), %ecx
movl %ecx, %edx
leaq 0x17fb5(%rip), %rcx     # 0x20fb0
imulq $0x22, %rdx, %rdx
addq %rdx, %rcx
addq $0x2, %rcx
addq $0x20, %rcx
cmpq %rcx, %rax
jae 0x9037
movq -0x58(%rbp), %rax
movw (%rax), %cx
movq -0x60(%rbp), %rax
movw %cx, (%rax)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x60(%rbp)
jmp 0x8fe7
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movslq -0x14(%rbp), %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movslq (%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rcx
xorl %eax, %eax
subl (%rcx), %eax
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x10(%rbp), %rcx
subl (%rcx), %eax
movl %eax, -0x70(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x910e
movl -0x28(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x910c
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x90ec
movl -0x78(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jl 0x90d1
movl -0x78(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x90d1
movslq -0x74(%rbp), %rax
movzwl -0x50(%rbp,%rax,2), %eax
andl $0x1, %eax
cltq
movb -0x22(%rbp,%rax), %dl
movq -0x68(%rbp), %rax
movslq -0x78(%rbp), %rcx
movb %dl, (%rax,%rcx)
movslq -0x74(%rbp), %rax
movzwl -0x50(%rbp,%rax,2), %ecx
sarl %ecx
movw %cx, -0x50(%rbp,%rax,2)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x909c
movq -0x20(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x908d
jmp 0x910e
cmpl $0x0, -0x70(%rbp)
jg 0x9116
jmp 0x9132
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x8f96
popq %rbp
retq
nopw %cs:(%rax,%rax)