// File: /workspace/github_c_makefile_star2_O1/OpenGene[P]pecheck/src/read.cpp
// Function: ReadPair::fastMerge()
Read* ReadPair::fastMerge(){
	Read* rcRight = mRight->reverseComplement();
	int len1 = mLeft->length();
	int len2 = rcRight->length();
	// use the pointer directly for speed
	const char* str1 = mLeft->mSeq.mStr.c_str();
	const char* str2 = rcRight->mSeq.mStr.c_str();
	const char* qual1 = mLeft->mQuality.c_str();
	const char* qual2 = rcRight->mQuality.c_str();

	// we require at least 30 bp overlapping to merge a pair
	const int MIN_OVERLAP = 30;
	bool overlapped = false;
	int olen = MIN_OVERLAP;
	int diff = 0;
	// the diff count for 1 high qual + 1 low qual
	int lowQualDiff = 0;

	while(olen <= min(len1, len2)){
		diff = 0;
		lowQualDiff = 0;
		bool ok = true;
		int offset = len1 - olen;
		for(int i=0;i<olen;i++){
			if(str1[offset+i] != str2[i]){
				diff++;
				// one is >= Q30 and the other is <= Q15
				if((qual1[offset+i]>='?' && qual2[i]<='0') || (qual1[offset+i]<='0' && qual2[i]>='?')){
					lowQualDiff++;
				}
				// we disallow high quality diff, and only allow up to 3 low qual diff
				if(diff>lowQualDiff || lowQualDiff>=3){
					ok = false;
					break;
				}
			}
		}
		if(ok){
			overlapped = true;
			break;
		}
		olen++;
	}

	if(overlapped){
		int offset = len1 - olen;
		stringstream ss;
		ss << mLeft->mName << " merged offset:" << offset << " overlap:" << olen << " diff:" << diff;
		string mergedName = ss.str();
		string mergedSeq = mLeft->mSeq.mStr.substr(0, offset) + rcRight->mSeq.mStr;
		string mergedQual = mLeft->mQuality.substr(0, offset) + rcRight->mQuality;
		// quality adjuction and correction for low qual diff
		for(int i=0;i<olen;i++){
			if(str1[offset+i] != str2[i]){
				if(qual1[offset+i]>='?' && qual2[i]<='0'){
					mergedSeq[offset+i] = str1[offset+i];
					mergedQual[offset+i] = qual1[offset+i];
				} else {
					mergedSeq[offset+i] = str2[i];
					mergedQual[offset+i] = qual2[i];
				}
			} else {
				// add the quality of the pair to make a high qual
				mergedQual[offset+i] =  qual1[offset+i] + qual2[i] - 33;
			}
		}
		delete rcRight;
		return new Read(mergedName, mergedSeq, "+", mergedQual);
	}

	delete rcRight;
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
callq 0xafbc
movq %rax, %r14
movq (%rbx), %rdi
addq $0x20, %rdi
callq 0xcc9c
movl %eax, %ebp
leaq 0x20(%r14), %rdi
callq 0xcc9c
movq %rbx, 0x38(%rsp)
movq (%rbx), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, (%rsp)
movq 0x60(%rcx), %r12
movq 0x20(%r14), %rbx
movq %r14, 0x10(%rsp)
movq 0x60(%r14), %r14
cmpl %ebp, %eax
cmovgel %ebp, %eax
cmpl $0x1e, %eax
jge 0xbf1e
xorl %r13d, %r13d
movl $0x1e, %r15d
xorl %ecx, %ecx
jmp 0xbfd3
movl $0x1e, %r15d
xorl %ecx, %ecx
movl %ebp, %esi
subl %r15d, %esi
movl %r15d, %edx
cmpl $0x1, %r15d
adcl $0x0, %r15d
movslq %esi, %rdi
movq (%rsp), %rsi
leaq (%rsi,%rdi), %r8
addq %r12, %rdi
negq %r15
movb $0x1, %sil
movl $0x1, %r9d
xorl %r10d, %r10d
xorl %r13d, %r13d
movb -0x1(%r8,%r9), %r11b
cmpb -0x1(%rbx,%r9), %r11b
je 0xbf98
movb -0x1(%rdi,%r9), %r11b
cmpb $0x3f, %r11b
jl 0xbf75
cmpb $0x31, -0x1(%r14,%r9)
jl 0xbf83
cmpb $0x30, %r11b
jg 0xbf86
cmpb $0x3f, -0x1(%r14,%r9)
jl 0xbf86
incl %r10d
leal 0x1(%r13), %r11d
cmpl %r10d, %r13d
jge 0xbfb3
movl %r11d, %r13d
cmpl $0x2, %r10d
jg 0xbfb3
cmpq %rdx, %r9
setb %sil
leaq (%r15,%r9), %r11
incq %r11
incq %r9
cmpq $0x1, %r11
jne 0xbf56
movb $0x1, %cl
jmp 0xbfb6
movl %r11d, %r13d
movzbl %sil, %esi
movl %esi, %r15d
andl $0x1, %r15d
addl %edx, %r15d
testb $0x1, %sil
je 0xbfd3
cmpl %eax, %r15d
jle 0xbf26
testb $0x1, %cl
je 0xc0f7
leaq 0x120(%rsp), %rdi
callq 0x31e0
leaq 0x130(%rsp), %rdi
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
callq 0x3350
leaq 0x1861(%rip), %rsi      # 0xd86d
movl $0xf, %edx
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
callq 0x3350
movq 0x8(%rsp), %rdi
subl %r15d, %ebp
movl %ebp, %esi
callq 0x34f0
leaq 0x1847(%rip), %rsi      # 0xd87d
movl $0x9, %edx
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
callq 0x3350
movq 0x8(%rsp), %rdi
movl %r15d, %esi
callq 0x34f0
leaq 0x1829(%rip), %rsi      # 0xd887
movl $0x6, %edx
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
callq 0x3350
movq 0x8(%rsp), %rdi
movl %r13d, %esi
callq 0x34f0
leaq 0x138(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x3480
movq 0x38(%rsp), %rax
movq (%rax), %rsi
addq $0x20, %rsi
movslq %ebp, %r13
leaq 0x18(%rsp), %rdi
xorl %edx, %edx
movq %r13, %rcx
callq 0x3300
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
leaq 0x18(%rsp), %rdi
callq 0x30e0
leaq 0x50(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xc171
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0xc177
movq 0x10(%rsp), %rbx
leaq 0x70(%rbx), %rax
cmpq %rax, %r14
je 0xc113
movq (%rax), %rsi
incq %rsi
movq %r14, %rdi
callq 0x32e0
movq 0x40(%rbx), %rdi
leaq 0x50(%rbx), %rax
cmpq %rax, %rdi
je 0xc12b
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movq 0x20(%rbx), %rdi
leaq 0x30(%rbx), %rax
cmpq %rax, %rdi
je 0xc143
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movq (%rbx), %rdi
movq %rbx, %rax
addq $0x10, %rax
cmpq %rax, %rdi
je 0xc15d
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movl $0x88, %esi
movq %rbx, %rdi
callq 0x32e0
xorl %ebx, %ebx
jmp 0xc4d4
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x28(%rsp), %rbp
movq -0x10(%rbp), %rdi
cmpq %rbp, %rdi
je 0xc1aa
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x38(%rsp), %rax
movq (%rax), %rsi
addq $0x60, %rsi
leaq 0x100(%rsp), %rdi
xorl %edx, %edx
movq %r13, %rcx
callq 0x3300
movq 0x10(%rsp), %rax
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
leaq 0x100(%rsp), %rdi
callq 0x30e0
movq %rbp, 0x18(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xc205
movq %rdx, 0x18(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x28(%rsp)
jmp 0xc20c
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x20(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc245
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32e0
cmpl $0x2, %r15d
movl $0x1, %eax
cmovgel %r15d, %eax
xorl %ecx, %ecx
movq (%rsp), %rdx
movb (%rdx,%r13), %sil
movb (%rbx,%rcx), %dil
movb (%r12,%r13), %dl
cmpb %dil, %sil
jne 0xc272
addb (%r14,%rcx), %dl
addb $-0x21, %dl
jmp 0xc29a
cmpb $0x3f, %dl
jl 0xc28d
cmpb $0x30, (%r14,%rcx)
jg 0xc28d
movq 0x40(%rsp), %rdx
movb %sil, (%rdx,%r13)
movb (%r12,%r13), %dl
jmp 0xc29a
movq 0x40(%rsp), %rdx
movb %dil, (%rdx,%r13)
movb (%r14,%rcx), %dl
movq 0x18(%rsp), %rsi
movb %dl, (%rsi,%r13)
incq %rcx
incq %r13
cmpq %rcx, %rax
jne 0xc254
movq 0x10(%rsp), %rbx
movq 0x60(%rbx), %rdi
leaq 0x70(%rbx), %rax
cmpq %rax, %rdi
je 0xc2cb
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movq 0x40(%rbx), %rdi
leaq 0x50(%rbx), %rax
cmpq %rax, %rdi
je 0xc2e3
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movq 0x20(%rbx), %rdi
leaq 0x30(%rbx), %rax
cmpq %rax, %rdi
je 0xc2fb
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movq (%rbx), %rdi
movq %rbx, %rax
addq $0x10, %rax
cmpq %rax, %rdi
je 0xc315
movq (%rax), %rsi
incq %rsi
callq 0x32e0
movl $0x88, %esi
movq %rbx, %rdi
callq 0x32e0
movl $0x88, %edi
callq 0x32d0
movq %rax, %rbx
leaq 0xd0(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0xe0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %rdi
callq 0x4ea0
leaq 0xb0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0xa0(%rsp), %rdi
callq 0x4ea0
leaq 0x90(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x1364(%rip), %rsi      # 0xd6f9
leaq 0x135e(%rip), %rdx      # 0xd6fa
leaq 0x80(%rsp), %rdi
callq 0x4e10
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x4ea0
leaq 0xc0(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %rbx, %rdi
xorl %r9d, %r9d
callq 0xa640
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xc408
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0xc425
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0xc442
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0xc0(%rsp), %rdi
cmpq %r15, %rdi
je 0xc45f
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x18(%rsp), %rdi
cmpq %rbp, %rdi
je 0xc476
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xc492
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc4b3
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32e0
movq 0x5ade(%rip), %rsi      # 0x11f98
leaq 0x120(%rsp), %rdi
callq 0x3210
leaq 0x1a0(%rsp), %rdi
callq 0x30f0
movq %rbx, %rax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, (%rsp)
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xc50a
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0xc50a
movq %rax, (%rsp)
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0xc52d
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0xc52d
movq %rax, (%rsp)
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0xc550
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0xc550
movq %rax, (%rsp)
movq 0xc0(%rsp), %rdi
cmpq %r15, %rdi
je 0xc573
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0xc573
movq %rax, (%rsp)
movl $0x88, %esi
movq %rbx, %rdi
callq 0x32e0
movq 0x18(%rsp), %rdi
cmpq %rbp, %rdi
je 0xc5c2
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0xc5c2
movq %rax, (%rsp)
jmp 0xc580
movq %rax, (%rsp)
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc5c2
movq 0x110(%rsp), %rsi
jmp 0xc58f
movq %rax, (%rsp)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xc5de
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32e0
leaq 0xf0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc626
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32e0
jmp 0xc626
movq %rax, (%rsp)
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc5de
movq 0x28(%rsp), %rsi
jmp 0xc5d6
movq %rax, (%rsp)
jmp 0xc5de
jmp 0xc622
movq %rax, (%rsp)
movq 0x596b(%rip), %rsi      # 0x11f98
leaq 0x120(%rsp), %rdi
callq 0x3210
leaq 0x1a0(%rsp), %rdi
callq 0x30f0
movq (%rsp), %rdi
callq 0x3510