// File: /workspace/github_cpp_makefile_star2_O3/bcthomas[P]shrinksam/src/process_stream.cpp
// Function: process_shrunk_unpaired(std::basic_ifstream<char, std::char_traits<char>>&, std::basic_ofstream<char, std::char_traits<char>>&)
void process_shrunk_unpaired(std::ifstream &in, std::ofstream &shr_out) {
	string line;
	unordered_map<std::string, Shrinksam::Sam*> reads; // string -> Sam object (ptr)

	int total_reads = 0;
	int output_reads = 0;
	int discarded_reads = 0;
	if (in.is_open()) {
		while(getline(in,line)) {
			if (line[0] == '@') {           // header so just write to output
				shr_out << line << endl;
			} else {
				++total_reads;
				Sam *s = new Sam(line);

				if ( (s->get_qname().rfind("/1") != std::string::npos) ||
					 (s->get_qname().rfind("/2") != std::string::npos)) {
					// found a "/1" read or a "/2" read
					string read_base = parse_read_name(s->get_qname());
					auto it = reads.find(read_base);
					if (it == reads.end()) { // not in reads
						reads[read_base] = s; // store the read for later
					} else { // have already seen mate, so print this pair and remove from reads
						if (reads[read_base]->unmapped() && s->unmapped()) {
							discarded_reads += 2;
						} else { // one or the other read mapped, so keep both
							output_reads += 2;
							shr_out << *reads[read_base] << endl;
							shr_out << *s << endl;
						}
						delete reads[read_base]; // free memory for stored Sam object
						delete s;                // free memory for current Sam object
						reads.erase(read_base);  // remove key
					}
				} else {
					// Found nothing to pair on! Only include if sam
					// flag does NOT have bit 4 set
					if (!s->unmapped()) {
						++output_reads;
						shr_out << *s << endl;
						delete s; // free memory for current Sam object
					} else {
						delete s; // free memory for current Sam object
						++discarded_reads;
					}
			    }
			}
		} // end of while() loop
	}
	
	if (Shrinksam::verbose_flag) {
		float percent = (1.0 - ((float) output_reads / (float) total_reads)) * 100;
		cerr << "--- Shrink summary ---" << endl;
		cerr << "Total Reads: " << total_reads << endl;
		cerr << "Discarded Reads: " << discarded_reads << endl;
		cerr << "Output Reads: " << output_reads << endl;
		cerr << percent << "% shrunk" << endl;
	}

    // close files
    shr_out.close();
	// delete anything left in reads map
	for(auto it = reads.begin(); it != reads.end(); ++it)
		delete it->second;
	reads.empty();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x60(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0xa0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
addq $0x78, %rdi
callq 0x3070
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movl $0x0, %r14d
testb %al, %al
je 0x4752
movl $0x0, 0x4(%rsp)
leaq 0x50(%rsp), %r13
movl $0x0, (%rsp)
xorl %r14d, %r14d
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x33f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x474d
movq 0x50(%rsp), %rsi
cmpb $0x40, (%rsi)
jne 0x445a
movq 0x58(%rsp), %rdx
movq %rbx, %rdi
callq 0x3280
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
jmp 0x43e2
movl $0xf0, %edi
callq 0x3230
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
callq 0x4a06
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x5502
movl $0x2, %ecx
movq %rbp, %rdi
leaq 0x1f9e(%rip), %rsi      # 0x6440
movq $-0x1, %rdx
callq 0x32b0
movb $0x1, %bpl
cmpq $-0x1, %rax
jne 0x451c
movl %r14d, 0x2c(%rsp)
leaq 0x40(%rsp), %r14
movq %r14, 0x30(%rsp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0x5502
movl $0x2, %ecx
movq %rbp, %rdi
leaq 0x1f57(%rip), %rsi      # 0x6443
movq $-0x1, %rdx
callq 0x32b0
cmpq $-0x1, %rax
setne %bpl
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x4517
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3240
movl 0x2c(%rsp), %r14d
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4538
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3240
incl %r14d
testb %bpl, %bpl
jne 0x459d
testb $0x4, 0x20(%r15)
jne 0x46a1
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f13
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
incl (%rsp)
movq %r15, %rdi
callq 0x50fe
movl $0xf0, %esi
movq %r15, %rdi
callq 0x3240
jmp 0x43e2
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0x5502
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3f34
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x45e7
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x562e
testq %rax, %rax
je 0x46bf
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5746
movq (%rax), %rax
testb $0x4, 0x20(%rax)
je 0x4622
testb $0x4, 0x20(%r15)
jne 0x46d3
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5746
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x4f13
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f13
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
addl $0x2, (%rsp)
jmp 0x46d8
movq %r15, %rdi
callq 0x50fe
movl $0xf0, %esi
movq %r15, %rdi
callq 0x3240
incl 0x4(%rsp)
jmp 0x43e2
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5746
movq %r15, (%rax)
jmp 0x4728
addl $0x2, 0x4(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5746
movq (%rax), %rbp
testq %rbp, %rbp
je 0x46f7
movq %rbp, %rdi
callq 0x50fe
movl $0xf0, %esi
movq %rbp, %rdi
callq 0x3240
movq %r15, %rdi
callq 0x50fe
movl $0xf0, %esi
movq %r15, %rdi
callq 0x3240
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5a98
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x43e2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3240
jmp 0x43e2
leaq 0x60(%rsp), %r13
leaq 0x4bd8(%rip), %rax      # 0x9331
cmpb $0x1, (%rax)
jne 0x4917
movq 0x4887(%rip), %r15      # 0x8ff0
leaq 0x1cd6(%rip), %rsi      # 0x6446
movl $0x16, %edx
movq %r15, %rdi
callq 0x3280
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq 0x4855(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq 0x4841(%rip), %rdi      # 0x8ff0
leaq 0x1ca7(%rip), %rsi      # 0x645d
movl $0xd, %edx
callq 0x3280
movq 0x4829(%rip), %rdi      # 0x8ff0
movl %r14d, %esi
callq 0x33a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq 0x47f0(%rip), %rdi      # 0x8ff0
leaq 0x1c64(%rip), %rsi      # 0x646b
movl $0x11, %edx
callq 0x3280
movq 0x47d8(%rip), %rdi      # 0x8ff0
movl 0x4(%rsp), %esi
callq 0x33a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq 0x479e(%rip), %rdi      # 0x8ff0
leaq 0x1c24(%rip), %rsi      # 0x647d
movl $0xe, %edx
callq 0x3280
movq 0x4786(%rip), %rdi      # 0x8ff0
movl (%rsp), %esi
callq 0x33a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
cvtsi2ssl (%rsp), %xmm0
cvtsi2ss %r14d, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1b7a(%rip), %xmm1     # 0x6430
subsd %xmm0, %xmm1
mulsd 0x1b76(%rip), %xmm1     # 0x6438
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x471c(%rip), %rdi      # 0x8ff0
callq 0x3350
movq %rax, %r14
leaq 0x1ba9(%rip), %rsi      # 0x648c
movl $0x8, %edx
movq %rax, %rdi
callq 0x3280
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x32e0
movq 0x80(%rsp), %r14
testq %r14, %r14
je 0x494f
movq 0x28(%r14), %rbx
testq %rbx, %rbx
je 0x493d
movq %rbx, %rdi
callq 0x50fe
movl $0xf0, %esi
movq %rbx, %rdi
callq 0x3240
movq (%r14), %r14
jmp 0x4927
leaq 0x70(%rsp), %rdi
callq 0x5464
movq 0x50(%rsp), %rdi
cmpq %r13, %rdi
je 0x4970
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3240
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x49d5
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x49d8
movq 0x40(%rsp), %rsi
jmp 0x49b6
jmp 0x499f
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x49d8
movq 0x18(%rsp), %rsi
incq %rsi
jmp 0x49c6
movq %rax, %rbx
movl $0xf0, %esi
movq %r15, %rdi
callq 0x3240
jmp 0x49d8
jmp 0x49d5
jmp 0x49d5
jmp 0x49d5
jmp 0x49d5
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5464
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x49fe
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3240
movq %rbx, %rdi
callq 0x33c0