// File: /workspace/github_cpp_makefile_star2_O1/bcthomas[P]shrinksam/src/shrinksam.cpp
// Function: main
int main(int argc, char *argv[]) 
{
	locale loc("en_US");
	cout.imbue(loc);

	int c;                            // for getopt
	string input_name;                // input file name
	string shrunk_name;               // output file name
	bool delete_input = false;        // default to no delete
	bool using_stdin = true;          // default to true
	bool using_stdout = true;         // default to true
	bool unpaired = false;            // default to false, assumes paired

	Shrinksam::progname = argv[0];

//	if (argc < 2) {
//		show_usage(EXIT_FAILURE);
//	}

	while(1) {
		static struct option long_options[] =
		{
			{"verbose",      no_argument, 0, 'v'},
			{"version",      no_argument, 0, 'V'},
			{"help",         no_argument, 0, 'h'},
			{"delete-input", no_argument, 0, 'd'},
			{"unpaired",     no_argument, 0, 'u'},
			{"input",        required_argument, 0, 'i'},
			{"shrunk",       required_argument, 0, 'k'},
			{0, 0, 0, 0}
		};

		/* getopt_long stores the option index here. */
		int option_index = 0;

		c = getopt_long(argc, argv, "vVh?dui:k:", long_options, &option_index);

		/* Detect the end of the options. */
		if (c == -1)
			break;

		switch (c) {
			case 'v':
				Shrinksam::verbose_flag = true;
				break;

			case 'V':
				/* version */
				cerr << "Version is " << Shrinksam::version << endl;
				return EXIT_FAILURE;
				break;

			case 'h':
				show_usage(EXIT_FAILURE);
				break;

			case '?':
				/* getopt_long already printed an error message. */
				break;

			case 'i':
				input_name = optarg;
				using_stdin = false;
				break;

			case 'k':
				shrunk_name = optarg;
				using_stdout = false;
				break;

			case 'd':
				delete_input = true;
				break;

			case 'u':
				unpaired = true;
				break;

			default:
				abort ();
		}
	}

	/* check validity of given argument set */
	if (delete_input) {
		if (input_name.empty()) {
			cerr << "Error: Input file name (-i) is required when using -d option." << endl;
			return EXIT_FAILURE;
		}
	}

	if (Shrinksam::verbose_flag) {
		if (using_stdin)
			cerr << "Input is coming from stdin" << endl;
		else
			cerr << "Input is from " << input_name << endl;

		if (using_stdout)
			cerr << "Shrunk SAM output will be to stdout" << endl;
		else
			cerr << "Shrunk SAM output will be in " << shrunk_name << endl;

		if (delete_input)
			cerr << "Input file will be deleted" << endl;
		if (unpaired)
			cerr << "Input is an unpaired SAM file" << endl;
	}

	ifstream input;
	if (using_stdin)
		input.open("/dev/stdin"); // I know - using this code will only work on unix systems
	else
		input.open(input_name);

	ofstream output;
	if (using_stdout)
		output.open("/dev/stdout"); // I know - using this code will only work on unix systems
	else
		output.open(shrunk_name);

	// process the data
	if (unpaired) {
		process_shrunk_unpaired(input,output);
	} else {
		process_shrunk(input,output);
	}

	// delete original file
	if (delete_input) {
		if (Shrinksam::verbose_flag)
			cerr << "Deleting " << input_name << "... " << endl;
		if (remove(input_name.c_str()) != 0)
			cerr << "ERROR: could not delete input file!" << endl;
		else {
			if (Shrinksam::verbose_flag)
				cerr << "Success!" << endl;
		}
	}

	fclose(stderr);
	fclose(stdout);
	fclose(stdin);
	return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x2b48(%rip), %rsi      # 0x62ab
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x31a0
movq 0x5851(%rip), %rsi      # 0x8fc8
movq (%rsi), %rax
addq -0x18(%rax), %rsi
leaq 0x68(%rsp), %rdi
movq %r14, %rdx
callq 0x3090
leaq 0x68(%rsp), %rdi
callq 0x32d0
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
xorl %r13d, %r13d
movq %r13, -0x8(%rax)
movb %r13b, (%rax)
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq %r13, -0x8(%rax)
movb %r13b, (%rax)
movq (%rbx), %rax
movq %rax, 0x5b76(%rip)      # 0x9338
movb $0x1, %al
movq %rax, 0x28(%rsp)
leaq 0x2b0c(%rip), %r14      # 0x62dc
leaq 0x5a49(%rip), %r15      # 0x9220
leaq 0x270(%rsp), %r12
movq $0x0, 0x38(%rsp)
movb $0x1, %al
movq %rax, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x270(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x3370
cmpl $0x63, %eax
jle 0x3841
leal -0x64(%rax), %ecx
cmpl $0x7, %ecx
ja 0x389f
leaq 0x27da(%rip), %rdx      # 0x6004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
xorl %eax, %eax
movb $0x1, %cl
movq %rcx, 0x38(%rsp)
jmp 0x39bb
cmpl $-0x1, %eax
je 0x394e
cmpl $0x3f, %eax
je 0x38b8
cmpl $0x56, %eax
jne 0x3e59
movl $0xb, %edx
movq 0x578c(%rip), %rdi      # 0x8ff0
leaq 0x2a7c(%rip), %rsi      # 0x62e7
callq 0x3280
movq 0x5999(%rip), %r13      # 0x9210
testq %r13, %r13
je 0x3960
movq %r13, %rdi
callq 0x30e0
movq 0x5761(%rip), %rdi      # 0x8ff0
movq %r13, %rsi
movq %rax, %rdx
callq 0x3280
jmp 0x397e
cmpl $0x75, %eax
je 0x3955
cmpl $0x76, %eax
jne 0x3e59
movb $0x1, 0x5a79(%rip)      # 0x9331
xorl %eax, %eax
jmp 0x39bb
movq 0x56e2(%rip), %rax      # 0x8fa8
movq (%rax), %r14
movq 0x48(%rsp), %r15
movq %r14, %rdi
callq 0x30e0
movq $0x0, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
leaq 0x5930(%rip), %r15      # 0x9220
movq %r14, %rcx
leaq 0x29e2(%rip), %r14      # 0x62dc
movq %rax, %r8
callq 0x3330
xorl %eax, %eax
jmp 0x39bb
movq 0x5698(%rip), %rax      # 0x8fa8
movq (%rax), %r14
movq 0x8(%rsp), %r15
movq %r14, %rdi
callq 0x30e0
movq $0x0, 0x28(%rsp)
movq %rsp, %rdi
xorl %esi, %esi
movq %r15, %rdx
leaq 0x58e8(%rip), %r15      # 0x9220
movq %r14, %rcx
leaq 0x299a(%rip), %r14      # 0x62dc
movq %rax, %r8
callq 0x3330
xorl %eax, %eax
jmp 0x39bb
movl $0x3, %eax
jmp 0x39bb
xorl %eax, %eax
movb $0x1, %cl
movq %rcx, 0x20(%rsp)
jmp 0x39bb
movq 0x5689(%rip), %rcx      # 0x8ff0
movq (%rcx), %rax
movq -0x18(%rax), %rax
leaq (%rcx,%rax), %rdi
movl 0x20(%rcx,%rax), %esi
orl $0x1, %esi
callq 0x3380
movq 0x566b(%rip), %rcx      # 0x8ff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x564d(%rip), %rdi      # 0x8ff0
callq 0x3030
movl $0x1, %r13d
movq %rax, %rdi
callq 0x3170
movl $0x1, %eax
testl %eax, %eax
je 0x37f8
cmpl $0x3, %eax
jne 0x3e03
movq 0x38(%rsp), %r14
testb $0x1, %r14b
je 0x3a30
cmpq $0x0, 0x8(%rsp)
jne 0x3a30
movq 0x560a(%rip), %rbx      # 0x8ff0
leaq 0x2906(%rip), %rsi      # 0x62f3
movl $0x3d, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x55d8(%rip), %rdi      # 0x8ff0
callq 0x3030
movl $0x1, %r13d
movq %rax, %rdi
callq 0x3170
jmp 0x3e03
cmpb $0x1, 0x58fa(%rip)      # 0x9331
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %r13
jne 0x3c01
testb $0x1, %r13b
je 0x3a83
movq 0x5597(%rip), %rbx      # 0x8ff0
leaq 0x28d1(%rip), %rsi      # 0x6331
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
jmp 0x3ac7
movq 0x5566(%rip), %rdi      # 0x8ff0
leaq 0x28bb(%rip), %rsi      # 0x634c
movl $0xe, %edx
callq 0x3280
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x5545(%rip), %rdi      # 0x8ff0
callq 0x3280
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
testb $0x1, %r12b
je 0x3b11
movq 0x5509(%rip), %rbx      # 0x8ff0
leaq 0x286d(%rip), %rsi      # 0x635b
movl $0x23, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
jmp 0x3b56
movq 0x54d8(%rip), %rdi      # 0x8ff0
leaq 0x2860(%rip), %rsi      # 0x637f
movl $0x1d, %edx
callq 0x3280
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x54b6(%rip), %rdi      # 0x8ff0
callq 0x3280
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
testb $0x1, %r14b
je 0x3bb5
movq 0x547a(%rip), %rbx      # 0x8ff0
leaq 0x2820(%rip), %rsi      # 0x639d
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x5448(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
testb $0x1, %r15b
je 0x3c01
movq 0x542e(%rip), %rbx      # 0x8ff0
leaq 0x27ef(%rip), %rsi      # 0x63b8
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x53fc(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
leaq 0x270(%rsp), %rdi
callq 0x3270
testb $0x1, %r13b
je 0x3c2f
leaq 0x27bb(%rip), %rsi      # 0x63d6
leaq 0x270(%rsp), %rdi
movl $0x8, %edx
callq 0x3160
jmp 0x3c44
leaq 0x270(%rsp), %rdi
movq %rsp, %rsi
movl $0x8, %edx
callq 0x31e0
leaq 0x70(%rsp), %rdi
callq 0x3390
testb $0x1, %r12b
je 0x3c6c
leaq 0x2786(%rip), %rsi      # 0x63e1
leaq 0x70(%rsp), %rdi
movl $0x10, %edx
callq 0x3050
jmp 0x3c80
leaq 0x70(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl $0x10, %edx
callq 0x30f0
testb $0x1, %r15b
je 0x3c9a
leaq 0x270(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4394
jmp 0x3cac
leaq 0x270(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3ffd
testb $0x1, %r14b
je 0x3d94
cmpb $0x1, 0x5674(%rip)      # 0x9331
jne 0x3d2a
movq 0x532a(%rip), %rdi      # 0x8ff0
leaq 0x2720(%rip), %rsi      # 0x63ed
movl $0x9, %edx
callq 0x3280
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x5309(%rip), %rdi      # 0x8ff0
callq 0x3280
movq %rax, %rbx
leaq 0x2701(%rip), %rsi      # 0x63f7
movl $0x4, %edx
movq %rax, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq (%rsp), %rdi
callq 0x33e0
testl %eax, %eax
je 0x3d45
movl $0x23, %edx
leaq 0x26b9(%rip), %rsi      # 0x63fc
jmp 0x3d5a
cmpb $0x1, 0x55e5(%rip)      # 0x9331
jne 0x3d94
movl $0x8, %edx
leaq 0x26c6(%rip), %rsi      # 0x6420
movq 0x528f(%rip), %rbx      # 0x8ff0
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x5269(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq 0x5235(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
callq 0x3220
movq 0x51e6(%rip), %rax      # 0x8f90
movq (%rax), %rdi
callq 0x3220
movq 0x5207(%rip), %rax      # 0x8fc0
movq (%rax), %rdi
callq 0x3220
movq 0x51f0(%rip), %rax      # 0x8fb8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x78(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x70(%rsp,%rcx)
callq 0x31d0
leaq 0x168(%rsp), %rdi
callq 0x30b0
leaq 0x270(%rsp), %rdi
callq 0x3080
xorl %r13d, %r13d
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3e1f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3240
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3e3a
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x60(%rsp), %rdi
callq 0x32d0
movl %r13d, %eax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3140
movl $0x1, %edi
callq 0x358c
jmp 0x3e6a
movq %rax, %rbx
jmp 0x3e83
jmp 0x3e96
movq %rax, %rbx
jmp 0x3ed0
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3310
leaq 0x270(%rsp), %rdi
callq 0x3080
jmp 0x3e99
jmp 0x3e96
jmp 0x3e96
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3eb5
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3240
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3ed0
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x60(%rsp), %rdi
callq 0x32d0
movq %rbx, %rdi
callq 0x33c0
nop