// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/insetpix.c
// Function: do_render_tile
static void do_render_tile(deark *c, lctx *d, de_bitmap *img,
	struct insetpix_item_data *itd)
{
	i64 x_pos_in_tiles, y_pos_in_tiles;
	i64 x_origin_in_pixels, y_origin_in_pixels;
	dbuf *unc_pixels = NULL;
	i64 nrows_expected;
	i64 planespan;

	x_pos_in_tiles = (i64)itd->tile_num % d->stp_cols;
	y_pos_in_tiles = (i64)itd->tile_num / d->stp_cols;

	x_origin_in_pixels = x_pos_in_tiles * d->page_cols;
	y_origin_in_pixels = y_pos_in_tiles * d->page_rows;

	// "If the actual row bound of the tile exceeds the image, the extra
	// rows are not present."
	nrows_expected = d->height - y_origin_in_pixels;
	if(nrows_expected > d->page_rows) nrows_expected = d->page_rows;
	planespan = nrows_expected * d->rowspan;

	de_dbg(c, "tile (%d,%d), pixel position (%d,%d), size %d"DE_CHAR_TIMES"%d",
		(int)x_pos_in_tiles, (int)y_pos_in_tiles,
		(int)x_origin_in_pixels, (int)y_origin_in_pixels,
		(int)d->page_cols, (int)nrows_expected);

	unc_pixels = dbuf_create_membuf(c, 4096, 0);

	do_decompress_tile(c, d, itd, unc_pixels, nrows_expected);

	if(d->tile_img) {
		// Clear the previous image
		de_bitmap_rect(d->tile_img, 0, 0, d->page_cols, d->page_rows, 0, 0);
	}
	else {
		d->tile_img = de_bitmap_create(c, d->page_cols, d->page_rows, d->is_grayscale?1:3);
	}

	// This will try to convert 'd->page_rows' rows, when there might only be
	// 'nrows_expected' rows, but that won't cause a problem.
	de_convert_image_paletted_planar(unc_pixels, 0, d->gfore, d->rowspan, planespan,
		d->pal, d->tile_img, 0x2);

	de_bitmap_copy_rect(d->tile_img, img, 0, 0, d->page_cols, nrows_expected, x_origin_in_pixels,
		y_origin_in_pixels, 0);

	dbuf_close(unc_pixels);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cqto
idivq 0xc0(%rcx)
movq %rdx, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cqto
idivq 0xc0(%rcx)
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq 0xb0(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq 0xa8(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0xa8(%rcx), %rax
jle 0xb9068
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq 0xc8(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %eax, %r8d
movq -0x40(%rbp), %rax
movl %eax, %r9d
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl %eax, %r10d
movq -0x50(%rbp), %rax
leaq 0xfc568(%rip), %rsi     # 0x1b561b
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
xorl %edx, %edx
callq 0x168020
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
callq 0xb8d10
movq -0x10(%rbp), %rax
cmpq $0x0, 0xe0(%rax)
je 0xb9136
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %r8
xorl %eax, %eax
movl %eax, %edx
xorl %r9d, %r9d
movq %rdx, %rsi
movl $0x0, (%rsp)
callq 0x16cdc0
jmp 0xb917f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdx
movq -0x10(%rbp), %rax
movl 0xd8(%rax), %r8d
movl $0x3, %ecx
movl $0x1, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe0(%rax)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rcx
movq -0x58(%rbp), %r8
movq -0x10(%rbp), %r9
addq $0xf0, %r9
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rax
xorl %esi, %esi
movq %rax, (%rsp)
movl $0x2, 0x8(%rsp)
callq 0x16c690
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %r8
movq -0x50(%rbp), %r9
movq -0x38(%rbp), %r10
movq -0x40(%rbp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq %rcx, %rdx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16ce50
movq -0x48(%rbp), %rdi
callq 0x1676a0
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)