// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movl $0x50, %esi
callq 0x39c55
movq %rax, %r14
movl $0x19, %esi
movq %rbx, %rdi
callq 0x39c9e
movq %rax, 0x10(%rsp)
movq %r14, (%rsp)
imulq %r14, %rax
movl $0x800, %r15d           # imm = 0x800
cmpq $0x401, %rax            # imm = 0x401
jl 0x3909e
movl $0x1000, %r15d          # imm = 0x1000
cmpq $0x801, %rax            # imm = 0x801
jl 0x3909e
xorl %r15d, %r15d
cmpq $0x1001, %rax           # imm = 0x1001
setge %r15b
shll $0xd, %r15d
addq $0x2000, %r15           # imm = 0x2000
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
addq %r15, %rax
decq %rax
cqto
idivq %r15
testq %rax, %rax
jle 0x3932b
movq %rax, %r14
leaq 0xfd236(%rip), %rsi     # 0x1362fb
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x98, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r12
movb $0x1, 0x2(%rax)
cmpq $0x51, (%rsp)
setl %al
cmpq $0x1a, 0x10(%rsp)
setl %cl
testb %cl, %al
jne 0x39101
movb $0x1, 0x3(%r12)
movq %r14, 0x10(%r12)
movl $0x8, %eax
movq %rax, 0x20(%rsp)
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xea259
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 0xecbbc
movq (%rsp), %rax
leaq (%rax,%rax), %rcx
movq %rcx, 0x28(%rsp)
leaq -0x1(,%rax,2), %rax
movq %rax, 0x40(%rsp)
cmpq $0x2, %r14
movl $0x1, %eax
cmovgeq %r14, %rax
movq %rax, 0x38(%rsp)
leaq 0x68(%rsp), %r14
movq %r15, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movq %rbx, 0x30(%rsp)
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe969b
movq 0x18(%r12), %rcx
movq %rax, (%rcx,%r13,8)
movq %r13, %rcx
imulq %r15, %rcx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
subq %rcx, %rax
cmpq $0x2, %rax
jl 0x3931e
addq 0x40(%rsp), %rax
cqto
idivq 0x28(%rsp)
movq %rax, %r15
movq 0x10(%rsp), %rax
cmpq %rax, %r15
cmovgeq %rax, %r15
movq 0x18(%r12), %rax
movq %r13, 0x58(%rsp)
movq (%rax,%r13,8), %r13
movq (%rsp), %rax
movq %rax, (%r13)
movq %r15, 0x8(%r13)
movl $0x8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea259
movq %rax, 0x10(%r13)
movq %r14, %rdi
movl $0xd, %esi
callq 0xec207
movq %r15, 0x60(%rsp)
testq %r15, %r15
jle 0x392f7
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
movl $0x14, %edx
movq %rbx, %rdi
movq (%rsp), %r14
movq %r14, %rsi
callq 0xea259
movq 0x10(%r13), %rcx
movq %rax, (%rcx,%r15,8)
testq %r14, %r14
movq %rbx, %rcx
jle 0x392d7
movq 0x8(%rsp), %r12
xorl %r14d, %r14d
movq (%rsp), %rbx
leaq -0x1(%r12), %rsi
movq 0x18(%rcx), %rdi
callq 0xef0d5
movq %r13, %rbp
movl %eax, %r13d
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r12, %rsi
callq 0xef0d5
movzbl %r13b, %edi
movq %rbp, %r13
leaq 0x68(%rsp), %rsi
movl %eax, %ecx
andb $0xf, %cl
shrb $0x4, %al
movzbl %cl, %ecx
movq 0x10(%rbp), %rdx
movq (%rdx,%r15,8), %rdx
movl %ecx, 0x8(%rdx,%r14)
movzbl %al, %eax
movl %eax, 0xc(%rdx,%r14)
movl %edi, (%rdx,%r14)
callq 0xec6fd
movq 0x10(%rbp), %rcx
movq (%rcx,%r15,8), %rcx
movl %eax, 0x4(%rcx,%r14)
movq 0x30(%rsp), %rcx
addq $0x14, %r14
addq $0x2, %r12
decq %rbx
jne 0x3925e
incq %r15
movq 0x8(%rsp), %rax
addq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq 0x60(%rsp), %r15
movq %rcx, %rbx
jne 0x3922a
movq 0x58(%rsp), %r13
incq %r13
movq 0x50(%rsp), %r15
addq %r15, 0x20(%rsp)
cmpq 0x38(%rsp), %r13
movq 0x48(%rsp), %r12
leaq 0x68(%rsp), %r14
jne 0x39186
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf455b
jmp 0x3932e
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf44c8
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq