// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/makichan.c
// Function: read_mki_header
static int read_mki_header(deark *c, lctx *d)
{
	i64 xoffset, yoffset;
	i64 width_raw, height_raw;
	i64 pos;
	i64 flag_a_size;
	i64 pix_data_a_size;
	i64 pix_data_b_size;
	i64 expected_file_size;
	unsigned int extension_flags;
	int retval = 0;

	de_dbg(c, "MKI header at %d", (int)d->header_pos);
	de_dbg_indent(c, 1);

	pos = d->header_pos;

	d->flag_b_size = de_getu16be(pos+0);
	pix_data_a_size = de_getu16be(pos+2);
	pix_data_b_size = de_getu16be(pos+4);
	d->pixels_size = pix_data_a_size + pix_data_b_size;

	extension_flags = (unsigned int)de_getu16be(pos+6);
	de_dbg(c, "extension flags: 0x%04x", extension_flags);
	de_dbg_indent(c, 1);
	d->aspect_ratio_flag = extension_flags&0x0001;
	if(extension_flags&0x0002) {
		d->num_colors = 8;
	}
	else {
		d->num_colors = 16;
		d->bits_per_pixel = 4;
	}
	de_dbg(c, "number of colors: %d", (int)d->num_colors);
	de_dbg_indent(c, -1);

	xoffset = de_getu16be(pos+8);
	yoffset = de_getu16be(pos+10);
	de_dbg(c, "image offset: (%d,%d)", (int)xoffset, (int)yoffset);

	width_raw = de_getu16be(pos+12);
	d->width = width_raw - xoffset;
	height_raw = de_getu16be(pos+14);
	d->height = height_raw - yoffset;
	de_dbg_dimensions(c, d->width, d->height);
	if(d->width%64 != 0) {
		de_warn(c, "Width is not a multiple of 64. This image may not be handled correctly.");
	}
	d->width_adj = de_int_round_up(d->width, 64);
	if(d->height%4 != 0) {
		de_warn(c, "Height is not a multiple of 4. This image may not be handled correctly.");
	}
	d->height_adj = de_int_round_up(d->height, 4);

	d->flag_a_offset = pos + 16 + 48;
	// The documentation seems to say that flag A is *always* 1000 bytes, regardless of
	// how many bytes would actually be needed.
	// This would imply that a MAKI image can't have more than 256000 pixels.
	flag_a_size = 1000;
	//flag_a_size = (d->width_adj*d->height_adj)/256;

	d->flag_b_offset = d->flag_a_offset + flag_a_size;
	d->pixels_offset = d->flag_b_offset + d->flag_b_size;
	expected_file_size = d->pixels_offset + d->pixels_size;
	de_dbg(c, "flag A offset=%d, size=%d", (int)d->flag_a_offset, (int)flag_a_size);
	de_dbg(c, "flag B calculated_offset=%d, size=%d", (int)d->flag_b_offset, (int)d->flag_b_size);
	de_dbg(c, "pix data size_A=%d, size_B=%d", (int)pix_data_a_size, (int)pix_data_b_size);
	de_dbg(c, "pix data calculated_offset=%d, calculated_size=%d", (int)d->pixels_offset, (int)d->pixels_size);
	de_dbg(c, "calculated file size: %d", (int)expected_file_size);

	if(d->bits_per_pixel!=4 && d->bits_per_pixel!=8) {
		de_err(c, "Unsupported or unknown bits/pixel");
		goto done;
	}

	retval = 1;

done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0xe159b(%rip), %rsi     # 0x1bb947
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x0, %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x2, %rsi
callq 0x1651d0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x4, %rsi
callq 0x1651d0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rcx
addq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x6, %rsi
callq 0x1651d0
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %edx
leaq 0xe280c(%rip), %rsi     # 0x1bcc61
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movl -0x5c(%rbp), %eax
andl $0x1, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x70(%rax)
movl -0x5c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xda492
movq -0x10(%rbp), %rax
movq $0x8, 0x48(%rax)
jmp 0xda4aa
movq -0x10(%rbp), %rax
movq $0x10, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x4, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
leaq 0xc33a2(%rip), %rsi     # 0x19d861
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x8, %rsi
callq 0x1651d0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0xa, %rsi
callq 0x1651d0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
leaq 0xe143b(%rip), %rsi     # 0x1bb958
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0xc, %rsi
callq 0x1651d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0xe, %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x15bba0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x40, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0xda5b9
movq -0x8(%rbp), %rdi
leaq 0xe13bc(%rip), %rsi     # 0x1bb96e
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x40, %esi
callq 0xdadd0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0xda600
movq -0x8(%rbp), %rdi
leaq 0xe13bd(%rip), %rsi     # 0x1bb9b6
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x4, %esi
callq 0xdadd0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x38(%rbp), %rcx
addq $0x10, %rcx
addq $0x30, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq $0x3e8, -0x40(%rbp)     # imm = 0x3E8
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x40(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
movq -0x40(%rbp), %rax
movl %eax, %ecx
leaq 0xe136a(%rip), %rsi     # 0x1bb9fe
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, %ecx
leaq 0xe135e(%rip), %rsi     # 0x1bba18
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
movq -0x50(%rbp), %rax
movl %eax, %ecx
leaq 0xe1365(%rip), %rsi     # 0x1bba3d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %ecx
leaq 0xe135d(%rip), %rsi     # 0x1bba5b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
leaq 0xe1377(%rip), %rsi     # 0x1bba8d
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x4, 0x50(%rax)
je 0xda747
movq -0x10(%rbp), %rax
cmpq $0x8, 0x50(%rax)
je 0xda747
movq -0x8(%rbp), %rdi
leaq 0xe1368(%rip), %rsi     # 0x1bbaa6
movb $0x0, %al
callq 0x15bfb0
jmp 0xda74e
movl $0x1, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x60(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)