// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, %rbx
pushq $0x50
popq %rsi
callq 0x3843b
movq %rax, %r14
pushq $0x19
popq %rsi
movq %rbx, %rdi
callq 0x38484
movq %rax, 0x8(%rsp)
movq %r14, (%rsp)
imulq %r14, %rax
movl $0x800, %r14d           # imm = 0x800
cmpq $0x401, %rax            # imm = 0x401
jl 0x37924
movl $0x1000, %r14d          # imm = 0x1000
cmpq $0x801, %rax            # imm = 0x801
jb 0x37924
xorl %r14d, %r14d
cmpq $0x1001, %rax           # imm = 0x1001
setae %r14b
shll $0xd, %r14d
addq $0x2000, %r14           # imm = 0x2000
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
addq %r14, %rax
decq %rax
cqto
idivq %r14
testq %rax, %rax
jle 0x37b99
movq %rax, %r15
leaq 0xf1af3(%rip), %rsi     # 0x12943e
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movl $0x98, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r12
movb $0x1, 0x2(%rax)
cmpq $0x51, (%rsp)
setl %al
cmpq $0x1a, 0x8(%rsp)
setl %cl
testb %cl, %al
jne 0x37987
movb $0x1, 0x3(%r12)
movq %r15, 0x10(%r12)
pushq $0x8
popq %r13
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0xdf66f
movq %rax, 0x18(%r12)
movq %r12, %rcx
addq $0x20, %rcx
xorl %ebp, %ebp
pushq $0x14
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
movq (%rsp), %rax
leaq (%rax,%rax), %rcx
movq %rcx, 0x28(%rsp)
leaq -0x1(,%rax,2), %rcx
movq %rcx, 0x40(%rsp)
testq %rax, %rax
cmovleq %rbp, %rax
movq %rax, 0x60(%rsp)
movq %r13, 0x18(%rsp)
xorl %eax, %eax
movq %rbx, 0x30(%rsp)
movq %r14, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movq %r12, 0x48(%rsp)
cmpq %r15, %rax
je 0x37b8c
movq %rbx, %rdi
movq %rax, 0x20(%rsp)
pushq $0x18
popq %rsi
callq 0xdecaf
movq 0x20(%rsp), %rsi
movq 0x18(%r12), %rcx
movq %rax, (%rcx,%rsi,8)
movq %rsi, %rcx
imulq %r14, %rcx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
subq %rcx, %rax
cmpq $0x2, %rax
jl 0x37b8c
addq 0x40(%rsp), %rax
cqto
idivq 0x28(%rsp)
movq %rax, %r14
movq 0x8(%rsp), %rax
cmpq %rax, %r14
cmovgeq %rax, %r14
movq 0x18(%r12), %rax
movq (%rax,%rsi,8), %r15
movq (%rsp), %rax
movq %rax, (%r15)
movq %r14, 0x8(%r15)
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x8
popq %rdx
callq 0xdf66f
movq %r15, 0x38(%rsp)
movq %rax, 0x10(%r15)
leaq 0x78(%rsp), %rdi
pushq $0xd
popq %rsi
callq 0xe11fc
testq %r14, %r14
movl $0x0, %eax
cmovleq %rax, %r14
movq %r14, 0x68(%rsp)
movq 0x18(%rsp), %rbp
xorl %r12d, %r12d
cmpq 0x68(%rsp), %r12
je 0x37b6b
movq %rbx, %rdi
movq (%rsp), %rsi
pushq $0x14
popq %rdx
callq 0xdf66f
movq 0x38(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x60(%rsp), %r14
movq %rbp, 0x70(%rsp)
xorl %ebx, %ebx
subq $0x1, %r14
jb 0x37b54
leaq -0x1(%rbp), %rsi
movq 0x30(%rsp), %r15
movq 0x18(%r15), %rdi
callq 0xe3eb4
movl %eax, %r13d
movq 0x18(%r15), %rdi
movq %rbp, %rsi
callq 0xe3eb4
movzbl %r13b, %edi
movl %eax, %ecx
andb $0xf, %cl
shrb $0x4, %al
movzbl %cl, %ecx
movq 0x38(%rsp), %r15
movq 0x10(%r15), %rdx
movq (%rdx,%r12,8), %rdx
movl %ecx, 0x8(%rdx,%rbx)
movzbl %al, %eax
movl %eax, 0xc(%rdx,%rbx)
movl %edi, (%rdx,%rbx)
leaq 0x78(%rsp), %rsi
callq 0xe1679
movq 0x10(%r15), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, 0x4(%rcx,%rbx)
addq $0x14, %rbx
addq $0x2, %rbp
jmp 0x37ae3
incq %r12
movq 0x70(%rsp), %rbp
addq 0x28(%rsp), %rbp
movq 0x30(%rsp), %rbx
jmp 0x37ab0
movq 0x20(%rsp), %rax
incq %rax
movq 0x58(%rsp), %r14
addq %r14, 0x18(%rsp)
movq 0x50(%rsp), %r15
movq 0x48(%rsp), %r12
jmp 0x37a00
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8a63
jmp 0x37b9c
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe89d2
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq