// File: 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
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x3280
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x77f2
callq 0x3050
cmpl $0x2, (%rax)
jne 0x77f0
movb $0x0, -0x1(%rbp)
jmp 0x7e15
jmp 0x77f2
testb $0x1, 0x37b47(%rip)     # 0x3f340
je 0x7815
movq -0x10(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x23e91(%rip), %rdi     # 0x2b69f
movb $0x0, %al
callq 0x3060
cmpl $0x0, -0x2c(%rbp)
jge 0x783e
movq -0x10(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x23e4f(%rip), %rdi     # 0x2b67d
movb $0x0, %al
callq 0x30e0
movb $0x0, -0x1(%rbp)
jmp 0x7e15
movl -0x2c(%rbp), %edi
leaq -0x28(%rbp), %rsi
movl $0x18, %edx
callq 0x3480
movq %rax, -0x38(%rbp)
cmpq $0x18, -0x38(%rbp)
je 0x7885
movq -0x10(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x23e49(%rip), %rdi     # 0x2b6b6
movb $0x0, %al
callq 0x3360
movl -0x2c(%rbp), %edi
callq 0x3600
movb $0x0, -0x1(%rbp)
jmp 0x7e15
leaq -0x28(%rbp), %rdi
callq 0xc9b0
leaq -0x28(%rbp), %rdi
addq $0x4, %rdi
callq 0xc9d0
leaq -0x28(%rbp), %rdi
addq $0x6, %rdi
callq 0xc9f0
leaq -0x28(%rbp), %rdi
addq $0x7, %rdi
callq 0xc9f0
leaq -0x28(%rbp), %rdi
addq $0x8, %rdi
callq 0xc9b0
leaq -0x28(%rbp), %rdi
addq $0xc, %rdi
callq 0xc9b0
leaq -0x28(%rbp), %rdi
addq $0x10, %rdi
callq 0xc9b0
leaq -0x28(%rbp), %rdi
addq $0x14, %rdi
callq 0xc9b0
cmpl $0x5a44525a, -0x28(%rbp) # imm = 0x5A44525A
jne 0x7904
movzwl -0x24(%rbp), %eax
cmpl $0x1, %eax
jne 0x7904
movzbl -0x22(%rbp), %eax
cmpl $0x2, %eax
je 0x792f
movq -0x10(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x23d9f(%rip), %rdi     # 0x2b6b6
movb $0x0, %al
callq 0x3360
movl -0x2c(%rbp), %edi
callq 0x3600
movb $0x0, -0x1(%rbp)
jmp 0x7e15
leaq -0x50(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0xcb40
movq -0x1b8(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, %esi
addq $-0x18, %rsi
callq 0xcb60
jmp 0x7956
movl -0x2c(%rbp), %eax
movl %eax, -0x1cc(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0xcbf0
movq -0x1d8(%rbp), %rdi
movq %rax, -0x1c8(%rbp)
callq 0xa700
movl -0x1cc(%rbp), %edi
movq -0x1c8(%rbp), %rsi
movq %rax, %rdx
callq 0x3480
movq %rax, -0x1c0(%rbp)
jmp 0x79a0
movq -0x1c0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xa700
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
cmpq %rcx, %rax
je 0x7a0b
movq -0x10(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x23cd5(%rip), %rdi     # 0x2b6b6
xorl %eax, %eax
callq 0x3360
jmp 0x79ea
movb $0x0, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0x7dff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x7e0a
leaq -0x50(%rbp), %rdi
callq 0xce10
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xa870
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x7a7a
leaq -0x68(%rbp), %rdi
callq 0xce40
movw %ax, -0x1e2(%rbp)
jmp 0x7a46
movw -0x1e2(%rbp), %ax
movw %ax, -0x76(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xcea0
jmp 0x7a63
leaq -0x98(%rbp), %rdi
callq 0x31f0
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x7a2c
leaq -0xd0(%rbp), %rdi
callq 0xcf70
leaq -0x100(%rbp), %rdi
callq 0xcf90
movl -0x1c(%rbp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x68(%rbp), %rdi
callq 0xcfb0
movq %rax, -0x108(%rbp)
movl $0x0, -0x10c(%rbp)
movl -0x10c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x7be9
leaq -0x68(%rbp), %rdi
callq 0xce40
movw %ax, -0x1e4(%rbp)
jmp 0x7ad7
movw -0x1e4(%rbp), %ax
movw %ax, -0x10e(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xce40
movw %ax, -0x1e6(%rbp)
jmp 0x7af7
movw -0x1e6(%rbp), %ax
movw %ax, -0x110(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xcff0
movl %eax, -0x1ec(%rbp)
jmp 0x7b16
movl -0x1ec(%rbp), %eax
movl -0x20(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x114(%rbp)
movzwl -0x10e(%rbp), %eax
movl %eax, %esi
leaq -0x108(%rbp), %rdi
callq 0xcfb0
movq %rax, -0x120(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0xcea0
jmp 0x7b58
leaq -0xd0(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x114(%rbp), %rdx
callq 0xd090
movb %dl, -0x1f9(%rbp)
movq %rax, -0x1f8(%rbp)
jmp 0x7b81
movb -0x1f9(%rbp), %al
movq -0x1f8(%rbp), %rcx
movq %rcx, -0x150(%rbp)
movb %al, -0x148(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x31f0
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x7ab6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x7de5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x31f0
jmp 0x7de5
leaq 0x37858(%rip), %rsi     # 0x3f448
leaq -0xd0(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x7680
movb %al, -0x1fa(%rbp)
jmp 0x7c0b
movb -0x1fa(%rbp), %al
testb $0x1, %al
jne 0x7c31
jmp 0x7c17
movl -0x2c(%rbp), %edi
callq 0x3600
jmp 0x7c21
movb $0x1, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0x7dcb
jmp 0x7c33
movb $0x0, -0x151(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x9d30
leaq -0x100(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rdi
callq 0xd0e0
movq %rax, -0x198(%rbp)
movq -0x190(%rbp), %rdi
callq 0xd110
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0xd140
testb $0x1, %al
jne 0x7c96
jmp 0x7d3f
leaq -0x198(%rbp), %rdi
callq 0xd170
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1ac(%rbp)
movq -0x1a8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1b0(%rbp)
cmpl $0x1, -0x1b0(%rbp)
jne 0x7d2c
movq -0x1a8(%rbp), %rax
movl $0x2, 0x4(%rax)
movl -0x2c(%rbp), %edi
movl -0x1ac(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x35a0
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x188(%rbp), %rdx
callq 0x6fa0
jmp 0x7d06
movb $0x1, -0x151(%rbp)
jmp 0x7d2c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x9d50
jmp 0x7de5
jmp 0x7d2e
leaq -0x198(%rbp), %rdi
callq 0xd190
jmp 0x7c7a
testb $0x1, -0x151(%rbp)
jne 0x7d51
movl $0x8, -0x60(%rbp)
jmp 0x7d9a
leaq -0xd0(%rbp), %rdi
leaq -0x188(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x7680
movb %al, -0x1fb(%rbp)
jmp 0x7d73
movb -0x1fb(%rbp), %al
andb $0x1, %al
movb %al, -0x151(%rbp)
testb $0x1, -0x151(%rbp)
jne 0x7d93
movl $0x8, -0x60(%rbp)
jmp 0x7d9a
movl $0x0, -0x60(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x9d50
movl -0x60(%rbp), %eax
testl %eax, %eax
je 0x7db1
jmp 0x7daf
jmp 0x7db6
jmp 0x7c33
movl -0x2c(%rbp), %edi
callq 0x3600
jmp 0x7dc0
movb $0x1, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
leaq -0x100(%rbp), %rdi
callq 0xd1c0
leaq -0xd0(%rbp), %rdi
callq 0xd1e0
jmp 0x7dff
leaq -0x100(%rbp), %rdi
callq 0xd1c0
leaq -0xd0(%rbp), %rdi
callq 0xd1e0
jmp 0x7e0a
leaq -0x50(%rbp), %rdi
callq 0xcc10
jmp 0x7e15
leaq -0x50(%rbp), %rdi
callq 0xcc10
jmp 0x7e23
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x3540
nopl (%rax)