// File: /workspace/github_c_makefile_star2_O2/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 0x859c
movq %rax, %rbx
movq (%r14), %rdi
addq $0x20, %rdi
callq 0x972e
movl %eax, %r13d
leaq 0x20(%rbx), %rdi
movq %rdi, 0x58(%rsp)
callq 0x972e
movq %r14, 0x10(%rsp)
movq (%r14), %rcx
movq 0x20(%rcx), %r10
movq 0x60(%rcx), %r15
movq 0x20(%rbx), %rbp
movq %rbx, %r11
addq $0x60, %r11
cmpl %r13d, %eax
cmovgel %r13d, %eax
movq %rbx, 0x20(%rsp)
movq 0x60(%rbx), %r12
cltq
leal -0x1e(%r13), %ecx
pushq $0x1e
popq %r14
movq %r13, 0x8(%rsp)
cmpq %rax, %r14
jg 0x914b
movslq %ecx, %rbx
leaq (%r10,%rbx), %rdx
leaq (%r15,%rbx), %rsi
movl %r13d, %edi
subl %r14d, %edi
movl %edi, 0x1c(%rsp)
movslq %edi, %rdi
movq %rdi, 0x28(%rsp)
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r8d, %r8d
cmpq %rdi, %r14
je 0x8ec5
movb (%rdx,%rdi), %r9b
cmpb (%rbp,%rdi), %r9b
je 0x8eb4
movb (%rsi,%rdi), %r9b
cmpb $0x3f, %r9b
jl 0x8e96
cmpb $0x31, (%r12,%rdi)
jl 0x8ea3
jmp 0x8ea6
cmpb $0x30, %r9b
jg 0x8ea6
cmpb $0x3f, (%r12,%rdi)
jl 0x8ea6
incl %r8d
cmpl %r8d, %r13d
jge 0x8eb9
cmpl $0x2, %r8d
jg 0x8eb9
incl %r13d
incq %rdi
jmp 0x8e73
incq %r14
decl %ecx
movq 0x8(%rsp), %r13
jmp 0x8e45
movq %r11, 0x8(%rsp)
movq %r10, 0x50(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x31b0
leaq 0x150(%rsp), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
callq 0x3220
leaq 0x1975(%rip), %rsi      # 0xa86d
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
movl 0x1c(%rsp), %esi
callq 0x34c0
leaq 0x196a(%rip), %rsi      # 0xa87d
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
movl %r14d, %esi
callq 0x34c0
leaq 0x195a(%rip), %rsi      # 0xa887
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
movl %r13d, %esi
callq 0x34c0
leaq 0x158(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x3450
movq 0x10(%rsp), %rax
movq (%rax), %rsi
addq $0x20, %rsi
leaq 0x30(%rsp), %rdi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
callq 0x32a0
leaq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x6cd7
leaq 0x30(%rsp), %rdi
callq 0x3568
movq 0x10(%rsp), %rax
movq (%rax), %rsi
addq $0x60, %rsi
leaq 0x80(%rsp), %rdi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
callq 0x32a0
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x6cd7
leaq 0x80(%rsp), %rdi
callq 0x3568
movq 0x50(%rsp), %rsi
subq $0x1, %r14
jb 0x902d
movb (%rsi,%rbx), %cl
movb (%rbp), %dl
movb (%r15,%rbx), %al
cmpb %dl, %cl
jne 0x8ff5
addb (%r12), %al
addb $-0x21, %al
jmp 0x901a
cmpb $0x3f, %al
jl 0x900e
cmpb $0x30, (%r12)
jg 0x900e
movq 0x60(%rsp), %rax
movb %cl, (%rax,%rbx)
movb (%r15,%rbx), %al
jmp 0x901a
movq 0x60(%rsp), %rax
movb %dl, (%rax,%rbx)
movb (%r12), %al
movq 0x30(%rsp), %rcx
movb %al, (%rcx,%rbx)
incq %r12
incq %rbp
incq %rbx
jmp 0x8fd9
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x45e6
movl $0x88, %esi
movq %rbx, %rdi
callq 0x3290
movl $0x88, %edi
callq 0x3280
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x3380
leaq 0xe0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x3380
leaq 0x1677(%rip), %rsi      # 0xa6f9
leaq 0xc0(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x44ec
leaq 0xa0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x3380
leaq 0x100(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %r9d, %r9d
callq 0x7fa4
leaq 0xa0(%rsp), %rdi
callq 0x3568
leaq 0xc0(%rsp), %rdi
callq 0x3568
leaq 0xe0(%rsp), %rdi
callq 0x3568
leaq 0x100(%rsp), %rdi
callq 0x3568
leaq 0x30(%rsp), %rdi
callq 0x3568
leaq 0x60(%rsp), %rdi
callq 0x3568
leaq 0x120(%rsp), %rdi
callq 0x3568
leaq 0x140(%rsp), %rdi
callq 0x31e0
movq %rbx, %rax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x45e6
movl $0x88, %esi
movq %rbx, %rdi
callq 0x3290
xorl %ebx, %ebx
jmp 0x9136
movq %rax, %r14
leaq 0xa0(%rsp), %rdi
callq 0x3568
jmp 0x917e
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
callq 0x3568
jmp 0x9190
movq %rax, %r14
leaq 0xe0(%rsp), %rdi
callq 0x3568
jmp 0x91a2
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x3568
jmp 0x91b4
movq %rax, %r14
movl $0x88, %esi
movq %rbx, %rdi
callq 0x3290
leaq 0x30(%rsp), %rdi
callq 0x3568
leaq 0x60(%rsp), %rdi
callq 0x3568
leaq 0x120(%rsp), %rdi
callq 0x3568
jmp 0x920f
movq %rax, %r14
jmp 0x91c1
movq %rax, %r14
leaq 0x80(%rsp), %rdi
jmp 0x91c6
movq %rax, %r14
jmp 0x91cb
movq %rax, %r14
leaq 0x30(%rsp), %rdi
jmp 0x91d0
movq %rax, %r14
jmp 0x91d5
jmp 0x920c
movq %rax, %r14
leaq 0x140(%rsp), %rdi
callq 0x31e0
movq %r14, %rdi
callq 0x34e0