// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x70
popq %rsi
callq 0xdecaf
movq %rax, %r15
xorl %r12d, %r12d
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x48(%r15)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe88f4
movq %rax, %rbp
movb $0x0, (%rax)
leaq 0xd53f4(%rip), %rsi     # 0x104b97
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x2f7bb
movq %rax, %rdi
callq 0xdfb36
movslq %eax, %r12
movq 0x18(%rbx), %rsi
leaq 0x15(%rsp), %r14
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r14, %rdx
callq 0xec67d
cmpb $0x0, (%r14)
movq %rbp, 0x8(%rsp)
je 0x2f896
movq %rbx, %rdi
callq 0xec762
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %r13, %rdx
callq 0xec71a
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
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 0x2f899
movzbl 0x39(%r13), %ecx
cmpq $0x1, %rcx
jne 0x2f855
movzwl 0x58(%r13), %eax
testq %rax, %rax
jne 0x2f858
movq %rcx, %rax
movq 0x30(%r13), %rbp
addl %eax, %eax
movq %rax, 0x50(%r15)
movb 0x3a(%r13), %cl
testb $0x1, %cl
je 0x2f870
movb $0x1, 0x4d(%r15)
movl %ecx, %edx
andb $0x18, %dl
cmpb $0x10, %dl
jne 0x2f883
movq 0x8(%rsp), %rdx
movb $0x1, 0x3(%rdx)
andb $0x6, %cl
cmpb $0x4, %cl
jne 0x2f8ae
movq 0x8(%rsp), %rcx
movb $0x1, 0x1(%rcx)
jmp 0x2f8ae
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 0x2f8bd
movq %r12, 0x50(%r15)
jmp 0x2f8d4
testq %rax, %rax
je 0x2f8c7
movq %rax, %r12
jmp 0x2f8d4
movq $0xa0, (%r14)
movl $0xa0, %r12d
leaq (%r12,%r12), %rcx
movq %rbp, %rax
cqto
idivq %rcx
testq %rdx, %rdx
je 0x2f8fd
leaq 0xda9da(%rip), %rsi     # 0x10a2c6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq (%r14), %r12
leaq (%r12,%r12), %rcx
movq %r15, %r14
addq $0x30, %r14
movq %rbp, %rax
cqto
idivq %rcx
movq %rax, 0x58(%r15)
leaq 0xdaa5c(%rip), %rsi     # 0x10a373
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movl 0x58(%r15), %edx
leaq 0xda9f8(%rip), %rsi     # 0x10a327
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl $0x101, 0x2(%r15)       # imm = 0x101
movb $0x1, 0x4c(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x30(%r15)
andq $0x0, 0x38(%r15)
movq %rbp, 0x40(%r15)
movq %rbx, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0xee2e5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe89d2
movq %rbx, %rdi
movq %r13, %rsi
callq 0xec76a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x30db6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq