// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x28(%rsi), %r13
movq %r13, 0x8(%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 0x30e82
movq (%rbx), %rax
movw $0x101, (%rax)          # imm = 0x101
movq 0x10(%rbx), %rdi
callq 0xe58e8
movq (%rbx), %rax
cmpb $0x0, 0x1(%rax)
je 0x30e73
leaq 0xd4eec(%rip), %rsi     # 0x105d4d
movq %r14, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq (%rbx), %rax
movw $0x1, (%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x10(%rbx)
jne 0x30eb2
movl $0xfa0, %esi            # imm = 0xFA0
movq %r14, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, 0x10(%rbx)
movl $0xfa00, %esi           # imm = 0xFA00
movq %rax, %rdi
callq 0xe5a74
movq 0x10(%rbx), %rdi
callq 0xe589c
leaq 0x58(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x8(%rsp), %r15
movq (%rbx), %rax
cmpb $0x0, (%rax)
jne 0x30e48
cmpq %r13, 0x8(%rsp)
jge 0x30fde
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %esi
cmpb $0x20, %al
jb 0x30ef0
movq %rbx, %rdi
callq 0x31068
jmp 0x30ec0
cmpb $0xf, %al
ja 0x30ef9
movb %al, 0x59(%rbx)
jmp 0x30ec0
cmpb $0x17, %al
ja 0x30f05
shlb $0x4, %al
movb %al, 0x5a(%rbx)
jmp 0x30ec0
addl $-0x18, %esi
cmpl $0x3, %esi
ja 0x30e40
leaq 0xd9010(%rip), %rcx     # 0x109f28
movslq (%rcx,%rsi,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x68(%rbx), %r12
incq %r12
movq %r12, 0x68(%rbx)
andq $0x0, 0x60(%rbx)
cmpb $0x0, 0x58(%rbx)
jne 0x30ec0
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
imulq 0x50(%rax), %r12
addq %r12, %r12
callq 0xe651f
cmpq %r12, %rax
je 0x30ec0
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0xe5eb2
jmp 0x30ec0
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xe3f2a
movl %eax, %r12d
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %r12b, %ebp
incq %rbp
movzbl %al, %r12d
subq $0x1, %rbp
jb 0x30ec0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x31068
jmp 0x30f8b
cmpb $0x0, 0x5b(%rbx)
sete %al
shlb $0x7, %al
movb %al, 0x5b(%rbx)
jmp 0x30ec0
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %r12d
incq %r12
subq $0x1, %r12
jb 0x30ec0
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x31068
jmp 0x30fc7
cmpq $0x9f, 0x70(%rbx)
jg 0x30e43
movq 0x78(%rbx), %rdx
cmpq $0xc8, %rdx
jge 0x30e43
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax)
je 0x30e48
incl %edx
leaq 0xd9461(%rip), %rsi     # 0x10a475
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x80(%rbx), %edx
leaq 0xd945d(%rip), %rsi     # 0x10a488
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movq (%rbx), %rax
movq 0x70(%rbx), %rsi
incq %rsi
movq %rsi, 0x50(%rax)
movq 0x78(%rbx), %rdx
cmpq $0x19, %rdx
jl 0x31054
movq 0x80(%rbx), %rdx
incq %rdx
movq %rdx, 0x58(%rax)
movq %r14, %rdi
callq 0xdf038
jmp 0x30e48