// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/dlmaker.c
// Function: do_read_header
static int do_read_header(deark *c, lctx *d)
{
	int retval = 0;
	i64 pos = 0;
	size_t nsize;
	de_ucstring *s = NULL;

	d->ver = de_getbyte_p(&pos);
	de_dbg(c, "version: %u", (UI)d->ver);
	if(d->ver<1 || d->ver>3) {
		de_err(c, "Not a DL file");
		goto done;
	}

	if(d->ver==1) {
		d->v12.screen_format = 1;
	}
	else if(d->ver==2) {
		d->v12.screen_format = de_getbyte_p(&pos);
		de_dbg(c, "screen format: %u", (UI)d->v12.screen_format);
	}
	else {
		pos++;
	}

	if(d->ver<=2) {
		switch(d->v12.screen_format) {
		case 0:
			d->v12.img_xsize = 320;
			d->v12.img_ysize = 200;
			break;
		case 1:
			d->v12.img_xsize = 160;
			d->v12.img_ysize = 100;
			break;
		case 2:
			d->v12.img_xsize = 80;
			d->v12.img_ysize = 50;
			break;
		default:
			de_err(c, "Invalid/unsupported DL format");
			goto done;
		}

		d->v12.imgs_per_screen = (V12_SCREEN_WIDTH/d->v12.img_xsize) *
			(V12_SCREEN_HEIGHT/d->v12.img_ysize);
		if(d->ver==2) {
			de_dbg_indent(c, 1);
			de_dbg_dimensions(c, d->v12.img_xsize, d->v12.img_ysize);
			de_dbg(c, "images/screen: %u", (UI)d->v12.imgs_per_screen);
			de_dbg_indent(c, -1);
		}
	}

	if(d->ver==3) pos += 50;

	s = ucstring_create(c);
	nsize = (d->ver==3) ? 40 : 20;
	read_name(c, d, pos, s, nsize);
	de_dbg(c, "title: \"%s\"", ucstring_getpsz_d(s));
	pos += nsize;

	if(d->ver!=1) {
		ucstring_empty(s);
		read_name(c, d, pos, s, nsize);
		de_dbg(c, "author: \"%s\"", ucstring_getpsz_d(s));
		pos += nsize;
	}

	if(d->ver==3) {
		d->num_images = de_getu16le_p(&pos);
		de_dbg(c, "num images: %u", (UI)d->num_images);
	}
	else {
		d->v12.num_screens = (i64)de_getbyte_p(&pos);
		de_dbg(c, "num screens: %u", (UI)d->v12.num_screens);
		d->num_images = d->v12.num_screens * d->v12.imgs_per_screen;
		de_dbg(c, "num images (calculated): %"I64_FMT, d->num_images);
	}

	if(d->ver==1) {
		d->num_anim_code_units = de_getu16le_p(&pos);
		d->anim_code_unit_size = 1;
	}
	else if(d->ver==3) {
		d->num_anim_code_units = de_getu16le_p(&pos);
		d->anim_code_unit_size = 2;
	}
	else {
		d->num_anim_code_units = de_getu32le_p(&pos);
		d->anim_code_unit_size = 2;
	}
	de_dbg(c, "num frames: %"I64_FMT, d->num_anim_code_units);

	if(d->ver==3) {
		d->num_audio_components = de_getu16le_p(&pos);
		de_dbg(c, "num audio components: %"I64_FMT, d->num_audio_components);
	}

	de_read_simple_palette(c, c->infile, pos, 256, 3, d->pal, 256, DE_RDPALTYPE_VGA18BIT, 0);
	pos += 256*3;
	d->hdr_size = pos;
	retval = 1;

done:
	ucstring_destroy(s);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %edx
leaq 0x13bf32(%rip), %rsi    # 0x1c2686
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jl 0x86775
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x3, %eax
jle 0x8678c
movq -0x8(%rbp), %rdi
leaq 0x128181(%rip), %rsi    # 0x1ae901
movb $0x0, %al
callq 0x15bfb0
jmp 0x86c18
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x867a3
movq -0x10(%rbp), %rax
movb $0x1, 0xc(%rax)
jmp 0x867f4
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x2, %eax
jne 0x867e6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0xc(%rax), %edx
leaq 0x128132(%rip), %rsi    # 0x1ae90f
movb $0x0, %al
callq 0x15b0d0
jmp 0x867f2
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x867f4
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x2, %eax
jg 0x8691c
movq -0x10(%rbp), %rax
movzbl 0xc(%rax), %eax
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0x8682a
jmp 0x86816
movl -0x34(%rbp), %eax
subl $0x1, %eax
je 0x86844
jmp 0x86820
movl -0x34(%rbp), %eax
subl $0x2, %eax
je 0x8685e
jmp 0x86878
movq -0x10(%rbp), %rax
movq $0x140, 0x10(%rax)      # imm = 0x140
movq -0x10(%rbp), %rax
movq $0xc8, 0x18(%rax)
jmp 0x8688f
movq -0x10(%rbp), %rax
movq $0xa0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq $0x64, 0x18(%rax)
jmp 0x8688f
movq -0x10(%rbp), %rax
movq $0x50, 0x10(%rax)
movq -0x10(%rbp), %rax
movq $0x32, 0x18(%rax)
jmp 0x8688f
movq -0x8(%rbp), %rdi
leaq 0x12809e(%rip), %rsi    # 0x1ae921
movb $0x0, %al
callq 0x15bfb0
jmp 0x86c18
movq -0x10(%rbp), %rcx
movl $0x140, %eax            # imm = 0x140
cqto
idivq 0x10(%rcx)
movq %rax, %rcx
movq -0x10(%rbp), %rsi
movl $0xc8, %eax
cqto
idivq 0x18(%rsi)
imulq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x2, %eax
jne 0x8691a
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0x12803a(%rip), %rsi    # 0x1ae93f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0x8691c
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x3, %eax
jne 0x86935
movq -0x20(%rbp), %rax
addq $0x32, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %edx
movl $0x14, %eax
movl $0x28, %ecx
cmpl $0x3, %edx
cmovel %ecx, %eax
cltq
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x86fd0
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x172650
movq -0x40(%rbp), %rdi
movq %rax, %rdx
leaq 0x12cf89(%rip), %rsi    # 0x1b3921
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
je 0x86a0c
movq -0x30(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x86fd0
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x172650
movq -0x48(%rbp), %rdi
movq %rax, %rdx
leaq 0x127f58(%rip), %rsi    # 0x1ae951
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x3, %eax
jne 0x86a53
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %edx
leaq 0x127f14(%rip), %rsi    # 0x1ae95e
movb $0x0, %al
callq 0x15b0d0
jmp 0x86ac0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0x127ee7(%rip), %rsi    # 0x1ae96d
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdx
leaq 0x127ec4(%rip), %rsi    # 0x1ae97d
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x86af7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x1, 0x50(%rax)
jmp 0x86b58
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x3, %eax
jne 0x86b2e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x2, 0x50(%rax)
jmp 0x86b56
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x2, 0x50(%rax)
jmp 0x86b58
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdx
leaq 0x127e2f(%rip), %rsi    # 0x1ae99a
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x3, %eax
jne 0x86bb5
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdx
leaq 0x127dfc(%rip), %rsi    # 0x1ae9aa
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %r9
addq $0x70, %r9
movl $0x100, %ecx            # imm = 0x100
movl $0x3, %r8d
xorl %eax, %eax
movq $0x100, (%rsp)          # imm = 0x100
movl $0x2, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16c160
movq -0x20(%rbp), %rax
addq $0x300, %rax            # imm = 0x300
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movl $0x1, -0x14(%rbp)
movq -0x30(%rbp), %rdi
callq 0x171750
movl -0x14(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)