// File: bcthomas[P]shrinksam/src/process_stream.cpp
// Function: process_shrunk(std::basic_ifstream<char, std::char_traits<char>>&, std::basic_ofstream<char, std::char_traits<char>>&)
void process_shrunk(std::ifstream &in, std::ofstream &shr_out) {
	string line;

	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_flag() & BAM_FPAIRED) {
					if ((s->get_flag() & BAM_FUNMAP) && (s->get_flag() & BAM_FMUNMAP)) {
						// both mates of the pair are unmapped
						++discarded_reads;
					} else {
						shr_out << *s << endl;
						++output_reads;
					}
				} else {
					++discarded_reads;
				}
				delete s;
			}
		} // 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();
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x3370
movl $0x0, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
movl $0x0, 0xbc(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3400
movb %al, 0x9b(%rsp)
jmp 0x424b
movb 0x9b(%rsp), %al
testb $0x1, %al
jne 0x425b
jmp 0x448a
jmp 0x425d
movq 0xf0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3070
movq %rax, 0x90(%rsp)
jmp 0x427c
movq 0x90(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3330
movb %al, 0x8f(%rsp)
jmp 0x429c
movb 0x8f(%rsp), %al
testb $0x1, %al
jne 0x42ac
jmp 0x4488
xorl %eax, %eax
movl %eax, %esi
leaq 0xc8(%rsp), %rdi
callq 0x34d0
movq %rax, 0x80(%rsp)
jmp 0x42c7
movq 0x80(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x4324
movq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3250
movq %rax, 0x78(%rsp)
jmp 0x42f3
movq 0x78(%rsp), %rdi
movq 0x8ca1(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x4306
jmp 0x4483
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x4642
movl 0xc4(%rsp), %eax
incl %eax
movl %eax, 0xc4(%rsp)
movl $0xf0, %edi
callq 0x3280
movq %rax, 0x70(%rsp)
jmp 0x4345
movq 0x70(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x5160
jmp 0x4359
movq 0x70(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x5570
movl %eax, 0x6c(%rsp)
jmp 0x4379
movl 0x6c(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4444
movq 0xa0(%rsp), %rdi
callq 0x5570
movl %eax, 0x68(%rsp)
jmp 0x439c
movl 0x68(%rsp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x4402
movq 0xa0(%rsp), %rdi
callq 0x5570
movl %eax, 0x64(%rsp)
jmp 0x43bb
movl 0x64(%rsp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x4402
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x4442
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
movl $0xf0, %esi
callq 0x3290
jmp 0x4642
movq 0xe8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x5580
movq %rax, 0x58(%rsp)
jmp 0x441e
movq 0x58(%rsp), %rdi
movq 0x8b76(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x4431
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x4455
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x4481
movq 0x50(%rsp), %rdi
callq 0x5800
movq 0x50(%rsp), %rdi
movl $0xf0, %esi
callq 0x3290
jmp 0x4483
jmp 0x425d
jmp 0x448a
leaq 0x8f20(%rip), %rax      # 0xd3b1
testb $0x1, (%rax)
je 0x461e
cvtsi2ssl 0xc0(%rsp), %xmm0
cvtsi2ssl 0xc4(%rsp), %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x5034(%rip), %xmm0     # 0x94f0
subsd %xmm1, %xmm0
movsd 0x5030(%rip), %xmm1     # 0x94f8
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x9c(%rsp)
movq 0x8b10(%rip), %rdi      # 0xcff0
leaq 0x5023(%rip), %rsi      # 0x950a
callq 0x3260
movq %rax, 0x48(%rsp)
jmp 0x44f3
movq 0x48(%rsp), %rdi
movq 0x8aa1(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x4506
movq 0x8ae3(%rip), %rdi      # 0xcff0
leaq 0x500d(%rip), %rsi      # 0x9521
callq 0x3260
movq %rax, 0x40(%rsp)
jmp 0x4520
movq 0x40(%rsp), %rdi
movl 0xc4(%rsp), %esi
callq 0x3460
movq %rax, 0x38(%rsp)
jmp 0x4538
movq 0x38(%rsp), %rdi
movq 0x8a5c(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x454b
movq 0x8a9e(%rip), %rdi      # 0xcff0
leaq 0x4fd6(%rip), %rsi      # 0x952f
callq 0x3260
movq %rax, 0x30(%rsp)
jmp 0x4565
movq 0x30(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x3460
movq %rax, 0x28(%rsp)
jmp 0x457d
movq 0x28(%rsp), %rdi
movq 0x8a17(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x4590
movq 0x8a59(%rip), %rdi      # 0xcff0
leaq 0x4fa3(%rip), %rsi      # 0x9541
callq 0x3260
movq %rax, 0x20(%rsp)
jmp 0x45aa
movq 0x20(%rsp), %rdi
movl 0xc0(%rsp), %esi
callq 0x3460
movq %rax, 0x18(%rsp)
jmp 0x45c2
movq 0x18(%rsp), %rdi
movq 0x89d2(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x45d5
movss 0x9c(%rsp), %xmm0
movq 0x8a0b(%rip), %rdi      # 0xcff0
callq 0x31d0
movq %rax, 0x10(%rsp)
jmp 0x45f1
movq 0x10(%rsp), %rdi
leaq 0x4f53(%rip), %rsi      # 0x9550
callq 0x3260
movq %rax, 0x8(%rsp)
jmp 0x4609
movq 0x8(%rsp), %rdi
movq 0x898b(%rip), %rsi      # 0xcfa0
callq 0x32b0
jmp 0x461c
jmp 0x461e
movq 0xe8(%rsp), %rdi
callq 0x3390
jmp 0x462d
leaq 0xc8(%rsp), %rdi
callq 0x3180
addq $0xf8, %rsp
retq
leaq 0xc8(%rsp), %rdi
callq 0x3180
movq 0xb0(%rsp), %rdi
callq 0x3470
nopl (%rax)