// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/diet.c
// Function: read_header
static void read_header(deark *c, lctx *d)
{
	i64 pos = 0;
	i64 n;
	u8 x;
	de_ucstring *flags_str = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	flags_str = ucstring_create(c);

	if(d->idd.dlz_pos_known) {
		de_dbg(c, "header at %"I64_FMT, d->idd.dlz_pos);
		de_dbg_indent(c, 1);

		pos = d->idd.dlz_pos + 3;
		x = de_getbyte_p(&pos);
		d->hdr_flags1 = x & 0xf0;
		if(d->hdr_flags1 & 0x80) ucstring_append_flags_item(flags_str, "has following block");
		if(d->hdr_flags1 & 0x20) ucstring_append_flags_item(flags_str, "new EXE format");
		if(d->hdr_flags1 & 0x10) ucstring_append_flags_item(flags_str, "has segment refresh data");
		de_dbg(c, "flags: 0x%02x (%s)", d->hdr_flags1, ucstring_getpsz(flags_str));
		d->cmpr_len = (i64)(x & 0x0f)<<16;
		n = de_getu16le_p(&pos);
		d->cmpr_len |= n;
		de_dbg(c, "cmpr len: %"I64_FMT, d->cmpr_len);
		d->cmpr_len_known = 1;
	}
	else if(d->idd.fmt==FMT_EXE_100) {
		d->cmpr_len = de_getu32le(32);
		d->cmpr_len &= 0xfffff; // Dunno if this is 24-bits, or maybe just 20 bits
		de_dbg(c, "cmpr len: %"I64_FMT, d->cmpr_len);
		d->cmpr_len_known = 1;
	}

	if(d->idd.crc_pos_known) {
		d->crc_reported = (u32)de_getu16le(d->idd.crc_pos);
		de_dbg(c, "crc (reported): 0x%04x", (UI)d->crc_reported);
	}

	if(d->idd.dlz_pos_known) {
		pos = d->idd.dlz_pos + 8;
		x = de_getbyte_p(&pos);
		d->orig_len = (i64)(x & 0xfc)<<14;
		d->hdr_flags2 = (i64)(x & 0x03); // probably unused
		n = de_getu16le_p(&pos);
		d->orig_len |= n;
		de_dbg(c, "orig len: %"I64_FMT, d->orig_len);
		d->orig_len_known = 1;
	}

	if(!d->cmpr_len_known && d->idd.fmt==FMT_DATA_100) {
		d->cmpr_len = c->infile->len - d->cmpr_pos;
		d->cmpr_len_known = 1;
	}

	ucstring_destroy(flags_str);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x6a06b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x144e38(%rip), %rsi    # 0x1aed63
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq $0x3, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x165170
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
andl $0xf0, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x2d(%rax)
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x69f98
movq -0x30(%rbp), %rdi
leaq 0x1401fd(%rip), %rsi    # 0x1aa190
callq 0x172680
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x69fb8
movq -0x30(%rbp), %rdi
leaq 0x1401f1(%rip), %rsi    # 0x1aa1a4
callq 0x172680
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x69fd8
movq -0x30(%rbp), %rdi
leaq 0x1401e0(%rip), %rsi    # 0x1aa1b3
callq 0x172680
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x172500
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %edx
movq %rax, %rcx
leaq 0x14fde1(%rip), %rsi    # 0x1b9de6
movb $0x0, %al
callq 0x15b0d0
movzbl -0x21(%rbp), %eax
andl $0xf, %eax
movslq %eax, %rcx
shlq $0x10, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
orq 0x30(%rax), %rcx
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x16c607(%rip), %rsi    # 0x1d6661
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movb $0x1, 0x2a(%rax)
jmp 0x6a0c8
movq -0x10(%rbp), %rax
cmpl $0x7, (%rax)
jne 0x6a0c6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x20, %esi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
andq $0xfffff, %rcx          # imm = 0xFFFFF
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x16c5aa(%rip), %rsi    # 0x1d6661
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movb $0x1, 0x2a(%rax)
jmp 0x6a0c8
movq -0x10(%rbp), %rax
cmpb $0x0, 0x9(%rax)
je 0x6a109
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x165280
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x48(%rax), %edx
leaq 0x16684a(%rip), %rsi    # 0x1d094c
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x6a1ac
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x165170
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
andl $0xfc, %eax
movslq %eax, %rcx
shlq $0xe, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movzbl -0x21(%rbp), %eax
andl $0x3, %eax
cltq
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x2e(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
orq 0x38(%rax), %rcx
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0x136dfa(%rip), %rsi    # 0x1a0f97
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movb $0x1, 0x2b(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2a(%rax)
jne 0x6a1e3
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x6a1e3
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x40(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x2a(%rax)
movq -0x30(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
addq $0x40, %rsp
popq %rbp
retq
nop