// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movq 0x28(%rsi), %r13
movq %r13, (%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 0x18(%rdi), %rax
cmpq 0x18(%rax), %r13
jle 0x31efb
movq (%rbx), %rax
movw $0x101, (%rax)          # imm = 0x101
jmp 0x31f33
cmpq $0x0, 0x10(%rbx)
jne 0x31f2b
movl $0xfa0, %esi            # imm = 0xFA0
movq %r14, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x10(%rbx)
movl $0xfa00, %esi           # imm = 0xFA00
movq %rax, %rdi
callq 0xf0a7c
movq 0x10(%rbx), %rdi
callq 0xf0855
movq (%rbx), %rax
cmpb $0x0, (%rax)
je 0x31f6d
movq 0x10(%rbx), %rdi
callq 0xf08ba
movq (%rbx), %rax
cmpb $0x0, 0x1(%rax)
je 0x31f5e
leaq 0xe0e21(%rip), %rsi     # 0x112d6d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9295
movq (%rbx), %rax
movw $0x1, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %r13, (%rsp)
jge 0x32090
movq %rsp, %r15
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xeeb4b
cmpb $0x20, %al
jb 0x31f97
movzbl %al, %esi
movq %rbx, %rdi
callq 0x32119
jmp 0x31faa
cmpb $0xf, %al
ja 0x31fa0
movb %al, 0x59(%rbx)
jmp 0x31faa
cmpb $0x17, %al
ja 0x31fbd
shlb $0x4, %al
movb %al, 0x5a(%rbx)
movq (%rbx), %rax
cmpb $0x0, (%rax)
jne 0x31f33
cmpq %r13, (%rsp)
jl 0x31f7a
jmp 0x32090
movzbl %al, %eax
addl $-0x18, %eax
cmpl $0x3, %eax
ja 0x31ef1
leaq 0xe4fe5(%rip), %rcx     # 0x116fb8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x68(%rbx), %r12
incq %r12
movq %r12, 0x68(%rbx)
movq $0x0, 0x60(%rbx)
cmpb $0x0, 0x58(%rbx)
jne 0x31faa
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
imulq 0x50(%rax), %r12
addq %r12, %r12
callq 0xf1701
cmpq %r12, %rax
je 0x31faa
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0xf0f27
jmp 0x31faa
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xeeb4b
movl %eax, %ebp
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xeeb4b
movzbl %bpl, %r12d
incq %r12
movzbl %al, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x32119
decq %r12
jne 0x32040
jmp 0x31faa
cmpb $0x0, 0x5b(%rbx)
sete %al
shlb $0x7, %al
movb %al, 0x5b(%rbx)
jmp 0x31faa
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xeeb4b
movzbl %al, %r12d
incq %r12
movq %rbx, %rdi
movl $0x20, %esi
callq 0x32119
decq %r12
jne 0x32079
jmp 0x31faa
cmpq $0x9f, 0x70(%rbx)
jg 0x31ef4
movq 0x78(%rbx), %rdx
cmpq $0xc8, %rdx
jge 0x31ef4
leaq 0x58(%rbx), %rax
cmpb $0x0, (%rax)
je 0x31f33
incl %edx
leaq 0xe5440(%rip), %rsi     # 0x117505
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x80(%rbx), %edx
leaq 0xe543c(%rip), %rsi     # 0x117518
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
movq (%rbx), %rax
movq 0x70(%rbx), %rsi
incq %rsi
movq %rsi, 0x50(%rax)
movq 0x78(%rbx), %rdx
cmpq $0x19, %rdx
jl 0x32105
movq 0x80(%rbx), %rdx
incq %rdx
movq %rdx, 0x58(%rax)
movq %r14, %rdi
callq 0xe8f88
jmp 0x31f33