// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: cal_integer_most_dis(unsigned int, unsigned long*, unsigned long, double)
uint64_t cal_integer_most_dis(uint32_t qid, uint64_t *a, uint64_t a_n, double cluster_rate)
{
    if(a_n <= 0) return (uint64_t)-1;
    // fprintf(stderr, "\n[M::%s::] a_n::%lu\n", __func__, a_n);
    uint64_t k, l, i, m, r_an = a_n, max_m, max_i, cc, cd, nd; int64_t z;
    for (k = 1, l = m = max_m = 0, max_i = (uint64_t)-1; k <= a_n; k++) {
        a[k-1] <<= 1; a[k-1] >>= 1;
        // fprintf(stderr, "[k->%lu] d::%lu, rev::%lu\n", k-1, a[k-1]>>1, a[k-1]&1);
        if((k == a_n) || (((a[k]&((uint64_t)(0x7fffffffffffffff)))>>1) 
                                        != ((a[l]&((uint64_t)(0x7fffffffffffffff)))>>1))) {
            for (i = l; i < k; i++) {
                if(!(a[i]&1)) break;
            }
            a[m] = a[l]; a[m] >>= 1; a[m] <<= 1; 
            ///if all integer sequence are mapped reversely 
            if(i >= k) a[m] += 1;
            a[m] |= ((uint64_t)(k-l))<<32;
            if((k-l) > max_m) {
                max_m = k - l; max_i = m;
            } else if((k-l) == max_m && i < k) {///i < k means this distance is supported by forward sequences
                max_m = k - l; max_i = m;
            }
            l = k; m++;
        }
    }
    a_n = m; assert(a_n > 0);
    // fprintf(stderr, "[M::%s::] m::%lu\n", __func__, m);
    if(max_m > 0 && max_m > (r_an>>1)) {
        // fprintf(stderr, "[M::%s::] dis::%u\n", __func__, ((uint32_t)a[max_i])>>1);
        return ((uint32_t)a[max_i])>>1;
    }

    for (k = 0, max_m = 0, max_i = (uint64_t)-1; k < a_n; k++) {
        cd = (((uint32_t)a[k])>>1); z = k;
        // fprintf(stderr, "[mk->%lu] cd::%lu\n", k, cd);
        for (cc = cd, z--; z >= 0; z--) {
            nd = (((uint32_t)a[z])>>1); assert(nd < cd);
            if(((cd-nd) > (nd*cluster_rate)) && ((cd-nd) > 512)) break;
            cc += (a[z]>>32);
        }
        for (i = k+1; i < a_n; i++) {
            nd = (((uint32_t)a[i])>>1); 
            // if(!(nd > cd)) {
            //     fprintf(stderr, "[M::%s::] qid::%u, a_n::%lu, i::%lu, k::%lu, cd::%lu, nd::%lu\n", 
            //     __func__, qid, a_n, i, k, cd, nd);
            // }
            assert(nd > cd);
            if(((nd-cd) > (nd*cluster_rate)) && ((nd-cd) > 512)) break;
            cc += (a[i]>>32);
        }

        if(cc > max_m) {
            max_m = cc; max_i = k;
        } else if(cc == max_m && (!(a[k]&1))) {///means this distance is supported by forward sequences
            max_m = cc; max_i = k;
        }
    }

    k = max_i;
    cd = (((uint32_t)a[k])>>1); z = k;
    for (max_m = cd, max_i = k, z--; z >= 0; z--) {
        nd = (((uint32_t)a[z])>>1); assert(nd < cd);
        if(((cd-nd) > (nd*cluster_rate)) && ((cd-nd) > 512)) break;
        cc = (a[z]>>32);
        if(cc > max_m) {
            max_m = cc; max_i = z;
        } else if(cc == max_m && (!(a[z]&1))) {///means this distance is supported by forward sequences
            max_m = cc; max_i = z;
        }
    }
    for (i = k+1; i < a_n; i++) {
        nd = (((uint32_t)a[i])>>1); assert(nd > cd);
        if(((nd-cd) > (nd*cluster_rate)) && ((nd-cd) > 512)) break;
        cc = (a[i]>>32);
        if(cc > max_m) {
            max_m = cc; max_i = i;
        } else if(cc == max_m && (!(a[i]&1))) {///means this distance is supported by forward sequences
            max_m = cc; max_i = i;
        }
    }

    // fprintf(stderr, "[M::%s::] dis::%u\n", __func__, ((uint32_t)a[max_i])>>1);
    return ((uint32_t)a[max_i])>>1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0x205167
leaq 0x1(%rdx), %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovaeq %rax, %rcx
movabsq $0x7ffffffffffffffe, %r8 # imm = 0x7FFFFFFFFFFFFFFE
movl $0x1, %r10d
movq $-0x1, %rdi
xorl %r11d, %r11d
leaq 0x1(%r8), %rbx
xorl %r9d, %r9d
xorl %eax, %eax
xorl %r14d, %r14d
andq %rbx, -0x8(%rsi,%r10,8)
cmpq %rdx, %r10
je 0x204e8a
movq (%rsi,%r14,8), %r15
xorq (%rsi,%r10,8), %r15
testq %r8, %r15
je 0x204f02
movb $0x1, %bpl
movq %r10, %r15
subq %r14, %r15
jbe 0x204ec3
testb $0x1, (%rsi,%r14,8)
jne 0x204ea0
xorl %ebp, %ebp
jmp 0x204ec3
leaq 0x1(%r14), %r13
movq %r13, %r12
addq %r11, %r13
cmpq $0x1, %r13
je 0x204ebc
leaq 0x1(%r12), %r13
testb $0x1, (%rsi,%r12,8)
jne 0x204ea4
cmpq %r10, %r12
setae %bpl
movq (%rsi,%r14,8), %r14
andq $-0x2, %r14
movzbl %bpl, %r12d
movq %r15, %r13
shlq $0x20, %r13
orq %r12, %r13
orq %r14, %r13
testb %r12b, %r12b
movq %rax, %r12
cmovneq %rdi, %r12
cmpq %r9, %r15
cmovneq %rdi, %r12
cmovaq %r15, %r9
cmovaq %rax, %r12
movq %r13, (%rsi,%rax,8)
incq %rax
movq %r10, %r14
movq %r12, %rdi
incq %r10
decq %r11
cmpq %rcx, %r10
jne 0x204e73
testq %rax, %rax
je 0x2051f9
shrq %rdx
cmpq %rdx, %r9
ja 0x205160
movq $-0x1, %rcx
movl $0x1, %edx
xorl %edi, %edi
movq 0x2c344(%rip), %xmm1    # 0x231280
movapd 0x2c34c(%rip), %xmm2    # 0x231290
xorl %ebx, %ebx
movq %rbx, %r8
movq (%rsi,%rbx,8), %r9
movl %r9d, %r11d
shrl %r11d
movq %r11, %r10
testq %rbx, %rbx
jle 0x204fbf
movq %rdx, %rbx
movq %r11, %r10
movq -0x10(%rsi,%rbx,8), %r14
movl %r14d, %r15d
shrl %r15d
cmpl %r15d, %r11d
jbe 0x20517d
movq %r11, %r12
subq %r15, %r12
movq %r12, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r12            # imm = 0x201
jb 0x204faf
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r15d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x204fbf
shrq $0x20, %r14
addq %r14, %r10
decq %rbx
cmpq $0x1, %rbx
ja 0x204f61
leaq 0x1(%r8), %rbx
cmpq %rax, %rbx
jae 0x205024
movq %rbx, %r14
movq (%rsi,%r14,8), %r15
movl %r15d, %r12d
shrl %r12d
movq %r12, %r13
subq %r11, %r13
jbe 0x20519c
movq %r13, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r13            # imm = 0x201
jb 0x205015
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r12d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x205024
shrq $0x20, %r15
addq %r15, %r10
incq %r14
cmpq %rax, %r14
jb 0x204fcb
cmpq %rdi, %r10
jbe 0x205031
movq %r10, %rdi
movq %r8, %rcx
jmp 0x20503b
jne 0x20503b
testb $0x1, %r9b
cmoveq %r8, %rcx
incq %rdx
cmpq %rax, %rbx
jne 0x204f46
movl (%rsi,%rcx,8), %edx
shrl %edx
movq %rdx, %r8
movq %rcx, %rdi
testq %rcx, %rcx
jle 0x2050e4
leaq -0x1(%rcx), %r9
movq %rcx, %rdi
movq %rdx, %r8
movq (%rsi,%r9,8), %r10
movl %r10d, %r11d
shrl %r11d
cmpl %r11d, %edx
jbe 0x2051bb
movq %rdx, %rbx
subq %r11, %rbx
movq %rbx, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %rbx            # imm = 0x201
jb 0x2050b2
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r11d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x2050e4
movq %r10, %r11
shrq $0x20, %r11
cmpq %r8, %r11
jbe 0x2050c6
movq %r11, %r8
movq %r9, %rdi
jmp 0x2050d4
xorq %r8, %r11
andl $0x1, %r10d
orq %r11, %r10
cmoveq %r9, %rdi
leaq -0x1(%r9), %r10
incq %r9
cmpq $0x1, %r9
movq %r10, %r9
ja 0x205065
incq %rcx
cmpq %rax, %rcx
jae 0x205160
movq (%rsi,%rcx,8), %r9
movl %r9d, %r10d
shrl %r10d
movq %r10, %r11
subq %rdx, %r11
jbe 0x2051da
movq %r11, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r11            # imm = 0x201
jb 0x205136
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
xorps %xmm5, %xmm5
cvtsi2sd %r10d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x205160
movq %r9, %r10
shrq $0x20, %r10
cmpq %r8, %r10
jbe 0x20514a
movq %r10, %r8
movq %rcx, %rdi
jmp 0x205158
xorq %r8, %r10
andl $0x1, %r9d
orq %r10, %r9
cmoveq %rcx, %rdi
incq %rcx
cmpq %rcx, %rax
jne 0x2050ec
movl (%rsi,%rdi,8), %eax
shrl %eax
jmp 0x20516e
movq $-0x1, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x420ca(%rip), %rdi     # 0x24724e
leaq 0x4047e(%rip), %rsi     # 0x245609
leaq 0x42076(%rip), %rcx     # 0x247208
movl $0x1886, %edx           # imm = 0x1886
callq 0x2110
leaq 0x420b3(%rip), %rdi     # 0x247256
leaq 0x4045f(%rip), %rsi     # 0x245609
leaq 0x42057(%rip), %rcx     # 0x247208
movl $0x1890, %edx           # imm = 0x1890
callq 0x2110
leaq 0x4208c(%rip), %rdi     # 0x24724e
leaq 0x40440(%rip), %rsi     # 0x245609
leaq 0x42038(%rip), %rcx     # 0x247208
movl $0x189f, %edx           # imm = 0x189F
callq 0x2110
leaq 0x42075(%rip), %rdi     # 0x247256
leaq 0x40421(%rip), %rsi     # 0x245609
leaq 0x42019(%rip), %rcx     # 0x247208
movl $0x18a9, %edx           # imm = 0x18A9
callq 0x2110
leaq 0x42000(%rip), %rdi     # 0x247200
leaq 0x40402(%rip), %rsi     # 0x245609
leaq 0x41ffa(%rip), %rcx     # 0x247208
movl $0x187b, %edx           # imm = 0x187B
callq 0x2110