// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl $0x518, %esi            # imm = 0x518
callq 0xdecaf
movq %rax, %r14
addq $0x10, %rax
xorl %ecx, %ecx
leaq 0xaa87e(%rip), %rdx     # 0x119f20
cmpq $0x60, %rcx
je 0x6f6c9
movb 0x1(%rcx,%rdx), %sil
movzbl (%rcx,%rdx), %edi
shll $0x5, %edi
movb %sil, 0x9(%rax,%rdi)
movq 0x4(%rcx,%rdx), %rsi
movq %rsi, 0xc(%rax,%rdi)
addq $0xc, %rcx
jmp 0x6f6a2
movw $0x5101, 0x8(%r14)      # imm = 0x5101
movb $0x51, 0xa(%r14)
leaq 0xaacd0(%rip), %rsi     # 0x11a3ac
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x6f6f5
movq %rax, %rdi
callq 0xdfb36
movb %al, 0x8(%r14)
leaq 0xaacbb(%rip), %rsi     # 0x11a3b7
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x6f715
movq %rax, %rdi
callq 0xdfb36
movb %al, 0x9(%r14)
leaq 0xaaca6(%rip), %rsi     # 0x11a3c2
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x6f735
movq %rax, %rdi
callq 0xdfb36
movb %al, 0xa(%r14)
leaq 0xaac92(%rip), %rsi     # 0x11a3ce
movq %rbx, %rdi
xorl %edx, %edx
callq 0xdfae5
movb %al, 0xb(%r14)
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe404f
movq %rax, (%r14)
leaq 0xa3b78(%rip), %rsi     # 0x1132d8
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
cmpq %rax, (%r14)
jle 0x6f77c
movq %rax, (%r14)
leaq 0xaac59(%rip), %rsi     # 0x11a3dc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x6f92d
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0xb(%r14)
je 0x6f831
leaq 0xaaebb(%rip), %rsi     # 0x11a67b
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0xaaeb6(%rip), %r15     # 0x11a68c
leaq 0x10(%rsp), %r12
pushq $0x20
popq %r13
pushq $0x2
popq %rbp
xorl %ebx, %ebx
cmpq $0x100, %rbx            # imm = 0x100
je 0x6f82a
cmpq $0x0, 0x28(%r14,%rbx)
je 0x6f824
movl 0x1c(%r14,%rbx), %ecx
movl 0x20(%r14,%rbx), %r8d
movzbl 0x19(%r14,%rbx), %r9d
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0xde40c
movq 0x28(%r14,%rbx), %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0xe70b7
addq $0x20, %rbx
jmp 0x6f7e4
movq 0x8(%rsp), %rbx
jmp 0x6f866
leaq 0xaabba(%rip), %rsi     # 0x11a3f2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6f92d
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x28
popq %r12
leaq 0xaaba3(%rip), %r15     # 0x11a414
cmpq $0x128, %r12            # imm = 0x128
je 0x6f8b8
movq (%r14,%r12), %rdi
testq %rdi, %rdi
je 0x6f8b2
cmpb $0x0, -0x10(%r14,%r12)
jne 0x6f8a8
cmpb $0x0, 0xb(%r14)
jne 0x6f8a8
movq -0x18(%r14,%r12), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0xde896
movq (%r14,%r12), %rdi
callq 0xe7084
andq $0x0, (%r14,%r12)
addq $0x20, %r12
jmp 0x6f871
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq