// File: chhylp123[P]hifiasm/Correct.cpp
// Function: get_affine_gap_score(overlap_region*, UC_Read*, UC_Read*, unsigned char*, unsigned char*, unsigned long, unsigned long)
long long get_affine_gap_score(overlap_region* ovc, UC_Read* g_read, UC_Read* overlap_read, uint8_t* x_num,
uint8_t* y_num, uint64_t EstimateXOlen, uint64_t EstimateYOlen)
{
    char* x_string;
    char* y_string;
    uint64_t yStrand;
    long long minus_score_thres = (MAX(EstimateXOlen, EstimateYOlen)*HIGH_HET_ERROR_RATE*(MATCH_SCORE_KSW+(MAX(MISMATCH_SCORE_KSW,GAP_EXT_KSW))));
    long long total_score_thres = MAX(EstimateXOlen, EstimateYOlen)*MATCH_SCORE_KSW - minus_score_thres;

    yStrand = ovc->y_pos_strand;

    if(yStrand == 0)
    {
        recover_UC_Read(overlap_read, &R_INF, ovc->y_id);
    }
    else
    {
        recover_UC_Read_RC(overlap_read, &R_INF, ovc->y_id);
    }
    x_string = g_read->seq;
    y_string = overlap_read->seq;

    long long sum = 0;
    
    if(fill_chain_by_affine_gap(&(ovc->f_cigar), x_string, y_string, ovc, Get_READ_LENGTH(R_INF, ovc->x_id), 
    Get_READ_LENGTH(R_INF, ovc->y_id), NULL, seq_nt6_table, x_num, y_num, &minus_score_thres, &sum) == 0)
    {
        return 0;
    }

    if(sum >= total_score_thres) return 1;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0xb0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x10(%rbp), %rax
jb 0x4cffe
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x4d006
movq 0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, %xmm0
movaps 0x334ffa(%rip), %xmm1   # 0x382010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x334ffe(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x335257(%rip), %xmm1   # 0x382290
mulsd %xmm1, %xmm0
movsd 0x335243(%rip), %xmm1   # 0x382288
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x10(%rbp), %rax
jb 0x4d069
movq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x4d074
movq 0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
shlq %rax
subq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x4d0b3
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
leaq 0x378224(%rip), %rsi    # 0x3c52d0
callq 0xa380
jmp 0x4d0cc
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
leaq 0x378209(%rip), %rsi    # 0x3c52d0
callq 0xa500
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x3781cb(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x20(%rbp), %r8
movl (%r8), %r8d
movq (%rax,%r8,8), %r8
leaq 0x3781b5(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x20(%rbp), %r9
movl 0x10(%r9), %r9d
movq (%rax,%r9,8), %r9
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %r11
xorl %eax, %eax
leaq 0x377514(%rip), %r14    # 0x3c4650
leaq -0x68(%rbp), %r10
leaq -0x78(%rbp), %rax
movq $0x0, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x4cf80
cmpl $0x0, %eax
jne 0x4d179
movq $0x0, -0x18(%rbp)
jmp 0x4d195
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jl 0x4d18d
movq $0x1, -0x18(%rbp)
jmp 0x4d195
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0xb0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)