// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_char_simple_run
void fmtutil_char_simple_run(deark *c, struct fmtutil_char_simplectx *csctx,
	struct de_char_context *charctx)
{
	i64 i, j;
	i64 inf_endpos;
	i64 fg_stride;
	i64 attr_offset;
	u8 ccode, acode;
	u8 fgcol, bgcol;
	struct de_char_screen *screen;
	de_encoding encoding;
	struct de_encconv_state es;

	inf_endpos = csctx->inf_pos + csctx->inf_len;
	if(csctx->use_default_pal) {
		de_copy_std_palette(DE_PALID_PC16, 0, 0, charctx->pal, 16, 0);
	}
	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));
	screen = charctx->screens[0];
	screen->width = csctx->width_in_chars;
	screen->height = csctx->height_in_chars;
	screen->cell_rows = de_mallocarray(c, csctx->height_in_chars, sizeof(struct de_char_cell*));

	if(csctx->input_encoding==DE_ENCODING_UNKNOWN) {
		encoding = DE_ENCODING_CP437;
	}
	else {
		encoding = csctx->input_encoding;
	}
	de_encconv_init(&es, DE_EXTENC_MAKE(encoding, DE_ENCSUBTYPE_PRINTABLE));

	if(csctx->fg_stride) {
		fg_stride = csctx->fg_stride;
		attr_offset = csctx->attr_offset;
	}
	else {
		fg_stride = 2;
		attr_offset = 1;
	}

	for(j=0; j<csctx->height_in_chars; j++) {
		screen->cell_rows[j] = de_mallocarray(c, csctx->width_in_chars, sizeof(struct de_char_cell));

		for(i=0; i<csctx->width_in_chars; i++) {
			i64 pos;

			pos = csctx->inf_pos + j*csctx->width_in_chars*fg_stride + i*fg_stride;
			if(pos < inf_endpos)
				ccode = dbuf_getbyte(csctx->inf, pos);
			else
				ccode = 0;
			if(pos < inf_endpos)
				acode = dbuf_getbyte(csctx->inf, pos+attr_offset);
			else
				acode = 0;

			if((acode&0x80) && !csctx->nonblink) {
				screen->cell_rows[j][i].blink = 1;
				acode -= 0x80;
			}

			fgcol = (acode & 0x0f);
			bgcol = (acode & 0xf0) >> 4;

			screen->cell_rows[j][i].fgcol = (u32)fgcol;
			screen->cell_rows[j][i].bgcol = (u32)bgcol;
			screen->cell_rows[j][i].codepoint = (i32)ccode;
			screen->cell_rows[j][i].codepoint_unicode = de_char_to_unicode_ex((i32)ccode, &es);
		}
	}

	de_char_output_to_file(c, charctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movq 0x10(%rsi), %rax
addq 0x8(%rsi), %rax
movq %rax, 0x28(%rsp)
cmpb $0x0, 0x1c(%rsi)
je 0xfa1bd
leaq 0x20(%rbx), %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xecbbc
movq $0x1, 0x10(%rbx)
movl $0x1, %esi
movl $0x8, %edx
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xea259
movq %rax, 0x18(%rbx)
movl $0x18, %esi
movq %r14, %rdi
callq 0xe969b
movq 0x18(%rbx), %rcx
movq %rax, (%rcx)
movq 0x18(%rbx), %rax
movq (%rax), %r12
movq 0x20(%r15), %rax
movq %rax, (%r12)
movq 0x28(%r15), %rsi
movq %rsi, 0x8(%r12)
movl $0x8, %edx
movq %r14, %rdi
callq 0xea259
movq %r12, 0x10(%rsp)
movq %rax, 0x10(%r12)
movl 0x18(%r15), %eax
movzbl %al, %ecx
orl $0x400, %ecx            # imm = 0x400
testl %eax, %eax
movl $0x40d, %esi            # imm = 0x40D
cmovnel %ecx, %esi
leaq 0x38(%rsp), %rdi
callq 0xec207
movq 0x30(%r15), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
movq %rbx, 0x30(%rsp)
je 0xfa262
movq 0x38(%r15), %rax
movq %rax, 0x18(%rsp)
jmp 0xfa276
movl $0x1, %eax
movq %rax, 0x18(%rsp)
movl $0x2, %eax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x28(%r15)
jle 0xfa377
xorl %r12d, %r12d
movq 0x20(%r15), %rsi
movl $0x14, %edx
movq 0x8(%rsp), %rdi
callq 0xea259
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x20(%r15), %r13
testq %r13, %r13
jle 0xfa36a
xorl %ebx, %ebx
xorl %r14d, %r14d
imulq %r12, %r13
addq %r14, %r13
imulq 0x20(%rsp), %r13
addq 0x8(%r15), %r13
xorl %ebp, %ebp
cmpq 0x28(%rsp), %r13
jge 0xfa2de
movq (%r15), %rdi
movq %r13, %rsi
callq 0xef0d5
movzbl %al, %ebp
cmpq 0x28(%rsp), %r13
jge 0xfa2f7
movq (%r15), %rdi
addq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0xef0d5
jmp 0xfa2f9
xorl %eax, %eax
testb %al, %al
jns 0xfa318
cmpb $0x0, 0x1d(%r15)
jne 0xfa318
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movb $0x1, 0x12(%rcx,%rbx)
andb $0x7f, %al
movl %eax, %ecx
andb $0xf, %cl
shrb $0x4, %al
movzbl %cl, %ecx
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rdx
movq (%rdx,%r12,8), %rdx
movl %ecx, 0x8(%rdx,%rbx)
movzbl %al, %eax
movl %eax, 0xc(%rdx,%rbx)
movl %ebp, (%rdx,%rbx)
movl %ebp, %edi
leaq 0x38(%rsp), %rsi
callq 0xec6fd
movq 0x10(%r13), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, 0x4(%rcx,%rbx)
incq %r14
movq 0x20(%r15), %r13
addq $0x14, %rbx
cmpq %r13, %r14
jl 0xfa2b6
incq %r12
cmpq 0x28(%r15), %r12
jl 0xfa284
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0xf455b
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq