// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/icns.c
// Function: find_mask
static struct mask_wrapper *find_mask(deark *c, lctx *d, struct page_ctx *pg)
{
	struct mask_wrapper *mw = NULL;
	struct mask_wrapper *mw1 = NULL;
	struct mask_wrapper *mw8 = NULL;
	const struct image_type_info *t;
	int found_mask = 0;
	u8 opt;

	t = pg->type_info;

	// TODO: What is the correct way to match masks to images?

	if(t->code==0x49434f4e) { // 'ICON'
		// I'm assuming this format doesn't have a mask.
		return NULL;
	}

	if(t->width==16 && t->height==12 && t->bpp<=8) {
		mw1 = &d->mask[MASKTYPEID_16_12_1];
		goto afterdiscovery;
	}
	if(t->width==16 && t->height==16) {
		mw1 = &d->mask[MASKTYPEID_16_16_1];
		mw8 = &d->mask[MASKTYPEID_16_16_8];
		goto afterdiscovery;
	}
	if(t->width==32) {
		mw1 = &d->mask[MASKTYPEID_32_32_1];
		mw8 = &d->mask[MASKTYPEID_32_32_8];
		goto afterdiscovery;
	}
	if(t->width==48) {
		mw1 = &d->mask[MASKTYPEID_48_48_1];
		mw8 = &d->mask[MASKTYPEID_48_48_8];
		goto afterdiscovery;
	}
	if(t->width==128) {
		mw8 = &d->mask[MASKTYPEID_128_128_8];
		goto afterdiscovery;
	}

afterdiscovery:
	if(t->bpp==1) {
		opt = d->opt_mask1;
	}
	else if(t->bpp<=8) {
		opt = d->opt_mask8;
	}
	else {
		opt = d->opt_mask24;
	}

	if(opt==1) {
		mw = mw1;
	}
	else if(opt==8) {
		mw = mw8;
	}
	else if(opt==18) {
		if(mw1 && mw1->img) mw = mw1;
		else mw = mw8;
	}
	else if(opt==81) {
		if(mw8 && mw8->img) mw = mw8;
		else mw = mw1;
	}

	found_mask = (mw && mw->img);
	if(!found_mask) goto notfound;

	if(t->image_type==IMGTYPE_IMAGE_AND_MASK && mw==mw1) {
		// Sanity check. This could fail if there are multiple icons of
		// the same type.
		if(pg->segment_pos != mw->segment_pos) {
			goto notfound;
		}
	}

	mw->used_flag = 1;
	de_dbg(c, "[using mask at %"I64_FMT"]", mw->segment_pos);
	return mw;

notfound:
	de_dbg(c, "[no mask found for icon at %"I64_FMT"]", pg->segment_pos);
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpl $0x49434f4e, (%rax)     # imm = 0x49434F4E
jne 0xa8198
movq $0x0, -0x8(%rbp)
jmp 0xa83e4
movq -0x40(%rbp), %rax
cmpl $0x10, 0x4(%rax)
jne 0xa81c7
movq -0x40(%rbp), %rax
cmpl $0xc, 0x8(%rax)
jne 0xa81c7
movq -0x40(%rbp), %rax
cmpl $0x8, 0xc(%rax)
jg 0xa81c7
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x30(%rbp)
jmp 0xa827e
movq -0x40(%rbp), %rax
cmpl $0x10, 0x4(%rax)
jne 0xa81ff
movq -0x40(%rbp), %rax
cmpl $0x10, 0x8(%rax)
jne 0xa81ff
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0x80, %rax
movq %rax, -0x38(%rbp)
jmp 0xa827e
movq -0x40(%rbp), %rax
cmpl $0x20, 0x4(%rax)
jne 0xa822d
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0x40, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0xa0, %rax
movq %rax, -0x38(%rbp)
jmp 0xa827e
movq -0x40(%rbp), %rax
cmpl $0x30, 0x4(%rax)
jne 0xa825b
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0x60, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0xc0, %rax
movq %rax, -0x38(%rbp)
jmp 0xa827e
movq -0x40(%rbp), %rax
cmpl $0x80, 0x4(%rax)
jne 0xa827c
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0xe0, %rax
movq %rax, -0x38(%rbp)
jmp 0xa827e
jmp 0xa827e
movq -0x40(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0xa8294
movq -0x18(%rbp), %rax
movb 0x8(%rax), %al
movb %al, -0x45(%rbp)
jmp 0xa82b6
movq -0x40(%rbp), %rax
cmpl $0x8, 0xc(%rax)
jg 0xa82aa
movq -0x18(%rbp), %rax
movb 0x9(%rax), %al
movb %al, -0x45(%rbp)
jmp 0xa82b4
movq -0x18(%rbp), %rax
movb 0xa(%rax), %al
movb %al, -0x45(%rbp)
jmp 0xa82b6
movzbl -0x45(%rbp), %eax
cmpl $0x1, %eax
jne 0xa82c9
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xa8340
movzbl -0x45(%rbp), %eax
cmpl $0x8, %eax
jne 0xa82dc
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xa833e
movzbl -0x45(%rbp), %eax
cmpl $0x12, %eax
jne 0xa830b
cmpq $0x0, -0x30(%rbp)
je 0xa8301
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xa8301
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xa8309
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xa833c
movzbl -0x45(%rbp), %eax
cmpl $0x51, %eax
jne 0xa833a
cmpq $0x0, -0x38(%rbp)
je 0xa8330
movq -0x38(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xa8330
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xa8338
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xa833a
jmp 0xa833c
jmp 0xa833e
jmp 0xa8340
xorl %eax, %eax
cmpq $0x0, -0x28(%rbp)
movb %al, -0x46(%rbp)
je 0xa835b
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
setne %al
movb %al, -0x46(%rbp)
movb -0x46(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0xa836e
jmp 0xa83c2
movq -0x40(%rbp), %rax
cmpl $0x4, 0x10(%rax)
jne 0xa8397
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0xa8397
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
je 0xa8395
jmp 0xa83c2
jmp 0xa8397
movq -0x28(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x10a819(%rip), %rsi    # 0x1b2bca
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa83e4
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x10a809(%rip), %rsi    # 0x1b2bde
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop