// File: /workspace/github_cpp_makefile_star2_O2/ksherlock[P]wdc-utils/link.cpp
// Function: one_lib(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
bool one_lib(const std::string &path) {

	Lib_head h;

	int fd = open(path.c_str(), O_RDONLY | O_BINARY);
	if (fd < 0) {
		if (errno == ENOENT) return false;
	}

	if (flags.v) printf("Processing library %s\n", path.c_str());

	if (fd < 0) {
		warn("Unable to open %s", path.c_str());
		return false;
	}

	ssize_t ok;

	ok = read(fd, &h, sizeof(h));
	if (ok != sizeof(h)) {
		warnx("Invalid library file: %s", path.c_str());
		close(fd);
		return false;
	}

	le_to_host(h.l_magic);
	le_to_host(h.l_version);
	le_to_host(h.l_filtyp);
	le_to_host(h.l_unused1);
	le_to_host(h.l_modstart);
	le_to_host(h.l_numsyms);
	le_to_host(h.l_symsize);
	le_to_host(h.l_numfiles);

	if (h.l_magic != MOD_MAGIC || h.l_version != MOD_VERSION || h.l_filtyp != MOD_LIBRARY) {
		warnx("Invalid library file: %s", path.c_str());
		close(fd);
		return false;
	}

	// read the symbol dictionary.

	std::vector<uint8_t> data;
	data.resize(h.l_modstart - sizeof(h));
	ok = read(fd, data.data(), data.size());
	if (ok != data.size()) {
		warnx("Invalid library file: %s", path.c_str());
		return false;
	}

	auto iter = data.begin();
	auto end = data.end();


	// files -- only reading since it's variable length.
	for (unsigned i = 0; i < h.l_numfiles; ++i) {

		// fileno, pstring file name
		uint16_t fileno = read_16(iter);
		std::string s = read_pstring(iter);
		//uint8_t size = read_8(iter);
		//iter += size; // don't care about the name.
	}

	std::unordered_map<std::string, uint32_t> lib_symbol_map;


	// map of which modules have been loaded or are pending processing.
	std::map<uint32_t, int> modules;


	auto name_iter = iter + h.l_numsyms * 8;
	for (unsigned i = 0; i < h.l_numsyms; ++i) {
		uint16_t name_offset = read_16(iter);
		uint16_t file_number = read_16(iter);
		uint32_t offset = read_32(iter) + h.l_modstart;

		auto tmp = name_iter + name_offset;
		std::string name = read_pstring(tmp);

		lib_symbol_map.emplace(std::move(name), offset);

		//modules[offset] = 0;
	}





	// find an intersection of undefined symbols and symbols defined in lib_symbol_map

	if (!intersection(lib_symbol_map, undefined_symbols, modules)) {
		close(fd);
		return true;
	}

	for(;;) {
		bool delta = false;

		std::set<std::string> local_undefined_symbols;


		for (auto &x : modules) {
			uint32_t offset = x.first;
			int status = x.second;

			if (status == kPending) {
				x.second = kProcessed;
				lseek(fd, offset, SEEK_SET);
				one_module(path, fd, &local_undefined_symbols);
				delta = true;
			}
		}
		if (!delta) break;

		delta = intersection(lib_symbol_map, local_undefined_symbols, modules);
		if (!delta) break;
	}

	close(fd);
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %r14
movq (%rdi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x21f0
movl %eax, %ebx
testl %eax, %eax
jns 0x484a
callq 0x2040
cmpl $0x2, (%rax)
jne 0x484a
xorl %r15d, %r15d
jmp 0x4b66
cmpb $0x1, 0x10a1f(%rip)     # 0x15270
jne 0x4864
movq (%r14), %rsi
leaq 0x8d42(%rip), %rdi      # 0xd59f
xorl %eax, %eax
callq 0x2050
testl %ebx, %ebx
js 0x494f
leaq 0x40(%rsp), %rsi
pushq $0x18
popq %rdx
movl %ebx, %edi
callq 0x2330
cmpq $0x18, %rax
jne 0x492f
cmpl $0x5a44525a, 0x40(%rsp) # imm = 0x5A44525A
jne 0x492f
cmpw $0x1, 0x44(%rsp)
jne 0x492f
cmpb $0x2, 0x46(%rsp)
jne 0x492f
leaq 0x20(%rsp), %rdi
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movl 0x48(%rsp), %eax
movq %rax, 0x60(%rsp)
leaq -0x18(%rax), %rsi
callq 0x624e
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
subq %rsi, %rdx
movl %ebx, %edi
callq 0x2330
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
subq %rcx, %rdx
cmpq %rdx, %rax
sete %r15b
jne 0x4968
movl %r15d, 0xc(%rsp)
leaq 0x10(%rsp), %r15
movq %rcx, (%r15)
movl 0x54(%rsp), %ebp
leaq 0xd0(%rsp), %r12
subl $0x1, %ebp
jb 0x497e
addq $0x2, 0x10(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x627a
movq %r12, %rdi
callq 0x2130
jmp 0x490f
movq (%r14), %rsi
leaq 0x8c7d(%rip), %rdi      # 0xd5b6
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2280
movl %ebx, %edi
callq 0x2480
jmp 0x4b66
movq (%r14), %rsi
leaq 0x8c24(%rip), %rdi      # 0xd57d
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x20d0
jmp 0x4b66
movq (%r14), %rsi
leaq 0x8c44(%rip), %rdi      # 0xd5b6
xorl %eax, %eax
callq 0x2280
jmp 0x4b5c
leaq 0x100(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
leaq 0xa8(%rsp), %r13
andl $0x0, (%r13)
movups %xmm0, -0x20(%rax)
andq $0x0, 0x8(%r13)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
andq $0x0, 0x20(%r13)
movl 0x4c(%rsp), %r15d
leal (,%r15,8), %eax
leaq 0x10(%rsp), %rcx
addq (%rcx), %rax
movq %rax, 0x58(%rsp)
leaq 0x70(%rsp), %r12
subl $0x1, %r15d
jb 0x4a54
movq 0x10(%rsp), %rax
movzbl (%rax), %ecx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rsp)
movzbl 0x1(%rax), %ebp
shll $0x8, %ebp
orq %rcx, %rbp
addq 0x58(%rsp), %rbp
addq $0x4, %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x62e6
addl 0x60(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq %rbp, 0x68(%rsp)
movq %r12, %rdi
leaq 0x68(%rsp), %rsi
callq 0x627a
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
leaq 0x1c(%rsp), %rdx
callq 0x9cd2
movq %r12, %rdi
callq 0x2130
jmp 0x49e2
leaq 0x1091d(%rip), %rsi     # 0x15378
leaq 0xd0(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x4737
movl 0xc(%rsp), %r15d
testb %al, %al
je 0x4b3b
leaq 0x70(%rsp), %r12
andl $0x0, 0x78(%rsp)
andq $0x0, 0x80(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x88(%rsp)
movq %rax, 0x90(%rsp)
andq $0x0, 0x98(%rsp)
movq 0xb8(%rsp), %rbp
xorl %r15d, %r15d
cmpq %r13, %rbp
je 0x4af4
cmpl $0x1, 0x24(%rbp)
jne 0x4ae7
movl 0x20(%rbp), %esi
movl $0x2, 0x24(%rbp)
movl %ebx, %edi
xorl %edx, %edx
callq 0x2420
movb $0x1, %r15b
movq %r14, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0x439d
movq %rbp, %rdi
callq 0x21d0
movq %rax, %rbp
jmp 0x4ab9
testb $0x1, %r15b
je 0x4b2c
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
leaq 0xa0(%rsp), %rdx
callq 0x4737
movl 0xc(%rsp), %r15d
movl %eax, %ebp
movq %r12, %rdi
callq 0x65ca
testb %bpl, %bpl
jne 0x4a82
jmp 0x4b3b
leaq 0x70(%rsp), %rdi
callq 0x65ca
movl 0xc(%rsp), %r15d
movl %ebx, %edi
callq 0x2480
leaq 0xa0(%rsp), %rdi
callq 0x6948
leaq 0xd0(%rsp), %rdi
callq 0x69a0
leaq 0x20(%rsp), %rdi
callq 0x6ac0
movl %r15d, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4b81
jmp 0x4b95
jmp 0x4b9a
movq %rax, %rbx
jmp 0x4ba7
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x2130
jmp 0x4ba7
movq %rax, %rbx
jmp 0x4bc1
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x65ca
leaq 0xa0(%rsp), %rdi
callq 0x6948
leaq 0xd0(%rsp), %rdi
callq 0x69a0
leaq 0x20(%rsp), %rdi
callq 0x6ac0
movq %rbx, %rdi
callq 0x23c0