// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/xwd.c
// Function: interpret_header
static void interpret_header(deark *c, lctx *d)
{
	u8 need_fixup_warning = 0;
	u8 depth_1248;
	UI k;

	d->cmap_pos = (i64)d->hf[HF_HSIZE];
	d->width = (i64)d->hf[HF_WIDTH];
	d->height = (i64)d->hf[HF_HEIGHT];
	d->pixel_byte_order = !d->hf[HF_BYTE_ORDER];
	d->nplanes = 1; // tentative
	d->rowspan = (i64)d->hf[HF_BYTES_PER_LINE];

	depth_1248 = (d->hf[HF_DEPTH]==8 || d->hf[HF_DEPTH]==4 ||
		d->hf[HF_DEPTH]==2 || d->hf[HF_DEPTH]==1);

	// paletted or grayscale, planar
	if(d->imgtype==0 &&
		(d->vclass_adj==0 || d->vclass_adj==2) &&
		d->hf[HF_PIXFMT]==1 &&
		d->hf[HF_BITS_PER_PIX]==1 &&
		(d->hf[HF_DEPTH]>=1 && d->hf[HF_DEPTH]<=8))
	{
		d->imgtype = XWD_IMGTYPE_PAL_OR_GRAY;
		d->bits_per_pixel = (i64)d->hf[HF_DEPTH];
		d->nplanes = (i64)d->hf[HF_DEPTH];
	}

	// paletted or grayscale, typical
	if(d->imgtype==0 &&
		(d->vclass_adj==0 || d->vclass_adj==2) &&
		depth_1248 &&
		d->hf[HF_BITS_PER_PIX]==d->hf[HF_DEPTH])
	{
		d->imgtype = XWD_IMGTYPE_PAL_OR_GRAY;
		d->bits_per_pixel = (i64)d->hf[HF_BITS_PER_PIX];
	}

	// paletted or grayscale, with unused bits (?)
	if(d->imgtype==0 && (d->vclass_adj==0 || d->vclass_adj==2) &&
		depth_1248 &&
		d->hf[HF_BITS_PER_PIX]>d->hf[HF_DEPTH] &&
		d->hf[HF_BITS_PER_PIX]==8)
	{
		d->imgtype = XWD_IMGTYPE_PAL_OR_GRAY;
		d->bits_per_pixel = (i64)d->hf[HF_BITS_PER_PIX];
	}

	// RGB 24 or 32 bits/pixel
	if(d->imgtype==0 && d->vclass_adj==4 &&
		(d->hf[HF_BITS_PER_PIX]==24 || d->hf[HF_BITS_PER_PIX]==32) &&
		(d->hf[HF_DEPTH]==24 || d->hf[HF_DEPTH]==32))
	{
		d->imgtype = XWD_IMGTYPE_RGB;
		// HF_BITS_PER_PIX is usually correct. We'll change it later if
		// it seems wrong.
		d->bits_per_pixel = (i64)d->hf[HF_BITS_PER_PIX];
		d->bytes_per_pixel = d->bits_per_pixel/8;
	}

	// RGB 16bits/pixel
	if(d->imgtype==0 && d->vclass_adj==4 &&
		d->hf[HF_BITS_PER_PIX]==16 &&
		d->hf[HF_DEPTH]==16)
	{
		d->imgtype = XWD_IMGTYPE_RGB;
		d->bytes_per_pixel = 2;
	}

	// e.g. "MARBLES.XWD"
	if(d->imgtype==XWD_IMGTYPE_RGB &&
		d->bytes_per_pixel==3 &&
		d->hf[HF_SCANLINE_PAD]==8 &&
		d->hf[HF_WIDTH]*4 == d->hf[HF_BYTES_PER_LINE])
	{
		d->bytes_per_pixel = 4;
		d->bits_per_pixel = 0;
		d->imgtype = XWD_IMGTYPE_RGB;
		need_fixup_warning = 1;
	}

	// Decode masks if needed
	if(d->imgtype==XWD_IMGTYPE_RGB) {
		for(k=0; k<3; k++) {
			decode_bitfield(d->hf[HF_RMASK+k], &d->sample_bit_shift[k],
				&d->sample_maxval[k]);
		}
	}

	if(need_fixup_warning) {
		de_warn(c, "Inconsistent or unusual image parameters. Attempting to correct.");
	}

	d->expected_image_size = d->height * d->rowspan * d->nplanes;

	if(d->bits_per_pixel==0) {
		d->bits_per_pixel = 8*d->bytes_per_pixel;
	}
	if(d->imgtype!=0) {
		de_dbg(c, "interpreted bits/pixel: %d", (int)d->bits_per_pixel);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq $0x1, 0xd0(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xc8(%rax)
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpl $0x8, 0x10(%rcx)
movb %al, -0x19(%rbp)
je 0x159dc7
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpl $0x4, 0x10(%rcx)
movb %al, -0x19(%rbp)
je 0x159dc7
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpl $0x2, 0x10(%rcx)
movb %al, -0x19(%rbp)
je 0x159dc7
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
sete %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb %al, -0x12(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xdc(%rax)
jne 0x159e51
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0x159df3
movq -0x10(%rbp), %rax
cmpl $0x2, 0x68(%rax)
jne 0x159e51
movq -0x10(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x159e51
movq -0x10(%rbp), %rax
cmpl $0x1, 0x30(%rax)
jne 0x159e51
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jb 0x159e51
movq -0x10(%rbp), %rax
cmpl $0x8, 0x10(%rax)
ja 0x159e51
movq -0x10(%rbp), %rax
movl $0x1, 0xdc(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xd0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xdc(%rax)
jne 0x159ead
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0x159e72
movq -0x10(%rbp), %rax
cmpl $0x2, 0x68(%rax)
jne 0x159ead
movzbl -0x12(%rbp), %eax
cmpl $0x0, %eax
je 0x159ead
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x159ead
movq -0x10(%rbp), %rax
movl $0x1, 0xdc(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xdc(%rax)
jne 0x159f13
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0x159ece
movq -0x10(%rbp), %rax
cmpl $0x2, 0x68(%rax)
jne 0x159f13
movzbl -0x12(%rbp), %eax
cmpl $0x0, %eax
je 0x159f13
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jbe 0x159f13
movq -0x10(%rbp), %rax
cmpl $0x8, 0x30(%rax)
jne 0x159f13
movq -0x10(%rbp), %rax
movl $0x1, 0xdc(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xdc(%rax)
jne 0x159f97
movq -0x10(%rbp), %rax
cmpl $0x4, 0x68(%rax)
jne 0x159f97
movq -0x10(%rbp), %rax
cmpl $0x18, 0x30(%rax)
je 0x159f3e
movq -0x10(%rbp), %rax
cmpl $0x20, 0x30(%rax)
jne 0x159f97
movq -0x10(%rbp), %rax
cmpl $0x18, 0x10(%rax)
je 0x159f52
movq -0x10(%rbp), %rax
cmpl $0x20, 0x10(%rax)
jne 0x159f97
movq -0x10(%rbp), %rax
movl $0x3, 0xdc(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xdc(%rax)
jne 0x159fdf
movq -0x10(%rbp), %rax
cmpl $0x4, 0x68(%rax)
jne 0x159fdf
movq -0x10(%rbp), %rax
cmpl $0x10, 0x30(%rax)
jne 0x159fdf
movq -0x10(%rbp), %rax
cmpl $0x10, 0x10(%rax)
jne 0x159fdf
movq -0x10(%rbp), %rax
movl $0x3, 0xdc(%rax)
movq -0x10(%rbp), %rax
movq $0x2, 0xa8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x3, 0xdc(%rax)
jne 0x15a047
movq -0x10(%rbp), %rax
cmpq $0x3, 0xa8(%rax)
jne 0x15a047
movq -0x10(%rbp), %rax
cmpl $0x8, 0x2c(%rax)
jne 0x15a047
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
shll $0x2, %eax
movq -0x10(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jne 0x15a047
movq -0x10(%rbp), %rax
movq $0x4, 0xa8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0xb0(%rax)
movq -0x10(%rbp), %rax
movl $0x3, 0xdc(%rax)
movb $0x1, -0x11(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0xdc(%rax)
jne 0x15a0ad
movl $0x0, -0x18(%rbp)
cmpl $0x3, -0x18(%rbp)
jae 0x15a0ab
movq -0x10(%rbp), %rax
movl -0x18(%rbp), %ecx
addl $0xe, %ecx
movl %ecx, %ecx
movl 0x4(%rax,%rcx,4), %edi
movq -0x10(%rbp), %rsi
addq $0xe0, %rsi
movl -0x18(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x10(%rbp), %rdx
addq $0xf0, %rdx
movl -0x18(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdx
callq 0x15a2c0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x15a05b
jmp 0x15a0ad
cmpb $0x0, -0x11(%rbp)
je 0x15a0c5
movq -0x8(%rbp), %rdi
leaq 0x77ae9(%rip), %rsi     # 0x1d1ba7
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0xc8(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0xd0(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
jne 0x15a11b
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rcx
shlq $0x3, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xdc(%rax)
je 0x15a147
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rax
movl %eax, %edx
leaq 0x77aa8(%rip), %rsi     # 0x1d1be8
movb $0x0, %al
callq 0x15b0d0
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)