// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x88, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x70, %esi
callq 0x15b780
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xd, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x16d7b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movb $0x1, 0x2(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x4c(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x6, %esi
callq 0x165060
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %edx
leaq 0x1638bd(%rip), %rsi    # 0x1a31f5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x1, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
addq $0x2, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
leaq 0x163864(%rip), %rsi    # 0x1a31d9
movb $0x0, %al
callq 0x15b350
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x0, %eax
jne 0x3f9b1
movq -0x18(%rbp), %rax
movq $0x71, 0x48(%rax)
movq -0x18(%rbp), %rax
movl $0x440affae, 0x50(%rax) # imm = 0x440AFFAE
movq -0x18(%rbp), %rax
movq $0xb0, 0x28(%rax)
jmp 0x3fa38
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x1, %eax
jne 0x3f9e3
movq -0x18(%rbp), %rax
movq $0x44, 0x48(%rax)
movq -0x18(%rbp), %rax
movl $0xe427d2e3, 0x50(%rax) # imm = 0xE427D2E3
movq -0x18(%rbp), %rax
movq $0x5e, 0x28(%rax)
jmp 0x3fa36
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x2, %eax
jne 0x3fa15
movq -0x18(%rbp), %rax
movq $0xb1, 0x48(%rax)
movq -0x18(%rbp), %rax
movl $0x492a698d, 0x50(%rax) # imm = 0x492A698D
movq -0x18(%rbp), %rax
movq $0xf0, 0x28(%rax)
jmp 0x3fa34
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %edx
leaq 0x1637c1(%rip), %rsi    # 0x1a31e9
movb $0x0, %al
callq 0x15bfb0
jmp 0x3fdc5
jmp 0x3fa36
jmp 0x3fa38
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x15eac0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
callq 0x15f500
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x15f340
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0x163774(%rip), %rsi    # 0x1a3208
movb $0x0, %al
callq 0x15b350
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x50(%rcx), %eax
je 0x3fab9
movq -0x8(%rbp), %rdi
leaq 0x163769(%rip), %rsi    # 0x1a321b
movb $0x0, %al
callq 0x15bc10
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x0, %eax
jne 0x3fb29
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5, %esi
callq 0x165060
movzbl %al, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x58(%rax), %rdx
callq 0x15bba0
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x1, %eax
jne 0x3fbba
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq $0x50, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x1636f4(%rip), %rsi    # 0x1a3262
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rdi
movl $0x50, %esi
callq 0x15ce90
movl $0x50, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x50(%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x58(%rax), %rdx
callq 0x15bba0
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x2, %eax
jne 0x3fbfe
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0x196a6a(%rip), %rsi    # 0x1d6661
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x7, %esi
callq 0x165360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x163644(%rip), %rsi    # 0x1a3272
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x171c2a(%rip), %rsi    # 0x1b1872
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movzbl 0x30(%rax), %eax
cmpl $0x2, %eax
jne 0x3fccf
movq -0x8(%rbp), %rdi
leaq 0x16361b(%rip), %rsi    # 0x1a3282
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x1, %edx
callq 0x41380
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpb $0x0, (%rax)
je 0x3fc9f
jmp 0x3fdc5
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x41380
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpb $0x0, (%rax)
je 0x3fcbf
jmp 0x3fdc5
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0x3fd03
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rax
subq 0x28(%rax), %rdx
callq 0x1691b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x3fd2a
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpq $0x1, 0x50(%rax)
jl 0x3fd2a
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpq $0x1, 0x58(%rax)
jge 0x3fd3a
movq -0x18(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x1(%rax)
jmp 0x3fdc5
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpq $0x50, 0x50(%rax)
jg 0x3fd56
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpq $0x19, 0x58(%rax)
jle 0x3fd62
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movb $0x1, 0x3(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpb $0x0, (%rax)
je 0x3fd70
jmp 0x3fdc5
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq $0x0, 0x38(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
addq $0x30, %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x176610
cmpq $0x0, -0x18(%rbp)
je 0x3fe53
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3fdfc
movq -0x18(%rbp), %rax
movq (%rax), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x3fdfc
movq -0x8(%rbp), %rdi
leaq 0x16349b(%rip), %rsi    # 0x1a3290
movb $0x0, %al
callq 0x15bfb0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1676a0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x3fe36
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x16d7e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x16d910
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x15f2e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x41280
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x15b5a0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)