// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/rsc.c
// Function: do_ciconblk_struct
static int do_ciconblk_struct(deark *c, lctx *d, i64 icon_idx, i64 pos1,
	i64 *bytes_consumed)
{
	struct iconinfo *ii = NULL;
	i64 pos;
	i64 n_cicons;
	i64 mono_bitmapsize;
	i64 color_bitmapsize;
	i64 next_res;
	i64 sel_data_flag;
	int retval = 0;
	i64 i;
	i64 mono_fgpos, mono_maskpos;
	int saved_indent_level;
	char token[16];

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "CICONBLK[%d] at %"I64_FMT, (int)icon_idx, pos1);
	de_dbg_indent(c, 1);

	ii = de_malloc(c, sizeof(struct iconinfo));

	pos = pos1;
	if(!do_scan_iconblk(c, d, pos, ii)) {
		goto done;
	}
	pos+=34;

	n_cicons = gem_getu32(d, pos);
	de_dbg(c, "number of color depths for this icon: %d", (int)n_cicons);
	pos += 4;

	ii->mono_rowspan = ((ii->width+15)/16)*2; // guess

	de_dbg2(c, "bilevel image data at %"I64_FMT" (deferred)", pos);
	mono_bitmapsize = ii->mono_rowspan * ii->height;
	mono_fgpos = pos;
	pos += mono_bitmapsize; // foreground
	mono_maskpos = pos;
	pos += mono_bitmapsize; // mask
	de_dbg2(c, "bilevel image data ends at %"I64_FMT, pos);

	if(!ii->icon_text) {
		ii->icon_text = ucstring_create(c);
	}
	ucstring_empty(ii->icon_text);
	dbuf_read_to_ucstring(c->infile, pos, 12, ii->icon_text, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "icon text: \"%s\"", ucstring_getpsz_d(ii->icon_text));
	pos += 12;

	if(!de_good_image_dimensions(c, ii->width, ii->height)) {
		goto done;
	}

	// Go back and read the bilevel icon. (We wanted to read the icon text first.)
	de_dbg(c, "bilevel image data at %"I64_FMT, mono_fgpos);
	de_dbg_indent(c, 1);
	de_dbg(c, "fg at %"I64_FMT, mono_fgpos);
	de_dbg(c, "mask at %"I64_FMT, mono_maskpos);
	do_bilevel_icon(c, d, ii, mono_fgpos, mono_maskpos, "1");
	de_dbg_indent(c, -1);

	for(i=0; i<n_cicons; i++) {
		if(pos >= c->infile->len) goto done;
		de_dbg(c, "color depth %d of %d, at %"I64_FMT, (int)(i+1), (int)n_cicons, pos);
		de_dbg_indent(c, 1);

		ii->nplanes = gem_getu16(d, pos);
		de_dbg(c, "planes: %d", (int)ii->nplanes);
		pos += 2;

		pos += 4; // col_data (placeholder)
		pos += 4; // col_mask (placeholder)

		sel_data_flag = gem_getu32(d, pos);
		de_dbg(c, "sel_data flag: %d", (int)sel_data_flag);
		pos += 4; // sel_data

		pos += 4; // sel_mask (placeholder)

		next_res = gem_getu32(d, pos);
		de_dbg(c, "next_res flag: %d", (int)next_res);
		pos += 4;

		color_bitmapsize = mono_bitmapsize * ii->nplanes;

		de_dbg(c, "unselected image at %"I64_FMT, pos);
		de_dbg_indent(c, 1);
		de_dbg(c, "fg at %"I64_FMT, pos);
		de_dbg(c, "mask at %"I64_FMT, pos+color_bitmapsize);
		de_snprintf(token, sizeof(token), "%d", (int)ii->nplanes);
		do_color_icon(c, d, ii, pos, pos+color_bitmapsize, token, 0);
		pos += color_bitmapsize; // color_data
		pos += mono_bitmapsize; // color_mask
		de_dbg_indent(c, -1);

		if(sel_data_flag) {
			de_dbg(c, "selected image at %"I64_FMT, pos);
			de_dbg_indent(c, 1);
			de_dbg(c, "fg at %"I64_FMT, pos);
			de_dbg(c, "mask at %"I64_FMT, pos+color_bitmapsize);
			de_snprintf(token, sizeof(token), "%d.sel", (int)ii->nplanes);
			do_color_icon(c, d, ii, pos, pos+color_bitmapsize, token, 1);
			pos += color_bitmapsize; // select_data
			pos += mono_bitmapsize; // select_mask
			de_dbg_indent(c, -1);
		}

		*bytes_consumed = pos - pos1;

		de_dbg_indent(c, -1);
	}

	retval = 1;
done:
	destroy_iconinfo(c, ii);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rcx
leaq 0xa23e9(%rip), %rsi     # 0x1c69cc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x28, %esi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x1240e0
cmpl $0x0, %eax
jne 0x124631
jmp 0x124b46
movq -0x38(%rbp), %rax
addq $0x22, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x123920
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0xa2381(%rip), %rsi     # 0x1c69e0
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
addq $0xf, %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, %rcx
shlq %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xa2365(%rip), %rsi     # 0x1c6a09
movb $0x0, %al
callq 0x15b350
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
imulq 0x8(%rcx), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xa2337(%rip), %rsi     # 0x1c6a2e
movb $0x0, %al
callq 0x15b350
movq -0x30(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x12471d
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %r9d
movl $0xc, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x172650
movq -0xa8(%rbp), %rdi
movq %rax, %rdx
leaq 0xa22ce(%rip), %rsi     # 0x1c6a4d
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x1247b4
jmp 0x124b46
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rdx
leaq 0xa229a(%rip), %rsi     # 0x1c6a5d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rdx
leaq 0xa214e(%rip), %rsi     # 0x1c6935
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq 0xa19ca(%rip), %rsi     # 0x1c61c7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x80(%rbp), %r8
leaq 0x9a65d(%rip), %r9      # 0x1bee7c
callq 0x1241b0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x124b3f
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x12485f
jmp 0x124b46
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
addq $0x1, %rax
movl %eax, %edx
movq -0x40(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %r8
leaq 0xa21f9(%rip), %rsi     # 0x1c6a77
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x123250
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0xa243f(%rip), %rsi     # 0x1c6cff
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x123920
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %edx
leaq 0xa2187(%rip), %rsi     # 0x1c6a94
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x123920
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
leaq 0xa2158(%rip), %rsi     # 0x1c6aa6
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
imulq 0x18(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xa2137(%rip), %rsi     # 0x1c6ab8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xa1f90(%rip), %rsi     # 0x1c6935
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
addq -0x50(%rbp), %rdx
leaq 0xa1808(%rip), %rsi     # 0x1c61c7
movb $0x0, %al
callq 0x15b0d0
leaq -0xa0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %ecx
movl $0x10, %esi
leaq 0x9d23b(%rip), %rdx     # 0x1c1c1e
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %r8
addq -0x50(%rbp), %r8
leaq -0xa0(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x124b70
movq -0x50(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpq $0x0, -0x60(%rbp)
je 0x124b11
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xa2063(%rip), %rsi     # 0x1c6aba
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xa1eba(%rip), %rsi     # 0x1c6935
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
addq -0x50(%rbp), %rdx
leaq 0xa1732(%rip), %rsi     # 0x1c61c7
movb $0x0, %al
callq 0x15b0d0
leaq -0xa0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %ecx
movl $0x10, %esi
leaq 0xa2017(%rip), %rdx     # 0x1c6ad0
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %r8
addq -0x50(%rbp), %r8
leaq -0xa0(%rbp), %r9
movl $0x1, (%rsp)
callq 0x124b70
movq -0x50(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x38(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x12483a
movl $0x1, -0x64(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x124360
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0x15b5a0
movl -0x64(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nop