// File: /workspace/github_c_makefile_star2_O3/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)
movl $0x2, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, %r12
movl $0x4, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, %r13
movl $0x6, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, 0x8(%rbx)
movl $0x8, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, %r14
shlq $0x4, %rax
movq %rax, 0x38(%rbx)
movl $0xe, %esi
movq %r15, %rdi
callq 0xeec55
movq %rax, 0x10(%rbx)
movl $0x10, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, 0x18(%rbx)
movl $0x14, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, 0x28(%rbx)
movl $0x16, %esi
movq %r15, %rdi
callq 0xeec55
movq %rax, 0x20(%rbx)
movl $0x18, %esi
movq %r15, %rdi
callq 0xeebc8
movq %rax, 0x30(%rbx)
addq 0x20(%rbx), %r14
shlq $0x4, %r14
addq 0x28(%rbx), %r14
movq %r14, 0x40(%rbx)
movl $0x3c, %esi
movq %r15, %rdi
callq 0xeed61
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 0x10517b
movq (%rbx), %rdi
cmpq %r14, 0x18(%rdi)
jle 0x10517b
cmpq $0x3c, 0x38(%rbx)
jg 0x1050c8
cmpq $0x3c, %rax
jg 0x10517b
movq 0x30(%rbx), %rax
leaq -0x3d(%rax), %rcx
cmpq $0x3, %rcx
jb 0x10517b
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
jle 0x1050f7
cmpq $0x3f, %rax
jg 0x1050f7
leaq (%rax,%rcx,4), %rax
cmpq $0x3c, %rax
jg 0x10517b
movq %r14, %rsi
callq 0xeed0d
cmpq $0x50450000, %rax       # imm = 0x50450000
jne 0x105111
movb $0x1, %al
movb %al, 0x60(%rbx)
movb %al, 0x62(%rbx)
jmp 0x10513f
andl $0xffff0000, %eax       # imm = 0xFFFF0000
cmpl $0x4c450000, %eax       # imm = 0x4C450000
je 0x105133
cmpl $0x4c580000, %eax       # imm = 0x4C580000
je 0x105133
cmpl $0x4e450000, %eax       # imm = 0x4E450000
jne 0x105139
movw $0x101, 0x60(%rbx)      # imm = 0x101
jmp 0x10513f
movb $0x1, 0x60(%rbx)
jmp 0x10513f
cmpb $0x0, 0x60(%rbx)
je 0x10517b
movq %r14, 0x58(%rbx)
cmpb $0x0, 0x62(%rbx)
je 0x10515e
movq (%rbx), %rdi
addq $0x17, %r14
movq %r14, %rsi
callq 0xeead3
testb $0x20, %al
jne 0x105177
jmp 0x10517b
cmpb $0x0, 0x61(%rbx)
je 0x10517b
movq (%rbx), %rdi
addq $0xd, %r14
movq %r14, %rsi
callq 0xeead3
testb %al, %al
jns 0x10517b
movb $0x1, 0x63(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq