// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r12
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf3eb8
movq %rax, %r13
movb $0x1, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0xf3fdf
movq 0x18(%rbx), %rsi
leaq 0x35(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rdx
movl $0x1, %ecx
callq 0xf7f3d
cmpb $0x0, (%r15)
je 0x30135
movq %rbx, %rdi
callq 0xf8028
movq %rax, %r14
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xf7fdd
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movups 0x8(%r14), %xmm0
movups %xmm0, 0x68(%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 0xeebc8
movq %rax, 0x50(%r12)
movq 0x18(%rbx), %rdi
movl $0x7, %esi
callq 0xeebc8
movq %rax, 0x58(%r12)
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xeead3
movzbl %al, %eax
movq %rax, 0x8(%r12)
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeead3
movl %eax, %ebp
movl 0x50(%r12), %edx
movl 0x58(%r12), %ecx
leaq 0xe6fcd(%rip), %rsi     # 0x11715f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x8(%r12), %edx
leaq 0xe7024(%rip), %rsi     # 0x1171cc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl %bpl, %ebp
leaq 0xffeb9(%rip), %rsi     # 0x130076
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
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 0xe8a15
movzbl 0x2(%r12), %edx
leaq 0xe6f61(%rip), %rsi     # 0x11717d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl 0x3(%r12), %edx
leaq 0xe6f5a(%rip), %rsi     # 0x11718d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl 0x4(%r12), %edx
leaq 0xe3614(%rip), %rsi     # 0x11385e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl 0x4d(%r12), %edx
leaq 0xe6f39(%rip), %rsi     # 0x11719a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl 0x5(%r12), %edx
leaq 0xe6f35(%rip), %rsi     # 0x1171ad
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, 0x3(%r12)
je 0x302be
movq 0x8(%r12), %rdx
leaq -0x21(%rdx), %rax
cmpq $-0x21, %rax
ja 0x302be
leaq 0xe6f17(%rip), %rsi     # 0x1171c4
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x30670
cmpb $0x0, 0x2(%r12)
movq %r13, 0x8(%rsp)
je 0x302e5
movl $0xb, %edx
movq %rbx, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x31aae
movl $0x3b, %r13d
jmp 0x30302
leaq 0xe6ef0(%rip), %rsi     # 0x1171dc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movb $0x1, 0x4c(%r12)
movl $0xb, %r13d
cmpb $0x0, 0x3(%r12)
je 0x30370
movq %rbx, %rdi
callq 0xf5358
movq %rax, 0x28(%r12)
movw $0x101, 0x10(%rax)      # imm = 0x101
movb $0x0, 0x12(%rax)
movb 0x5(%r12), %dl
xorl %esi, %esi
testb %dl, %dl
sete %cl
setne %sil
shll $0x8, %esi
addq $0x100, %rsi            # imm = 0x100
movq %rsi, 0x18(%rax)
movq 0x8(%r12), %rax
xorb $0x9, %cl
shlq %cl, %rax
movq %rax, 0x10(%r12)
testb %dl, %dl
je 0x30394
leaq 0xe6e98(%rip), %rsi     # 0x1171f2
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
callq 0xe9295
jmp 0x3042b
cmpb $0x0, 0x5(%r12)
je 0x3040b
leaq 0xe6e92(%rip), %rsi     # 0x117215
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x3042b
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x31bf0
addq 0x10(%r12), %r13
cmpl $0x0, 0x20(%r12)
je 0x303b8
movq 0x8(%rsp), %rax
movb $0x1, 0x4(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31d05
testl %eax, %eax
je 0x30429
cmpl $0x2, 0x90(%rbx)
movq 0x28(%r12), %rax
movq %r14, 0x20(%rsp)
jl 0x30455
testq %rax, %rax
je 0x30453
movq %rbx, %rdi
callq 0xf62bc
cmpb $0x3, 0xd5(%rbx)
je 0x303f9
movq 0x28(%r12), %rax
movb $0x1, 0x13(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31e07
movq 0x28(%r12), %rax
jmp 0x30455
movq %r14, 0x20(%rsp)
movq 0x8(%r12), %rdx
testq $-0x11, %rdx
jne 0x30435
movq $0x10, 0x8(%r12)
jmp 0x3045e
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
jmp 0x30670
movq 0x8(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x3041e
leaq 0xe6df7(%rip), %rsi     # 0x11723e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0x3041e
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq 0x18(%rsp), %r14
addq $0x30, %r14
leaq 0x10ac49(%rip), %rsi    # 0x13b0b7
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
cmpb $0x0, 0x4(%r12)
je 0x3062b
movq %r14, 0x18(%rsp)
movq 0x50(%r12), %rsi
imulq 0x58(%r12), %rsi
addq %rsi, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %rbp
movq %r13, 0x10(%rsp)
movq 0x18(%rbx), %rdi
cmpq %r13, 0x18(%rdi)
jle 0x30624
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 0x30624
movq %rdx, 0x40(%rsp)
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0xeeb4b
movl %eax, %ecx
shrb $0x6, %cl
andb $0x3f, %al
incb %al
movzbl %al, %edx
movq 0x18(%rbx), %rdi
movzbl %cl, %eax
leaq 0xe6a8c(%rip), %rcx     # 0x116fa8
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 0xef481
addq %r15, 0x10(%rsp)
jmp 0x305ff
movq %r12, %rsi
movq %rdx, %rbx
callq 0xeeb4b
movzbl %al, %r15d
movq 0x18(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %esi
movq %rbp, %rdi
callq 0xf0bd6
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf0bd6
decq %rbx
jne 0x30557
jmp 0x305ff
movq %r12, %rsi
callq 0xeeb4b
movl %eax, %r15d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movzbl %r15b, %r15d
movzbl %al, %r12d
movq 0x28(%rsp), %rbx
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf0bd6
movq %rbp, %rdi
movl %r12d, %esi
callq 0xf0bd6
decq %rbx
jne 0x305a6
jmp 0x305ff
movq %r12, %rsi
movq %rdx, %rbx
callq 0xeeb4b
movzbl %al, %r15d
movq 0x18(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
movl %eax, %r12d
movq %rbp, %rdi
movl %r15d, %esi
callq 0xf0bd6
movzbl %r12b, %esi
movq %rbp, %rdi
callq 0xf0bd6
decq %rbx
jne 0x305d2
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 0x304ca
movq 0x18(%rsp), %r14
jmp 0x30641
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r13, %rdx
movq %r13, %rsi
callq 0xf1388
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 0xf9c77
movq 0x20(%rsp), %r14
movq %rbp, %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf3ec2
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf3f7e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf8032
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31e6b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq