// File: /workspace/github_c_makefile_star2_O3/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 $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, %r14
movq 0x8(%rdi), %rdi
callq 0xaca4
movq %rax, %rbp
movq %rax, 0x8(%rsp)
movq (%r14), %rdi
addq $0x20, %rdi
callq 0xc964
movl %eax, %ebx
leaq 0x20(%rbp), %rdi
callq 0xc964
movq %rbx, %r11
movq 0x60(%rbp), %r15
cmpl %r11d, %eax
cmovgel %r11d, %eax
cmpl $0x1e, %eax
jge 0xbc61
leaq 0x70(%rbp), %rax
cmpq %rax, %r15
je 0xbc00
movq (%rax), %rsi
incq %rsi
movq %r15, %rdi
callq 0x32d0
movq 0x8(%rsp), %rbx
movq 0x40(%rbx), %rdi
leaq 0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xbc1d
movq (%rax), %rsi
incq %rsi
callq 0x32d0
movq 0x20(%rbx), %rdi
leaq 0x30(%rbp), %rax
cmpq %rax, %rdi
je 0xbc35
movq (%rax), %rsi
incq %rsi
callq 0x32d0
movq (%rbx), %rdi
addq $0x10, %rbp
cmpq %rbp, %rdi
je 0xbc4d
movq (%rbp), %rsi
incq %rsi
callq 0x32d0
movl $0x88, %esi
movq %rbx, %rdi
callq 0x32d0
xorl %ebx, %ebx
jmp 0xc183
movq (%r14), %rcx
movq 0x20(%rcx), %r10
movq 0x60(%rcx), %rsi
movq 0x20(%rbp), %rbx
incl %eax
leal -0x1e(%r11), %ecx
movl $0x1e, %r12d
movq %rbp, 0x68(%rsp)
movq %r14, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movslq %ecx, %r14
leaq (%r10,%r14), %rdx
addq %r14, %rsi
movl %r11d, %edi
subl %r12d, %edi
movl %edi, 0x44(%rsp)
movslq %edi, %rdi
movq %rdi, 0x70(%rsp)
xorl %edi, %edi
xorl %r8d, %r8d
xorl %ebp, %ebp
movb (%rdx,%rdi), %r9b
cmpb (%rbx,%rdi), %r9b
je 0xbce8
movb (%rsi,%rdi), %r9b
cmpb $0x3f, %r9b
jl 0xbccb
cmpb $0x31, (%r15,%rdi)
jl 0xbcd8
jmp 0xbcdb
cmpb $0x30, %r9b
jg 0xbcdb
cmpb $0x3f, (%r15,%rdi)
jl 0xbcdb
incl %r8d
cmpl %r8d, %ebp
jge 0xbcf2
cmpl $0x2, %r8d
jg 0xbcf2
incl %ebp
incq %rdi
cmpq %rdi, %r12
jne 0xbcae
jmp 0xbd0b
incq %r12
decl %ecx
cmpq %rax, %r12
movq 0x68(%rsp), %rbp
movq 0x30(%rsp), %rsi
jne 0xbc8b
jmp 0xbbe9
movq %r10, 0x78(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x31d0
leaq 0x150(%rsp), %rdi
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
callq 0x3340
leaq 0x1b2d(%rip), %rsi      # 0xd86d
movl $0xf, %edx
movq %rax, %r13
movq %rax, %rdi
callq 0x3340
movq %r13, %rdi
movl 0x44(%rsp), %esi
callq 0x34e0
movq %rax, %r13
leaq 0x1b17(%rip), %rsi      # 0xd87d
movl $0x9, %edx
movq %rax, %rdi
callq 0x3340
movq %r13, %rdi
movl %r12d, %esi
callq 0x34e0
movq %rax, %r13
leaq 0x1aff(%rip), %rsi      # 0xd887
movl $0x6, %edx
movq %rax, %rdi
callq 0x3340
movq %r13, %rdi
movl %ebp, %esi
callq 0x34e0
leaq 0x158(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x3470
movq 0x38(%rsp), %rax
movq (%rax), %rsi
addq $0x20, %rsi
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
movq 0x70(%rsp), %rcx
callq 0x32f0
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
leaq 0x10(%rsp), %rdi
callq 0x30e0
leaq 0x58(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xbe0f
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0xbe15
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rbp
movq -0x10(%rbp), %rdi
cmpq %rbp, %rdi
je 0xbe48
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x38(%rsp), %rax
movq (%rax), %rsi
addq $0x60, %rsi
leaq 0x120(%rsp), %rdi
xorl %edx, %edx
movq 0x70(%rsp), %rcx
callq 0x32f0
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
leaq 0x120(%rsp), %rdi
callq 0x30e0
movq %rbp, 0x10(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
movq 0x30(%rsp), %r8
je 0xbeaa
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0xbeb1
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xbeef
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x30(%rsp), %r8
xorl %eax, %eax
movq 0x78(%rsp), %rdi
movb (%rdi,%r14), %dl
movb (%rbx,%rax), %sil
movb (%r8,%r14), %cl
cmpb %sil, %dl
jne 0xbf10
addb (%r15,%rax), %cl
addb $-0x21, %cl
jmp 0xbf38
cmpb $0x3f, %cl
jl 0xbf2b
cmpb $0x30, (%r15,%rax)
jg 0xbf2b
movq 0x48(%rsp), %rcx
movb %dl, (%rcx,%r14)
movb (%r8,%r14), %cl
jmp 0xbf38
movq 0x48(%rsp), %rcx
movb %sil, (%rcx,%r14)
movb (%r15,%rax), %cl
movq 0x10(%rsp), %rdx
movb %cl, (%rdx,%r14)
incq %rax
incq %r14
cmpq %rax, %r12
jne 0xbef6
movq 0x8(%rsp), %rbx
movq 0x60(%rbx), %rdi
movq 0x68(%rsp), %r14
leaq 0x70(%r14), %rax
cmpq %rax, %rdi
je 0xbf6e
movq (%rax), %rsi
incq %rsi
callq 0x32d0
movq 0x40(%rbx), %rdi
leaq 0x50(%r14), %rax
cmpq %rax, %rdi
je 0xbf86
movq (%rax), %rsi
incq %rsi
callq 0x32d0
movq 0x20(%rbx), %rdi
leaq 0x30(%r14), %rax
cmpq %rax, %rdi
je 0xbf9e
movq (%rax), %rsi
incq %rsi
callq 0x32d0
movq (%rbx), %rdi
addq $0x10, %r14
cmpq %r14, %rdi
je 0xbfb5
movq (%r14), %rsi
incq %rsi
callq 0x32d0
movl $0x88, %esi
movq %rbx, %rdi
callq 0x32d0
movl $0x88, %edi
callq 0x32c0
movq %rax, %rbx
leaq 0xf0(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rdi
callq 0x4d7a
leaq 0xd0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %rdi
callq 0x4d7a
leaq 0xb0(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x16c4(%rip), %rsi      # 0xd6f9
leaq 0x16be(%rip), %rdx      # 0xd6fa
leaq 0xa0(%rsp), %rdi
callq 0x4ce4
leaq 0x90(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %rdi
callq 0x4d7a
leaq 0xe0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x80(%rsp), %r8
movq %rbx, %rdi
xorl %r9d, %r9d
callq 0xa328
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0xc0b7
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
je 0xc0d4
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0xc0(%rsp), %rdi
cmpq %r12, %rdi
je 0xc0f1
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0xc10e
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0xc125
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0xc141
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x32d0
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc162
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x5e2f(%rip), %rsi      # 0x11f98
leaq 0x140(%rsp), %rdi
callq 0x3200
leaq 0x1c0(%rsp), %rdi
callq 0x30f0
movq %rbx, %rax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0xc1c1
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0xc1c1
movq %rax, 0x8(%rsp)
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
je 0xc1e5
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0xc1e5
movq %rax, 0x8(%rsp)
movq 0xc0(%rsp), %rdi
cmpq %r12, %rdi
je 0xc209
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0xc209
movq %rax, 0x8(%rsp)
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0xc22d
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0xc22d
movq %rax, 0x8(%rsp)
movl $0x88, %esi
movq %rbx, %rdi
callq 0x32d0
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0xc27f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0xc27f
movq %rax, 0x8(%rsp)
jmp 0xc23a
movq %rax, 0x8(%rsp)
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc27f
movq 0x130(%rsp), %rsi
jmp 0xc249
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0xc29b
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x32d0
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc2e6
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x32d0
jmp 0xc2e6
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xc29b
movq 0x20(%rsp), %rsi
jmp 0xc293
movq %rax, 0x8(%rsp)
jmp 0xc29b
jmp 0xc2e1
movq %rax, 0x8(%rsp)
movq 0x5cab(%rip), %rsi      # 0x11f98
leaq 0x140(%rsp), %rdi
callq 0x3200
leaq 0x1c0(%rsp), %rdi
callq 0x30f0
movq 0x8(%rsp), %rdi
callq 0x3500
nop