// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/xwd.c
// Function: find_cmap_and_image
static void find_cmap_and_image(deark *c, lctx *d)
{
	u8 need_cmap_warning = 0;
	i64 size1, size2;
	i64 avail_size;

	d->cmap_num_entries = (i64)d->hf[HF_CMAP_NUM_ENTRIES];
	d->cmap_size_in_bytes = d->cmap_num_entries * 12;

	// It's critical that we know exactly how many entries are in the colormap.
	// Sometimes its in one field, sometimes in another, and it's not clear
	// how to tell which.

	// We hope these two fields are the same.
	if(d->hf[HF_CMAP_NUM_ENTRIES] == d->hf[HF_NCOLORS]) goto done;

	if(d->hf[HF_NCOLORS] > d->hf[HF_CMAP_NUM_ENTRIES]) {
		// I haven't seen this happen.
		goto done;
	}

	// d->hf[HF_NCOLORS] < d->hf[HF_CMAP_NUM_ENTRIES]

	size1 = (i64)d->hf[HF_NCOLORS] * 12; // Note, size1 is smaller than size2
	size2 = (i64)d->hf[HF_CMAP_NUM_ENTRIES] * 12;
	avail_size = c->infile->len - d->expected_image_size - d->cmap_pos;

	if(size2==avail_size) {
		goto done;
	}
	if(size1==avail_size) {
		d->cmap_num_entries = (i64)d->hf[HF_NCOLORS];
		goto done;
	}
	if(size2>avail_size && size1<avail_size) {
		d->cmap_num_entries = (i64)d->hf[HF_NCOLORS];
		need_cmap_warning = 1;
		goto done;
	}

	need_cmap_warning = 1;

done:
	if(need_cmap_warning) {
		de_warn(c, "Can't reliably locate the image. Might not be decoded correctly.");
	}
	d->cmap_size_in_bytes = d->cmap_num_entries * 12;
	d->imgpos = d->cmap_pos + d->cmap_size_in_bytes;
	d->actual_image_size = c->infile->len - d->imgpos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x10(%rbp), %rax
imulq $0xc, 0x78(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jne 0x15a19e
jmp 0x15a24c
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jbe 0x15a1b3
jmp 0x15a24c
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
imulq $0xc, %rax, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %eax
imulq $0xc, %rax, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0x90(%rcx), %rax
movq -0x10(%rbp), %rcx
subq 0x70(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x15a200
jmp 0x15a24c
movq -0x20(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x15a21d
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
jmp 0x15a24c
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jle 0x15a248
movq -0x20(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x15a248
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movb $0x1, -0x11(%rbp)
jmp 0x15a24c
movb $0x1, -0x11(%rbp)
cmpb $0x0, -0x11(%rbp)
je 0x15a264
movq -0x8(%rbp), %rdi
leaq 0x779a6(%rip), %rsi     # 0x1d1c03
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
imulq $0xc, 0x78(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x80(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x88(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x98(%rax)
addq $0x30, %rsp
popq %rbp
retq
nop