// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdx, %rbp
movl %esi, %ebx
movq %rdi, %r15
leaq 0x38(%rsp), %rsi
movl $0x18, %edx
movl %ebx, %edi
callq 0x2320
testq %rax, %rax
je 0x4bc8
cmpq $0x17, %rax
ja 0x4bcf
movq (%r15), %rsi
leaq 0xa83d(%rip), %rdi      # 0xf3f7
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2290
jmp 0x4e0d
xorl %ebp, %ebp
jmp 0x4e0d
cmpl $0x5a44525a, 0x38(%rsp) # imm = 0x5A44525A
jne 0x4e21
cmpw $0x1, 0x3c(%rsp)
jne 0x4e40
cmpb $0x1, 0x3e(%rsp)
jne 0x4e5f
leaq 0x80(%rsp), %r14
movq %r14, -0x10(%r14)
xorl %eax, %eax
movq %rax, -0x8(%r14)
movb $0x0, (%r14)
movq %rsp, %rdi
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movzbl 0x3f(%rsp), %r12d
movq %r12, %rsi
callq 0x70d8
movq (%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
movq %rax, %r13
cmpq %r12, %rax
jne 0x4c85
movq %r15, 0x90(%rsp)
movq %rbp, %r15
movq (%rsp), %rbp
leaq 0x70(%rsp), %rax
movq 0x8(%rax), %r14
movq %rbp, %rdi
callq 0x2130
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movq %r14, %rdx
leaq 0x80(%rsp), %r14
movq %rbp, %rcx
movq %r15, %rbp
movq 0x90(%rsp), %r15
movq %rax, %r8
callq 0x2360
jmp 0x4c96
movq (%r15), %rsi
leaq 0xa768(%rip), %rdi      # 0xf3f7
xorl %eax, %eax
callq 0x2290
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4cac
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
cmpq %r12, %r13
jne 0x4d98
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rax, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq %rax, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0x40(%rsp), %r12d
movq %r12, %rsi
callq 0x7104
movq (%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
cmpq %r12, %rax
jne 0x4d9c
movzwl 0x44(%rsp), %r12d
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x7104
movq 0x20(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
cmpq %r12, %rax
jne 0x4d9c
movl 0x46(%rsp), %r12d
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x7104
movq 0x50(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
cmpq %r12, %rax
jne 0x4d9c
cmpb $0x1, 0x10510(%rip)     # 0x15260
jne 0x4d68
movq (%r15), %rsi
movq 0x70(%rsp), %rdx
leaq 0xa743(%rip), %rdi      # 0xf4a4
xorl %eax, %eax
callq 0x2050
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %rbp, %rcx
callq 0x2f43
movzwl 0x4a(%rsp), %esi
movb $0x1, %bpl
testq %rsi, %rsi
je 0x4daf
movl %ebx, %edi
movl $0x1, %edx
callq 0x2420
jmp 0x4daf
xorl %ebp, %ebp
jmp 0x4df3
movq (%r15), %rsi
xorl %ebp, %ebp
leaq 0xa6e2(%rip), %rdi      # 0xf48a
xorl %eax, %eax
callq 0x2290
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4dc6
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4ddd
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4df3
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x4e0d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa5e7(%rip), %rdi      # 0xf40f
leaq 0xa29d(%rip), %rsi      # 0xf0cc
leaq 0xa5f0(%rip), %rcx      # 0xf426
movl $0x4ff, %edx            # imm = 0x4FF
callq 0x21c0
leaq 0xa622(%rip), %rdi      # 0xf469
leaq 0xa27e(%rip), %rsi      # 0xf0cc
leaq 0xa5d1(%rip), %rcx      # 0xf426
movl $0x500, %edx            # imm = 0x500
callq 0x21c0
leaq 0xa614(%rip), %rdi      # 0xf47a
leaq 0xa25f(%rip), %rsi      # 0xf0cc
leaq 0xa5b2(%rip), %rcx      # 0xf426
movl $0x501, %edx            # imm = 0x501
callq 0x21c0
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4e98
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %rsp, %rdx
callq 0x2523
jmp 0x4ec5
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4ec5
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x4edf
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23c0