// File: /workspace/github_cpp_makefile_star2_O1/ksherlock[P]wdc-utils/link.cpp
// Function: main
int main(int argc, char **argv) {


	int c;
	while ((c = getopt(argc, argv, "vCXSL:l:o:t:")) != -1) {
		switch(c) {
			case 'h': usage(0); break;

			case 'v': flags.v = true; break;
			case 'S': flags.S = true; break;

			case '1': flags.omf_flags |= OMF_V1; break;
			case 'X': flags.omf_flags |= OMF_NO_EXPRESS; break;
			case 'C': flags.omf_flags |= OMF_NO_SUPER; break;

			case 'o': flags.o = optarg; break;

			case 'l': {
				if (*optarg) flags.l.emplace_back(optarg);
				break;
			}
			case 'L': {
				std::string tmp(optarg);
				if (tmp.empty()) tmp = ".";
				if (tmp.back() != '/') tmp.push_back('/');
				flags.L.emplace_back(std::move(tmp));
				break;
			}
			case 't': {
				// -t xx[:xxxx] -- set file/auxtype.
				if (!parse_ft(optarg)) {
					errx(EX_USAGE, "Invalid -t argument: %s", optarg);
				}
				break;
			}


			case ':':
			case '?':
			default:
				usage(EX_USAGE);
				break;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) usage(EX_USAGE);

	init();

	for (int i = 0 ; i < argc; ++i) {
		if (!one_file(argv[i])) flags.errors++;
	}


	if (flags.v && !undefined_symbols.empty()) {
		printf("Undefined Symbols:\n");
		for (const auto & s : undefined_symbols) {
			printf("%s\n", s.c_str());
		}
		printf("\n");
	}

	if (!undefined_symbols.empty()) libraries();
	//

	if (!undefined_symbols.empty()) {

		fprintf(stderr, "Unable to resolve the following symbols:\n");
		for (auto &s : undefined_symbols) fprintf(stderr, "%s\n", s.c_str());

		exit(EX_DATAERR);
	}

	generate_end();
	simplify();


	if (flags.v) {
		for (const auto &s : sections) {
			//if (s.flags & SEC_REF_ONLY) continue;
			printf("section %3d %-20s $%04x $%04x\n",
				s.number, s.name.c_str(), (uint32_t)s.data.size(), s.size);
		}
		fputs("\n", stdout);
	}

	build_omf_segments();

	if (flags.v) {
		for (const auto &s : omf_segments) {
			printf("segment %3d %-20s $%04x\n",
				s.segnum, s.segname.c_str(), (uint32_t)s.data.size());

			for (auto &r : s.relocs) {
				printf("  %02x %02x %06x %06x\n",
					r.size, r.shift, r.offset, r.value);
			}
			for (auto &r : s.intersegs) {
				printf("  %02x %02x %06x %02x %04x %06x\n",
					r.size, r.shift, r.offset, r.file, r.segment, r.segment_offset);
			}
		}
	}

	if (flags.o.empty()) flags.o = "out.omf";
	if (!flags.file_type) {
		flags.file_type = 0xb3;
	}

	int set_file_type(const std::string &path, uint16_t file_type, uint32_t aux_type);

	save_omf(flags.o, omf_segments, flags.omf_flags);
	set_file_type(flags.o, flags.file_type, flags.aux_type);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movl %edi, %ebx
leaq 0x9e83(%rip), %r12      # 0xf6b4
movq 0xf790(%rip), %r15      # 0x14fc8
leaq 0x10(%rsp), %r13
movl %edi, 0x34(%rsp)
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
cmpl $0x57, %eax
jle 0x5878
cmpl $0x6e, %eax
jle 0x5898
cmpl $0x6f, %eax
je 0x59b3
cmpl $0x74, %eax
je 0x58e0
cmpl $0x76, %eax
jne 0x5cd9
movb $0x1, 0xf9ea(%rip)      # 0x15260
jmp 0x5841
cmpl $0x4b, %eax
jg 0x58c6
cmpl $0x31, %eax
je 0x59a7
cmpl $0x43, %eax
jne 0x5a09
orb $0x2, 0xfa2e(%rip)      # 0x152c4
jmp 0x5841
cmpl $0x58, %eax
je 0x59fd
cmpl $0x6c, %eax
jne 0x5cb7
movq (%r15), %rax
cmpb $0x0, (%rax)
je 0x5841
leaq 0xf9cf(%rip), %rdi      # 0x15288
movq %r15, %rsi
callq 0x720e
jmp 0x5841
cmpl $0x4c, %eax
je 0x5924
cmpl $0x53, %eax
jne 0x5cd9
movb $0x1, 0xf986(%rip)      # 0x15261
jmp 0x5841
movq (%r15), %rsi
movq %r13, %rdi
leaq 0xf(%rsp), %rdx
callq 0x6e7c
movq %r13, %rdi
callq 0x5658
movl %eax, %ebp
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5916
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
testb %bpl, %bpl
jne 0x5841
jmp 0x5cf9
movq (%r15), %rsi
movq %r13, %rdi
leaq 0xf(%rsp), %rdx
callq 0x6e7c
movq 0x18(%rsp), %rdx
testq %rdx, %rdx
jne 0x5955
movl $0x1, %r8d
movq %r13, %rdi
xorl %esi, %esi
leaq 0x9e4f(%rip), %rcx      # 0xf79f
callq 0x2360
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpb $0x2f, -0x1(%rax,%rcx)
je 0x5973
movq %r13, %rdi
movl $0x2f, %esi
callq 0x2220
leaq 0xf926(%rip), %rdi      # 0x152a0
movq %r13, %rsi
callq 0x7254
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5841
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x5841
orb $0x1, 0xf916(%rip)      # 0x152c4
jmp 0x5841
movq (%r15), %rbp
movq %r14, %rbx
movq %r12, %r14
movq %r13, %r12
movq %r15, %r13
movq 0xf8a7(%rip), %r15      # 0x15270
movq %rbp, %rdi
callq 0x2130
leaq 0xf890(%rip), %rdi      # 0x15268
xorl %esi, %esi
movq %r15, %rdx
movq %r13, %r15
movq %r12, %r13
movq %r14, %r12
movq %rbx, %r14
movl 0x34(%rsp), %ebx
movq %rbp, %rcx
movq %rax, %r8
callq 0x2360
jmp 0x5841
orb $0x8, 0xf8c0(%rip)      # 0x152c4
jmp 0x5841
cmpl $-0x1, %eax
jne 0x5cd9
movq 0xf5c7(%rip), %rax      # 0x14fe0
movslq (%rax), %r15
subl %r15d, %ebx
je 0x5cd9
callq 0x3d7c
testl %ebx, %ebx
jle 0x5a8a
leaq (%r14,%r15,8), %r15
movl %ebx, %r12d
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
leaq 0xf(%rsp), %r14
movq (%r15,%r13,8), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x6e7c
movq %rbx, %rdi
callq 0x4ee7
movl %eax, %ebp
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5a77
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
testb %bpl, %bpl
jne 0x5a82
incl 0xf836(%rip)            # 0x152b8
incq %r13
cmpq %r13, %r12
jne 0x5a42
cmpb $0x1, 0xf7cf(%rip)      # 0x15260
jne 0x5adf
cmpq $0x0, 0xf8f5(%rip)      # 0x15390
je 0x5adf
leaq 0x9e5a(%rip), %rdi      # 0xf8fe
callq 0x2350
movq 0xf8d0(%rip), %rbx      # 0x15380
leaq 0xf8b9(%rip), %r14      # 0x15370
cmpq %r14, %rbx
je 0x5ad5
movq 0x20(%rbx), %rdi
callq 0x2350
movq %rbx, %rdi
callq 0x22c0
movq %rax, %rbx
cmpq %r14, %rax
jne 0x5abc
movl $0xa, %edi
callq 0x2300
cmpq $0x0, 0xf8a9(%rip)      # 0x15390
je 0x5aee
callq 0x54f1
cmpq $0x0, 0xf89a(%rip)      # 0x15390
jne 0x5d0f
callq 0x431a
callq 0x2e11
cmpb $0x1, 0xf753(%rip)      # 0x15260
jne 0x5b65
movq 0xf7ea(%rip), %r14      # 0x15300
movq 0xf7eb(%rip), %r15      # 0x15308
cmpq %r15, %r14
je 0x5b4f
leaq 0x9bda(%rip), %rbx      # 0xf703
movl 0x2c(%r14), %esi
movq (%r14), %rdx
movl 0x38(%r14), %ecx
subl 0x30(%r14), %ecx
movl 0x28(%r14), %r8d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2050
addq $0x68, %r14
cmpq %r15, %r14
jne 0x5b29
movq 0xf462(%rip), %rax      # 0x14fb8
movq (%rax), %rsi
leaq 0x9ca9(%rip), %rdi      # 0xf809
callq 0x22a0
callq 0x469b
cmpb $0x1, 0xf6ef(%rip)      # 0x15260
jne 0x5c3b
movq 0xf96a(%rip), %r12      # 0x154e8
movq 0xf96b(%rip), %r13      # 0x154f0
cmpq %r13, %r12
je 0x5c3b
leaq 0x9bbd(%rip), %r14      # 0xf752
leaq 0x9b9f(%rip), %r15      # 0xf73b
movzwl (%r12), %esi
movq 0x30(%r12), %rdx
movl 0x58(%r12), %ecx
subl 0x50(%r12), %ecx
leaq 0x9b6b(%rip), %rdi      # 0xf722
xorl %eax, %eax
callq 0x2050
movq 0x80(%r12), %rbx
movq 0x88(%r12), %rbp
cmpq %rbp, %rbx
je 0x5bf1
movzbl (%rbx), %esi
movzbl 0x1(%rbx), %edx
movl 0x4(%rbx), %ecx
movl 0x8(%rbx), %r8d
movq %r15, %rdi
xorl %eax, %eax
callq 0x2050
addq $0xc, %rbx
jmp 0x5bce
movq 0x68(%r12), %rbp
movq 0x70(%r12), %rbx
cmpq %rbx, %rbp
je 0x5c2b
movzbl (%rbp), %esi
movzbl 0x1(%rbp), %edx
movzwl 0x8(%rbp), %r8d
movzwl 0xa(%rbp), %r9d
movl 0x4(%rbp), %ecx
movl 0xc(%rbp), %eax
movl %eax, (%rsp)
movq %r14, %rdi
xorl %eax, %eax
callq 0x2050
addq $0x10, %rbp
jmp 0x5bfb
addq $0x98, %r12
cmpq %r13, %r12
jne 0x5b9c
movq 0xf62e(%rip), %rdx      # 0x15270
testq %rdx, %rdx
jne 0x5c62
leaq 0xf61a(%rip), %rdi      # 0x15268
leaq 0x9b1e(%rip), %rcx      # 0xf773
movl $0x7, %r8d
xorl %esi, %esi
callq 0x2360
cmpw $0x0, 0xf652(%rip)      # 0x152bc
jne 0x5c75
movw $0xb3, 0xf647(%rip)     # 0x152bc
movl 0xf649(%rip), %edx      # 0x152c4
leaq 0xf5e6(%rip), %rbx      # 0x15268
leaq 0xf85f(%rip), %rsi      # 0x154e8
movq %rbx, %rdi
callq 0xcb37
movzwl 0xf624(%rip), %esi      # 0x152bc
movl 0xf622(%rip), %edx      # 0x152c0
movq %rbx, %rdi
callq 0xe048
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x5cd9
movq 0xf2f5(%rip), %rax      # 0x14fb8
movq (%rax), %rsi
leaq 0x984f(%rip), %rdi      # 0xf51c
callq 0x22a0
xorl %edi, %edi
callq 0x22d0
movq 0xf2d8(%rip), %rax      # 0x14fb8
movq (%rax), %rsi
leaq 0x9832(%rip), %rdi      # 0xf51c
callq 0x22a0
movl $0x40, %edi
callq 0x22d0
movq (%r15), %rdx
leaq 0x99be(%rip), %rsi      # 0xf6c1
movl $0x40, %edi
xorl %eax, %eax
callq 0x2430
callq 0x2551
jmp 0x5d18
jmp 0x5d1d
movq %rax, %rbx
jmp 0x5d3c
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5d3c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23c0