// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/bintext.c
// Function: de_run_xbin
static void de_run_xbin(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	struct de_char_context *charctx = NULL;
	struct de_SAUCE_detection_data sdd;
	struct de_SAUCE_info *si = NULL;
	i64 pos = 0;
	u8 flags;
	dbuf *unc_data = NULL;

	d = de_malloc(c, sizeof(lctx));

	charctx = de_create_charctx(c, 0);
	charctx->prefer_image_output = 1;
	de_char_decide_output_format(c, charctx);

	fmtutil_detect_SAUCE(c, c->infile, &sdd, 0x1);
	if(sdd.has_SAUCE) {
		si = fmtutil_create_SAUCE(c);

		de_dbg_indent(c, 1);
		fmtutil_handle_SAUCE(c, c->infile, si);
		de_dbg_indent(c, -1);

		charctx->title = si->title;
		charctx->artist = si->artist;
		charctx->organization = si->organization;
		charctx->creation_date = si->creation_date;
		charctx->comment = si->comment;
	}

	d->csctx.width_in_chars = de_getu16le(5);
	d->csctx.height_in_chars = de_getu16le(7);
	d->font_height = (i64)de_getbyte(9);

	flags = de_getbyte(10);
	de_dbg(c, "dimensions: %d"DE_CHAR_TIMES"%d characters", (int)d->csctx.width_in_chars,
		(int)d->csctx.height_in_chars);
	de_dbg(c, "font height: %d", (int)d->font_height);
	de_dbg(c, "flags: 0x%02x", (UI)flags);
	d->has_palette = (flags&0x01)?1:0;
	d->has_font = (flags&0x02)?1:0;
	d->compression = (flags&0x04)?1:0;
	d->csctx.nonblink = (flags&0x08)?1:0;
	d->has_512chars = (flags&0x10)?1:0;
	de_dbg_indent(c, 1);
	de_dbg(c, "has palette: %u", (UI)d->has_palette);
	de_dbg(c, "has font: %u", (UI)d->has_font);
	de_dbg(c, "compression: %u", (UI)d->compression);
	de_dbg(c, "non-blink mode: %u", (UI)d->csctx.nonblink);
	de_dbg(c, "512 character mode: %u", (UI)d->has_512chars);
	de_dbg_indent(c, -1);

	if(d->has_font && (d->font_height<1 || d->font_height>32)) {
		de_err(c, "Invalid font height: %d", (int)d->font_height);
		goto done;
	}
	pos = 11;

	if(d->has_palette) {
		do_read_palette(c, d, charctx, pos, 0);
		pos += 48;
	}
	else {
		de_dbg(c, "using default palette");
		d->csctx.use_default_pal = 1;
	}

	if(d->has_font) {
		d->font = de_create_bitmap_font(c);
		d->font->has_nonunicode_codepoints = 1;
		d->font->has_unicode_codepoints = 1;
		d->font->prefer_unicode = 0;
		d->font->num_chars = d->has_512chars ? 512 : 256;
		d->font_data_len = d->font->num_chars * d->font_height;
		if(d->font->num_chars!=256) {
			de_err(c, "%d-character mode is not supported", (int)d->font->num_chars);
			goto done;
		}

		do_read_font_data(c, d, pos);
		pos += d->font_data_len;

		if(d->is_standard_font) {
			charctx->suppress_custom_font_warning = 1;
		}

		if(!do_generate_font(c, d)) goto done;

		if(c->extract_level>=2 && d->font) {
			de_font_decide_output_fmt(c);
			if(c->font_fmt_req!=DE_FONTFMT_IMAGE) {
				d->font->force_fontfile_output = 1;
			}
			do_extract_font(c, d);
		}

		charctx->font = d->font;
	}
	else {
		// Use default font

		if(d->has_512chars) {
			de_err(c, "This type of XBIN file is not supported.");
			goto done;
		}

		if(d->font_height==0) {
			// Not really legal, but we'll let it mean "default".
		}
		else if(d->font_height!=16) {
			if(charctx->outfmt==1) { // image output
				de_warn(c, "Incompatible font height (%d), using 16 instead.", (int)d->font_height);
			}
		}
		d->font_height = 16;
	}

	de_dbg(c, "image data at %"I64_FMT, pos);

	if(d->compression) {
		unc_data = dbuf_create_membuf(c, d->csctx.width_in_chars * d->csctx.height_in_chars * 2, 1);
		xbin_decompress_data(c, d, pos, unc_data);
	}
	else {
		unc_data = dbuf_open_input_subfile(c->infile, pos, c->infile->len-pos);
	}
	d->csctx.inf = unc_data;
	d->csctx.inf_pos = 0;
	d->csctx.inf_len = unc_data->len;
	fmtutil_char_simple_run(c, &d->csctx, charctx);

done:
	dbuf_close(unc_data);
	de_free_charctx_screens(c, charctx);
	de_destroy_charctx(c, charctx);
	fmtutil_free_SAUCE(c, si);
	free_lctx(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl $0x70, %esi
callq 0xe969b
movq %rax, %r12
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf43cc
movq %rax, %r13
movb $0x1, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0xf44ef
movq 0x18(%rbx), %rsi
leaq 0x35(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rdx
movl $0x1, %ecx
callq 0xf840d
cmpb $0x0, (%r15)
je 0x30293
movq %rbx, %rdi
callq 0xf84f4
movq %rax, %r14
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xf84ad
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x8(%r14), %rax
movq %rax, 0x68(%r13)
movq 0x10(%r14), %rax
movq %rax, 0x70(%r13)
movq 0x18(%r14), %rax
movq %rax, 0x78(%r13)
movups 0x20(%r14), %xmm0
movups %xmm0, 0x80(%r13)
movq 0x60(%r14), %rax
movq %rax, 0x90(%r13)
movq 0x18(%rbx), %rdi
movl $0x5, %esi
callq 0xef1ca
movq %rax, 0x50(%r12)
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xef1ca
movq %rax, 0x58(%r12)
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xef0d5
movzbl %al, %eax
movq %rax, 0x8(%r12)
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef0d5
movl %eax, %ebp
movl 0x50(%r12), %edx
movl 0x58(%r12), %ecx
leaq 0xe6dcf(%rip), %rsi     # 0x1170bf
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x8(%r12), %edx
leaq 0xe6e26(%rip), %rsi     # 0x11712c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl %bpl, %ebp
leaq 0xffa4f(%rip), %rsi     # 0x12fd6a
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movl %ebp, %eax
andb $0x1, %al
movb %al, 0x2(%r12)
movl %ebp, %eax
shrb %al
andb $0x1, %al
movb %al, 0x3(%r12)
movl %ebp, %eax
shrb $0x2, %al
andb $0x1, %al
movb %al, 0x4(%r12)
movl %ebp, %eax
shrb $0x3, %al
andb $0x1, %al
movb %al, 0x4d(%r12)
shrb $0x4, %bpl
andb $0x1, %bpl
movb %bpl, 0x5(%r12)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movzbl 0x2(%r12), %edx
leaq 0xe6d63(%rip), %rsi     # 0x1170dd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl 0x3(%r12), %edx
leaq 0xe6d5c(%rip), %rsi     # 0x1170ed
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl 0x4(%r12), %edx
leaq 0xe3406(%rip), %rsi     # 0x1137ae
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl 0x4d(%r12), %edx
leaq 0xe6d3b(%rip), %rsi     # 0x1170fa
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl 0x5(%r12), %edx
leaq 0xe6d37(%rip), %rsi     # 0x11710d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpb $0x0, 0x3(%r12)
je 0x3041c
movq 0x8(%r12), %rdx
leaq -0x21(%rdx), %rax
cmpq $-0x21, %rax
ja 0x3041c
leaq 0xe6d19(%rip), %rsi     # 0x117124
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x307ab
cmpb $0x0, 0x2(%r12)
movq %r13, 0x8(%rsp)
je 0x30443
movl $0xb, %edx
movq %rbx, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x31c19
movl $0x3b, %r13d
jmp 0x30460
leaq 0xe6cf2(%rip), %rsi     # 0x11713c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movb $0x1, 0x4c(%r12)
movl $0xb, %r13d
cmpb $0x0, 0x3(%r12)
je 0x304ca
movq %rbx, %rdi
callq 0xf57ec
movq %rax, 0x28(%r12)
movw $0x101, 0x10(%rax)      # imm = 0x101
movb $0x0, 0x12(%rax)
movb 0x5(%r12), %sil
xorl %edx, %edx
testb %sil, %sil
sete %cl
setne %dl
shll $0x8, %edx
addq $0x100, %rdx            # imm = 0x100
movq %rdx, 0x18(%rax)
movq 0x8(%r12), %rax
xorb $0x9, %cl
shlq %cl, %rax
movq %rax, 0x10(%r12)
testb %sil, %sil
je 0x304ee
leaq 0xe6c99(%rip), %rsi     # 0x117152
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x307a6
cmpb $0x0, 0x5(%r12)
je 0x30570
leaq 0xe6c98(%rip), %rsi     # 0x117175
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x307a6
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x31d5b
addq 0x10(%r12), %r13
cmpl $0x0, 0x20(%r12)
je 0x30512
movq 0x8(%rsp), %rax
movb $0x1, 0x4(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31e70
testl %eax, %eax
je 0x307a4
cmpl $0x2, 0x90(%rbx)
jl 0x3055b
cmpq $0x0, 0x28(%r12)
je 0x3055b
movq %rbx, %rdi
callq 0xf6717
cmpb $0x3, 0xd5(%rbx)
je 0x30550
movq 0x28(%r12), %rax
movb $0x1, 0x13(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31f7f
movq %r14, 0x20(%rsp)
movq 0x28(%r12), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
jmp 0x30590
movq 0x8(%r12), %rdx
testq $-0x11, %rdx
jne 0x307ee
movq %r14, 0x20(%rsp)
movq $0x10, 0x8(%r12)
movq 0x18(%rsp), %r14
addq $0x30, %r14
leaq 0x10a77f(%rip), %rsi    # 0x13ad1f
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
cmpb $0x0, 0x4(%r12)
je 0x3075d
movq %r14, 0x18(%rsp)
movq 0x50(%r12), %rsi
imulq 0x58(%r12), %rsi
addq %rsi, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %rbp
movq %r13, 0x10(%rsp)
movq 0x18(%rbx), %rdi
cmpq %r13, 0x18(%rdi)
jle 0x30756
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %rbx, %r14
movq %r12, 0x38(%rsp)
movq %r13, %rax
xorl %r13d, %r13d
cmpq 0x50(%r12), %rcx
movl $0x0, %edx
cmovlq %rcx, %rdx
setge %r13b
addq %rax, %r13
cmpq 0x58(%r12), %r13
jge 0x30756
movq %rdx, 0x40(%rsp)
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0xef14d
movl %eax, %ecx
shrb $0x6, %cl
andb $0x3f, %al
incb %al
movzbl %al, %edx
movq 0x18(%rbx), %rdi
movzbl %cl, %eax
leaq 0xe68aa(%rip), %rcx     # 0x116ef8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rdx, 0x28(%rsp)
jmpq *%rax
movq 0x10(%rsp), %rsi
leal (%rdx,%rdx), %r15d
movq %r15, %rdx
movq %rbp, %rcx
callq 0xefabc
addq %r15, 0x10(%rsp)
jmp 0x30731
movq %r12, %rsi
movq %rdx, %rbx
callq 0xef14d
movzbl %al, %r15d
movq 0x18(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef14d
movzbl %al, %esi
movq %rbp, %rdi
callq 0xf11be
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf11be
decq %rbx
jne 0x30689
jmp 0x30731
movq %r12, %rsi
callq 0xef14d
movl %eax, %r15d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef14d
movzbl %r15b, %r15d
movzbl %al, %r12d
movq 0x28(%rsp), %rbx
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf11be
movq %rbp, %rdi
movl %r12d, %esi
callq 0xf11be
decq %rbx
jne 0x306d8
jmp 0x30731
movq %r12, %rsi
movq %rdx, %rbx
callq 0xef14d
movzbl %al, %r15d
movq 0x18(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef14d
movl %eax, %r12d
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf11be
movzbl %r12b, %esi
movq %rbp, %rdi
callq 0xf11be
decq %rbx
jne 0x30704
movq 0x40(%rsp), %rcx
addq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %r14, %rbx
movq 0x18(%r14), %rdi
cmpq 0x18(%rdi), %rax
movq 0x38(%rsp), %r12
jl 0x305fc
movq 0x18(%rsp), %r14
jmp 0x30773
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r13, %rdx
movq %r13, %rsi
callq 0xf1988
movq %rax, %rbp
movq %rbp, 0x30(%r12)
movq $0x0, 0x38(%r12)
movq 0x18(%rbp), %rax
movq %rax, 0x40(%r12)
movq %rbx, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %r13
movq %r13, %rdx
callq 0xfa176
movq 0x20(%rsp), %r14
jmp 0x307ab
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
movq %rbp, %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf43d6
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf448e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf84fe
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31fe3
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x30582
leaq 0xe699a(%rip), %rsi     # 0x11719e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x30582