// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/exe.c
// Function: do_mz_fileheader
static int do_mz_fileheader(deark *c, lctx *d)
{
	i64 n;
	i64 lfb_1, lfb;
	i64 nblocks_1, nblocks;
	i64 pos1 = 0;
	i64 pos = pos1;
	i64 regCS, regIP;
	int retval = 0;
	int saved_indent_level;
	struct de_fourcc sig;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "DOS file header at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	dbuf_read_fourcc(c->infile, pos, &sig, 2, 0x0);
	de_dbg(c, "signature: '%s'", sig.id_dbgstr);
	if(sig.id!=0x4d5aU && sig.id!=0x5a4dU) {
		de_err(c, "Not an EXE file");
		goto done;
	}
	pos += 2;
	retval = 1;

	lfb_1 = de_getu16le_p(&pos);
	lfb = lfb_1 & 511;
	if(lfb_1>=512) {
		de_warn(c, "Bad 'length of final block' field (0x%x, truncating to 0x%x)",
			(UI)lfb_1, (UI)lfb);
	}
	de_dbg(c, "length of final block: %u%s", (UI)lfb, ((lfb==0)?" (=512)":""));

	nblocks_1 = de_getu16le_p(&pos);
	nblocks = nblocks_1;
	if(nblocks_1 > 0x7ff) {
		// In case of overflow, do what DOSBox does
		// (src/dos/dos_execute.cpp, head.pages&=0x07ff).
		nblocks = nblocks_1 & 0x7ff;
		de_warn(c, "Bad 'number of blocks' field (0x%x, truncating to 0x%x)",
			(UI)nblocks_1, (UI)nblocks);
	}
	de_dbg(c, "num blocks: %u", (UI)nblocks);

	d->end_of_dos_code = nblocks*512;
	if(lfb) {
		d->end_of_dos_code = d->end_of_dos_code - 512 + lfb;
	}
	if(d->end_of_dos_code > c->infile->len) {
		de_warn(c, "Reported file size (%"I64_FMT") is larger than actual "
			"size (%"I64_FMT")", d->end_of_dos_code, c->infile->len);
	}

	d->num_relocs = de_getu16le_p(&pos);
	de_dbg(c, "num reloc table entries: %u", (UI)d->num_relocs);
	n = de_getu16le_p(&pos);
	d->file_hdr_size = n*16;
	de_dbg(c, "file hdr size: %u ("DE_CHAR_TIMES"16=%"I64_FMT")", (UI)n, d->file_hdr_size);

	n = de_getu16le_p(&pos);
	de_dbg(c, "min mem: %u ("DE_CHAR_TIMES"16=%"I64_FMT")", (UI)n, (i64)n*16);
	n = de_getu16le_p(&pos);
	de_dbg(c, "max mem: %u ("DE_CHAR_TIMES"16=%"I64_FMT")", (UI)n, (i64)n*16);

	n = de_geti16le_p(&pos);
	de_dbg(c, "regSS: %d", (int)n);
	n = de_getu16le_p(&pos);
	de_dbg(c, "regSP: %u", (UI)n);

	d->checksum_reported = (UI)de_getu16le_p(&pos);
	de_dbg(c, "checksum (reported): 0x%04x", d->checksum_reported);
	if(d->check_checksum) {
		do_checksum(c, d);
	}

	regIP = de_getu16le_p(&pos);
	de_dbg(c, "regIP: %u", (UI)regIP);
	regCS = de_geti16le_p(&pos);
	de_dbg(c, "regCS: %d ("DE_CHAR_TIMES"16=%d)", (int)regCS, (int)(16*regCS));

	d->reloc_tbl_offset = de_getu16le_p(&pos);
	de_dbg(c, "reloc table offset: %"I64_FMT"%s", d->reloc_tbl_offset,
		(d->num_relocs==0)?" (no entries)":"");

	n = de_getu16le_p(&pos);
	de_dbg(c, "overlay indicator: %u", (UI)n);

	de_dbg(c, "DOS executable code: start=%"I64_FMT", len=%"I64_FMT", end=%"I64_FMT"",
		d->file_hdr_size, d->end_of_dos_code-d->file_hdr_size, d->end_of_dos_code);
	de_dbg(c, "DOS entry point: %"I64_FMT, d->file_hdr_size + 16*regCS + regIP);
	if(d->end_of_dos_code < c->infile->len) {
		de_dbg(c, "bytes after DOS executable code: %"I64_FMT, c->infile->len - d->end_of_dos_code);
	}

	de_dbg_indent(c, -1);

done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x148496(%rip), %rsi    # 0x1a82bc
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rdi
leaq -0x90(%rbp), %rdx
addq $0x10, %rdx
leaq 0x148462(%rip), %rsi    # 0x1a82d3
movb $0x0, %al
callq 0x15b0d0
cmpl $0x4d5a, -0x8c(%rbp)    # imm = 0x4D5A
je 0x5fea7
cmpl $0x5a4d, -0x8c(%rbp)    # imm = 0x5A4D
je 0x5fea7
movq -0x8(%rbp), %rdi
leaq 0x148448(%rip), %rsi    # 0x1a82e3
movb $0x0, %al
callq 0x15bfb0
jmp 0x60325
movq -0x48(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x48(%rbp)
movl $0x1, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
andq $0x1ff, %rax            # imm = 0x1FF
movq %rax, -0x28(%rbp)
cmpq $0x200, -0x20(%rbp)     # imm = 0x200
jl 0x5ff05
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl %eax, %ecx
leaq 0x1483f5(%rip), %rsi    # 0x1a82f3
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rsi
leaq 0x170cb6(%rip), %rcx    # 0x1d0bd0
leaq 0x14842b(%rip), %rax    # 0x1a834c
cmpq $0x0, %rsi
cmoveq %rax, %rcx
leaq 0x148400(%rip), %rsi    # 0x1a8330
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x7ff, -0x30(%rbp)     # imm = 0x7FF
jle 0x5ff8a
movq -0x30(%rbp), %rax
andq $0x7ff, %rax            # imm = 0x7FF
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0x1483d1(%rip), %rsi    # 0x1a8354
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x1483f1(%rip), %rsi    # 0x1a838c
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rcx
shlq $0x9, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
cmpq $0x0, -0x28(%rbp)
je 0x5ffd4
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
subq $0x200, %rcx            # imm = 0x200
addq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x60010
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
leaq 0x148392(%rip), %rsi    # 0x1a839b
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0x148394(%rip), %rsi    # 0x1a83d5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rcx
shlq $0x4, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
leaq 0x14836b(%rip), %rsi    # 0x1a83f1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rcx
shlq $0x4, %rcx
leaq 0x148353(%rip), %rsi    # 0x1a840e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x18(%rbp), %rcx
shlq $0x4, %rcx
leaq 0x148335(%rip), %rsi    # 0x1a8425
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1653f0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x14831f(%rip), %rsi    # 0x1a843c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x1482fc(%rip), %rsi    # 0x1a8446
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %edx
leaq 0x176663(%rip), %rsi    # 0x1d67e0
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x5(%rax)
je 0x6019b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x607b0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
leaq 0x14828f(%rip), %rsi    # 0x1a8450
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1653f0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
movq -0x50(%rbp), %rax
shlq $0x4, %rax
movl %eax, %ecx
leaq 0x148262(%rip), %rsi    # 0x1a845a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
leaq 0x17099a(%rip), %rcx    # 0x1d0bd0
leaq 0x14824b(%rip), %rax    # 0x1a8488
cmpq $0x0, %rsi
cmoveq %rax, %rcx
leaq 0x148222(%rip), %rsi    # 0x1a846e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x14821d(%rip), %rsi    # 0x1a8496
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %r8
leaq 0x148201(%rip), %rsi    # 0x1a84ac
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x50(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
addq -0x58(%rbp), %rdx
leaq 0x148209(%rip), %rsi    # 0x1a84dd
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x60317
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0x30(%rax), %rdx
leaq 0x1481e2(%rip), %rsi    # 0x1a84f2
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movl -0x60(%rbp), %esi
callq 0x15b5a0
movl -0x5c(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)