// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/bsave.c
// Function: do_char
static void do_char(deark *c, lctx *d)
{
	struct de_char_context *charctx = NULL;
	i64 numpages;
	i64 pgnum;
	i64 width, height;
	i64 height_for_this_page;
	i64 bytes_per_page;
	i64 bytes_for_this_page;
	i64 pg_offset_in_data;

	width = get_width(c, d, 80);
	height = get_height(c, d, 25);

	// If there are multiple pages, the usually have some unused space between
	// them. Try to guess how much.

	if(width*height*2 <= 2048) {
		bytes_per_page = 2048; // E.g. 40x25(*2) = 2000
	}
	else if(width*height*2 <= 4096) {
		bytes_per_page = 4096; // E.g. 80x25(*2) = 4000
	}
	else if(width*height*2 <= 8192) {
		bytes_per_page = 8192; // Just guessing. Maybe 80x50.
	}
	else {
		bytes_per_page = 16384; // E.g. 80x100 (160x100) pseudo-graphics mode.
	}

	numpages = (d->data_size + (bytes_per_page-1))/bytes_per_page;
	if(numpages<1) {
		goto done;
	}
	de_dbg(c, "pages: %d", (int)numpages);

	charctx = de_malloc(c, sizeof(struct de_char_context));
	charctx->screen_image_flag = 1;
	if(width>80 || height>25) {
		charctx->no_density = 1;
	}
	charctx->nscreens = numpages;
	charctx->screens = de_mallocarray(c, numpages, sizeof(struct de_char_screen*));

	de_copy_std_palette(DE_PALID_PC16, 0, 0, charctx->pal, 16, 0);

	for(pgnum=0; pgnum<numpages; pgnum++) {
		charctx->screens[pgnum] = de_malloc(c, sizeof(struct de_char_screen));

		pg_offset_in_data = bytes_per_page*pgnum;
		bytes_for_this_page = d->data_size - pg_offset_in_data;
		if(bytes_for_this_page<2) break;

		// Reduce the height if there's not enough data for it.
		height_for_this_page = (bytes_for_this_page+(width*2-1)) / (width*2);
		if(height_for_this_page>height) {
			height_for_this_page = height;
		}

		do_char_1screen(c, d, charctx->screens[pgnum], pgnum, pg_offset_in_data, width, height_for_this_page);
	}

	de_char_output_to_file(c, charctx);

done:
	de_free_charctx(c, charctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
movl $0x50, %esi
callq 0x39b51
movq %rax, %r14
movl $0x19, %esi
movq %rbx, %rdi
callq 0x39b9a
movq %rax, 0x18(%rsp)
movq %r14, 0x8(%rsp)
imulq %r14, %rax
movl $0x800, %r14d           # imm = 0x800
cmpq $0x401, %rax            # imm = 0x401
jl 0x38f97
movl $0x1000, %r14d          # imm = 0x1000
cmpq $0x801, %rax            # imm = 0x801
jb 0x38f97
xorl %r14d, %r14d
cmpq $0x1001, %rax           # imm = 0x1001
setae %r14b
shll $0xd, %r14d
addq $0x2000, %r14           # imm = 0x2000
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
addq %r14, %rax
decq %rax
cqto
idivq %r14
testq %rax, %rax
jle 0x39233
movq %rax, %r15
leaq 0xfd780(%rip), %rsi     # 0x13673e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x98, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
movb $0x1, 0x2(%rax)
cmpq $0x51, 0x8(%rsp)
setl %al
cmpq $0x1a, 0x18(%rsp)
setl %cl
testb %cl, %al
jne 0x38ffb
movb $0x1, 0x3(%r12)
movq %r15, 0x10(%r12)
movl $0x8, %eax
movq %rax, 0x28(%rsp)
movl $0x8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe96f7
movq %rax, 0x18(%r12)
movq %r12, %rcx
addq $0x20, %rcx
xorl %r13d, %r13d
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
movq 0x8(%rsp), %rax
leaq (%rax,%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq -0x1(,%rax,2), %rax
movq %rax, 0x40(%rsp)
leaq 0x78(%rsp), %rbp
movq %r14, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movq %rbx, 0x38(%rsp)
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq 0x18(%r12), %rcx
movq %rax, (%rcx,%r13,8)
movq %r13, %rcx
imulq %r14, %rcx
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
subq %rcx, %rax
cmpq $0x2, %rax
jl 0x39226
addq 0x40(%rsp), %rax
cqto
idivq 0x30(%rsp)
movq %rax, %r14
movq 0x18(%rsp), %rax
cmpq %rax, %r14
cmovgeq %rax, %r14
movq 0x18(%r12), %rax
movq %r13, 0x60(%rsp)
movq (%rax,%r13,8), %r15
movq 0x8(%rsp), %rax
movq %rax, (%r15)
movq %r14, 0x8(%r15)
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe96f7
movq %rax, 0x10(%r15)
movq %rbp, %rdi
movl $0xd, %esi
callq 0xebbf7
movq %r14, 0x68(%rsp)
testq %r14, %r14
jle 0x39201
movq %r15, %r14
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
movl $0x14, %edx
movq %rbx, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xe96f7
movq 0x10(%r14), %rcx
movq %rax, (%rcx,%r12,8)
testq %r15, %r15
movq %rbx, %rcx
movq %r12, %r15
jle 0x391de
movq 0x10(%rsp), %r12
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
movq %r15, 0x70(%rsp)
leaq -0x1(%r12), %rsi
movq 0x18(%rcx), %rdi
callq 0xeead3
movq %r14, %r15
movq %rbp, %r14
movl %eax, %ebp
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r12, %rsi
callq 0xeead3
movzbl %bpl, %edi
movq %r14, %rbp
movq %r15, %r14
movq 0x70(%rsp), %r15
movl %eax, %ecx
andb $0xf, %cl
shrb $0x4, %al
movzbl %cl, %ecx
movq 0x10(%r14), %rdx
movq (%rdx,%r15,8), %rdx
movl %ecx, 0x8(%rdx,%r13)
movzbl %al, %eax
movl %eax, 0xc(%rdx,%r13)
movl %edi, (%rdx,%r13)
movq %rbp, %rsi
callq 0xec0ad
movq 0x10(%r14), %rcx
movq (%rcx,%r15,8), %rcx
movl %eax, 0x4(%rcx,%r13)
movq 0x38(%rsp), %rcx
addq $0x14, %r13
addq $0x2, %r12
decq %rbx
jne 0x39159
incq %r15
movq 0x10(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq 0x68(%rsp), %r15
movq %rcx, %rbx
movq %r15, %r12
jne 0x3911b
movq 0x60(%rsp), %r13
incq %r13
movq 0x58(%rsp), %r14
addq %r14, 0x28(%rsp)
movq 0x50(%rsp), %r15
cmpq %r15, %r13
movq 0x48(%rsp), %r12
jne 0x39074
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf404b
jmp 0x39236
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf3fb8
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq