// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: do_one_ripicon
static int do_one_ripicon(deark *c, i64 pos1, i64 *pbytes_consumed, int scan_mode)
{
	i64 width, height;
	de_bitmap *img = NULL;
	i64 chunk_span;
	i64 src_rowspan;
	i64 bitmap_len;
	i64 pos = pos1;
	int saved_indent_level;
	de_color pal[16];

	if(pos1+8 > c->infile->len) return 0;
	width = 1 + de_getu16le_p(&pos);
	height = 1 + de_getu16le_p(&pos);
	if(width>MAX_RIPICON_DIMENSION || height>MAX_RIPICON_DIMENSION) return 0;
	chunk_span = (width+7)/8;
	src_rowspan = 4*chunk_span;
	bitmap_len = src_rowspan * height;
	if(pos+bitmap_len > c->infile->len) return 0;

	*pbytes_consumed = 4 + bitmap_len + 2;
	if(scan_mode) return 1;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "image at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);
	de_dbg_dimensions(c, width, height);

	de_dbg(c, "bitmap at %"I64_FMT", len=%"I64_FMT, pos, bitmap_len);
	if(!de_good_image_dimensions(c, width, height)) goto done;
	img = de_bitmap_create2(c, width, chunk_span*8, height, 3);
	de_copy_std_palette(DE_PALID_PC16, 0, 0, pal, 16, 0);
	de_convert_image_paletted_planar(c->infile, pos, 4, src_rowspan, chunk_span,
		pal, img, 0);
	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);

done:
	if(img) de_bitmap_destroy(img);
	de_dbg_indent_restore(c, saved_indent_level);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %r14d
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
leaq 0x8(%rsi), %rcx
movq 0x18(%rdi), %rdi
xorl %eax, %eax
cmpq 0x18(%rdi), %rcx
jg 0x69098
movq %rdx, %r12
movq %rsi, %r15
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0xe3fc4
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3fc4
movq %rax, %r13
movl $0x800, %eax            # imm = 0x800
cmpq %rax, %rbp
setge %cl
cmpq %rax, %r13
setge %al
orb %cl, %al
je 0x68f50
xorl %eax, %eax
jmp 0x69098
incq %r13
movq %rbp, %rdi
leaq 0x8(%rbp), %rax
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, %rdx
leaq (,%rax,4), %rsi
movq %rsi, %rbp
imulq %r13, %rbp
movq 0x8(%rsp), %rax
addq %rbp, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
movl $0x0, %eax
jg 0x69098
leaq 0x6(%rbp), %rax
movq %rax, (%r12)
testl %r14d, %r14d
jne 0x69095
movq %rsi, 0x18(%rsp)
movq %rdx, %r14
movq %rdi, %r12
incq %r12
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0xdebae
leaq 0xb85de(%rip), %rsi     # 0x1215a0
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xdf038
movq 0x8(%rsp), %rdx
leaq 0xb8d36(%rip), %rsi     # 0x121d2a
movq %rbx, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x69089
movq %r14, %rbp
movq %r14, %rdx
shlq $0x3, %rdx
pushq $0x3
popq %r8
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
callq 0xe746f
movq %rax, %r14
pushq $0x14
popq %rdi
leaq 0x20(%rsp), %r15
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %rcx
xorl %r9d, %r9d
callq 0xe1b11
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
movq 0x18(%rsp), %rcx
movq %rbp, %r8
movq %r15, %r9
pushq $0x0
pushq %r14
callq 0xe7de1
popq %rax
popq %rcx
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0xe70b7
testq %r14, %r14
je 0x69089
movq %r14, %rdi
callq 0xe7084
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq