// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
leaq 0x8(%rsi), %rax
movq 0x18(%rdi), %rdi
xorl %r15d, %r15d
cmpq 0x18(%rdi), %rax
jg 0x6cfe4
movq %rdx, %r14
movq %rsi, %rbp
movl %ecx, 0x10(%rsp)
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0xef1ed
movq %rax, %r13
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef1ed
movq %r13, %rdi
movq %rax, %r13
movl $0x800, %eax            # imm = 0x800
cmpq %rax, %rdi
setge %cl
cmpq %rax, %r13
setge %al
orb %cl, %al
jne 0x6cfe4
incq %r13
leaq 0x8(%rdi), %rax
leaq 0xf(%rdi), %rdx
testq %rax, %rax
cmovnsq %rax, %rdx
sarq $0x3, %rdx
leaq (,%rdx,4), %rsi
movq %rsi, %r12
imulq %r13, %r12
movq 0x8(%rsp), %rax
addq %r12, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x6cfe4
leaq 0x6(%r12), %rax
movq %rax, (%r14)
movl $0x1, %r15d
cmpl $0x0, 0x10(%rsp)
jne 0x6cfe4
movq %rsi, 0x10(%rsp)
movq %rdx, %r15
incq %rdi
leaq 0x1c(%rsp), %rsi
movq %rdi, %r14
movq %rbx, %rdi
callq 0xe958c
leaq 0xc161e(%rip), %rsi     # 0x12e518
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xe9b05
movq 0x8(%rsp), %rdx
leaq 0xc1d74(%rip), %rsi     # 0x12eca2
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xf241d
testl %eax, %eax
je 0x6cfc2
leaq (,%r15,8), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
movl $0x3, %r8d
callq 0xf2ce6
movq %rax, %r12
leaq 0x20(%rsp), %r14
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
xorl %r9d, %r9d
callq 0xecbbc
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movl $0x4, %edx
movq 0x10(%rsp), %rcx
movq %r15, %r8
movq %r14, %r9
pushq $0x0
pushq %r12
callq 0xf36f9
addq $0x10, %rsp
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf2923
jmp 0x6cfc5
xorl %r12d, %r12d
testq %r12, %r12
je 0x6cfd2
movq %r12, %rdi
callq 0xf28d4
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movl $0x1, %r15d
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq