// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
leaq 0x8(%rsi), %rcx
movq 0x18(%rdi), %rdi
xorl %eax, %eax
cmpq 0x18(%rdi), %rcx
jg 0x6cdaa
movq %rdx, %r14
movq %rsi, %r15
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0xeebeb
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeebeb
movq %rax, %r13
movl $0x800, %eax            # imm = 0x800
cmpq %rax, %r12
setge %cl
cmpq %rax, %r13
setge %al
orb %cl, %al
je 0x6cc48
xorl %eax, %eax
jmp 0x6cdaa
movl %ebp, %edi
incq %r13
leaq 0x8(%r12), %rax
leaq 0xf(%r12), %rdx
testq %rax, %rax
cmovnsq %rax, %rdx
sarq $0x3, %rdx
leaq (,%rdx,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 0x6cdaa
leaq 0x6(%rbp), %rax
movq %rax, (%r14)
movl $0x1, %eax
testl %edi, %edi
jne 0x6cdaa
movq %rsi, 0x18(%rsp)
movq %rdx, %r14
incq %r12
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
leaq 0xc1b51(%rip), %rsi     # 0x12e810
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xe8f88
movq 0x8(%rsp), %rdx
leaq 0xc22bf(%rip), %rsi     # 0x12efb2
movq %rbx, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x6cd99
movq %r14, %rbp
leaq (,%r14,8), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
movl $0x3, %r8d
callq 0xf26a5
movq %rax, %r14
leaq 0x20(%rsp), %r15
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %rcx
xorl %r9d, %r9d
callq 0xec55a
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movl $0x4, %edx
movq 0x18(%rsp), %rcx
movq %rbp, %r8
movq %r15, %r9
pushq $0x0
pushq %r14
callq 0xf30b3
addq $0x10, %rsp
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf22dc
testq %r14, %r14
je 0x6cd99
movq %r14, %rdi
callq 0xf228d
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq