// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0x1b9171
pushq $-0x1
popq %rdi
pushq $0x1
popq %rcx
movabsq $0x7ffffffffffffffe, %r9 # imm = 0x7FFFFFFFFFFFFFFE
xorl %r11d, %r11d
leaq 0x1(%r9), %r10
xorl %eax, %eax
xorl %r8d, %r8d
cmpq %rdx, %rcx
ja 0x1b904d
andq %r10, -0x8(%rsi,%rcx,8)
cmpq %rdx, %rcx
je 0x1b8fec
movq (%rsi,%r11,8), %rbx
xorq (%rsi,%rcx,8), %rbx
testq %r9, %rbx
je 0x1b9048
movq %r11, %rbx
movq %rbx, %r14
cmpq %rcx, %rbx
jae 0x1b9007
leaq 0x1(%r14), %rbx
testb $0x1, (%rsi,%r14,8)
jne 0x1b8fef
movq %rax, %rbx
jmp 0x1b900a
movq %rdi, %rbx
xorl %r15d, %r15d
cmpq %rcx, %r14
setae %r15b
movq (%rsi,%r11,8), %r14
andq $-0x2, %r14
movq %rcx, %r12
subq %r11, %r12
movq %r12, %r11
shlq $0x20, %r11
orq %r15, %r11
orq %r14, %r11
cmpq %r8, %r12
cmoveq %rbx, %rdi
cmovaq %r12, %r8
cmovaq %rax, %rdi
movq %r11, (%rsi,%rax,8)
incq %rax
movq %rcx, %r11
incq %rcx
jmp 0x1b8fd0
testq %rax, %rax
je 0x1b930c
shrq %rdx
cmpq %rdx, %r8
ja 0x1b927f
pushq $-0x1
popq %rdi
xorl %r11d, %r11d
movq 0x2a200(%rip), %xmm1    # 0x1e3270
movapd 0x2a208(%rip), %xmm2    # 0x1e3280
xorl %edx, %edx
movq %rdi, %rcx
cmpq %rax, %r11
je 0x1b9179
movq %r11, %rdi
movq (%rsi,%r11,8), %r8
movl %r8d, %r10d
shrl %r10d
movq %r10, %r9
testq %r11, %r11
jle 0x1b90f2
movq -0x8(%rsi,%r11,8), %rbx
movl %ebx, %r14d
shrl %r14d
cmpl %r14d, %r10d
jbe 0x1b9290
movq %r10, %r15
subq %r14, %r15
movq %r15, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r15            # imm = 0x201
jb 0x1b90e6
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
cvtsi2sd %r14d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1b90f2
shrq $0x20, %rbx
addq %rbx, %r9
decq %r11
jmp 0x1b9096
leaq 0x1(%rdi), %r11
movq %r11, %rbx
cmpq %rax, %rbx
jae 0x1b9151
movq (%rsi,%rbx,8), %r14
movl %r14d, %r15d
shrl %r15d
movq %r15, %r12
subq %r10, %r12
jbe 0x1b92af
movq %r12, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
cmpq $0x201, %r12            # imm = 0x201
jb 0x1b9145
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
cvtsi2sd %r15d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1b9151
shrq $0x20, %r14
addq %r14, %r9
incq %rbx
jmp 0x1b90f9
cmpq %rdx, %r9
ja 0x1b9169
jne 0x1b907d
testb $0x1, %r8b
cmoveq %rdi, %rcx
jmp 0x1b907d
movq %r9, %rdx
jmp 0x1b907a
pushq $-0x1
popq %rax
jmp 0x1b9284
movl (%rsi,%rcx,8), %edx
shrl %edx
movq %rdx, %r8
movq %rcx, %rdi
movq %rcx, %r9
testq %r9, %r9
jle 0x1b9277
movq -0x8(%rsi,%r9,8), %r10
movl %r10d, %r11d
shrl %r11d
cmpl %r11d, %edx
jbe 0x1b92ce
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 0x1b91df
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
cvtsi2sd %r11d, %xmm5
addsd %xmm3, %xmm4
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1b9277
movq %r10, %r11
shrq $0x20, %r11
decq %r9
cmpq %r8, %r11
ja 0x1b91fe
xorq %r8, %r11
andl $0x1, %r10d
orq %r11, %r10
cmoveq %r9, %rdi
jmp 0x1b9187
movq %r11, %r8
movq %r9, %rdi
jmp 0x1b9187
movq (%rsi,%rcx,8), %r9
movl %r9d, %r10d
shrl %r10d
movq %r10, %r11
subq %rdx, %r11
jbe 0x1b92ed
cmpq $0x201, %r11            # imm = 0x201
jb 0x1b9255
movq %r11, %xmm1
punpckldq 0x2a03e(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x2a046(%rip), %xmm1    # 0x1e3280
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
cvtsi2sd %r10d, %xmm3
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x1b927f
movq %r9, %r10
shrq $0x20, %r10
cmpq %r8, %r10
ja 0x1b9271
xorq %r8, %r10
andl $0x1, %r9d
orq %r10, %r9
cmoveq %rcx, %rdi
jmp 0x1b9277
movq %r10, %r8
movq %rcx, %rdi
incq %rcx
cmpq %rax, %rcx
jb 0x1b9206
movl (%rsi,%rdi,8), %eax
shrl %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x3ff76(%rip), %rdi     # 0x1f920d
leaq 0x3e23b(%rip), %rsi     # 0x1f74d9
leaq 0x3ff22(%rip), %rcx     # 0x1f91c7
movl $0x1886, %edx           # imm = 0x1886
callq 0x2110
leaq 0x3ff5f(%rip), %rdi     # 0x1f9215
leaq 0x3e21c(%rip), %rsi     # 0x1f74d9
leaq 0x3ff03(%rip), %rcx     # 0x1f91c7
movl $0x1890, %edx           # imm = 0x1890
callq 0x2110
leaq 0x3ff38(%rip), %rdi     # 0x1f920d
leaq 0x3e1fd(%rip), %rsi     # 0x1f74d9
leaq 0x3fee4(%rip), %rcx     # 0x1f91c7
movl $0x189f, %edx           # imm = 0x189F
callq 0x2110
leaq 0x3ff21(%rip), %rdi     # 0x1f9215
leaq 0x3e1de(%rip), %rsi     # 0x1f74d9
leaq 0x3fec5(%rip), %rcx     # 0x1f91c7
movl $0x18a9, %edx           # imm = 0x18A9
callq 0x2110
leaq 0x3feac(%rip), %rdi     # 0x1f91bf
leaq 0x3e1bf(%rip), %rsi     # 0x1f74d9
leaq 0x3fea6(%rip), %rcx     # 0x1f91c7
movl $0x187b, %edx           # imm = 0x187B
callq 0x2110