// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/bintext.c
// Function: de_run_thedraw_com
static void de_run_thedraw_com(deark *c, de_module_params *mparams)
{
	struct thedrawcom_ctx *tdc = NULL;
	u32 cv;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	tdc = de_malloc(c, sizeof(struct thedrawcom_ctx));
	tdc->d = de_malloc(c, sizeof(lctx));
	tdc->d->csctx.input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);
	tdc->charctx = de_create_charctx(c, 0);
	tdc->charctx->screen_image_flag = 1;
	tdc->d->csctx.use_default_pal = 1;

	tdc->fmt_subtype = de_getbyte(6);
	de_dbg(c, "format subtype: %u", (UI)tdc->fmt_subtype);

	tdc->viewer_start = 2 + (i64)de_getbyte(1);
	de_dbg2(c, "viewer pos: %"I64_FMT, tdc->viewer_start);

	if(tdc->fmt_subtype==0) {
		tdc->viewer_len = 113;
		tdc->viewer_expected_crc = 0x440affaeU;
		tdc->data_pos = 176;
	}
	else if(tdc->fmt_subtype==1) {
		tdc->viewer_len = 68;
		tdc->viewer_expected_crc = 0xe427d2e3U;
		tdc->data_pos = 94;
	}
	else if(tdc->fmt_subtype==2) {
		tdc->viewer_len = 177;
		tdc->viewer_expected_crc = 0x492a698d;
		tdc->data_pos = 240;
	}
	else {
		de_err(c, "Unsupported format subtype: %u", (UI)tdc->fmt_subtype);
		goto done;
	}

	tdc->crco = de_crcobj_create(c, DE_CRCOBJ_CRC32_IEEE);
	de_crcobj_addslice(tdc->crco, c->infile, tdc->viewer_start, tdc->viewer_len);

	cv = de_crcobj_getval(tdc->crco);
	de_dbg2(c, "viewer crc: 0x%08x", (UI)cv);
	if(cv != tdc->viewer_expected_crc) {
		de_warn(c, "Unrecognized format variant. This file might not be "
			"decoded correctly.");
	}

	if(tdc->fmt_subtype==0) {
		tdc->d->csctx.height_in_chars = (i64)de_getbyte(4);
		tdc->d->csctx.width_in_chars = (i64)de_getbyte(5);
		de_dbg_dimensions(c, tdc->d->csctx.width_in_chars, tdc->d->csctx.height_in_chars);
	}

	if(tdc->fmt_subtype==1) {
		i64 nchars;

		tdc->d->csctx.width_in_chars = 80;
		nchars = de_getu16le(4);
		de_dbg(c, "num. chars: %"I64_FMT, nchars);
		tdc->d->csctx.height_in_chars = de_pad_to_n(nchars, 80)/80;
		de_dbg_dimensions(c, tdc->d->csctx.width_in_chars, tdc->d->csctx.height_in_chars);
	}

	if(tdc->fmt_subtype==2) {
		tdc->cmpr_len = de_getu16le(4);
		de_dbg(c, "cmpr len: %"I64_FMT, tdc->cmpr_len);
	}

	tdc->screen_pos_raw = de_geti16le(7);
	de_dbg(c, "screen pos: %"I64_FMT, tdc->screen_pos_raw);
	// TODO? The screen pos is relevant if just a block, instead of a whole
	// screen, was saved.
	// We could artificially pad the image with spaces above/left/right in
	// that case.

	de_dbg(c, "data pos: %"I64_FMT, tdc->data_pos);

	if(tdc->fmt_subtype==2) {
		de_dbg(c, "decompressing");
		de_dbg_indent(c, 1);
		// The first decompression is to figure out the dimensions.
		thedrawcom_decrunch(c, tdc, 1);
		if(tdc->d->errflag) goto done;
		thedrawcom_decrunch(c, tdc, 0);
		if(tdc->d->errflag) goto done;
		de_dbg_indent(c, -1);
	}
	else {
		tdc->unc_data = dbuf_open_input_subfile(c->infile, tdc->data_pos,
			c->infile->len-tdc->data_pos);
	}

	if(!tdc->unc_data || tdc->d->csctx.width_in_chars<1 || tdc->d->csctx.height_in_chars<1) {
		tdc->d->need_errmsg = 1;
		goto done;
	}
	if(tdc->d->csctx.width_in_chars>80 || tdc->d->csctx.height_in_chars>25) {
		tdc->charctx->no_density = 1;
	}

	if(tdc->d->errflag) goto done;
	tdc->d->csctx.inf = tdc->unc_data;
	tdc->d->csctx.inf_pos = 0;
	tdc->d->csctx.inf_len = tdc->unc_data->len;
	fmtutil_char_simple_run(c, &tdc->d->csctx, tdc->charctx);

done:
	if(tdc) {
		if(tdc->d && tdc->d->need_errmsg) {
			de_err(c, "Failed to decode this file");
		}

		dbuf_close(tdc->unc_data);
		if(tdc->charctx) {
			de_free_charctx_screens(c, tdc->charctx);
			de_destroy_charctx(c, tdc->charctx);
		}
		de_crcobj_destroy(tdc->crco);
		free_lctx(c, tdc->d);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x4(%rsp), %rsi
callq 0xe958c
movl $0x88, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movl $0x70, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb7d1
movq (%r14), %rcx
movl %eax, 0x48(%rcx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf43cc
movq %rax, 0x8(%r14)
movb $0x1, %cl
movb %cl, 0x2(%rax)
movq (%r14), %rax
movb %cl, 0x4c(%rax)
movq 0x18(%rbx), %rdi
movl $0x6, %esi
callq 0xef0d5
movb %al, 0x30(%r14)
movzbl %al, %edx
leaq 0xe6467(%rip), %rsi     # 0x1173af
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x1, %esi
callq 0xef0d5
movzbl %al, %edx
addq $0x2, %rdx
movq %rdx, 0x40(%r14)
leaq 0xe6421(%rip), %rsi     # 0x117393
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe93cf
movzbl 0x30(%r14), %edx
cmpl $0x3, %edx
jae 0x31198
leaq 0xe5fef(%rip), %rax     # 0x116f80
movq (%rax,%rdx,8), %rax
leaq 0xe5ffc(%rip), %rcx     # 0x116f98
movl (%rcx,%rdx,4), %ecx
movl %edx, %edx
leaq 0xe6000(%rip), %rsi     # 0x116fa8
movq (%rsi,%rdx,8), %rdx
movq %rax, 0x48(%r14)
movl %ecx, 0x50(%r14)
movq %rdx, 0x28(%r14)
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeba56
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rsi
movq 0x40(%r14), %rdx
movq 0x48(%r14), %rcx
movq %rax, %rdi
callq 0xebf09
movq 0x18(%r14), %rdi
callq 0xebe2a
movl %eax, %ebp
leaq 0xe63d3(%rip), %rsi     # 0x1173c2
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe93cf
cmpl 0x50(%r14), %ebp
je 0x31012
leaq 0xe63cd(%rip), %rsi     # 0x1173d5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
cmpb $0x0, 0x30(%r14)
jne 0x31055
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef0d5
movzbl %al, %eax
movq (%r14), %rcx
movq %rax, 0x58(%rcx)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xef0d5
movzbl %al, %esi
movq (%r14), %rax
movq %rsi, 0x50(%rax)
movq 0x58(%rax), %rdx
movq %rbx, %rdi
callq 0xe9b05
cmpb $0x1, 0x30(%r14)
jne 0x310c7
movq (%r14), %rax
movq $0x50, 0x50(%rax)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef1ca
movq %rax, %r15
leaq 0xe639d(%rip), %rsi     # 0x11741c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movl $0x50, %esi
movq %r15, %rdi
callq 0xea80b
movabsq $0x6666666666666667, %rcx # imm = 0x6666666666666667
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x5, %rdx
addq %rax, %rdx
movq (%r14), %rax
movq %rdx, 0x58(%rax)
movq 0x50(%rax), %rsi
movq %rbx, %rdi
callq 0xe9b05
cmpb $0x2, 0x30(%r14)
jne 0x310f4
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef1ca
movq %rax, 0x38(%r14)
leaq 0x11954e(%rip), %rsi    # 0x14a635
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xef257
movq %rax, 0x20(%r14)
leaq 0xe631f(%rip), %rsi     # 0x11742c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x28(%r14), %rdx
leaq 0xf4ce0(%rip), %rsi     # 0x125e05
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpb $0x2, 0x30(%r14)
jne 0x311ae
leaq 0xe62ff(%rip), %rsi     # 0x11743c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x3202c
movq (%r14), %rax
cmpb $0x0, (%rax)
jne 0x31232
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3202c
movq (%r14), %rax
cmpb $0x0, (%rax)
jne 0x31232
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
jmp 0x311c6
leaq 0xe6204(%rip), %rsi     # 0x1173a3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x31232
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1988
movq %rax, 0x10(%r14)
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x3122b
movq (%r14), %rcx
movq 0x50(%rcx), %rdx
testq %rdx, %rdx
jle 0x3122b
movq 0x58(%rcx), %rsi
testq %rsi, %rsi
jle 0x3122b
cmpq $0x51, %rdx
setl %dl
cmpq $0x1a, %rsi
setl %sil
testb %sil, %dl
jne 0x31200
movq 0x8(%r14), %rdx
movb $0x1, 0x3(%rdx)
cmpb $0x0, (%rcx)
jne 0x31232
leaq 0x30(%rcx), %rsi
movq %rax, 0x30(%rcx)
movq $0x0, 0x38(%rcx)
movq 0x18(%rax), %rax
movq %rax, 0x40(%rcx)
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xfa176
jmp 0x31232
movq (%r14), %rax
movb $0x1, 0x1(%rax)
movq (%r14), %rax
testq %rax, %rax
je 0x31251
cmpb $0x0, 0x1(%rax)
je 0x31251
leaq 0xe6203(%rip), %rsi     # 0x11744a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x10(%r14), %rdi
callq 0xf0b13
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x31277
movq %rbx, %rdi
callq 0xf43d6
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xf448e
movq 0x18(%r14), %rdi
callq 0xebe1d
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x31fe3
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq