// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/mmm.c
// Function: do_mmm_clut
static void do_mmm_clut(deark *c, struct mmm_ctx *d, struct de_iffctx *ictx,
	i64 pos1, i64 len)
{
	i64 pos;
	i64 num_entries;
	i64 i;
	int rsrc_id;
	u8 is_correct_pass;
	u8 keep_this_pal = 0;

	pos = pos1;
	mmm_read_rsrcid_p(c, d, ictx, &pos, "pal id");
	rsrc_id = d->tmp_rsrcid;
	mmm_read_name_p(c, d, ictx, &pos, NULL);

	num_entries = (pos1+len-pos)/6;

	de_dbg(c, "palette at %"I64_FMT", %u entries", pos, (UI)num_entries);

	if(num_entries!=16 && num_entries!=256) {
		de_warn(c, "Unsupported type of palette");
		goto done;
	}

	is_correct_pass = (d->pass==1 && !d->use_alt_palfile) ||
		(d->pass==100 && d->use_alt_palfile);

	if(d->force_pal) {
		if(rsrc_id==d->pal_id_to_use && is_correct_pass) {
			keep_this_pal = 1;
		}
	}
	else if(!d->have_pal && is_correct_pass) {
		keep_this_pal = 1;
	}

	de_dbg_indent(c, 1);
	for(i=0; i<num_entries; i++) {
		UI samp[3];
		de_color clr;
		i64 idx;
		UI k;

		idx = num_entries-1-i;

		for(k=0; k<3; k++) {
			samp[k] = (UI)dbuf_getu16be_p(ictx->f, &pos);
			samp[k] = (UI)de_sample_nbit_to_8bit(16, samp[k]);
		}
		clr = DE_MAKE_RGB(samp[0], samp[1], samp[2]);
		de_dbg_pal_entry(c, idx, clr);

		if(keep_this_pal) {
			d->pal[idx] = clr;
		}
	}
	de_dbg_indent(c, -1);
	if(keep_this_pal) {
		d->have_pal = 1;
	}

done:
	d->clut_count++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movb $0x0, -0x46(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rcx
leaq 0xdd8c6(%rip), %r8      # 0x1bc2b5
callq 0xdf600
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdf680
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
subq -0x30(%rbp), %rax
movl $0x6, %ecx
cqto
idivq %rcx
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0xdd875(%rip), %rsi     # 0x1bc2bc
movb $0x0, %al
callq 0x15b0d0
cmpq $0x10, -0x38(%rbp)
je 0xdea76
cmpq $0x100, -0x38(%rbp)     # imm = 0x100
je 0xdea76
movq -0x8(%rbp), %rdi
leaq 0xdd86d(%rip), %rsi     # 0x1bc2d7
movb $0x0, %al
callq 0x15bc10
jmp 0xdebec
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jne 0xdea8e
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpb $0x0, 0x5(%rcx)
movb %al, -0x65(%rbp)
je 0xdeab3
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpl $0x64, (%rcx)
movb %al, -0x66(%rbp)
jne 0xdeaad
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x66(%rbp)
movb -0x66(%rbp), %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb %al, -0x45(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x6(%rax)
je 0xdeae3
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0xdeae1
movzbl -0x45(%rbp), %eax
cmpl $0x0, %eax
je 0xdeae1
movb $0x1, -0x46(%rbp)
jmp 0xdeafc
movq -0x10(%rbp), %rax
cmpb $0x0, 0x20(%rax)
jne 0xdeafa
movzbl -0x45(%rbp), %eax
cmpl $0x0, %eax
je 0xdeafa
movb $0x1, -0x46(%rbp)
jmp 0xdeafc
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0xdebce
movq -0x38(%rbp), %rax
subq $0x1, %rax
subq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x3, -0x64(%rbp)
jae 0xdeb7d
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movl %eax, %ecx
movl -0x64(%rbp), %eax
movl %ecx, -0x54(%rbp,%rax,4)
movl -0x64(%rbp), %eax
movl -0x54(%rbp,%rax,4), %esi
movl $0x10, %edi
callq 0x161680
movzbl %al, %ecx
movl -0x64(%rbp), %eax
movl %ecx, -0x54(%rbp,%rax,4)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xdeb37
movl -0x54(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl -0x50(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
orl -0x4c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl -0x58(%rbp), %edx
callq 0x15bf00
cmpb $0x0, -0x46(%rbp)
je 0xdebbb
movl -0x58(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movl %edx, 0x38(%rax,%rcx,4)
jmp 0xdebbd
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xdeb12
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpb $0x0, -0x46(%rbp)
je 0xdebea
movq -0x10(%rbp), %rax
movb $0x1, 0x20(%rax)
jmp 0xdebec
movq -0x10(%rbp), %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
addq $0x70, %rsp
popq %rbp
retq
nop