// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/bintext.c
// Function: thedrawcom_decrunch
static void thedrawcom_decrunch(deark *c, struct thedrawcom_ctx *tdc, u8 prescan_mode)
{
	i64 pos = tdc->data_pos;
	i64 endpos = tdc->data_pos + tdc->cmpr_len;
	struct thedrawcom_dcmpr_state *dc = &tdc->dc;

	de_zeromem(dc, sizeof(struct thedrawcom_dcmpr_state));
	dc->prescan_mode = prescan_mode;
	dc->curr_fg_code = 0xf;

	if(endpos > c->infile->len) {
		tdc->d->errflag = 1;
		tdc->d->need_errmsg = 1;
		goto done;
	}

	if(!tdc->unc_data) {
		tdc->unc_data = dbuf_create_membuf(c, 80*2*25, 0);
		dbuf_set_length_limit(tdc->unc_data,
			THEDRAWCOM_MAX_WIDTH*2*THEDRAWCOM_MAX_HEIGHT);
		dbuf_enable_wbuffer(tdc->unc_data);
	}

	while(1) {
		u8 b0, b1, b2;
		i64 k;

		if(tdc->d->errflag) goto done;
		if(pos >= endpos) break;

		b0 = de_getbyte_p(&pos);

		if(b0>=32) {
			tdc_decrunch_emit_char(c, tdc, b0);
			continue;
		}
		if(b0<=15) {
			dc->curr_fg_code = b0;
		}
		else if(b0<=23) {
			dc->curr_bg_code = (b0-16)<<4;
		}
		else if(b0==24) { // Newline

			dc->next_ypos++;
			dc->next_xpos = 0;

			if(!dc->prescan_mode) {
				i64 expected_len;
				i64 actual_len;

				// Make sure we're in the right place in the decompressed data.
				expected_len = tdc->d->csctx.width_in_chars * 2 * dc->next_ypos;
				actual_len = dbuf_get_length(tdc->unc_data);
				if(actual_len != expected_len) {
					// It would be better to pad with spaces, but in practice
					// this doesn't happen.
					dbuf_truncate(tdc->unc_data, expected_len);
				}
			}
		}
		else if(b0==25) { // Run of spaces
			b1 = de_getbyte_p(&pos);
			for(k=0; k<(i64)b1+1; k++) {
				tdc_decrunch_emit_char(c, tdc, 32);
			}
		}
		else if(b0==26) { // Run of an arbitrary character
			b1 = de_getbyte_p(&pos);
			b2 = de_getbyte_p(&pos);
			for(k=0; k<(i64)b1+1; k++) {
				tdc_decrunch_emit_char(c, tdc, b2);
			}
		}
		else if(b0==27) { // Toggle blink mode
			dc->curr_blink_code = (dc->curr_blink_code==0) ? 0x80 : 0x00;
		}
		else { // Unknown opcode
			tdc->d->errflag = 1;
			tdc->d->need_errmsg = 1;
			goto done;
		}
	}

	// Decompression finished normally

	if((dc->max_xpos+1 > THEDRAWCOM_MAX_WIDTH) ||
		(dc->max_ypos+1 > THEDRAWCOM_MAX_HEIGHT))
	{
		tdc->d->errflag = 1;
		tdc->d->need_errmsg = 1;
		goto done;
	}

	if(dc->prescan_mode && !tdc->d->errflag) {
		de_dbg(c, "number of rows: %d", (int)(dc->max_ypos+1));
		de_dbg(c, "last nonblank row: %d", (int)dc->ypos_of_last_nonblank);
		tdc->d->csctx.width_in_chars = dc->max_xpos + 1;

		if(dc->max_ypos>24) {
			tdc->d->csctx.height_in_chars = dc->ypos_of_last_nonblank + 1;
		}
		else {
			tdc->d->csctx.height_in_chars = dc->max_ypos + 1;
		}

		de_dbg_dimensions(c, tdc->d->csctx.width_in_chars, tdc->d->csctx.height_in_chars);
	}

done:
	dbuf_flush(tdc->unc_data);

	if(tdc->d->need_errmsg) {
		de_err(c, "Decompression failed");
		tdc->d->need_errmsg = 0;
		tdc->d->errflag = 1;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq 0x28(%rsi), %r13
movq %r13, 0x10(%rsp)
addq 0x38(%rsi), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x58(%rsi)
movups %xmm0, 0x68(%rsi)
movups %xmm0, 0x78(%rsi)
movb %dl, 0x58(%rsi)
movb $0xf, 0x59(%rsi)
movq %rdi, (%rsp)
movq 0x18(%rdi), %rax
cmpq 0x18(%rax), %r13
jle 0x3207b
movq (%rbx), %rax
movw $0x101, (%rax)          # imm = 0x101
jmp 0x3226c
cmpq $0x0, 0x10(%rbx)
jne 0x320ac
movl $0xfa0, %esi            # imm = 0xFA0
movq (%rsp), %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x10(%rbx)
movl $0xfa00, %esi           # imm = 0xFA00
movq %rax, %rdi
callq 0xf1066
movq 0x10(%rbx), %rdi
callq 0xf0e3f
leaq 0x58(%rbx), %rax
movq %rax, 0x18(%rsp)
movl $0x3, %eax
movl %eax, %eax
movq %rax, 0x20(%rsp)
leaq 0xe4e50(%rip), %r12     # 0x116f18
xorl %eax, %eax
movl %eax, %eax
movq %rax, 0x8(%rsp)
movq (%rbx), %rax
movl $0x2, %r15d
cmpb $0x0, (%rax)
jne 0x32260
movl $0x4, %r15d
cmpq %r13, 0x10(%rsp)
jge 0x32260
movq (%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef14d
cmpb $0x20, %al
jb 0x32123
movzbl %al, %esi
movq %rbx, %rdi
callq 0x3233a
movq 0x20(%rsp), %rax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
cmpb $0xf, %al
ja 0x32138
movb %al, 0x59(%rbx)
movq 0x8(%rsp), %rax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
cmpb $0x17, %al
ja 0x32150
shlb $0x4, %al
movb %al, 0x5a(%rbx)
movq 0x8(%rsp), %rax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movzbl %al, %eax
addl $-0x18, %eax
cmpl $0x3, %eax
ja 0x32252
leaq 0xe4da2(%rip), %rcx     # 0x116f08
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x68(%rbx), %rbp
incq %rbp
movq %rbp, 0x68(%rbx)
movq $0x0, 0x60(%rbx)
xorl %r15d, %r15d
cmpb $0x0, 0x58(%rbx)
jne 0x32260
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
imulq 0x50(%rax), %rbp
addq %rbp, %rbp
callq 0xf1d13
cmpq %rbp, %rax
je 0x32260
movq 0x10(%rbx), %rdi
movq %rbp, %rsi
callq 0xf152a
movq 0x8(%rsp), %rax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rsp), %r14
movq 0x18(%r14), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xef14d
movl %eax, %ebp
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xef14d
movzbl %bpl, %r14d
incq %r14
movzbl %al, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3233a
xorl %r15d, %r15d
decq %r14
jne 0x321f3
jmp 0x32260
cmpb $0x0, 0x5b(%rbx)
sete %al
shlb $0x7, %al
movb %al, 0x5b(%rbx)
movq 0x8(%rsp), %rax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef14d
movzbl %al, %r14d
incq %r14
movq %rbx, %rdi
movl $0x20, %esi
callq 0x3233a
xorl %r15d, %r15d
decq %r14
jne 0x3223b
jmp 0x32260
movq (%rbx), %rax
movw $0x101, (%rax)          # imm = 0x101
movl $0x2, %r15d
movl %r15d, %eax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x10(%rbx), %rdi
callq 0xf0ea4
movq (%rbx), %rax
cmpb $0x0, 0x1(%rax)
je 0x32298
leaq 0xe0ad8(%rip), %rsi     # 0x112d5d
movq (%rsp), %rdi
xorl %eax, %eax
callq 0xe9df7
movq (%rbx), %rax
movw $0x1, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x9f, 0x70(%rbx)
jg 0x3206e
movq 0x78(%rbx), %rdx
cmpq $0xc8, %rdx
jge 0x3206e
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
je 0x3226c
movq (%rbx), %rax
cmpb $0x0, (%rax)
jne 0x3226c
incl %edx
leaq 0xe5184(%rip), %rsi     # 0x117465
movq (%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x80(%rbx), %edx
leaq 0xe517c(%rip), %rsi     # 0x117478
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq (%rbx), %rax
movq 0x70(%rbx), %rsi
incq %rsi
movq %rsi, 0x50(%rax)
movq 0x78(%rbx), %rdx
cmpq $0x19, %rdx
jl 0x32325
movq 0x80(%rbx), %rdx
incq %rdx
movq %rdx, 0x58(%rax)
movq (%rsp), %rdi
callq 0xe9b05
jmp 0x3226c