// File: /workspace/github_cpp_makefile_star2_O3/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 0x4b6c
cmpq $0x17, %rax
ja 0x4b73
movq (%r15), %rsi
leaq 0xa899(%rip), %rdi      # 0xf3f7
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2290
jmp 0x4db1
xorl %ebp, %ebp
jmp 0x4db1
cmpl $0x5a44525a, 0x38(%rsp) # imm = 0x5A44525A
jne 0x4dc5
cmpw $0x1, 0x3c(%rsp)
jne 0x4de4
cmpb $0x1, 0x3e(%rsp)
jne 0x4e03
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 0x700e
movq (%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
movq %rax, %r13
cmpq %r12, %rax
jne 0x4c29
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 0x4c3a
movq (%r15), %rsi
leaq 0xa7c4(%rip), %rdi      # 0xf3f7
xorl %eax, %eax
callq 0x2290
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4c50
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
cmpq %r12, %r13
jne 0x4d3c
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 0x703a
movq (%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
cmpq %r12, %rax
jne 0x4d40
movzwl 0x44(%rsp), %r12d
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x703a
movq 0x20(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
cmpq %r12, %rax
jne 0x4d40
movl 0x46(%rsp), %r12d
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x703a
movq 0x50(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
callq 0x2320
cmpq %r12, %rax
jne 0x4d40
cmpb $0x1, 0x1056c(%rip)     # 0x15260
jne 0x4d0c
movq (%r15), %rsi
movq 0x70(%rsp), %rdx
leaq 0xa79f(%rip), %rdi      # 0xf4a4
xorl %eax, %eax
callq 0x2050
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %rbp, %rcx
callq 0x2f63
movzwl 0x4a(%rsp), %esi
movb $0x1, %bpl
testq %rsi, %rsi
je 0x4d53
movl %ebx, %edi
movl $0x1, %edx
callq 0x2420
jmp 0x4d53
xorl %ebp, %ebp
jmp 0x4d97
movq (%r15), %rsi
xorl %ebp, %ebp
leaq 0xa73e(%rip), %rdi      # 0xf48a
xorl %eax, %eax
callq 0x2290
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4d6a
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4d81
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4d97
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x4db1
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 0xa643(%rip), %rdi      # 0xf40f
leaq 0xa2f9(%rip), %rsi      # 0xf0cc
leaq 0xa64c(%rip), %rcx      # 0xf426
movl $0x4ff, %edx            # imm = 0x4FF
callq 0x21c0
leaq 0xa67e(%rip), %rdi      # 0xf469
leaq 0xa2da(%rip), %rsi      # 0xf0cc
leaq 0xa62d(%rip), %rcx      # 0xf426
movl $0x500, %edx            # imm = 0x500
callq 0x21c0
leaq 0xa670(%rip), %rdi      # 0xf47a
leaq 0xa2bb(%rip), %rsi      # 0xf0cc
leaq 0xa60e(%rip), %rcx      # 0xf426
movl $0x501, %edx            # imm = 0x501
callq 0x21c0
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4e3c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %rsp, %rdx
callq 0x2523
jmp 0x4e69
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4e69
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x4e83
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23c0