// File: /workspace/github_c_makefile_star2_O0/gplessis[P]dotdeb-redis/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 {
        serverPanic("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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd 0xa8380(%rip), %xmm0    # 0x147758
movsd %xmm0, -0x18(%rbp)
movsd 0xa836b(%rip), %xmm0    # 0x147750
divsd -0x18(%rbp), %xmm0
movsd 0x98076(%rip), %xmm1    # 0x137468
addsd %xmm0, %xmm1
movsd 0xa834a(%rip), %xmm0    # 0x147748
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
cmpl $0x0, 0xedca2(%rip)     # 0x18d0b0
jne 0x9f494
movsd 0x9804c(%rip), %xmm0    # 0x137468
movsd %xmm0, 0xedc9c(%rip)    # 0x18d0c0
movl $0x1, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
jge 0x9f48a
movb -0x2c(%rbp), %cl
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %xmm1
movaps 0x98058(%rip), %xmm0    # 0x1374a0
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x9805c(%rip), %xmm0    # 0x1374b0
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x97ffd(%rip), %xmm0    # 0x137468
divsd %xmm1, %xmm0
movslq -0x2c(%rbp), %rcx
leaq 0xedc46(%rip), %rax     # 0x18d0c0
movsd %xmm0, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x9f42b
movl $0x1, 0xedc1c(%rip)     # 0x18d0b0
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0x9f4c3
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
leaq 0xedc10(%rip), %rsi     # 0x18d0c0
leaq -0x30(%rbp), %rdx
callq 0x9df70
movsd %xmm0, -0x20(%rbp)
jmp 0x9f55e
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x9f50a
movq -0x8(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x9e660
movq -0x40(%rbp), %rdi
subq $0x10, %rax
movl %eax, %esi
movq -0x10(%rbp), %r8
leaq 0xedbc6(%rip), %rdx     # 0x18d0c0
leaq -0x30(%rbp), %rcx
callq 0x9f020
movsd %xmm0, -0x20(%rbp)
jmp 0x9f55c
movq -0x8(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0xff, %eax
jne 0x9f538
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
leaq 0xedb98(%rip), %rsi     # 0x18d0c0
leaq -0x30(%rbp), %rdx
callq 0x9f1a0
movsd %xmm0, -0x20(%rbp)
jmp 0x9f55a
leaq 0xa8291(%rip), %rdi     # 0x1477d0
leaq 0xa827c(%rip), %rsi     # 0x1477c2
movl $0x3e2, %edx            # imm = 0x3E2
callq 0x76d80
movl $0x1, %edi
callq 0x263c0
jmp 0x9f55c
jmp 0x9f55e
movsd 0x97f02(%rip), %xmm0    # 0x137468
divsd -0x20(%rbp), %xmm0
mulsd -0x28(%rbp), %xmm0
mulsd -0x18(%rbp), %xmm0
mulsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm1
movsd 0xa81d4(%rip), %xmm0    # 0x147760
mulsd -0x18(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x9f5d0
cmpl $0x0, -0x30(%rbp)
je 0x9f5d0
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x18(%rbp), %xmm0
cvtsi2sdl -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x264a0
movaps %xmm0, %xmm1
movsd -0x48(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x9f6d7
movsd -0x18(%rbp), %xmm0
movsd 0xa817b(%rip), %xmm1    # 0x147758
ucomisd %xmm1, %xmm0
jne 0x9f6d5
jp 0x9f6d5
movsd 0xa8173(%rip), %xmm0    # 0x147768
ucomisd -0x20(%rbp), %xmm0
jbe 0x9f6d5
movsd -0x20(%rbp), %xmm0
mulsd -0x20(%rbp), %xmm0
mulsd -0x20(%rbp), %xmm0
mulsd -0x20(%rbp), %xmm0
movsd -0x20(%rbp), %xmm2
mulsd -0x20(%rbp), %xmm2
mulsd -0x20(%rbp), %xmm2
movsd 0xa8165(%rip), %xmm1    # 0x147790
mulsd %xmm2, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0xa813a(%rip), %xmm2    # 0x147788
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd -0x20(%rbp), %xmm1
mulsd -0x20(%rbp), %xmm1
movsd 0xa8118(%rip), %xmm2    # 0x147780
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x20(%rbp), %xmm0
movsd 0xa80fb(%rip), %xmm2    # 0x147778
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xa80e3(%rip), %xmm1    # 0x147770
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x20(%rbp), %xmm0
movsd -0x38(%rbp), %xmm2
movsd 0x97df0(%rip), %xmm1    # 0x137498
divsd %xmm1, %xmm2
movsd -0x20(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x9f6d7
movsd -0x20(%rbp), %xmm0
movsd 0xa80b4(%rip), %xmm2    # 0x147798
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rcx
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
andq %rdx, %rcx
orq %rcx, %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)