// File: /workspace/github_cpp_makefile_star2_O3/ksherlock[P]wdc-utils/dumpobj.cpp
// Function: dump_lib(char const*, int)
void dump_lib(const char *name, int fd)
{
	Lib_head h;
	ssize_t ok;

	ok = read(fd, &h, sizeof(h));
	if (ok != sizeof(h))
		errx(EX_DATAERR, "%s is not an object file", name);


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

	assert(h.l_magic == MOD_MAGIC);
	assert(h.l_version == 1);
	assert(h.l_filtyp == 2);

	printf("; library %s\n\n", name);
	/*
	printf("; modstart      : $%04x\n", h.l_modstart);
	printf("; number symbols: $%04x\n", h.l_numsyms);
	printf("; number files  : $%04x\n", h.l_numfiles);
	printf("\n");
	*/

	std::vector<uint8_t> data;
	long count = h.l_modstart - sizeof(h);
	if (count < 0) errx(EX_DATAERR, "%s", name);
	data.resize(count);
	ok = read(fd, data.data(), count);
	if (ok != count) errx(EX_DATAERR, "%s truncated", name);


	// files
	printf("; files:\n");
	auto iter = data.begin();
	for (int i = 0; i < h.l_numfiles; ++i) {
		uint16_t file_number = read_16(iter);
		std::string s = read_pstring(iter);
		printf("; $%02x %s\n", file_number, s.c_str());
	}
	printf("\n");

	// symbols
	printf("; symbols:\n");
	auto name_iter = iter + h.l_numsyms * 8;
	for (int 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);
		auto tmp = name_iter + name_offset;
		std::string name = read_pstring(tmp);

		printf("; $%04x %s\n", i, name.c_str());
		//printf("name offset: %02x\n", name_offset);
		//printf("file_number  : $%02x\n", file_number);
		//printf("module offset: $%04x\n", offset); 
	}
	printf("\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x48(%rsp), %rsi
movl $0x18, %edx
movl %ebp, %edi
callq 0x2250
cmpq $0x18, %rax
jne 0x62ab
cmpl $0x5a44525a, 0x48(%rsp) # imm = 0x5A44525A
jne 0x62c1
cmpw $0x1, 0x4c(%rsp)
jne 0x62e0
cmpb $0x2, 0x4e(%rsp)
jne 0x62ff
leaq 0x9250(%rip), %rdi      # 0xf336
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl 0x50(%rsp), %r14d
cmpq $0x18, %r14
jb 0x631e
addq $-0x18, %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x66ec
movq 0x10(%rsp), %rsi
movl %ebp, %edi
movq %r14, %rdx
callq 0x2250
cmpq %r14, %rax
jne 0x6327
leaq 0x929e(%rip), %rdi      # 0xf3de
callq 0x2270
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x5c(%rsp), %r12d
testl %r12d, %r12d
je 0x61cc
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x91e8(%rip), %r15      # 0xf352
movq 0x8(%rsp), %rax
movzbl (%rax), %r13d
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
movzbl 0x1(%rax), %ebp
addq $0x2, %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68dd
shll $0x8, %ebp
orl %r13d, %ebp
movq 0x28(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x61c7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
decl %r12d
jne 0x616a
movl $0xa, %edi
callq 0x2240
leaq 0x920a(%rip), %rdi      # 0xf3e7
callq 0x2270
movl 0x54(%rsp), %r12d
testl %r12d, %r12d
je 0x627b
leal (,%r12,8), %r13d
addq 0x8(%rsp), %r13
leaq 0x28(%rsp), %rbx
leaq 0x60(%rsp), %r14
leaq 0x9150(%rip), %r15      # 0xf35e
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movzbl (%rax), %ecx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rsp)
movzbl 0x1(%rax), %edx
shll $0x8, %edx
orq %rcx, %rdx
addq %r13, %rdx
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq %rdx, 0x60(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68dd
movq 0x28(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6274
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
incl %ebp
cmpl %ebp, %r12d
jne 0x6210
movl $0xa, %edi
callq 0x2240
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x629c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8e0a(%rip), %rsi      # 0xf0bc
movl $0x41, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2310
leaq 0x9015(%rip), %rdi      # 0xf2dd
leaq 0x8e1d(%rip), %rsi      # 0xf0ec
leaq 0x901e(%rip), %rcx      # 0xf2f4
movl $0x246, %edx            # imm = 0x246
callq 0x2120
leaq 0x902e(%rip), %rdi      # 0xf315
leaq 0x8dfe(%rip), %rsi      # 0xf0ec
leaq 0x8fff(%rip), %rcx      # 0xf2f4
movl $0x247, %edx            # imm = 0x247
callq 0x2120
leaq 0x9020(%rip), %rdi      # 0xf326
leaq 0x8ddf(%rip), %rsi      # 0xf0ec
leaq 0x8fe0(%rip), %rcx      # 0xf2f4
movl $0x248, %edx            # imm = 0x248
callq 0x2120
leaq 0x99b5(%rip), %rsi      # 0xfcda
jmp 0x632e
leaq 0x9017(%rip), %rsi      # 0xf345
movl $0x41, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2310
jmp 0x6341
jmp 0x6341
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x635b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22c0