// File: /workspace/github_cpp_makefile_star2_O2/afinit[P]afin/fusion.cpp
// Function: Fusion::check_fusion_support(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
double Fusion::check_fusion_support( string contig, string contig_ref ){
  Extension *exten = new Extension( reads, contig_ref.length(), contig ); 
  
  string support_string( "" );
  double score = 0;
  const int pos = contig_ref.length() - 1;
  const int start = -1;
  
  exten->matches.start_match();

  // return if matches found is less than min_cov
  if( exten->matches.get_matchlist_size() < min_cov ){
    delete exten;
    return 1.0;
  }

  // create missed bp's vector to keep track of how many bp's each read contains that are below the max at that position
  vector<int> mismatch( exten->matches.get_matchlist_size(), 0 );
  vector<int> ambiguous_bp( exten->matches.get_matchlist_size(), 0 );
  int mismatch_tot = 0;
  int mismatch_avg = 0;
  int cmp_len = 0;

  // count mismatch's in each read match
  for( int i=0; i<contig_ref.length(); i++ ){
    int next_char_ref = contig_ref[pos-i];
    for( int j=0; j<exten->matches.get_matchlist_size(); j++ ){
      int next_char = exten->matches.get_pos( j, start-i );
      if( next_char == 'N' ){
        ambiguous_bp[j]++;
      }
      // check if next_char matches or exists in the current read
      else if( next_char != next_char_ref && next_char != -1 ){
        mismatch[j]++;
      }
    }
  }

  // remove reads with more than the max_missed
  exten->set_missed_bp( mismatch );
  exten->set_missed_avg( max_missed );
  exten->error_removal();

  // remove reads with more than 3 N's
  exten->set_missed_bp( ambiguous_bp );
  exten->set_missed_avg( 3 );
  exten->error_removal();
  
  // if matchlist is smaller than min_cov, bail, return 1.0 (the least supportive return value)
  if( exten->matches.get_matchlist_size() < min_cov ){
    delete exten;
    return 1.0;
  }

  // get string built from remaining matches.
  exten->build_string();
  support_string = exten->get_extension();
  cmp_len = support_string.length();

  // use the smaller of contig_ref.length() and min_overlap/2 as min_length to compare support_string with matching contig
  if( cmp_len < contig_ref.length() && cmp_len < (min_overlap/2 + min_overlap%2) ){
    delete exten;
    return 1.0;
  }

  // get smallest of string lengths
  if( cmp_len > contig_ref.length() ){
    cmp_len = contig_ref.length();
  }

  score = mismatch_score( support_string.substr( support_string.length() - cmp_len), contig_ref.substr( contig_ref.length() - cmp_len ) );
  
  delete exten;
  return score;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movl $0xd8, %edi
callq 0x4320
movq %rax, %rbx
movq 0x8(%r14), %r14
movq %r15, -0x40(%rbp)
movq 0x8(%r15), %r15
leaq -0x108(%rbp), %rdi
movq %r12, %rsi
callq 0x4410
leaq -0x108(%rbp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0xf468
leaq -0x108(%rbp), %rdi
callq 0x4160
leaq 0x9889(%rip), %rsi      # 0x12f4c
leaq -0xe8(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x4120
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x48(%rbp)
movq %rbx, %rdi
callq 0xb520
movq %rbx, %rdi
callq 0xb3dc
leaq 0x11f30(%rip), %rcx     # 0x1b628
movslq (%rcx), %rcx
cmpq %rcx, %rax
jae 0x9722
movq %rbx, %rdi
callq 0x5828
movq %rbx, %rdi
callq 0x42f0
movsd 0x99d0(%rip), %xmm0     # 0x130e8
movsd %xmm0, -0x38(%rbp)
jmp 0x9976
movq %rbx, %rdi
callq 0xb3dc
leaq -0xc8(%rbp), %rdx
andl $0x0, (%rdx)
leaq -0x78(%rbp), %rdi
leaq -0x60(%rbp), %rcx
movq %rax, %rsi
callq 0xaf74
movq %rbx, %rdi
callq 0xb3dc
leaq -0xc8(%rbp), %rdx
andl $0x0, (%rdx)
leaq -0x60(%rbp), %r12
leaq -0x29(%rbp), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq 0xaf74
xorl %r13d, %r13d
movq -0x40(%rbp), %r14
cmpq %r13, 0x8(%r14)
jbe 0x97da
movq %r13, -0x38(%rbp)
notl %r13d
movq -0x48(%rbp), %rax
addl %r13d, %eax
cltq
movq (%r14), %rcx
movb (%rcx,%rax), %r15b
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0xb3dc
cmpq %r14, %rax
jbe 0x97d1
movq %rbx, %rdi
movl %r14d, %esi
movl %r13d, %edx
callq 0xb3fa
movq %r12, %rcx
cmpb $0x4e, %al
je 0x97c5
cmpb %r15b, %al
sete %cl
cmpb $-0x1, %al
sete %al
orb %cl, %al
leaq -0x78(%rbp), %rcx
jne 0x97cc
movq (%rcx), %rax
incl (%rax,%r14,4)
incq %r14
jmp 0x9790
movq -0x38(%rbp), %r13
incq %r13
jmp 0x976c
leaq -0xa8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x60b4
movq %r14, %r12
leaq -0xa8(%rbp), %rsi
movq %rbx, %rdi
callq 0xf5aa
leaq -0xa8(%rbp), %rdi
callq 0x5b30
leaq 0x11e29(%rip), %rax     # 0x1b638
movl (%rax), %esi
movq %rbx, %rdi
callq 0xf5b8
movq %rbx, %rdi
callq 0xf7c0
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x60b4
leaq -0x90(%rbp), %rsi
movq %rbx, %rdi
callq 0xf5aa
leaq -0x90(%rbp), %rdi
callq 0x5b30
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0xf5b8
movq %rbx, %rdi
callq 0xf7c0
movq %rbx, %rdi
callq 0xb3dc
leaq 0x11dba(%rip), %rcx     # 0x1b628
movslq (%rcx), %rcx
movsd 0x986f(%rip), %xmm0     # 0x130e8
movsd %xmm0, -0x38(%rbp)
cmpq %rcx, %rax
jb 0x9954
movq %rbx, %rdi
callq 0xf87a
leaq -0xc8(%rbp), %rdi
movq %rbx, %rsi
callq 0xfc04
leaq -0xe8(%rbp), %r14
leaq -0xc8(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4390
movq %r15, %rdi
callq 0x4160
movq 0x8(%r14), %r8
movslq %r8d, %rsi
movq 0x8(%r12), %rcx
cmpq %rcx, %rsi
jae 0x98e6
leaq 0x11d55(%rip), %rax     # 0x1b62c
movl (%rax), %eax
pushq $0x2
popq %rdi
cltd
idivl %edi
addl %eax, %edx
cmpl %r8d, %edx
jg 0x9954
cmpq %rcx, %rsi
cmovbeq %r8, %rcx
movslq %ecx, %r14
subq %r14, %r8
leaq -0x128(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
pushq $-0x1
popq %rcx
movq %r8, %rdx
callq 0x4330
movq 0x8(%r12), %rdx
subq %r14, %rdx
leaq -0x148(%rbp), %rdi
pushq $-0x1
popq %rcx
movq %r12, %rsi
callq 0x4330
leaq -0x128(%rbp), %r14
leaq -0x148(%rbp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0x95d6
movsd %xmm0, -0x38(%rbp)
movq %r15, %rdi
callq 0x4160
movq %r14, %rdi
callq 0x4160
movq %rbx, %rdi
callq 0x5828
movq %rbx, %rdi
callq 0x42f0
leaq -0x60(%rbp), %rdi
callq 0x5b30
leaq -0x78(%rbp), %rdi
callq 0x5b30
leaq -0xe8(%rbp), %rdi
callq 0x4160
movsd -0x38(%rbp), %xmm0
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq -0x128(%rbp), %rdi
callq 0x4160
jmp 0x9a03
jmp 0x9a00
movq %rax, %r14
leaq -0x90(%rbp), %rdi
jmp 0x99c2
movq %rax, %r14
leaq -0xa8(%rbp), %rdi
callq 0x5b30
jmp 0x9a03
jmp 0x99cb
movq %rax, %r14
jmp 0x9a0c
jmp 0x99f7
jmp 0x99f7
movq %rax, %r14
jmp 0x9a21
movq %rax, %r14
leaq -0x108(%rbp), %rdi
callq 0x4160
jmp 0x99ed
movq %rax, %r14
movq %rbx, %rdi
callq 0x42f0
jmp 0x9a21
movq %rax, %r14
jmp 0x9a15
jmp 0x9a00
jmp 0x9a00
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x5b30
leaq -0x78(%rbp), %rdi
callq 0x5b30
leaq -0xe8(%rbp), %rdi
callq 0x4160
movq %r14, %rdi
callq 0x4590
nop