// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/icns.c
// Function: de_run_icns
static void de_run_icns(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	const char *s;

	d = de_malloc(c, sizeof(lctx));
	init_masktype_info(d);

	// (If these options are undocumented, it's because they're still in
	// development/testing.)
	// 81 = Use 8-bit mask if present, otherwise 1-bit mask
	// 18 = Use 1-bit mask if present, otherwise 8-bit mask
	// 8 = Use 8-bit mask only
	// 1 = Use 1-bit mask only
	// 0 = No transparency
	//
	// TODO: Maybe set opt_mask1 = 81
	d->opt_mask1 = 1; // Setting for (most) 1bpp icons
	d->opt_mask8 = 81; // Setting for 4 and 8bpp icons
	d->opt_mask24 = 81; // Setting for 24bpp icons
	s = de_get_ext_option(c, "icns:mask1");
	if(s) {
		d->opt_mask1 = (u8)de_atoi(s);
	}
	s = de_get_ext_option(c, "icns:mask8");
	if(s) {
		d->opt_mask8 = (u8)de_atoi(s);
	}
	s = de_get_ext_option(c, "icns:mask24");
	if(s) {
		d->opt_mask24 = (u8)de_atoi(s);
	}

	d->opt_getmasks = de_get_ext_option_bool(c, "icns:getmasks", 0);

	d->file_size = de_getu32be(4);
	de_dbg(c, "reported file size: %d", (int)d->file_size);
	if(d->file_size > c->infile->len) d->file_size = c->infile->len;

	de_dbg(c, "pass 1: reading masks");
	de_dbg_indent(c, 1);
	de_run_icns_pass(c, d, 1);
	de_dbg_indent(c, -1);

	if(d->opt_getmasks) {
		extract_masks(c, d);
		goto done;
	}

	de_dbg(c, "pass 2: decoding/extracting icons");
	de_dbg_indent(c, 1);
	de_run_icns_pass(c, d, 2);
	de_dbg_indent(c, -1);

done:
	if(d) {
		int i;

		for(i=0; i<NUM_MASKTYPES; i++) {
			if(d->mask[i].img) {
				if(!d->mask[i].used_flag && !d->opt_getmasks) {
					de_dbg(c, "[mask at %"I64_FMT" was not used]", d->mask[i].segment_pos);
				}
				de_bitmap_destroy(d->mask[i].img);
				d->mask[i].img = NULL;
			}
		}
		de_free(c, d);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
movl $0x518, %esi            # imm = 0x518
callq 0xe8b2b
movq %rax, %r14
addq $0x10, %rax
movl $0x4, %ecx
leaq 0xb39a8(%rip), %rdx     # 0x127120
movb -0x3(%rcx,%rdx), %sil
movzbl -0x4(%rcx,%rdx), %edi
shll $0x5, %edi
movb %sil, 0x9(%rax,%rdi)
movq (%rcx,%rdx), %rsi
movq %rsi, 0xc(%rax,%rdi)
addq $0xc, %rcx
cmpq $0x64, %rcx
jne 0x73778
movw $0x5101, 0x8(%r14)      # imm = 0x5101
movb $0x51, 0xa(%r14)
leaq 0xb3dfc(%rip), %rsi     # 0x1275ac
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x737c9
movq %rax, %rdi
callq 0xe9c2a
movb %al, 0x8(%r14)
leaq 0xb3de7(%rip), %rsi     # 0x1275b7
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x737e9
movq %rax, %rdi
callq 0xe9c2a
movb %al, 0x9(%r14)
leaq 0xb3dd2(%rip), %rsi     # 0x1275c2
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x73809
movq %rax, %rdi
callq 0xe9c2a
movb %al, 0xa(%r14)
leaq 0xb3dbe(%rip), %rsi     # 0x1275ce
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe9bd0
movb %al, 0xb(%r14)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeed0d
movq %rax, (%r14)
leaq 0xacb94(%rip), %rsi     # 0x1203ca
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
cmpq %rax, (%r14)
jle 0x73852
movq %rax, (%r14)
leaq 0xb3d83(%rip), %rsi     # 0x1275dc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x73a09
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, 0xb(%r14)
je 0x738fd
leaq 0xb3fe0(%rip), %rsi     # 0x12787b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
leaq 0xb3fe0(%rip), %r15     # 0x12788c
movq %rsp, %r12
xorl %r13d, %r13d
cmpq $0x0, 0x28(%r14,%r13)
je 0x738ee
movl 0x1c(%r14,%r13), %ecx
movl 0x20(%r14,%r13), %r8d
movzbl 0x19(%r14,%r13), %r9d
movl $0x20, %esi
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8193
movq 0x28(%r14,%r13), %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0xf22dc
addq $0x20, %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x738b2
jmp 0x73938
leaq 0xb3cee(%rip), %rsi     # 0x1275f2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0x73a09
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl $0x28, %r12d
leaq 0xb3ccf(%rip), %r15     # 0x127614
movq (%r14,%r12), %rdi
testq %rdi, %rdi
je 0x73980
cmpb $0x0, -0x10(%r14,%r12)
jne 0x73973
cmpb $0x0, 0xb(%r14)
jne 0x73973
movq -0x18(%r14,%r12), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0xe8617
movq (%r14,%r12), %rdi
callq 0xf228d
movq $0x0, (%r14,%r12)
addq $0x20, %r12
cmpq $0x128, %r12            # imm = 0x128
jne 0x73945
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq