// File: /workspace/github_c_makefile_star2_O3/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 0xe8a1c
movl $0x88, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movl $0x70, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movq (%r14), %rcx
movl %eax, 0x48(%rcx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf3eb8
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 0xeead3
movb %al, 0x30(%r14)
movzbl %al, %edx
leaq 0xe6666(%rip), %rsi     # 0x11744f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x1, %esi
callq 0xeead3
movzbl %al, %edx
addq $0x2, %rdx
movq %rdx, 0x40(%r14)
leaq 0xe6620(%rip), %rsi     # 0x117433
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movzbl 0x30(%r14), %edx
cmpl $0x3, %edx
jae 0x31044
leaq 0xe61ee(%rip), %rax     # 0x117020
movq (%rax,%rdx,8), %rax
leaq 0xe61fb(%rip), %rcx     # 0x117038
movl (%rcx,%rdx,4), %ecx
movl %edx, %edx
leaq 0xe61ff(%rip), %rsi     # 0x117048
movq (%rsi,%rdx,8), %rdx
movq %rax, 0x48(%r14)
movl %ecx, 0x50(%r14)
movq %rdx, 0x28(%r14)
movq %rbx, %rdi
movl $0x10, %esi
callq 0xeb499
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rsi
movq 0x40(%r14), %rdx
movq 0x48(%r14), %rcx
movq %rax, %rdi
callq 0xeb944
movq 0x18(%r14), %rdi
callq 0xeb865
movl %eax, %ebp
leaq 0xe65d2(%rip), %rsi     # 0x117462
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe87e4
cmpl 0x50(%r14), %ebp
je 0x30eb3
leaq 0xe65cc(%rip), %rsi     # 0x117475
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movb 0x30(%r14), %al
testb %al, %al
jne 0x30efb
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeead3
movzbl %al, %eax
movq (%r14), %rcx
movq %rax, 0x58(%rcx)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xeead3
movzbl %al, %esi
movq (%r14), %rax
movq %rsi, 0x50(%rax)
movq 0x58(%rax), %rdx
movq %rbx, %rdi
callq 0xe8f88
movb 0x30(%r14), %al
cmpb $0x1, %al
jne 0x30f6e
movq (%r14), %rax
movq $0x50, 0x50(%rax)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeebc8
movq %rax, %r15
leaq 0xe659a(%rip), %rsi     # 0x1174bc
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movl $0x50, %esi
movq %r15, %rdi
callq 0xe9cb4
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 0xe8f88
movb 0x30(%r14), %al
cmpb $0x2, %al
jne 0x30f98
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeebc8
movq %rax, 0x38(%r14)
leaq 0x119c5a(%rip), %rsi    # 0x14abe5
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xeec55
movq %rax, 0x20(%r14)
leaq 0xe651b(%rip), %rsi     # 0x1174cc
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x28(%r14), %rdx
leaq 0xf506c(%rip), %rsi     # 0x126035
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpb $0x2, 0x30(%r14)
jne 0x31066
leaq 0xe64f7(%rip), %rsi     # 0x1174dc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x31eb4
movq (%r14), %rcx
cmpb $0x0, (%rcx)
jne 0x310e6
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x31eb4
movq (%r14), %rcx
cmpb $0x0, (%rcx)
jne 0x310e6
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x10(%r14), %rax
jmp 0x3107e
leaq 0xe63f8(%rip), %rsi     # 0x117443
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq (%r14), %rcx
testq %rcx, %rcx
jne 0x310e6
jmp 0x310fd
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1388
movq %rax, 0x10(%r14)
movq (%r14), %rcx
testq %rax, %rax
je 0x310e2
movq 0x50(%rcx), %rdx
testq %rdx, %rdx
jle 0x310e2
movq 0x58(%rcx), %rsi
testq %rsi, %rsi
jle 0x310e2
cmpq $0x51, %rdx
setb %dl
cmpq $0x1a, %rsi
setb %sil
testb %sil, %dl
jne 0x310b4
movq 0x8(%r14), %rdx
movb $0x1, 0x3(%rdx)
cmpb $0x0, (%rcx)
jne 0x31055
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 0xf9c77
jmp 0x31055
movb $0x1, 0x1(%rcx)
cmpb $0x0, 0x1(%rcx)
je 0x310fd
leaq 0xe63f7(%rip), %rsi     # 0x1174ea
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x10(%r14), %rdi
callq 0xf052b
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x31123
movq %rbx, %rdi
callq 0xf3ec2
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xf3f7e
movq 0x18(%r14), %rdi
callq 0xeb858
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x31e6b
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq