// File: /workspace/github_cpp_makefile_star2_O3/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 0x9f0f(%rip), %r12      # 0xf6b4
movq 0xf81c(%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 0x57ec
cmpl $0x6e, %eax
jle 0x580c
cmpl $0x6f, %eax
je 0x5929
cmpl $0x74, %eax
je 0x5854
cmpl $0x76, %eax
jne 0x5c4f
movb $0x1, 0xfa76(%rip)      # 0x15260
jmp 0x57b5
cmpl $0x4b, %eax
jg 0x583a
cmpl $0x31, %eax
je 0x591d
cmpl $0x43, %eax
jne 0x597f
orb $0x2, 0xfaba(%rip)      # 0x152c4
jmp 0x57b5
cmpl $0x58, %eax
je 0x5973
cmpl $0x6c, %eax
jne 0x5c2d
movq (%r15), %rax
cmpb $0x0, (%rax)
je 0x57b5
leaq 0xfa5b(%rip), %rdi      # 0x15288
movq %r15, %rsi
callq 0x7144
jmp 0x57b5
cmpl $0x4c, %eax
je 0x5898
cmpl $0x53, %eax
jne 0x5c4f
movb $0x1, 0xfa12(%rip)      # 0x15261
jmp 0x57b5
movq (%r15), %rsi
movq %r13, %rdi
leaq 0xf(%rsp), %rdx
callq 0x6dc8
movq %r13, %rdi
callq 0x564f
movl %eax, %ebp
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x588a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
testb %bpl, %bpl
jne 0x57b5
jmp 0x5c6f
movq (%r15), %rsi
movq %r13, %rdi
leaq 0xf(%rsp), %rdx
callq 0x6dc8
movq 0x18(%rsp), %rax
testq %rax, %rax
jne 0x58d0
movl $0x1, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x9ed9(%rip), %rcx      # 0xf79f
callq 0x2360
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
cmpb $0x2f, -0x1(%rcx,%rax)
je 0x58e9
movq %r13, %rdi
movl $0x2f, %esi
callq 0x2220
leaq 0xf9b0(%rip), %rdi      # 0x152a0
movq %r13, %rsi
callq 0x7190
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x57b5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x57b5
orb $0x1, 0xf9a0(%rip)      # 0x152c4
jmp 0x57b5
movq (%r15), %rbp
movq %r14, %rbx
movq %r12, %r14
movq %r13, %r12
movq %r15, %r13
movq 0xf931(%rip), %r15      # 0x15270
movq %rbp, %rdi
callq 0x2130
leaq 0xf91a(%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 0x57b5
orb $0x8, 0xf94a(%rip)      # 0x152c4
jmp 0x57b5
cmpl $-0x1, %eax
jne 0x5c4f
movq 0xf651(%rip), %rax      # 0x14fe0
movslq (%rax), %r15
subl %r15d, %ebx
je 0x5c4f
callq 0x3d99
testl %ebx, %ebx
jle 0x5a00
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 0x6dc8
movq %rbx, %rdi
callq 0x4e8b
movl %eax, %ebp
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x59ed
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
testb %bpl, %bpl
jne 0x59f8
incl 0xf8c0(%rip)            # 0x152b8
incq %r13
cmpq %r13, %r12
jne 0x59b8
cmpb $0x0, 0xf859(%rip)      # 0x15260
je 0x5a55
cmpq $0x0, 0xf97f(%rip)      # 0x15390
je 0x5a72
leaq 0x9ee4(%rip), %rdi      # 0xf8fe
callq 0x2350
movq 0xf95a(%rip), %rbx      # 0x15380
leaq 0xf943(%rip), %r14      # 0x15370
cmpq %r14, %rbx
je 0x5a4b
movq 0x20(%rbx), %rdi
callq 0x2350
movq %rbx, %rdi
callq 0x22c0
movq %rax, %rbx
cmpq %r14, %rax
jne 0x5a32
movl $0xa, %edi
callq 0x2300
cmpq $0x0, 0xf933(%rip)      # 0x15390
je 0x5a72
callq 0x54e4
cmpq $0x0, 0xf924(%rip)      # 0x15390
jne 0x5c85
callq 0x4328
callq 0x2e1f
cmpb $0x1, 0xf7dd(%rip)      # 0x15260
jne 0x5adb
movq 0xf874(%rip), %r14      # 0x15300
movq 0xf875(%rip), %r15      # 0x15308
cmpq %r15, %r14
je 0x5ac5
leaq 0x9c64(%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 0x5a9f
movq 0xf4ec(%rip), %rax      # 0x14fb8
movq (%rax), %rsi
leaq 0x9d33(%rip), %rdi      # 0xf809
callq 0x22a0
callq 0x4632
cmpb $0x1, 0xf779(%rip)      # 0x15260
jne 0x5bb1
movq 0xf9f4(%rip), %r12      # 0x154e8
movq 0xf9f5(%rip), %r13      # 0x154f0
cmpq %r13, %r12
je 0x5bb1
leaq 0x9c47(%rip), %r14      # 0xf752
leaq 0x9c29(%rip), %r15      # 0xf73b
movzwl (%r12), %esi
movq 0x30(%r12), %rdx
movl 0x58(%r12), %ecx
subl 0x50(%r12), %ecx
leaq 0x9bf5(%rip), %rdi      # 0xf722
xorl %eax, %eax
callq 0x2050
movq 0x80(%r12), %rbx
movq 0x88(%r12), %rbp
cmpq %rbp, %rbx
je 0x5b67
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 0x5b44
movq 0x68(%r12), %rbp
movq 0x70(%r12), %rbx
cmpq %rbx, %rbp
je 0x5ba1
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 0x5b71
addq $0x98, %r12
cmpq %r13, %r12
jne 0x5b12
cmpq $0x0, 0xf6b7(%rip)      # 0x15270
jne 0x5bd8
leaq 0xf6a6(%rip), %rdi      # 0x15268
leaq 0x9baa(%rip), %rcx      # 0xf773
movl $0x7, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2360
cmpw $0x0, 0xf6dc(%rip)      # 0x152bc
jne 0x5beb
movw $0xb3, 0xf6d1(%rip)     # 0x152bc
movl 0xf6d3(%rip), %edx      # 0x152c4
leaq 0xf670(%rip), %rbx      # 0x15268
leaq 0xf8e9(%rip), %rsi      # 0x154e8
movq %rbx, %rdi
callq 0xcae1
movzwl 0xf6ae(%rip), %esi      # 0x152bc
movl 0xf6ac(%rip), %edx      # 0x152c0
movq %rbx, %rdi
callq 0xe01c
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x5c4f
movq 0xf37f(%rip), %rax      # 0x14fb8
movq (%rax), %rsi
leaq 0x98d9(%rip), %rdi      # 0xf51c
callq 0x22a0
xorl %edi, %edi
callq 0x22d0
movq 0xf362(%rip), %rax      # 0x14fb8
movq (%rax), %rsi
leaq 0x98bc(%rip), %rdi      # 0xf51c
callq 0x22a0
movl $0x40, %edi
callq 0x22d0
movq (%r15), %rdx
leaq 0x9a48(%rip), %rsi      # 0xf6c1
movl $0x40, %edi
xorl %eax, %eax
callq 0x2430
callq 0x2551
jmp 0x5c8e
jmp 0x5c93
movq %rax, %rbx
jmp 0x5cb2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5cb2
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23c0