// File: /workspace/github_cpp_makefile_star2_O2/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 0x89b4(%rip), %r12      # 0xd76c
movq 0x10209(%rip), %r15     # 0x14fc8
leaq 0x18(%rsp), %r13
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
cmpl $0x31, %eax
je 0x4e2e
cmpl $0x43, %eax
je 0x4e13
cmpl $0x4c, %eax
je 0x4e9b
cmpl $0x53, %eax
je 0x4e1c
cmpl $0x58, %eax
je 0x4e25
cmpl $0x6c, %eax
je 0x4e67
cmpl $0x6f, %eax
je 0x4e87
cmpl $0x74, %eax
je 0x4e37
cmpl $0x76, %eax
jne 0x4efc
movb $0x1, 0x1045f(%rip)     # 0x15270
jmp 0x4dc4
orb $0x2, 0x104ba(%rip)     # 0x152d4
jmp 0x4dc4
movb $0x1, 0x1044e(%rip)     # 0x15271
jmp 0x4dc4
orb $0x8, 0x104a8(%rip)     # 0x152d4
jmp 0x4dc4
orb $0x1, 0x1049f(%rip)     # 0x152d4
jmp 0x4dc4
movq (%r15), %rsi
movq %r13, %rdi
leaq 0x17(%rsp), %rdx
callq 0x5fc6
movq %r13, %rdi
callq 0x4ca3
movl %eax, %ebp
movq %r13, %rdi
callq 0x2130
testb %bpl, %bpl
jne 0x4dc4
jmp 0x51f0
movq (%r15), %rax
cmpb $0x0, (%rax)
je 0x4dc4
leaq 0x1041e(%rip), %rdi     # 0x15298
movq %r15, %rsi
callq 0x638c
jmp 0x4dc4
movq (%r15), %rsi
leaq 0x103e7(%rip), %rdi     # 0x15278
callq 0x2450
jmp 0x4dc4
movq (%r15), %rsi
movq %r13, %rdi
leaq 0x17(%rsp), %rdx
callq 0x5fc6
movq 0x20(%rsp), %rax
testq %rax, %rax
jne 0x4ec9
movq %r13, %rdi
leaq 0x8998(%rip), %rsi      # 0xd857
callq 0x2450
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpb $0x2f, -0x1(%rcx,%rax)
je 0x4ee0
movq %r13, %rdi
pushq $0x2f
popq %rsi
callq 0x2220
leaq 0x103c9(%rip), %rdi     # 0x152b0
movq %r13, %rsi
callq 0x63c6
movq %r13, %rdi
callq 0x2130
jmp 0x4dc4
cmpl $-0x1, %eax
jne 0x51dc
movq 0x100d4(%rip), %rax     # 0x14fe0
movslq (%rax), %rax
subl %eax, %ebx
je 0x51e8
leaq (%r14,%rax,8), %r12
callq 0x3839
xorl %r13d, %r13d
testl %ebx, %ebx
cmovlel %r13d, %ebx
leaq 0x18(%rsp), %r14
leaq 0x17(%rsp), %r15
cmpq %r13, %rbx
je 0x4f69
movq (%r12,%r13,8), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x5fc6
movq %r14, %rdi
callq 0x4657
movl %eax, %ebp
movq %r14, %rdi
callq 0x2130
testb %bpl, %bpl
jne 0x4f64
incl 0x10364(%rip)           # 0x152c8
incq %r13
jmp 0x4f33
cmpb $0x0, 0x10300(%rip)     # 0x15270
je 0x4fb9
cmpq $0x0, 0x10426(%rip)     # 0x153a0
je 0x4fd6
leaq 0x8a33(%rip), %rdi      # 0xd9b6
callq 0x2360
movq 0x10401(%rip), %rbx     # 0x15390
leaq 0x103ea(%rip), %r14     # 0x15380
cmpq %r14, %rbx
je 0x4fb1
movq 0x20(%rbx), %rdi
callq 0x2360
movq %rbx, %rdi
callq 0x22b0
movq %rax, %rbx
jmp 0x4f96
pushq $0xa
popq %rdi
callq 0x2310
cmpq $0x0, 0x103df(%rip)     # 0x153a0
je 0x4fd6
callq 0x4bd3
cmpq $0x0, 0x103d0(%rip)     # 0x153a0
jne 0x5180
callq 0x3c79
callq 0x2a47
cmpb $0x1, 0x10289(%rip)     # 0x15270
jne 0x503c
movq 0x10320(%rip), %r14     # 0x15310
movq 0x10321(%rip), %r15     # 0x15318
leaq 0x87bd(%rip), %rbx      # 0xd7bb
cmpq %r15, %r14
je 0x5026
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
jmp 0x4ffe
movq 0xff83(%rip), %rax      # 0x14fb0
movq (%rax), %rsi
leaq 0x888a(%rip), %rdi      # 0xd8c1
callq 0x2290
callq 0x3f57
cmpb $0x1, 0x10228(%rip)     # 0x15270
jne 0x510e
movq 0x104a3(%rip), %r12     # 0x154f8
movq 0x104a4(%rip), %r13     # 0x15500
leaq 0x87a7(%rip), %r14      # 0xd80a
leaq 0x8789(%rip), %r15      # 0xd7f3
cmpq %r13, %r12
je 0x510e
movzwl (%r12), %esi
movq 0x30(%r12), %rdx
movl 0x58(%r12), %ecx
subl 0x50(%r12), %ecx
leaq 0x874c(%rip), %rdi      # 0xd7da
xorl %eax, %eax
callq 0x2050
movq 0x80(%r12), %rbx
movq 0x88(%r12), %rbp
cmpq %rbp, %rbx
je 0x50c8
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 0x50a5
movq 0x68(%r12), %rbp
movq 0x70(%r12), %rbx
cmpq %rbx, %rbp
je 0x5102
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 0x50d2
addq $0x98, %r12
jmp 0x506a
cmpq $0x0, 0x1016a(%rip)     # 0x15280
jne 0x512b
leaq 0x10159(%rip), %rdi     # 0x15278
leaq 0x8705(%rip), %rsi      # 0xd82b
callq 0x2450
cmpw $0x0, 0x10199(%rip)     # 0x152cc
jne 0x513e
movw $0xb3, 0x1018e(%rip)    # 0x152cc
movl 0x10190(%rip), %edx     # 0x152d4
leaq 0x1012d(%rip), %rbx     # 0x15278
leaq 0x103a6(%rip), %rsi     # 0x154f8
movq %rbx, %rdi
callq 0xb4b3
movzwl 0x1016b(%rip), %esi     # 0x152cc
movl 0x10169(%rip), %edx     # 0x152d0
movq %rbx, %rdi
callq 0xc878
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xfe51(%rip), %r15      # 0x14fd8
movq (%r15), %rcx
leaq 0x8600(%rip), %rdi      # 0xd791
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2460
movq 0x101ed(%rip), %r14     # 0x15390
leaq 0x101d6(%rip), %r12     # 0x15380
leaq 0x86f1(%rip), %rbx      # 0xd8a2
cmpq %r12, %r14
je 0x51d4
movq (%r15), %rdi
movq 0x20(%r14), %rdx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2320
movq %r14, %rdi
callq 0x22b0
movq %rax, %r14
jmp 0x51b1
pushq $0x41
popq %rdi
callq 0x22c0
cmpl $0x68, %eax
jne 0x51e8
xorl %edi, %edi
callq 0x4d7e
pushq $0x40
popq %rdi
callq 0x4d7e
movq (%r15), %rdx
leaq 0x857f(%rip), %rsi      # 0xd779
pushq $0x40
popq %rdi
xorl %eax, %eax
callq 0x2430
jmp 0x520c
jmp 0x5211
jmp 0x520c
jmp 0x5211
movq %rax, %rbx
jmp 0x521e
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x23c0