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

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

	if(de_get_ext_option(c, "ansiart:no24bitcolor")) {
		d->opt_disable_24bitcolor = 1;
	}
	if(de_get_ext_option(c, "ansiart:noblink")) {
		d->opt_disable_blink = 1;
	}
	if(de_get_ext_option(c, "ansiart:vt100")) {
		d->vt100_mode = 1;
	}
	s=de_get_ext_option(c, "char:width");
	if(s) {
		width_req = de_atoi(s);
	}

	de_encconv_init(&d->es_main, DE_ENCODING_CP437_G);
	de_encconv_init(&d->es_dec_special_gr, DE_ENCODING_DEC_SPECIAL_GRAPHICS);

	d->effective_file_size = c->infile->len;

	charctx = de_malloc(c, sizeof(struct de_char_context));

	// Read SAUCE metadata, if present.
	si = fmtutil_create_SAUCE(c);
	fmtutil_detect_SAUCE(c, c->infile, &sdd, 0x1);

	if(sdd.has_SAUCE) {
		de_dbg_indent(c, 1);
		fmtutil_handle_SAUCE(c, c->infile, si);
		de_dbg_indent(c, -1);

		d->effective_file_size = si->original_file_size;

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

		if(si->is_valid && si->data_type==1 && (si->file_type==1 || si->file_type==2)) {
			valid_sauce = 1;
		}
	}

	if(valid_sauce) {
		if(si->tflags & 0x01) {
			d->sauce_disable_blink = 1;
		}
		if((si->tflags & 0x18)>>3 == 0x02) {
			// Square pixels requested.
			// TODO: This is a little bit wrong. Knowing that the pixels are
			// square is different from preventing a density from being written
			// to the output file. For one thing, we should consider what to do
			// if the user set "-opt char:charwidth=9".
			charctx->no_density = 1;
		}
		if((si->tflags & 0x06)>>1 == 0x02) {
			charctx->prefer_9col_mode = 1;
		}
	}

	// Ignore any Ctrl-Z at the end of data.
	if(de_getbyte(d->effective_file_size-1) == 0x1a) {
		de_dbg(c, "found Ctrl+Z byte at %"I64_FMT, (i64)(d->effective_file_size-1));
		d->effective_file_size--;
	}
	if(d->effective_file_size!=c->infile->len) {
		de_dbg(c, "effective file size set to %"I64_FMT, (i64)d->effective_file_size);
	}

	charctx->nscreens = 1;
	charctx->screens = de_mallocarray(c, charctx->nscreens, sizeof(struct de_char_screen*));
	charctx->screens[0] = de_malloc(c, sizeof(struct de_char_screen));

	d->screen = charctx->screens[0];

	if(width_req>0) {
		// Use the width requested by the user.
		d->screen->width = width_req;
	}
	else if(valid_sauce && si->width_in_chars>=40 && si->width_in_chars<=2048) {
		// Use the width from SAUCE, if it's available and seems sensible.
		d->screen->width = si->width_in_chars;
	}
	else {
		// Otherwise, assume 80 characters per row.
		d->screen->width = 80;
	}
	// We don't know the height yet. This will be updated as we read the file.
	d->screen->height = 1;

	d->screen->cell_rows = de_mallocarray(c, MAX_ROWS, sizeof(struct de_char_cell*));
	d->row_data = de_mallocarray(c, MAX_ROWS, sizeof(struct row_data_struct));

	de_copy_palette_from_rgb24(ansi_palette, charctx->pal, 16);

	do_main(c, d);

	fixup_doublesize_rows(c, d);

	if(d->vt100_mode) {
		charctx->no_density = 1;
	}

	de_char_output_to_file(c, charctx);

	de_free_charctx(c, charctx);
	de_free(c, d->row_data);
	fmtutil_free_SAUCE(c, si);
	de_free(c, d);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movl $0x288, %esi            # imm = 0x288
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x17e120(%rip), %rsi    # 0x19dc44
callq 0x15cb30
cmpq $0x0, %rax
je 0x1fb39
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x8(%rbp), %rdi
leaq 0x17e115(%rip), %rsi    # 0x19dc59
callq 0x15cb30
cmpq $0x0, %rax
je 0x1fb5a
movq -0x18(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x8(%rbp), %rdi
leaq 0x17e104(%rip), %rsi    # 0x19dc69
callq 0x15cb30
cmpq $0x0, %rax
je 0x1fb78
movq -0x18(%rbp), %rax
movb $0x1, 0x72(%rax)
movq -0x8(%rbp), %rdi
leaq 0x17e0f4(%rip), %rsi    # 0x19dc77
callq 0x15cb30
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x1fba2
movq -0x40(%rbp), %rdi
callq 0x15cca0
cltq
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
addq $0x78, %rdi
movl $0xd, %esi
callq 0x160150
movq -0x18(%rbp), %rdi
addq $0x98, %rdi
movl $0x17, %esi
callq 0x160150
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movl $0x98, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x173e20
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x23(%rbp), %rdx
movl $0x1, %ecx
callq 0x173ca0
cmpb $0x0, -0x23(%rbp)
je 0x1fcfb
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x173db0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, 0x80(%rax)
movq 0x28(%rcx), %rcx
movq %rcx, 0x88(%rax)
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1fcf9
movq -0x30(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl $0x1, %eax
jne 0x1fcf9
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %eax
cmpl $0x1, %eax
je 0x1fcf2
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %eax
cmpl $0x2, %eax
jne 0x1fcf9
movl $0x1, -0x34(%rbp)
jmp 0x1fcfb
cmpl $0x0, -0x34(%rbp)
je 0x1fd50
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1fd19
movq -0x18(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x18, %eax
sarl $0x3, %eax
cmpl $0x2, %eax
jne 0x1fd34
movq -0x20(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %eax
andl $0x6, %eax
sarl %eax
cmpl $0x2, %eax
jne 0x1fd4e
movq -0x20(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x1fd50
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
subq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x1a, %eax
jne 0x1fd9f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
subq $0x1, %rdx
leaq 0x17defa(%rip), %rsi    # 0x19dc82
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
je 0x1fdcf
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x17ded3(%rip), %rsi    # 0x19dc9b
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq $0x1, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, -0x48(%rbp)
jle 0x1fe43
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, (%rax)
jmp 0x1fe88
cmpl $0x0, -0x34(%rbp)
je 0x1fe77
movq -0x30(%rbp), %rax
cmpq $0x28, 0x40(%rax)
jl 0x1fe77
movq -0x30(%rbp), %rax
cmpq $0x800, 0x40(%rax)      # imm = 0x800
jg 0x1fe77
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, (%rax)
jmp 0x1fe86
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq $0x50, (%rax)
jmp 0x1fe88
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq $0x1, 0x8(%rax)
movq -0x8(%rbp), %rdi
movl $0x1388, %esi           # imm = 0x1388
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movl $0x1388, %esi           # imm = 0x1388
movl $0x1, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rsi
addq $0x20, %rsi
leaq 0x17dd09(%rip), %rdi    # 0x19dbf0
movl $0x10, %edx
callq 0x1613d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x20140
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x20390
movq -0x18(%rbp), %rax
cmpb $0x0, 0x72(%rax)
je 0x1ff1d
movq -0x20(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16da40
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x16d960
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x173e40
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)