// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
leaq 0xc(%rsp), %rsi
callq 0xdebae
movl $0x88, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
pushq $0x70
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, (%r14)
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movq (%r14), %rcx
movl %eax, 0x48(%rcx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe88f4
movq %rax, 0x8(%r14)
movb $0x1, %cl
movb %cl, 0x2(%rax)
movq (%r14), %rax
movb %cl, 0x4c(%rax)
movq 0x18(%rbx), %rdi
pushq $0x6
popq %rsi
callq 0xe3eb4
movb %al, 0x30(%r14)
movzbl %al, %edx
leaq 0xda5ee(%rip), %rsi     # 0x10a3bf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0xe3eb4
movzbl %al, %edx
addq $0x2, %rdx
movq %rdx, 0x40(%r14)
leaq 0xda5aa(%rip), %rsi     # 0x10a3a3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
movzbl 0x30(%r14), %edx
cmpl $0x3, %edx
jae 0x30004
leaq 0xda178(%rip), %rax     # 0x109f90
movq (%rax,%rdx,8), %rax
leaq 0xda185(%rip), %rcx     # 0x109fa8
movl (%rcx,%rdx,4), %ecx
movl %edx, %edx
leaq 0xda189(%rip), %rsi     # 0x109fb8
movq (%rsi,%rdx,8), %rdx
movq %rax, 0x48(%r14)
movl %ecx, 0x50(%r14)
movq %rdx, 0x28(%r14)
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rsi
movq 0x40(%r14), %rdx
movq 0x48(%r14), %rcx
movq %rax, %rdi
callq 0xe0f33
movq 0x18(%r14), %rdi
callq 0xe0e95
movl %eax, %ebp
leaq 0xda55e(%rip), %rsi     # 0x10a3d2
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde9f1
cmpl 0x50(%r14), %ebp
je 0x2fe97
leaq 0xda558(%rip), %rsi     # 0x10a3e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movb 0x30(%r14), %al
testb %al, %al
jne 0x2fedb
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe3eb4
movzbl %al, %eax
movq (%r14), %rcx
movq %rax, 0x58(%rcx)
movq 0x18(%rbx), %rdi
pushq $0x5
popq %rsi
callq 0xe3eb4
movzbl %al, %esi
movq (%r14), %rax
movq %rsi, 0x50(%rax)
movq 0x58(%rax), %rdx
movq %rbx, %rdi
callq 0xdf038
movb 0x30(%r14), %al
cmpb $0x1, %al
jne 0x2ff37
movq (%r14), %rax
pushq $0x50
popq %r15
movq %r15, 0x50(%rax)
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe3fa3
movq %rax, %r12
leaq 0xda52c(%rip), %rsi     # 0x10a42c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %r12, %rdi
movq %r15, %rsi
callq 0xdfbb6
cqto
idivq %r15
movq (%r14), %rcx
movq %rax, 0x58(%rcx)
movq 0x50(%rcx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movb 0x30(%r14), %al
cmpb $0x2, %al
jne 0x2ff5f
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe3fa3
movq %rax, 0x38(%r14)
leaq 0x10d7c3(%rip), %rsi    # 0x13d715
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x7
popq %rsi
callq 0xe400a
movq %rax, 0x20(%r14)
leaq 0xda4c6(%rip), %rsi     # 0x10a43c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x28(%r14), %rdx
leaq 0xe8ea7(%rip), %rsi     # 0x118e35
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpb $0x2, 0x30(%r14)
jne 0x30026
leaq 0xda4a2(%rip), %rsi     # 0x10a44c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x30dff
movq (%r14), %rcx
cmpb $0x0, (%rcx)
jne 0x300a3
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x30dff
movq (%r14), %rcx
cmpb $0x0, (%rcx)
jne 0x300a3
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x10(%r14), %rax
jmp 0x3003e
leaq 0xda3a8(%rip), %rsi     # 0x10a3b3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq (%r14), %rcx
testq %rcx, %rcx
jne 0x300a3
jmp 0x300ba
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xe6248
movq %rax, 0x10(%r14)
movq (%r14), %rcx
testq %rax, %rax
je 0x3009f
movq 0x50(%rcx), %rdx
testq %rdx, %rdx
jle 0x3009f
movq 0x58(%rcx), %rsi
testq %rsi, %rsi
jle 0x3009f
cmpq $0x51, %rdx
setb %dl
cmpq $0x1a, %rsi
setb %sil
testb %sil, %dl
jne 0x30074
movq 0x8(%r14), %rdx
movb $0x1, 0x3(%rdx)
cmpb $0x0, (%rcx)
jne 0x30015
movq %rax, 0x30(%rcx)
andq $0x0, 0x38(%rcx)
leaq 0x30(%rcx), %rsi
movq 0x18(%rax), %rax
movq %rax, 0x40(%rcx)
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xee2e5
jmp 0x30015
movb $0x1, 0x1(%rcx)
cmpb $0x0, 0x1(%rcx)
je 0x300ba
leaq 0xda3aa(%rip), %rsi     # 0x10a45a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq 0x10(%r14), %rdi
callq 0xe5591
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x300e0
movq %rbx, %rdi
callq 0xe88fe
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xe8998
movq 0x18(%r14), %rdi
callq 0xe0e8d
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x30db6
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq