// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x40(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0xc0(%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, (%rsp)
movl $0x0, %r14d
movl $0x0, 0x4(%rsp)
testb %al, %al
je 0x47e2
movl $0x0, (%rsp)
leaq 0x30(%rsp), %r13
xorl %r14d, %r14d
movl $0x0, 0x4(%rsp)
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 0x47dd
movq 0x30(%rsp), %rsi
cmpb $0x40, (%rsi)
jne 0x449c
movq 0x38(%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 0x4424
movl $0xf0, %edi
callq 0x3230
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x4a9e
movl %r14d, %r15d
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x55ce
movl $0x2, %ecx
movq %r14, %rdi
leaq 0x1f51(%rip), %rsi      # 0x6440
movq $-0x1, %rdx
callq 0x32b0
movq %rax, %rbp
movb $0x1, %r14b
cmpq $-0x1, %rax
jne 0x4553
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x55ce
movl $0x2, %ecx
movq %r14, %rdi
leaq 0x1f04(%rip), %rsi      # 0x6443
movq $-0x1, %rdx
callq 0x32b0
cmpq $-0x1, %rax
setne %r14b
cmpq $-0x1, %rbp
movq 0x8(%rsp), %rbp
jne 0x4580
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x4580
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x3240
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x459c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3240
incl 0x4(%rsp)
testb %r14b, %r14b
jne 0x4609
testb $0x4, 0x20(%rbp)
movl %r15d, %r14d
jne 0x4720
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4fab
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 %r14d
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x5196
movl $0xf0, %esi
movq %r15, %rdi
callq 0x3240
jmp 0x4424
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x55ce
movl %r15d, %r14d
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x3f76
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x4657
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x56ee
testq %rax, %rax
je 0x473d
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5806
movq (%rax), %rax
testb $0x4, 0x20(%rax)
je 0x469c
movq 0x8(%rsp), %rax
testb $0x4, 0x20(%rax)
jne 0x4759
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5806
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x4fab
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 0x8(%rsp), %rsi
callq 0x4fab
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, %r14d
jmp 0x475d
movq %rbp, %rdi
callq 0x5196
movl $0xf0, %esi
movq %rbp, %rdi
callq 0x3240
incl (%rsp)
jmp 0x4424
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5806
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x47b8
addl $0x2, (%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5806
movq (%rax), %rbp
testq %rbp, %rbp
je 0x478c
movq %rbp, %rdi
callq 0x5196
movl $0xf0, %esi
movq %rbp, %rdi
callq 0x3240
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x5196
movl $0xf0, %esi
movq %r15, %rdi
callq 0x3240
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5b62
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4424
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3240
jmp 0x4424
leaq 0x40(%rsp), %r13
leaq 0x4b48(%rip), %rax      # 0x9331
cmpb $0x1, (%rax)
jne 0x49a8
movq 0x47f7(%rip), %r15      # 0x8ff0
leaq 0x1c46(%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 0x47c5(%rip), %rdi      # 0x8ff0
callq 0x3030
movq %rax, %rdi
callq 0x3170
movq 0x47b1(%rip), %rdi      # 0x8ff0
leaq 0x1c17(%rip), %rsi      # 0x645d
movl $0xd, %edx
callq 0x3280
movq 0x4799(%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 0x475f(%rip), %rdi      # 0x8ff0
leaq 0x1bd3(%rip), %rsi      # 0x646b
movl $0x11, %edx
callq 0x3280
movq 0x4747(%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
movq 0x470e(%rip), %rdi      # 0x8ff0
leaq 0x1b94(%rip), %rsi      # 0x647d
movl $0xe, %edx
callq 0x3280
movq 0x46f6(%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
cvtsi2ss %r14d, %xmm0
cvtsi2ssl 0x4(%rsp), %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1ae9(%rip), %xmm1     # 0x6430
subsd %xmm0, %xmm1
mulsd 0x1ae5(%rip), %xmm1     # 0x6438
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x468b(%rip), %rdi      # 0x8ff0
callq 0x3350
movq %rax, %r14
leaq 0x1b18(%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 0xa0(%rsp), %r14
testq %r14, %r14
je 0x49e0
movq 0x28(%r14), %rbx
testq %rbx, %rbx
je 0x49db
movq %rbx, %rdi
callq 0x5196
movl $0xf0, %esi
movq %rbx, %rdi
callq 0x3240
movq (%r14), %r14
jmp 0x49b8
leaq 0x90(%rsp), %rdi
callq 0x5530
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x4a04
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3240
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4a69
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x4a6c
movq 0x60(%rsp), %rsi
jmp 0x4a4a
jmp 0x4a33
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x4a6c
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x4a5a
movq %rax, %rbx
movl $0xf0, %esi
movq %rbp, %rdi
callq 0x3240
jmp 0x4a6c
jmp 0x4a69
jmp 0x4a69
jmp 0x4a69
jmp 0x4a69
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x5530
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x4a95
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3240
movq %rbx, %rdi
callq 0x33c0
nop