// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x50, %edx
callq 0x4cc30
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x19, %edx
callq 0x4cca0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
imulq -0x38(%rbp), %rax
shlq %rax
cmpq $0x800, %rax            # imm = 0x800
jg 0x4be62
movq $0x800, -0x48(%rbp)     # imm = 0x800
jmp 0x4beaa
movq -0x30(%rbp), %rax
imulq -0x38(%rbp), %rax
shlq %rax
cmpq $0x1000, %rax           # imm = 0x1000
jg 0x4be80
movq $0x1000, -0x48(%rbp)    # imm = 0x1000
jmp 0x4bea8
movq -0x30(%rbp), %rax
imulq -0x38(%rbp), %rax
shlq %rax
cmpq $0x2000, %rax           # imm = 0x2000
jg 0x4be9e
movq $0x2000, -0x48(%rbp)    # imm = 0x2000
jmp 0x4bea6
movq $0x4000, -0x48(%rbp)    # imm = 0x4000
jmp 0x4bea8
jmp 0x4beaa
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
addq %rcx, %rax
cqto
idivq -0x48(%rbp)
movq %rax, -0x20(%rbp)
cmpq $0x1, -0x20(%rbp)
jge 0x4bed3
jmp 0x4c046
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x17615c(%rip), %rsi    # 0x1c2040
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x98, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movb $0x1, 0x2(%rax)
cmpq $0x50, -0x30(%rbp)
jg 0x4bf13
cmpq $0x19, -0x38(%rbp)
jle 0x4bf1b
movq -0x18(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rcx
addq $0x20, %rcx
movl $0x14, %edi
xorl %r9d, %r9d
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %r8d
movl %r9d, %esi
callq 0x161320
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x4c039
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
imulq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x2, -0x50(%rbp)
jge 0x4bfc3
jmp 0x4c039
movq -0x50(%rbp), %rax
movq -0x30(%rbp), %rcx
shlq %rcx
subq $0x1, %rcx
addq %rcx, %rax
movq -0x30(%rbp), %rcx
shlq %rcx
cqto
idivq %rcx
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x4bff7
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rcx
movq -0x58(%rbp), %r8
movq -0x30(%rbp), %r9
movq -0x40(%rbp), %rax
movq %rax, (%rsp)
callq 0x4cd10
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4bf6e
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x16da40
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x16d960
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)