// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/makichan.c
// Function: mki_decompress_virtual_screen
static void mki_decompress_virtual_screen(deark *c, lctx *d)
{
	i64 i, j;
	i64 a_pos, b_pos;
	i64 vs_rowspan;
	i64 k;
	u8 tmpn[4];
	u8 v;
	u8 a_byte = 0x00;
	int a_bitnum;

	vs_rowspan = d->width_adj/16;
	a_pos = d->flag_a_offset;
	a_bitnum = -1;
	b_pos = d->flag_b_offset;
	d->virtual_screen = dbuf_create_membuf(c, vs_rowspan*d->height_adj, 1);

	for(j=0; j<d->height_adj/4; j++) {
		for(i=0; i<d->width_adj/8; i++) {
			u8 flag_a_bit;

			// Read next flag A bit
			if(a_bitnum<0) {
				a_byte = de_getbyte(a_pos++);
				a_bitnum = 7;
			}
			flag_a_bit = a_byte & (1 << a_bitnum--);

			if(!flag_a_bit)
				continue;

			// Read the next two bytes from flag B, and split them into 4 nibbles.
			tmpn[0] = de_getbyte(b_pos++);
			tmpn[2] = de_getbyte(b_pos++);
			tmpn[1] = tmpn[0]&0x0f;
			tmpn[3] = tmpn[2]&0x0f;
			tmpn[0] >>= 4;
			tmpn[2] >>= 4;

			for(k=0; k<4; k++) {
				i64 vs_pos;

				vs_pos = (4*j+k)*vs_rowspan + i/2;
				if(i%2==0) {
					v = tmpn[k]<<4;
				}
				else {
					v = dbuf_getbyte(d->virtual_screen, vs_pos) | tmpn[k];
				}
				dbuf_writebyte_at(d->virtual_screen, vs_pos, v);
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x46(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rax
imulq 0x68(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jge 0xdaafd
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jge 0xdaaea
cmpl $0x0, -0x4c(%rbp)
jge 0xda992
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
callq 0x165060
movb %al, -0x46(%rbp)
movl $0x7, -0x4c(%rbp)
movzbl -0x46(%rbp), %eax
movl -0x4c(%rbp), %ecx
movl %ecx, %edx
addl $-0x1, %edx
movl %edx, -0x4c(%rbp)
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
movb %al, -0x4d(%rbp)
cmpb $0x0, -0x4d(%rbp)
jne 0xda9ba
jmp 0xdaad9
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
callq 0x165060
movb %al, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
callq 0x165060
movb %al, -0x42(%rbp)
movzbl -0x44(%rbp), %eax
andl $0xf, %eax
movb %al, -0x43(%rbp)
movzbl -0x42(%rbp), %eax
andl $0xf, %eax
movb %al, -0x41(%rbp)
movzbl -0x44(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x44(%rbp)
movzbl -0x42(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x42(%rbp)
movq $0x0, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
jge 0xdaad7
movq -0x20(%rbp), %rax
shlq $0x2, %rax
addq -0x40(%rbp), %rax
imulq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x70(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0xdaa89
movq -0x40(%rbp), %rax
movzbl -0x44(%rbp,%rax), %eax
shll $0x4, %eax
movb %al, -0x45(%rbp)
jmp 0xdaaae
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
movq -0x40(%rbp), %rcx
movzbl -0x44(%rbp,%rcx), %ecx
orl %ecx, %eax
movb %al, -0x45(%rbp)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x58(%rbp), %rsi
movzbl -0x45(%rbp), %edx
callq 0x168790
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xdaa28
jmp 0xdaad9
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0xda93c
jmp 0xdaaec
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xda90a
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)