// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: de_run_iconmgr_ica
static void de_run_iconmgr_ica(deark *c, de_module_params *mparams)
{
	struct iconmgr_ica_ctx *d = NULL;
	struct iconmgr_icon_ctx *md = NULL;
	u8 b;
	i64 pos;

	d = de_malloc(c, sizeof(struct iconmgr_ica_ctx));
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_WINDOWS1252);

	b = de_getbyte(2);
	if(b=='M') {
		d->ver = 1;
	}
	else if(b=='2') {
		d->ver = 2;
	}
	else {
		d->need_errmsg = 1;
		goto done;
	}

	d->icon_count = de_getu16le(4);
	de_dbg(c, "count: %d", (int)d->icon_count);
	d->reported_file_size = de_getu32le(4);

	de_copy_std_palette(DE_PALID_WIN16, 0, 0, d->pal, 16, 0);
	pos = 18;
	while(1) {
		if(pos>=c->infile->len || pos>=d->reported_file_size) goto done;
		if(md) {
			iconmgr_icon_destroy(c, md);
			md = NULL;
		}
		md = de_malloc(c, sizeof(struct iconmgr_icon_ctx));
		md->icon_pos = pos;
		do_iconmgr_icon(c, d, md);
		if(md->total_len<=0) goto done;
		pos += md->total_len;
	}

done:
	iconmgr_icon_destroy(c, md);
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported file");
		}
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
pushq $0x58
popq %rsi
callq 0xdecaf
movq %rax, %r15
pushq $0x9
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x4(%r15)
movq 0x18(%rbx), %rdi
pushq $0x2
popq %rsi
callq 0xe3eb4
cmpb $0x4d, %al
je 0x6752c
movzbl %al, %eax
cmpl $0x32, %eax
jne 0x67801
movb $0x2, %al
jmp 0x6752e
movb $0x1, %al
movb %al, (%r15)
movq 0x18(%rbx), %rdi
pushq $0x4
popq %r14
movq %r14, %rsi
callq 0xe3fa3
movq %rax, 0x8(%r15)
leaq 0x9e664(%rip), %rsi     # 0x105bb0
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe409f
movq %rax, 0x10(%r15)
leaq 0x18(%r15), %rcx
pushq $0x32
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
movq %rcx, 0x20(%rsp)
xorl %r9d, %r9d
callq 0xe1b11
pushq $0x12
popq %r14
pushq $0x1
popq %rbp
xorl %r12d, %r12d
movq %r15, 0x10(%rsp)
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %r14
jge 0x67809
cmpq 0x10(%r15), %r14
jge 0x67809
testq %r12, %r12
je 0x675bb
movq %rbx, %rdi
movq %r12, %rsi
callq 0x69631
movq %rbx, %rdi
pushq $0x18
popq %rsi
callq 0xdecaf
movq %rax, %r12
movq %r14, (%rax)
movq %rbx, %rdi
leaq 0x1c(%rsp), %rsi
callq 0xdebae
movq (%r12), %rdx
movq %rbx, %rdi
leaq 0xb10ce(%rip), %rsi     # 0x1186b5
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq (%r12), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0xe3f2a
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3fc4
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3fc4
cmpb $0x4, %bpl
jne 0x676a8
cmpq $0xfff, %r15            # imm = 0xFFF
jg 0x676a8
cmpq $0x1000, %rax           # imm = 0x1000
jge 0x676a8
movq 0x10(%rsp), %r15
cmpb $0x2, (%r15)
jne 0x676be
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x10(%r12)
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
movl 0x4(%r15), %r9d
pushq $0x1a
popq %rdx
movq %rax, %rcx
pushq $0x1
popq %r8
callq 0xe4c9a
movq 0x10(%r12), %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0xadf2d(%rip), %rsi     # 0x1155bb
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x8(%rsp), %rdx
addq $0x1a, %rdx
movq %rdx, 0x8(%rsp)
jmp 0x676c3
movq 0x10(%rsp), %rax
movb $0x1, 0x1(%rax)
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x677bf
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
leaq 0xc1c38(%rip), %rsi     # 0x129305
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x20
popq %r13
movq %r13, %rsi
movq %r13, %rdx
pushq $0x4
popq %rcx
callq 0xe7437
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
andl $0x0, (%rsp)
pushq $0x4
popq %rdx
pushq $0x10
popq %rcx
movq 0x20(%rsp), %r8
movq %rax, %r9
callq 0xe7cbc
movq 0x8(%rsp), %rdx
movl $0x200, %eax            # imm = 0x200
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
movq %rbx, %rdi
leaq 0xc5f31(%rip), %rsi     # 0x12d659
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %r13, %rsi
movq %r13, %rdx
pushq $0x1
popq %rbp
movl %ebp, %ecx
callq 0xe7437
movq %rax, %r13
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xe77b7
subq $-0x80, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0xe83f8
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %rbp
cmpb $0x0, 0xd0(%rbx)
je 0x677a3
movq 0x10(%r12), %rdi
callq 0xeaf8a
testl %eax, %eax
je 0x677a3
movq 0x10(%r12), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xdfd55
movq %r15, %rdi
movq %rbp, %rsi
pushq $0x6
popq %rdx
callq 0xe70ab
movq 0x8(%rsp), %rax
subq (%r12), %rax
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0xe7084
movq %r13, %rdi
callq 0xe7084
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xdfd24
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq 0x8(%r12), %rax
addq %rax, %r14
testq %rax, %rax
movq 0x10(%rsp), %r15
pushq $0x1
popq %rbp
jg 0x67593
jmp 0x67809
movb $0x1, 0x1(%r15)
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x69631
testq %r15, %r15
je 0x6783c
cmpb $0x0, 0x1(%r15)
je 0x67831
leaq 0xa4678(%rip), %rsi     # 0x10be9f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq