// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/hyperloglog.c
// Function: hllCount
uint64_t hllCount(struct hllhdr *hdr, int *invalid) {
    double m = HLL_REGISTERS;
    double E, alpha = 0.7213/(1+1.079/m);
    int j, ez; /* Number of registers equal to 0. */

    /* We precompute 2^(-reg[j]) in a small table in order to
     * speedup the computation of SUM(2^-register[0..i]). */
    static int initialized = 0;
    static double PE[64];
    if (!initialized) {
        PE[0] = 1; /* 2^(-reg[j]) is 1 when m is 0. */
        for (j = 1; j < 64; j++) {
            /* 2^(-reg[j]) is the same as 1/2^reg[j]. */
            PE[j] = 1.0/(1ULL << j);
        }
        initialized = 1;
    }

    /* Compute SUM(2^-register[0..i]). */
    if (hdr->encoding == HLL_DENSE) {
        E = hllDenseSum(hdr->registers,PE,&ez);
    } else if (hdr->encoding == HLL_SPARSE) {
        E = hllSparseSum(hdr->registers,
                         sdslen((sds)hdr)-HLL_HDR_SIZE,PE,&ez,invalid);
    } else if (hdr->encoding == HLL_RAW) {
        E = hllRawSum(hdr->registers,PE,&ez);
    } else {
        redisPanic("Unknown HyperLogLog encoding in hllCount()");
    }

    /* Muliply the inverse of E for alpha_m * m^2 to have the raw estimate. */
    E = (1/E)*alpha*m*m;

    /* Use the LINEARCOUNTING algorithm for small cardinalities.
     * For larger values but up to 72000 HyperLogLog raw approximation is
     * used since linear counting error starts to increase. However HyperLogLog
     * shows a strong bias in the range 2.5*16384 - 72000, so we try to
     * compensate for it. */
    if (E < m*2.5 && ez != 0) {
        E = m*log(m/ez); /* LINEARCOUNTING() */
    } else if (m == 16384 && E < 72000) {
        /* We did polynomial regression of the bias for this range, this
         * way we can compute the bias for a given cardinality and correct
         * according to it. Only apply the correction for P=14 that's what
         * we use and the value the correction was verified with. */
        double bias = 5.9119*1.0e-18*(E*E*E*E)
                      -1.4253*1.0e-12*(E*E*E)+
                      1.2940*1.0e-7*(E*E)
                      -5.2921*1.0e-3*E+
                      83.3216;
        E -= E*(bias/100);
    }
    /* We don't apply the correction for E > 1/30 of 2^32 since we use
     * a 64 bit function and 6 bit counters. To apply the correction for
     * 1/30 of 2^64 is not needed since it would require a huge set
     * to approach such a value. */
    return (uint64_t) E;
}

// Assembly:
pushq %rax
movq %rsi, %r8
cmpb $0x0, 0x7ce2c(%rip)     # 0xde940
jne 0x61b54
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x7ce29(%rip)     # 0xde950
movl $0x1, %ecx
leaq 0x7ce1d(%rip), %rdx     # 0xde950
movq %rcx, %rsi
shlq $0x34, %rsi
movq %rax, %r9
subq %rsi, %r9
movq %r9, (%rdx,%rcx,8)
incq %rcx
cmpq $0x40, %rcx
jne 0x61b33
movb $0x1, 0x7cdec(%rip)     # 0xde940
movzbl 0x4(%rdi), %eax
cmpl $0xff, %eax
je 0x61ba0
cmpl $0x1, %eax
je 0x61b83
testl %eax, %eax
jne 0x61ca9
addq $0x10, %rdi
leaq 0x7cdd9(%rip), %rsi     # 0xde950
leaq 0x4(%rsp), %rdx
callq 0x61133
jmp 0x61bb5
movl -0x8(%rdi), %esi
addq $0x10, %rdi
addl $-0x10, %esi
leaq 0x7cdbc(%rip), %rdx     # 0xde950
leaq 0x4(%rsp), %rcx
callq 0x61971
jmp 0x61bb5
addq $0x10, %rdi
leaq 0x7cda5(%rip), %rsi     # 0xde950
leaq 0x4(%rsp), %rdx
callq 0x61a32
movapd %xmm0, %xmm1
movsd 0x356ef(%rip), %xmm0    # 0x972b0
divsd %xmm1, %xmm0
mulsd 0x3e4a3(%rip), %xmm0    # 0xa0070
movsd 0x3e4a3(%rip), %xmm2    # 0xa0078
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd 0x3e49b(%rip), %xmm1    # 0xa0080
ucomisd %xmm0, %xmm1
seta %cl
movl 0x4(%rsp), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x61c1a
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
callq 0x1f4b0
mulsd 0x3e460(%rip), %xmm0    # 0xa0078
jmp 0x61c88
movsd 0x3e466(%rip), %xmm1    # 0xa0088
ucomisd %xmm0, %xmm1
jbe 0x61c88
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movapd %xmm0, %xmm3
mulsd %xmm2, %xmm3
mulsd 0x3e448(%rip), %xmm2    # 0xa0090
mulsd 0x3e448(%rip), %xmm3    # 0xa0098
addsd %xmm2, %xmm3
mulsd 0x3e444(%rip), %xmm1    # 0xa00a0
addsd %xmm3, %xmm1
movsd 0x3e440(%rip), %xmm2    # 0xa00a8
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
addsd 0x3e438(%rip), %xmm2    # 0xa00b0
divsd 0x3e438(%rip), %xmm2    # 0xa00b8
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd 0x3e428(%rip), %xmm0    # 0xa00c0
cvttsd2si %xmm0, %rax
sarq $0x3f, %rdx
andq %rdx, %rax
orq %rcx, %rax
popq %rcx
retq
leaq 0x3e440(%rip), %rdi     # 0xa00f0
leaq 0x3e42b(%rip), %rsi     # 0xa00e2
movl $0x3e2, %edx            # imm = 0x3E2
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0