// File: /workspace/github_cpp_makefile_star2_O1/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 $0xe8, %rsp
movq %rdi, %r14
movq (%rdi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x21f0
movl %eax, %ebx
testl %eax, %eax
jns 0x50e8
callq 0x2040
cmpl $0x2, (%rax)
jne 0x50e8
xorl %ebp, %ebp
jmp 0x5466
cmpb $0x1, 0x10171(%rip)     # 0x15260
jne 0x5102
movq (%r14), %rsi
leaq 0xa3ec(%rip), %rdi      # 0xf4e7
xorl %eax, %eax
callq 0x2050
testl %ebx, %ebx
js 0x53f8
leaq 0x58(%rsp), %rsi
movl $0x18, %edx
movl %ebx, %edi
callq 0x2320
cmpq $0x18, %rax
jne 0x53dc
cmpl $0x5a44525a, 0x58(%rsp) # imm = 0x5A44525A
jne 0x53dc
cmpw $0x1, 0x5c(%rsp)
jne 0x53dc
cmpb $0x2, 0x5e(%rsp)
jne 0x53dc
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl 0x60(%rsp), %eax
movq %rax, 0x70(%rsp)
leaq -0x18(%rax), %rsi
callq 0x7104
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
subq %rsi, %rdx
movl %ebx, %edi
callq 0x2320
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq %rcx, %rdx
cmpq %rdx, %rax
sete %bpl
jne 0x540d
movl %ebp, 0x8(%rsp)
movq %rcx, (%rsp)
movl 0x6c(%rsp), %ebp
testl %ebp, %ebp
je 0x51f1
leaq 0x90(%rsp), %r13
leaq 0x80(%rsp), %r15
movq %rsp, %r12
addq $0x2, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x7130
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x51ed
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2260
decl %ebp
jne 0x51c0
leaq 0xb0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0xc0(%rsp), %r12
movl $0x0, (%r12)
xorl %eax, %eax
movq %rax, 0x8(%r12)
movq %r12, 0x10(%r12)
movq %r12, 0x18(%r12)
movq %rax, 0x20(%r12)
movl 0x64(%rsp), %ebp
testl %ebp, %ebp
je 0x5310
leal (,%rbp,8), %r13d
addq (%rsp), %r13
leaq 0x28(%rsp), %r15
movq (%rsp), %rax
movzbl (%rax), %edx
leaq 0x1(%rax), %rcx
movq %rcx, (%rsp)
movzbl 0x1(%rax), %ecx
shll $0x8, %ecx
orq %rdx, %rcx
addq %r13, %rcx
leaq 0x4(%rax), %rdx
movq %rdx, (%rsp)
movzbl 0x4(%rax), %edx
leaq 0x5(%rax), %rsi
movq %rsi, (%rsp)
movzbl 0x5(%rax), %esi
shll $0x8, %esi
orl %edx, %esi
leaq 0x6(%rax), %rdx
movq %rdx, (%rsp)
movzbl 0x6(%rax), %edx
shll $0x10, %edx
orl %esi, %edx
leaq 0x7(%rax), %rsi
movq %rsi, (%rsp)
movzbl 0x7(%rax), %esi
shll $0x18, %esi
orl %edx, %esi
addq $0x8, %rax
movq %rax, (%rsp)
addl 0x70(%rsp), %esi
movl %esi, 0xc(%rsp)
movq %rcx, 0x78(%rsp)
movq %r15, %rdi
leaq 0x78(%rsp), %rsi
callq 0x7130
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
callq 0xafa4
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x5308
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
decl %ebp
jne 0x525a
leaq 0x10051(%rip), %rsi     # 0x15368
leaq 0x80(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x4fc9
testb %al, %al
je 0x542a
leaq 0x28(%rsp), %r15
movl $0x0, 0x30(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x38(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq 0xd0(%rsp), %rbp
cmpq %r12, %rbp
je 0x5420
xorl %r13d, %r13d
cmpl $0x1, 0x24(%rbp)
jne 0x5399
movl 0x20(%rbp), %esi
movl $0x2, 0x24(%rbp)
movl %ebx, %edi
xorl %edx, %edx
callq 0x2420
movb $0x1, %r13b
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x4b7b
movq %rbp, %rdi
callq 0x21d0
movq %rax, %rbp
cmpq %r12, %rax
jne 0x5370
testb $0x1, %r13b
je 0x5420
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
leaq 0xb8(%rsp), %rdx
callq 0x4fc9
movl %eax, %ebp
movq %r15, %rdi
callq 0x7442
testb %bpl, %bpl
jne 0x5339
jmp 0x542a
movq (%r14), %rsi
leaq 0xa118(%rip), %rdi      # 0xf4fe
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2290
movl %ebx, %edi
callq 0x2460
jmp 0x5466
movq (%r14), %rsi
leaq 0xa0c3(%rip), %rdi      # 0xf4c5
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
jmp 0x5466
movq (%r14), %rsi
leaq 0xa0e7(%rip), %rdi      # 0xf4fe
xorl %eax, %eax
callq 0x2290
jmp 0x544f
leaq 0x28(%rsp), %rdi
callq 0x7442
movl %ebx, %edi
callq 0x2460
leaq 0xb8(%rsp), %rdi
callq 0x786e
leaq 0x80(%rsp), %rdi
callq 0x78be
movl 0x8(%rsp), %ebp
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5466
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5480
jmp 0x54a6
jmp 0x54ab
movq %rax, %rbx
jmp 0x54b8
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x54b8
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x54b8
movq %rax, %rbx
jmp 0x54d2
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7442
leaq 0xb8(%rsp), %rdi
callq 0x786e
leaq 0x80(%rsp), %rdi
callq 0x78be
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x54e9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23c0