// File: /workspace/github_cpp_makefile_star2_O2/ksherlock[P]wdc-utils/link.cpp
// Function: one_module(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>*)
bool one_module(const std::string &name, int fd, std::set<std::string> *local_undefined = nullptr) {
	Mod_head h;
	ssize_t ok;

	ok = read(fd, &h, sizeof(h));
	if (ok == 0) return false;

	if (ok < sizeof(h)) {
		warnx("Invalid object file: %s", name.c_str());
		return false;;
	}

	le_to_host(h.h_magic);
	le_to_host(h.h_version);
	le_to_host(h.h_filtyp);
	le_to_host(h.h_namlen);
	le_to_host(h.h_recsize);
	le_to_host(h.h_secsize);
	le_to_host(h.h_symsize);
	le_to_host(h.h_optsize);
	le_to_host(h.h_tot_secs);
	le_to_host(h.h_num_secs);
	le_to_host(h.h_num_syms);

	assert(h.h_magic == MOD_MAGIC);
	assert(h.h_version == 1);
	assert(h.h_filtyp == 1);


	std::string module_name;
	{
		// now read the name (h_namlen includes 0 terminator.)
		std::vector<char> tmp;
		tmp.resize(h.h_namlen);
		ok = read(fd, tmp.data(), h.h_namlen);
		if (ok != h.h_namlen) {
			warnx("Invalid object file: %s", name.c_str());
			return false;
		}
		module_name.assign(tmp.data());
	}

	std::vector<uint8_t> record_data;
	std::vector<uint8_t> symbol_data;
	std::vector<uint8_t> section_data;

	record_data.resize(h.h_recsize);
	ok = read(fd, record_data.data(), h.h_recsize);
	if (ok != h.h_recsize) {
		warnx("Truncated object file: %s", name.c_str());
		return false;
	}

	section_data.resize(h.h_secsize);
	ok = read(fd, section_data.data(), h.h_secsize);
	if (ok != h.h_secsize) {
		warnx("Truncated object file: %s", name.c_str());
		return false;
	}

	symbol_data.resize(h.h_symsize);
	ok = read(fd, symbol_data.data(), h.h_symsize);
	if (ok != h.h_symsize)  {
		warnx("Truncated object file: %s", name.c_str());
		return false;
	}

	if (flags.v) {
		printf("Processing %s:%s\n", name.c_str(), module_name.c_str());
	}

	// should probably pass in name and module....
	one_module(record_data, section_data, symbol_data, local_undefined);
	

	if (h.h_optsize) lseek(fd, h.h_optsize, SEEK_CUR);

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r14
movl %esi, %ebx
movq %rdi, %r15
leaq 0x18(%rsp), %rsi
pushq $0x18
popq %rdx
movl %ebx, %edi
callq 0x2330
testq %rax, %rax
je 0x43e8
cmpq $0x17, %rax
ja 0x43ef
movq (%r15), %rsi
leaq 0x90d5(%rip), %rdi      # 0xd4af
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2280
jmp 0x45a8
xorl %ebp, %ebp
jmp 0x45a8
cmpl $0x5a44525a, 0x18(%rsp) # imm = 0x5A44525A
jne 0x45bc
cmpw $0x1, 0x1c(%rsp)
jne 0x45db
cmpb $0x1, 0x1e(%rsp)
jne 0x45fa
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movzbl 0x1f(%rsp), %r12d
movq %r12, %rsi
callq 0x6222
movq (%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2330
movq %rax, %r13
cmpq %r12, %rax
jne 0x4467
movq (%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x2450
jmp 0x4478
movq (%r15), %rsi
leaq 0x903e(%rip), %rdi      # 0xd4af
xorl %eax, %eax
callq 0x2280
movq %rsp, %rdi
callq 0x98d4
cmpq %r12, %r13
jne 0x456b
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movl 0x20(%rsp), %r12d
movq %r12, %rsi
callq 0x624e
movq (%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2330
cmpq %r12, %rax
jne 0x456f
movzwl 0x24(%rsp), %r12d
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x624e
movq 0x30(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2330
cmpq %r12, %rax
jne 0x456f
movl 0x26(%rsp), %r12d
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x624e
movq 0x50(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2330
cmpq %r12, %rax
jne 0x456f
cmpb $0x1, 0x10d4b(%rip)     # 0x15270
jne 0x453d
movq (%r15), %rsi
movq 0x68(%rsp), %rdx
leaq 0x9026(%rip), %rdi      # 0xd55c
xorl %eax, %eax
callq 0x2050
movq %rsp, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %r14, %rcx
callq 0x2b6a
movzwl 0x2a(%rsp), %esi
movb $0x1, %bpl
testq %rsi, %rsi
je 0x4582
pushq $0x1
popq %rdx
movl %ebx, %edi
callq 0x2420
jmp 0x4582
xorl %ebp, %ebp
jmp 0x459e
movq (%r15), %rsi
xorl %ebp, %ebp
leaq 0x8fc7(%rip), %rdi      # 0xd542
xorl %eax, %eax
callq 0x2280
leaq 0x30(%rsp), %rdi
callq 0x6ac0
leaq 0x50(%rsp), %rdi
callq 0x6ac0
movq %rsp, %rdi
callq 0x6ac0
leaq 0x68(%rsp), %rdi
callq 0x2130
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8f04(%rip), %rdi      # 0xd4c7
leaq 0x8bba(%rip), %rsi      # 0xd184
leaq 0x8f0d(%rip), %rcx      # 0xd4de
movl $0x4ff, %edx            # imm = 0x4FF
callq 0x21c0
leaq 0x8f3f(%rip), %rdi      # 0xd521
leaq 0x8b9b(%rip), %rsi      # 0xd184
leaq 0x8eee(%rip), %rcx      # 0xd4de
movl $0x500, %edx            # imm = 0x500
callq 0x21c0
leaq 0x8f31(%rip), %rdi      # 0xd532
leaq 0x8b7c(%rip), %rsi      # 0xd184
leaq 0x8ecf(%rip), %rcx      # 0xd4de
movl $0x501, %edx            # imm = 0x501
callq 0x21c0
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x6ac0
leaq 0x50(%rsp), %rdi
callq 0x6ac0
movq %rsp, %rdi
callq 0x6ac0
jmp 0x4645
movq %rax, %rbx
movq %rsp, %rdi
callq 0x98d4
leaq 0x68(%rsp), %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x23c0