// File: /workspace/github_cpp_makefile_star2_O3/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 0x508d
callq 0x2040
cmpl $0x2, (%rax)
jne 0x508d
xorl %r15d, %r15d
jmp 0x5458
cmpb $0x1, 0x101cc(%rip)     # 0x15260
jne 0x50a7
movq (%r14), %rsi
leaq 0xa447(%rip), %rdi      # 0xf4e7
xorl %eax, %eax
callq 0x2050
testl %ebx, %ebx
js 0x541a
leaq 0x58(%rsp), %rsi
movl $0x18, %edx
movl %ebx, %edi
callq 0x2320
cmpq $0x18, %rax
jne 0x53fd
cmpl $0x5a44525a, 0x58(%rsp) # imm = 0x5A44525A
jne 0x53fd
cmpw $0x1, 0x5c(%rsp)
jne 0x53fd
cmpb $0x2, 0x5e(%rsp)
jne 0x53fd
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 0x703a
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
subq %rsi, %rdx
movl %ebx, %edi
callq 0x2320
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %rcx
subq %rbp, %rcx
cmpq %rcx, %rax
sete %r15b
jne 0x5430
movl %r15d, 0x8(%rsp)
movq %rbp, (%rsp)
movl 0x6c(%rsp), %r13d
testl %r13d, %r13d
je 0x519e
leaq 0x90(%rsp), %rbp
leaq 0x80(%rsp), %r15
movq %rsp, %r12
addq $0x2, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x7066
movq 0x80(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5195
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2260
decl %r13d
jne 0x5168
movq (%rsp), %rbp
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), %r13d
testl %r13d, %r13d
je 0x52bf
leal (,%r13,8), %eax
addq %rax, %rbp
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 %rbp, %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 0x7066
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
callq 0xadda
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x52b6
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
decl %r13d
jne 0x5208
leaq 0x100a2(%rip), %rsi     # 0x15368
leaq 0x80(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x4f6d
movl 0x8(%rsp), %r15d
testb %al, %al
je 0x53da
leaq 0x30(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq 0xd0(%rsp), %rbp
cmpq %r12, %rbp
je 0x53cb
xorl %r13d, %r13d
leaq 0x28(%rsp), %r15
cmpl $0x1, 0x24(%rbp)
je 0x533f
movq %rbp, %rdi
callq 0x21d0
movq %rax, %rbp
cmpq %r12, %rax
jne 0x531e
testb $0x1, %r13b
jne 0x5372
jmp 0x53cb
movl 0x20(%rbp), %esi
movl $0x2, 0x24(%rbp)
movl %ebx, %edi
xorl %edx, %edx
callq 0x2420
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x4b1f
movq %rbp, %rdi
callq 0x21d0
movq %rax, %rbp
movb $0x1, %r13b
cmpq %r12, %rax
jne 0x531e
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
leaq 0xb8(%rsp), %rdx
callq 0x4f6d
testb %al, %al
je 0x53cb
movq %r15, %rdi
callq 0x7390
movl $0x0, 0x30(%rsp)
xorl %r13d, %r13d
movq %r13, 0x38(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %r13, 0x50(%rsp)
movq 0xd0(%rsp), %rbp
cmpq %r12, %rbp
jne 0x531e
leaq 0x28(%rsp), %rdi
callq 0x7390
movl 0x8(%rsp), %r15d
movl %ebx, %edi
callq 0x2460
leaq 0xb8(%rsp), %rdi
callq 0x77cc
leaq 0x80(%rsp), %rdi
callq 0x781c
jmp 0x5441
movq (%r14), %rsi
leaq 0xa0f7(%rip), %rdi      # 0xf4fe
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2290
movl %ebx, %edi
callq 0x2460
jmp 0x5458
movq (%r14), %rsi
leaq 0xa0a1(%rip), %rdi      # 0xf4c5
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x20f0
jmp 0x5458
movq (%r14), %rsi
leaq 0xa0c4(%rip), %rdi      # 0xf4fe
xorl %eax, %eax
callq 0x2290
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5458
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5482
jmp 0x5471
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7390
jmp 0x54a6
jmp 0x54c2
movq %rax, %rbx
jmp 0x54a6
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x54a6
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
leaq 0xb8(%rsp), %rdi
callq 0x77cc
leaq 0x80(%rsp), %rdi
callq 0x781c
jmp 0x54c5
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x54dc
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23c0