// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: ff_lunalign(overlap_region*, double, double, long)
uint32_t inline ff_lunalign(overlap_region *z, double erate, double gap_rate, int64_t max_gap)
{
    if((z->w_list.n == 1) && (!(is_ualn_win(z->w_list.a[0]))) 
        && (z->w_list.a[0].x_start == ((int64_t)z->x_pos_s)) && (z->w_list.a[0].x_end == ((int64_t)z->x_pos_e)) 
            && (z->w_list.a[0].y_start == ((int64_t)z->y_pos_s)) && (z->w_list.a[0].y_end == ((int64_t)z->y_pos_e))) {
                return 1;
    }

    int64_t k, zwn = z->w_list.n, zq, zt, wq, wt, tot_e, tot_g, ql, tl; 

    // fprintf(stderr, "[M::%s]\n", __func__);
    
    zq = z->x_pos_s; zt = z->y_pos_s; tot_e = tot_g = 0; 
    for (k = 0; k < zwn; k++) {
        // fprintf(stderr, "[M::%s]\twk::%ld\tq::[%d, %d)\tt::[%d, %d)\terr::%d\n", __func__, 
        //     k, z->w_list.a[k].x_start, z->w_list.a[k].x_end + 1, z->w_list.a[k].y_start, z->w_list.a[k].y_end + 1, z->w_list.a[k].error);
        if(is_ualn_win(z->w_list.a[k])) continue;
        wq = z->w_list.a[k].x_start; 
        wt = z->w_list.a[k].y_start;

        if(wq != zq) tot_g += ((wq>=zq)?(wq-zq):(zq-wq));
        if(wt != zt) tot_g += ((wt>=zt)?(wt-zt):(zt-wt));

        zq = z->w_list.a[k].x_end + 1; 
        zt = z->w_list.a[k].y_end + 1;
        tot_e += z->w_list.a[k].error;

        // fprintf(stderr, "[M::%s]\twk::%ld\tq::[%d, %d)\tt::[%d, %d)\terr::%d\n", __func__, 
        //     k, z->w_list.a[k].x_start, z->w_list.a[k].x_end + 1, z->w_list.a[k].y_start, z->w_list.a[k].y_end + 1, z->w_list.a[k].error);
    }

    wq = z->x_pos_e + 1;
    wt = z->y_pos_e + 1;
    if(wq != zq) tot_g += ((wq>=zq)?(wq-zq):(zq-wq));
    if(wt != zt) tot_g += ((wt>=zt)?(wt-zt):(zt-wt));

    // fprintf(stderr, "[M::%s]\t%.*s(id::%u)\tq::[%u, %u)\t%.*s(id::%u)\tt::[%u, %u)\tre::%ld\trg::%ld\n", __func__, (int)Get_NAME_LENGTH(R_INF, z->x_id), Get_NAME(R_INF, z->x_id), z->x_id, z->x_pos_s, z->x_pos_e + 1, 
    // (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), z->y_id, z->y_pos_s, z->y_pos_e + 1, tot_e, tot_g);

    if(!tot_g) return 1;

    // fprintf(stderr, "-0-[M::%s]\n", __func__);

    if(tot_g > max_gap) return 0;

    // fprintf(stderr, "-1-[M::%s]\n", __func__);

    ql = z->x_pos_e + 1 - z->x_pos_s;
    tl = z->y_pos_e + 1 - z->y_pos_s;

    if((tot_g > (ql*gap_rate)) || (tot_g > (tl*gap_rate))) return 0;

    // fprintf(stderr, "-2-[M::%s]\n", __func__);

    tot_e += tot_g;

    if((tot_e > (ql*erate)) || (tot_e > (tl*erate))) return 0;

    // fprintf(stderr, "-3-[M::%s]\n", __func__);
    
    return 1;
}

// Assembly:
movq 0x48(%rdi), %r10
movl $0x1, %eax
cmpq $0x1, %r10
jne 0x63023
movq 0x58(%rdi), %rcx
cmpw $0x7fff, 0x14(%rcx)     # imm = 0x7FFF
jne 0x62fed
cmpl $0x0, 0x1c(%rcx)
jne 0x62fed
cmpw $0x0, 0x12(%rcx)
js 0x63023
movslq (%rcx), %rdx
movl 0x4(%rdi), %r8d
cmpq %r8, %rdx
jne 0x63023
movslq 0x4(%rcx), %rdx
movl 0x8(%rdi), %r8d
cmpq %r8, %rdx
jne 0x63023
movslq 0x8(%rcx), %rdx
movl 0x14(%rdi), %r8d
cmpq %r8, %rdx
jne 0x63023
movslq 0xc(%rcx), %rcx
movl 0x18(%rdi), %edx
cmpq %rdx, %rcx
je 0x6314d
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0x4(%rdi), %r9d
movl 0x14(%rdi), %ecx
xorl %ebx, %ebx
testq %r10, %r10
cmovleq %rbx, %r10
movq %r9, %r14
movq %rcx, %r11
xorl %edx, %edx
xorl %r8d, %r8d
subq $0x1, %r10
jb 0x630b7
movq 0x58(%rdi), %r12
movswq 0x14(%r12,%rbx), %r15
cmpq $0x7fff, %r15           # imm = 0x7FFF
jne 0x63071
cmpl $0x0, 0x1c(%r12,%rbx)
jne 0x63071
cmpw $0x0, 0x12(%r12,%rbx)
js 0x630b1
movslq (%r12,%rbx), %r13
subq %r13, %r14
je 0x63087
movq %r14, %r13
negq %r13
cmovsq %r14, %r13
addq %r13, %r8
movslq 0x8(%r12,%rbx), %r14
subq %r14, %r11
je 0x6309e
movq %r11, %r14
negq %r14
cmovsq %r11, %r14
addq %r14, %r8
movslq 0x4(%r12,%rbx), %r14
incq %r14
movslq 0xc(%r12,%rbx), %r11
incq %r11
addq %r15, %rdx
addq $0x20, %rbx
jmp 0x63047
movl 0x8(%rdi), %r10d
movl 0x18(%rdi), %edi
incl %r10d
incl %edi
subq %r10, %r14
je 0x630d5
movq %r14, %rbx
negq %rbx
cmovsq %r14, %rbx
addq %rbx, %r8
subq %rdi, %r11
je 0x630e7
movq %r11, %rbx
negq %rbx
cmovsq %r11, %rbx
addq %rbx, %r8
testq %r8, %r8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
je 0x6314d
xorl %eax, %eax
cmpq %rsi, %r8
jg 0x6314d
subl %r9d, %r10d
cvtsi2sd %r8, %xmm4
cvtsi2sd %r10, %xmm2
movapd %xmm2, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm4
ja 0x6314d
subl %ecx, %edi
cvtsi2sd %rdi, %xmm3
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm4
ja 0x6314d
addq %rdx, %r8
cvtsi2sd %r8, %xmm1
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm3
cmpnltpd %xmm1, %xmm3
cmpnltpd %xmm1, %xmm2
andpd %xmm3, %xmm2
movd %xmm2, %eax
andl $0x1, %eax
retq