// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/makichan.c
// Function: read_mag_header
static int read_mag_header(deark *c, lctx *d)
{
	i64 xoffset, yoffset;
	i64 width_raw, height_raw;
	i64 pos;
	u8 model_code;
	u8 model_flags;
	u8 screen_mode;
	u8 colors_code;
	int retval = 0;

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

	pos = d->header_pos;

	model_code = de_getbyte(pos+1);
	model_flags = de_getbyte(pos+2);
	de_dbg(c, "model code: 0x%02x, flags: 0x%02x",
		(unsigned int)model_code, (unsigned int)model_flags);
	if(model_code==0x03) {
		switch(model_flags & 0xf8) {
		case 0x00: case 0x10: case 0x50:
			break;
		case 0x20: case 0x40: case 0x60:
			d->is_max = 1;
			if(d->opt_allowbad) break;
			de_err(c, "This looks like MAX format, which is not supported.");
			goto done;
		default:
			de_err(c, "Unknown image type");
			goto done;
		}
	}

	screen_mode = de_getbyte(pos+3);
	de_dbg(c, "screen mode: %d", (int)screen_mode);
	de_dbg_indent(c, 1);
	d->aspect_ratio_flag = screen_mode&0x01;
	colors_code = screen_mode&0x82;
	if(colors_code==0x00) {
		d->num_colors = 16;
		d->bits_per_pixel = 4;
	}
	else if(colors_code==0x80) {
		d->num_colors = 256;
		d->bits_per_pixel = 8;
	}
	else if(colors_code==0x02) {
		d->num_colors = 8;
		// TODO: Support 8 color images
	}
	de_dbg(c, "number of colors: %d", (int)d->num_colors);
	de_dbg_indent(c, -1);

	xoffset = de_getu16le(pos+4);
	yoffset = de_getu16le(pos+6);
	de_dbg(c, "image offset: (%d,%d)", (int)xoffset, (int)yoffset);

	width_raw = de_getu16le(pos+8);
	height_raw = de_getu16le(pos+10);
	d->width = width_raw - xoffset + 1;
	d->height = height_raw - yoffset + 1;
	de_dbg_dimensions(c, d->width, d->height);

	d->flag_a_offset = de_getu32le(pos+12);
	d->flag_a_offset += d->header_pos;
	de_dbg(c, "flag A offset: %d", (int)d->flag_a_offset);

	d->flag_b_offset = de_getu32le(pos+16);
	d->flag_b_offset += d->header_pos;
	d->flag_b_size = de_getu32le(pos+20);
	de_dbg(c, "flag B offset: %d, size=%d", (int)d->flag_b_offset, (int)d->flag_b_size);

	d->pixels_offset = de_getu32le(pos+24);
	d->pixels_offset += d->header_pos;
	d->pixels_size = de_getu32le(pos+28);
	de_dbg(c, "pixels offset: %d, size=%d", (int)d->pixels_offset, (int)d->pixels_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 $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0xea092(%rip), %rsi     # 0x1c4f8e
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 $0x1, %rsi
callq 0x165060
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x2, %rsi
callq 0x165060
movb %al, -0x3a(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x39(%rbp), %edx
movzbl -0x3a(%rbp), %ecx
leaq 0xe0ba5(%rip), %rsi     # 0x1bbb05
movb $0x0, %al
callq 0x15b0d0
movzbl -0x39(%rbp), %eax
cmpl $0x3, %eax
jne 0xdaffe
movzbl -0x3a(%rbp), %eax
andl $0xf8, %eax
movl %eax, -0x44(%rbp)
je 0xdafb6
jmp 0xdaf84
movl -0x44(%rbp), %eax
subl $0x10, %eax
je 0xdafb6
jmp 0xdaf8e
movl -0x44(%rbp), %eax
subl $0x20, %eax
je 0xdafb8
jmp 0xdaf98
movl -0x44(%rbp), %eax
subl $0x40, %eax
je 0xdafb8
jmp 0xdafa2
movl -0x44(%rbp), %eax
subl $0x50, %eax
je 0xdafb6
jmp 0xdafac
movl -0x44(%rbp), %eax
subl $0x60, %eax
je 0xdafb8
jmp 0xdafe5
jmp 0xdaffc
movq -0x10(%rbp), %rax
movl $0x1, 0x74(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0xdafce
jmp 0xdaffc
movq -0x8(%rbp), %rdi
leaq 0xe0b4e(%rip), %rsi     # 0x1bbb27
movb $0x0, %al
callq 0x15bfb0
jmp 0xdb319
movq -0x8(%rbp), %rdi
leaq 0xe0b6b(%rip), %rsi     # 0x1bbb5b
movb $0x0, %al
callq 0x15bfb0
jmp 0xdb319
jmp 0xdaffe
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x3, %rsi
callq 0x165060
movb %al, -0x3b(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x3b(%rbp), %edx
leaq 0xe0b49(%rip), %rsi     # 0x1bbb6e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movzbl -0x3b(%rbp), %eax
andl $0x1, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x70(%rax)
movzbl -0x3b(%rbp), %eax
andl $0x82, %eax
movb %al, -0x3c(%rbp)
movzbl -0x3c(%rbp), %eax
cmpl $0x0, %eax
jne 0xdb079
movq -0x10(%rbp), %rax
movq $0x10, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x4, 0x50(%rax)
jmp 0xdb0b7
movzbl -0x3c(%rbp), %eax
cmpl $0x80, %eax
jne 0xdb09e
movq -0x10(%rbp), %rax
movq $0x100, 0x48(%rax)      # imm = 0x100
movq -0x10(%rbp), %rax
movq $0x8, 0x50(%rax)
jmp 0xdb0b5
movzbl -0x3c(%rbp), %eax
cmpl $0x2, %eax
jne 0xdb0b3
movq -0x10(%rbp), %rax
movq $0x8, 0x48(%rax)
jmp 0xdb0b5
jmp 0xdb0b7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %edx
leaq 0xc2795(%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 $0x4, %rsi
callq 0x165280
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x6, %rsi
callq 0x165280
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
leaq 0xe082e(%rip), %rsi     # 0x1bb958
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x8, %rsi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0xa, %rsi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
subq -0x18(%rbp), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
subq -0x20(%rbp), %rcx
addq $0x1, %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 -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0xc, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x20(%rax), %rcx
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0xe0991(%rip), %rsi     # 0x1bbb7e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x28(%rax), %rcx
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x14, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
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 0xe0929(%rip), %rsi     # 0x1bbb90
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x18, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x38(%rax), %rcx
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x1c, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
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 0xe08ca(%rip), %rsi     # 0x1bbbab
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x4, 0x50(%rax)
je 0xdb312
movq -0x10(%rbp), %rax
cmpq $0x8, 0x50(%rax)
je 0xdb312
movq -0x8(%rbp), %rdi
leaq 0xe079d(%rip), %rsi     # 0x1bbaa6
movb $0x0, %al
callq 0x15bfb0
jmp 0xdb319
movl $0x1, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x40(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq