// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-exe.c
// Function: fmtutil_collect_exe_info
void fmtutil_collect_exe_info(deark *c, dbuf *f, struct fmtutil_exe_info *ei)
{
	i64 hdrsize; // in 16-byte units
	i64 lfb, nblocks;
	i64 maybe_ext_hdr_pos;

	ei->f = f;
	lfb = dbuf_getu16le(f, 2);
	nblocks = dbuf_getu16le(f, 4);
	nblocks &= 0x7ff;
	ei->num_relocs = dbuf_getu16le(f, 6);
	hdrsize = dbuf_getu16le(f, 8);
	ei->start_of_dos_code = hdrsize*16;
	ei->regSS = dbuf_geti16le(f, 14);
	ei->regSP = dbuf_getu16le(f, 16);
	ei->regIP = dbuf_getu16le(f, 20);
	ei->regCS = dbuf_geti16le(f, 22);
	ei->reloc_table_pos = dbuf_getu16le(f, 24);
	ei->entry_point = (hdrsize + ei->regCS)*16 + ei->regIP;
	maybe_ext_hdr_pos = dbuf_getu32le(f, 60);

	ei->end_of_dos_code = nblocks*512;
	if(lfb>=1 && lfb<=511) {
		ei->end_of_dos_code = ei->end_of_dos_code - 512 + lfb;
	}
	ei->overlay_len = f->len - ei->end_of_dos_code;
	if(ei->overlay_len<0) ei->overlay_len = 0;

	check_for_ext_fmt(c, ei, maybe_ext_hdr_pos);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %rbx
movq %rsi, %r15
movq %rsi, (%rdx)
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, %r12
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, %r13
pushq $0x6
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, 0x8(%rbx)
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, %r14
shlq $0x4, %rax
movq %rax, 0x38(%rbx)
pushq $0xe
popq %rsi
movq %r15, %rdi
callq 0xe400a
movq %rax, 0x10(%rbx)
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, 0x18(%rbx)
pushq $0x14
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, 0x28(%rbx)
pushq $0x16
popq %rsi
movq %r15, %rdi
callq 0xe400a
movq %rax, 0x20(%rbx)
pushq $0x18
popq %rsi
movq %r15, %rdi
callq 0xe3fa3
movq %rax, 0x30(%rbx)
addq 0x20(%rbx), %r14
shlq $0x4, %r14
addq 0x28(%rbx), %r14
movq %r14, 0x40(%rbx)
pushq $0x3c
popq %rsi
movq %r15, %rdi
callq 0xe409f
movq %rax, %r14
shll $0x9, %r13d
andl $0xffe00, %r13d         # imm = 0xFFE00
leaq -0x1(%r12), %rax
cmpq $0x1ff, %rax            # imm = 0x1FF
leaq -0x200(%r13,%r12), %rax
cmovaeq %r13, %rax
movq %rax, 0x48(%rbx)
movq 0x18(%r15), %rcx
subq %rax, %rcx
xorl %edx, %edx
testq %rcx, %rcx
cmovgq %rcx, %rdx
movq %rdx, 0x50(%rbx)
cmpq $0x40, %r14
jl 0xf8a1a
movq (%rbx), %rdi
cmpq %r14, 0x18(%rdi)
jle 0xf8a1a
cmpq $0x3c, 0x38(%rbx)
jg 0xf8967
cmpq $0x3c, %rax
jg 0xf8a1a
movq 0x30(%rbx), %rax
leaq -0x3d(%rax), %rcx
cmpq $0x3, %rcx
jb 0xf8a1a
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
jle 0xf8996
cmpq $0x3f, %rax
jg 0xf8996
leaq (%rax,%rcx,4), %rax
cmpq $0x3c, %rax
jg 0xf8a1a
movq %r14, %rsi
callq 0xe404f
cmpq $0x50450000, %rax       # imm = 0x50450000
jne 0xf89b0
movb $0x1, %al
movb %al, 0x60(%rbx)
movb %al, 0x62(%rbx)
jmp 0xf89de
andl $0xffff0000, %eax       # imm = 0xFFFF0000
cmpl $0x4c450000, %eax       # imm = 0x4C450000
je 0xf89d2
cmpl $0x4c580000, %eax       # imm = 0x4C580000
je 0xf89d2
cmpl $0x4e450000, %eax       # imm = 0x4E450000
jne 0xf89d8
movw $0x101, 0x60(%rbx)      # imm = 0x101
jmp 0xf89de
movb $0x1, 0x60(%rbx)
jmp 0xf89de
cmpb $0x0, 0x60(%rbx)
je 0xf8a1a
movq %r14, 0x58(%rbx)
cmpb $0x0, 0x62(%rbx)
je 0xf89fd
movq (%rbx), %rdi
addq $0x17, %r14
movq %r14, %rsi
callq 0xe3eb4
testb $0x20, %al
jne 0xf8a16
jmp 0xf8a1a
cmpb $0x0, 0x61(%rbx)
je 0xf8a1a
movq (%rbx), %rdi
addq $0xd, %r14
movq %r14, %rsi
callq 0xe3eb4
testb %al, %al
jns 0xf8a1a
movb $0x1, 0x63(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq