// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/hyperloglog.c
// Function: pfselftestCommand
void pfselftestCommand(client *c) {
    unsigned int j, i;
    sds bitcounters = sdsnewlen(NULL,HLL_DENSE_SIZE);
    struct hllhdr *hdr = (struct hllhdr*) bitcounters, *hdr2;
    robj *o = NULL;
    uint8_t bytecounters[HLL_REGISTERS];

    /* Test 1: access registers.
     * The test is conceived to test that the different counters of our data
     * structure are accessible and that setting their values both result in
     * the correct value to be retained and not affect adjacent values. */
    for (j = 0; j < HLL_TEST_CYCLES; j++) {
        /* Set the HLL counters and an array of unsigned byes of the
         * same size to the same set of random values. */
        for (i = 0; i < HLL_REGISTERS; i++) {
            unsigned int r = rand() & HLL_REGISTER_MAX;

            bytecounters[i] = r;
            HLL_DENSE_SET_REGISTER(hdr->registers,i,r);
        }
        /* Check that we are able to retrieve the same values. */
        for (i = 0; i < HLL_REGISTERS; i++) {
            unsigned int val;

            HLL_DENSE_GET_REGISTER(val,hdr->registers,i);
            if (val != bytecounters[i]) {
                addReplyErrorFormat(c,
                    "TESTFAILED Register %d should be %d but is %d",
                    i, (int) bytecounters[i], (int) val);
                goto cleanup;
            }
        }
    }

    /* Test 2: approximation error.
     * The test adds unique elements and check that the estimated value
     * is always reasonable bounds.
     *
     * We check that the error is smaller than a few times than the expected
     * standard error, to make it very unlikely for the test to fail because
     * of a "bad" run.
     *
     * The test is performed with both dense and sparse HLLs at the same
     * time also verifying that the computed cardinality is the same. */
    memset(hdr->registers,0,HLL_DENSE_SIZE-HLL_HDR_SIZE);
    o = createHLLObject();
    double relerr = 1.04/sqrt(HLL_REGISTERS);
    int64_t checkpoint = 1;
    uint64_t seed = (uint64_t)rand() | (uint64_t)rand() << 32;
    uint64_t ele;
    for (j = 1; j <= 10000000; j++) {
        ele = j ^ seed;
        hllDenseAdd(hdr->registers,(unsigned char*)&ele,sizeof(ele));
        hllAdd(o,(unsigned char*)&ele,sizeof(ele));

        /* Make sure that for small cardinalities we use sparse
         * encoding. */
        if (j == checkpoint && j < server.hll_sparse_max_bytes/2) {
            hdr2 = o->ptr;
            if (hdr2->encoding != HLL_SPARSE) {
                addReplyError(c, "TESTFAILED sparse encoding not used");
                goto cleanup;
            }
        }

        /* Check that dense and sparse representations agree. */
        if (j == checkpoint && hllCount(hdr,NULL) != hllCount(o->ptr,NULL)) {
                addReplyError(c, "TESTFAILED dense/sparse disagree");
                goto cleanup;
        }

        /* Check error. */
        if (j == checkpoint) {
            int64_t abserr = checkpoint - (int64_t)hllCount(hdr,NULL);
            uint64_t maxerr = ceil(relerr*6*checkpoint);

            /* Adjust the max error we expect for cardinality 10
             * since from time to time it is statistically likely to get
             * much higher error due to collision, resulting into a false
             * positive. */
            if (j == 10) maxerr = 1;

            if (abserr < 0) abserr = -abserr;
            if (abserr > (int64_t)maxerr) {
                addReplyErrorFormat(c,
                    "TESTFAILED Too big error. card:%llu abserr:%llu",
                    (unsigned long long) checkpoint,
                    (unsigned long long) abserr);
                goto cleanup;
            }
            checkpoint *= 10;
        }
    }

    /* Success! */
    addReply(c,shared.ok);

cleanup:
    sdsfree(bitcounters);
    if (o) decrRefCount(o);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4048, %rsp           # imm = 0x4048
movq %rdi, 0x8(%rsp)
xorl %r14d, %r14d
movl $0x3010, %esi           # imm = 0x3010
xorl %edi, %edi
callq 0x31018
movq %rax, %r15
movq %rax, 0x28(%rsp)
addq $0x10, %r15
leaq 0x40(%rsp), %r12
pushq $0x3f
popq %rbp
pushq $-0x40
popq %r13
cmpl $0x3e8, %r14d           # imm = 0x3E8
je 0x718cb
xorl %ebx, %ebx
cmpq $0x18000, %rbx          # imm = 0x18000
je 0x71836
callq 0x26b70
andl $0x3f, %eax
movb %al, (%r12)
movl %ebx, %esi
shrl $0x3, %esi
movl %ebx, %ecx
andb $0x6, %cl
movl %ebp, %edi
shll %cl, %edi
notb %dil
andb (%r15,%rsi), %dil
movq %rax, %r8
shlq %cl, %r8
movb $0x8, %dl
subb %cl, %dl
movl %r13d, %r9d
movl %edx, %ecx
sarl %cl, %r9d
orb %dil, %r8b
movb %r8b, (%r15,%rsi)
andb 0x1(%r15,%rsi), %r9b
shrq %cl, %rax
orb %r9b, %al
movb %al, 0x1(%r15,%rsi)
addq $0x6, %rbx
incq %r12
jmp 0x717da
xorl %esi, %esi
xorl %edx, %edx
cmpq $0x4000, %rdx           # imm = 0x4000
je 0x7187c
movl %esi, %eax
shrl $0x3, %eax
movzbl (%r15,%rax), %edi
movl %esi, %ecx
andb $0x6, %cl
shrl %cl, %edi
movzbl 0x1(%r15,%rax), %r8d
movb $0x8, %al
subb %cl, %al
movl %eax, %ecx
shll %cl, %r8d
orl %edi, %r8d
andl $0x3f, %r8d
movzbl 0x40(%rsp,%rdx), %ecx
cmpl %ecx, %r8d
jne 0x71889
incq %rdx
addl $0x6, %esi
jmp 0x7183a
incl %r14d
leaq 0x40(%rsp), %r12
jmp 0x717cb
leaq 0x4cdcc(%rip), %rsi     # 0xbe65c
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x36b96
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x311c8
testq %r12, %r12
je 0x718b9
movq %r12, %rdi
callq 0x3a265
addq $0x4048, %rsp           # imm = 0x4048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x3000, %edx           # imm = 0x3000
movq %r15, %rdi
xorl %esi, %esi
callq 0x26110
callq 0x711b2
movq %rax, %r12
callq 0x26b70
movslq %eax, %rbx
callq 0x26b70
shlq $0x20, %rax
orq %rbx, %rax
movq %rax, 0x30(%rsp)
pushq $0x1
popq %rbx
pushq $0xa
popq %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rbp
movq %rbx, 0x20(%rsp)
movq 0x28(%rsp), %r14
movq %r12, 0x10(%rsp)
cmpq $0x989681, %rbp         # imm = 0x989681
je 0x71a26
movq 0x30(%rsp), %rax
xorq %rbp, %rax
movq %rax, 0x38(%rsp)
movq %r15, %rdi
leaq 0x38(%rsp), %r13
movq %r13, %rsi
pushq $0x8
popq %rbx
movq %rbx, %rdx
callq 0x703c1
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x71098
cmpq 0x20(%rsp), %rbp
jne 0x71a18
leaq 0x87f40(%rip), %rax     # 0xf98a8
movq 0x968(%rax), %rax
shrq %rax
cmpq %rbp, %rax
jbe 0x71986
movq 0x8(%r12), %rax
cmpb $0x1, 0x4(%rax)
jne 0x71a79
movq %r14, %rdi
xorl %esi, %esi
callq 0x70eca
movq %r14, %r13
movq %rax, %r14
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x70eca
cmpq %rax, %r14
jne 0x71a40
movq %r13, %rdi
xorl %esi, %esi
callq 0x70eca
movq %rbp, %rbx
subq %rax, %rbx
cvtsi2sd %rbp, %xmm0
mulsd 0x4cbe8(%rip), %xmm0    # 0xbe5b0
callq 0x261a0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x4cbcb(%rip), %xmm0    # 0xbe5a8
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
cmpq $0xa, %rbp
pushq $0x1
popq %rax
cmoveq %rax, %rdx
movq %rbx, %rcx
negq %rcx
cmovsq %rbx, %rcx
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq %rdx, %rcx
movq %r13, %r14
movq 0x10(%rsp), %r12
jg 0x71a5e
incq %rbp
addq $0xa, 0x18(%rsp)
jmp 0x71918
leaq 0x888e3(%rip), %rax     # 0xfa310
movq 0x8(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x36987
jmp 0x718a4
leaq 0x4cc67(%rip), %rsi     # 0xbe6ae
movq 0x8(%rsp), %rdi
callq 0x36b6f
movq %r13, %r14
movq 0x10(%rsp), %r12
jmp 0x718a4
leaq 0x4cc6a(%rip), %rsi     # 0xbe6cf
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x36b96
jmp 0x718a4
leaq 0x4cc0a(%rip), %rsi     # 0xbe68a
movq 0x8(%rsp), %rdi
callq 0x36b6f
jmp 0x718a4