// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/hyperloglog.c
// Function: pfselftestCommand
void pfselftestCommand(redisClient *c) {
    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 4 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);
            if (abserr < 0) abserr = -abserr;
            if (abserr > (uint64_t)(relerr*4*checkpoint)) {
                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 $0x4038, %rsp           # imm = 0x4038
movq %rdi, 0x8(%rsp)
xorl %r14d, %r14d
movl $0x3010, %esi           # imm = 0x3010
xorl %edi, %edi
callq 0x27158
movq %rax, %r15
movq %rax, 0x18(%rsp)
addq $0x10, %r15
leaq 0x30(%rsp), %r12
pushq $0x3f
popq %rbp
pushq $-0x40
popq %r13
cmpl $0x3e8, %r14d           # imm = 0x3E8
je 0x5d106
xorl %ebx, %ebx
cmpq $0x18000, %rbx          # imm = 0x18000
je 0x5d072
callq 0x1faf0
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 0x5d016
xorl %esi, %esi
xorl %edx, %edx
cmpq $0x4000, %rdx           # imm = 0x4000
je 0x5d0b8
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 0x30(%rsp,%rdx), %ecx
cmpl %ecx, %r8d
jne 0x5d0c5
incq %rdx
addl $0x6, %esi
jmp 0x5d076
incl %r14d
leaq 0x30(%rsp), %r12
jmp 0x5d007
leaq 0x39088(%rip), %rsi     # 0x96154
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x2c301
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x271f7
testq %rbx, %rbx
je 0x5d0f4
movq %rbx, %rdi
callq 0x2ee1f
addq $0x4038, %rsp           # imm = 0x4038
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 0x1f110
callq 0x5c9d7
movq %rax, (%rsp)
callq 0x1faf0
movslq %eax, %rbx
callq 0x1faf0
shlq $0x20, %rax
orq %rbx, %rax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %r13
pushq $0xa
popq %r12
movq %r13, 0x10(%rsp)
movq 0x18(%rsp), %r14
cmpq $0x989681, %r13         # imm = 0x989681
je 0x5d238
movq 0x20(%rsp), %rax
xorq %r13, %rax
movq %rax, 0x28(%rsp)
movq %r15, %rdi
leaq 0x28(%rsp), %rbp
movq %rbp, %rsi
pushq $0x8
popq %rbx
movq %rbx, %rdx
callq 0x5bc41
movq (%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x5c8aa
cmpq 0x10(%rsp), %r13
jne 0x5d22c
leaq 0x69e0e(%rip), %rax     # 0xc6fa8
movq 0x790(%rax), %rax
shrq %rax
cmpq %r13, %rax
jbe 0x5d1bb
movq (%rsp), %rax
movq 0x8(%rax), %rax
cmpb $0x1, 0x4(%rax)
jne 0x5d270
movq %r14, %rdi
xorl %esi, %esi
callq 0x5c6ed
movq %rax, %rbx
movq (%rsp), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
callq 0x5c6ed
cmpq %rax, %rbx
jne 0x5d24f
movq %r14, %rdi
xorl %esi, %esi
callq 0x5c6ed
movq %r13, %rdx
subq %rax, %rdx
movq %rdx, %rcx
negq %rcx
cmovsq %rdx, %rcx
cvtsi2sd %r13, %xmm0
mulsd 0x38ea5(%rip), %xmm0    # 0x960a8
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subsd 0x38e89(%rip), %xmm0    # 0x960a0
cvttsd2si %xmm0, %rsi
andq %rdx, %rsi
orq %rax, %rsi
movq %r12, 0x10(%rsp)
cmpq %rsi, %rcx
ja 0x5d258
incq %r13
addq $0xa, %r12
jmp 0x5d149
leaq 0x6a5c1(%rip), %rax     # 0xc7800
movq 0x8(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x2c10d
jmp 0x5d281
leaq 0x38f50(%rip), %rsi     # 0x961a6
jmp 0x5d277
leaq 0x38f68(%rip), %rsi     # 0x961c7
movq 0x8(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2c301
jmp 0x5d281
leaq 0x38f0b(%rip), %rsi     # 0x96182
movq 0x8(%rsp), %rdi
callq 0x2c2da
movq (%rsp), %rbx
jmp 0x5d0df