// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/bintext.c
// Function: de_run_bintext
static void de_run_bintext(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 effective_file_size = 0;
	int valid_sauce = 0;
	const char *s;
	i64 width_req = 0;

	d = de_malloc(c, sizeof(lctx));
	d->csctx.input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

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

	s=de_get_ext_option(c, "char:width");
	if(s) {
		width_req = de_atoi(s);
	}

	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;

		effective_file_size = si->original_file_size;

		if(si->data_type==5) {
			valid_sauce = 1;

			if(si->file_type==1 && si->tinfo1>0) {
				// Some files created by ACiDDraw do this.
				d->csctx.width_in_chars = 2*(i64)si->tinfo1;
			}
			else {
				// For BinText, the FileType field is inexplicably used for the width (usually).
				d->csctx.width_in_chars = 2*(i64)si->file_type;
			}

			if(si->tflags & 0x01) {
				d->csctx.nonblink = 1;
			}
			if((si->tflags & 0x18)>>3 == 0x02) {
				// Square pixels requested
				charctx->no_density = 1;
			}
			if((si->tflags & 0x06)>>1 == 0x02) {
				charctx->prefer_9col_mode = 1;
			}
		}
	}

	if(!valid_sauce) {
		d->csctx.width_in_chars = 160;
		effective_file_size = c->infile->len;
	}

	if(width_req>0) d->csctx.width_in_chars = width_req;

	if(d->csctx.width_in_chars<1) d->csctx.width_in_chars=160;
	if(effective_file_size%(d->csctx.width_in_chars*2)) {
		de_warn(c, "File does not contain a whole number of rows. The width may "
			"be wrong. Try \"-opt char:width=...\".");
	}
	d->csctx.height_in_chars = effective_file_size / (d->csctx.width_in_chars*2);

	de_dbg(c, "width: %d chars", (int)d->csctx.width_in_chars);
	de_dbg(c, "calculated height: %d chars", (int)d->csctx.height_in_chars);
	d->has_palette = 1;
	d->has_font = 1;
	d->compression = 0;
	d->has_512chars = 0;

	d->csctx.use_default_pal = 1;
	d->csctx.inf = c->infile;
	d->csctx.inf_pos = 0;
	d->csctx.inf_len = effective_file_size;
	fmtutil_char_simple_run(c, &d->csctx, charctx);

	de_free_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 $0x18, %rsp
movq %rdi, %rbx
movl $0x70, %esi
callq 0xe8b2b
movq %rax, %r15
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, 0x48(%r15)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf3eb8
movq %rax, %rbp
movb $0x0, (%rax)
leaq 0xe1438(%rip), %rsi     # 0x111bb7
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x30797
movq %rax, %rdi
callq 0xe9c2a
movslq %eax, %r12
movq 0x18(%rbx), %rsi
leaq 0x15(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
movl $0x1, %ecx
callq 0xf7f3d
cmpb $0x0, (%r14)
movq %rbp, 0x8(%rsp)
je 0x30878
movq %rbx, %rdi
callq 0xf8028
movq %rax, %r13
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %r13, %rdx
callq 0xf7fdd
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movups 0x8(%r13), %xmm0
movups %xmm0, 0x68(%rbp)
movq 0x18(%r13), %rax
movq %rax, 0x78(%rbp)
movups 0x20(%r13), %xmm0
movups %xmm0, 0x80(%rbp)
movq 0x60(%r13), %rax
movq %rax, 0x90(%rbp)
cmpb $0x5, 0x38(%r13)
jne 0x3087b
movzbl 0x39(%r13), %ecx
cmpq $0x1, %rcx
jne 0x30837
movzwl 0x58(%r13), %eax
testq %rax, %rax
jne 0x3083a
movq %rcx, %rax
movq 0x30(%r13), %rbp
addl %eax, %eax
movq %rax, 0x50(%r15)
movb 0x3a(%r13), %cl
testb $0x1, %cl
je 0x30852
movb $0x1, 0x4d(%r15)
movl %ecx, %edx
andb $0x18, %dl
cmpb $0x10, %dl
jne 0x30865
movq 0x8(%rsp), %rdx
movb $0x1, 0x3(%rdx)
andb $0x6, %cl
cmpb $0x4, %cl
jne 0x30890
movq 0x8(%rsp), %rcx
movb $0x1, 0x1(%rcx)
jmp 0x30890
xorl %r13d, %r13d
movq $0xa0, 0x50(%r15)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rbp
movl $0xa0, %eax
leaq 0x50(%r15), %r14
testq %r12, %r12
jle 0x3089f
movq %r12, 0x50(%r15)
jmp 0x308b6
testq %rax, %rax
je 0x308a9
movq %rax, %r12
jmp 0x308b6
movq $0xa0, (%r14)
movl $0xa0, %r12d
leaq (%r12,%r12), %rcx
movq %rbp, %rax
cqto
idivq %rcx
testq %rdx, %rdx
je 0x308df
leaq 0xe6a88(%rip), %rsi     # 0x117356
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq (%r14), %r12
leaq (%r12,%r12), %rcx
movq %r15, %r14
addq $0x30, %r14
movq %rbp, %rax
cqto
idivq %rcx
movq %rax, 0x58(%r15)
leaq 0xe6b0a(%rip), %rsi     # 0x117403
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movl 0x58(%r15), %edx
leaq 0xe6aa6(%rip), %rsi     # 0x1173b7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl $0x101, 0x2(%r15)       # imm = 0x101
movb $0x1, 0x4c(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x30(%r15)
movq $0x0, 0x38(%r15)
movq %rbp, 0x40(%r15)
movq %rbx, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0xf9c77
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf3fb8
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf8032
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31e6b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq