// File: /workspace/github_cpp_makefile_star2_O3/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 %ecx, %ecx
movq %rcx, -0x8(%rax)
movb %cl, (%rax)
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rcx, -0x8(%rax)
movq $0x0, 0x38(%rsp)
movb %cl, (%rax)
movq (%rbx), %rax
movq %rax, 0x5b70(%rip)      # 0x9338
movb $0x1, %al
movq %rax, 0x28(%rsp)
leaq 0x2b06(%rip), %r14      # 0x62dc
leaq 0x5a43(%rip), %r15      # 0x9220
leaq 0x270(%rsp), %r12
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
jg 0x3822
cmpl $0x3f, %eax
je 0x37f5
jmp 0x38ea
leal -0x64(%rax), %ecx
cmpl $0x7, %ecx
ja 0x3843
leaq 0x27d3(%rip), %rdx      # 0x6004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0x37f5
cmpl $0x75, %eax
je 0x38de
cmpl $0x76, %eax
jne 0x3e21
movb $0x1, 0x5ad5(%rip)      # 0x9331
jmp 0x37f5
movq 0x5743(%rip), %rax      # 0x8fa8
movq (%rax), %r13
movq 0x8(%rsp), %r14
movq %r13, %rdi
callq 0x30e0
movq $0x0, 0x28(%rsp)
movq %rsp, %rdi
xorl %esi, %esi
movq %r14, %rdx
leaq 0x2a4f(%rip), %r14      # 0x62dc
movq %r13, %rcx
movq %rax, %r8
callq 0x3330
jmp 0x37f5
movq 0x5704(%rip), %rax      # 0x8fa8
movq (%rax), %r14
movq 0x48(%rsp), %r13
movq %r14, %rdi
callq 0x30e0
movq $0x0, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r14, %rcx
leaq 0x2a0b(%rip), %r14      # 0x62dc
movq %rax, %r8
callq 0x3330
jmp 0x37f5
movb $0x1, %al
movq %rax, 0x20(%rsp)
jmp 0x37f5
cmpl $-0x1, %eax
je 0x3942
cmpl $0x56, %eax
jne 0x3e21
movq 0x56f1(%rip), %rbx      # 0x8ff0
leaq 0x29e1(%rip), %rsi      # 0x62e7
movl $0xb, %edx
movq %rbx, %rdi
callq 0x3280
movq 0x58f6(%rip), %r14      # 0x9210
testq %r14, %r14
je 0x3a01
movq %r14, %rdi
callq 0x30e0
movq 0x56be(%rip), %rdi      # 0x8ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x3280
jmp 0x3a18
cmpq $0x0, 0x8(%rsp)
sete %al
movq 0x38(%rsp), %r14
andb %r14b, %al
cmpb $0x1, %al
jne 0x39a7
movq 0x5692(%rip), %rbx      # 0x8ff0
leaq 0x298e(%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 0x5660(%rip), %rdi      # 0x8ff0
callq 0x3030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x3170
jmp 0x3dcc
cmpb $0x1, 0x5983(%rip)      # 0x9331
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %r13
jne 0x3bcb
testb $0x1, %r13b
je 0x3a4d
movq 0x561c(%rip), %rbx      # 0x8ff0
leaq 0x2956(%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 0x3a91
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3380
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x55ba(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
movl $0x1, %ebx
jmp 0x3dcc
movq 0x559c(%rip), %rdi      # 0x8ff0
leaq 0x28f1(%rip), %rsi      # 0x634c
movl $0xe, %edx
callq 0x3280
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x557b(%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 0x3adb
movq 0x553f(%rip), %rbx      # 0x8ff0
leaq 0x28a3(%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 0x3b20
movq 0x550e(%rip), %rdi      # 0x8ff0
leaq 0x2896(%rip), %rsi      # 0x637f
movl $0x1d, %edx
callq 0x3280
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x54ec(%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 0x3b7f
movq 0x54b0(%rip), %rbx      # 0x8ff0
leaq 0x2856(%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 0x547e(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
testb $0x1, %r15b
je 0x3bcb
movq 0x5464(%rip), %rbx      # 0x8ff0
leaq 0x2825(%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 0x5432(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
leaq 0x270(%rsp), %rdi
callq 0x3270
testb $0x1, %r13b
je 0x3bf9
leaq 0x27f1(%rip), %rsi      # 0x63d6
leaq 0x270(%rsp), %rdi
movl $0x8, %edx
callq 0x3160
jmp 0x3c0e
leaq 0x270(%rsp), %rdi
movq %rsp, %rsi
movl $0x8, %edx
callq 0x31e0
leaq 0x70(%rsp), %rdi
callq 0x3390
testb $0x1, %r12b
je 0x3c36
leaq 0x27bc(%rip), %rsi      # 0x63e1
leaq 0x70(%rsp), %rdi
movl $0x10, %edx
callq 0x3050
jmp 0x3c4a
leaq 0x70(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl $0x10, %edx
callq 0x30f0
testb $0x1, %r15b
je 0x3c64
leaq 0x270(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4352
jmp 0x3c76
leaq 0x270(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3fbb
testb $0x1, %r14b
je 0x3d5e
cmpb $0x1, 0x56aa(%rip)      # 0x9331
jne 0x3cf4
movq 0x5360(%rip), %rdi      # 0x8ff0
leaq 0x2756(%rip), %rsi      # 0x63ed
movl $0x9, %edx
callq 0x3280
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x533f(%rip), %rdi      # 0x8ff0
callq 0x3280
movq %rax, %rbx
leaq 0x2737(%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 0x3d0f
movl $0x23, %edx
leaq 0x26ef(%rip), %rsi      # 0x63fc
jmp 0x3d24
cmpb $0x1, 0x561b(%rip)      # 0x9331
jne 0x3d5e
movl $0x8, %edx
leaq 0x26fc(%rip), %rsi      # 0x6420
movq 0x52c5(%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 0x529f(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq 0x526b(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
callq 0x3220
movq 0x521c(%rip), %rax      # 0x8f90
movq (%rax), %rdi
callq 0x3220
movq 0x523d(%rip), %rax      # 0x8fc0
movq (%rax), %rdi
callq 0x3220
movq 0x5226(%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 %ebx, %ebx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3de8
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3240
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3e03
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x60(%rsp), %rdi
callq 0x32d0
movl %ebx, %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 0x3e32
movq %rax, %rbx
jmp 0x3e4b
jmp 0x3e5e
movq %rax, %rbx
jmp 0x3e98
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3310
leaq 0x270(%rsp), %rdi
callq 0x3080
jmp 0x3e61
jmp 0x3e5e
jmp 0x3e5e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3e7d
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3240
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3e98
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x60(%rsp), %rdi
callq 0x32d0
movq %rbx, %rdi
callq 0x33c0
nop