// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/hyperloglog.c
// Function: hllSparseAdd
int hllSparseAdd(robj *o, unsigned char *ele, size_t elesize) {
    struct hllhdr *hdr;
    uint8_t oldcount, count, *sparse, *end, *p, *prev, *next;
    long index, first, span;
    long is_zero = 0, is_xzero = 0, is_val = 0, runlen = 0;

    /* Update the register if this element produced a longer run of zeroes. */
    count = hllPatLen(ele,elesize,&index);

    /* If the count is too big to be representable by the sparse representation
     * switch to dense representation. */
    if (count > HLL_SPARSE_VAL_MAX_VALUE) goto promote;

    /* When updating a sparse representation, sometimes we may need to
     * enlarge the buffer for up to 3 bytes in the worst case (XZERO split
     * into XZERO-VAL-XZERO). Make sure there is enough space right now
     * so that the pointers we take during the execution of the function
     * will be valid all the time. */
    o->ptr = sdsMakeRoomFor(o->ptr,3);

    /* Step 1: we need to locate the opcode we need to modify to check
     * if a value update is actually needed. */
    sparse = p = ((uint8_t*)o->ptr) + HLL_HDR_SIZE;
    end = p + sdslen(o->ptr) - HLL_HDR_SIZE;

    first = 0;
    prev = NULL; /* Points to previos opcode at the end of the loop. */
    next = NULL; /* Points to the next opcode at the end of the loop. */
    span = 0;
    while(p < end) {
        long oplen;

        /* Set span to the number of registers covered by this opcode.
         *
         * This is the most performance critical loop of the sparse
         * representation. Sorting the conditionals from the most to the
         * least frequent opcode in many-bytes sparse HLLs is faster. */
        oplen = 1;
        if (HLL_SPARSE_IS_ZERO(p)) {
            span = HLL_SPARSE_ZERO_LEN(p);
        } else if (HLL_SPARSE_IS_VAL(p)) {
            span = HLL_SPARSE_VAL_LEN(p);
        } else { /* XZERO. */
            span = HLL_SPARSE_XZERO_LEN(p);
            oplen = 2;
        }
        /* Break if this opcode covers the register as 'index'. */
        if (index <= first+span-1) break;
        prev = p;
        p += oplen;
        first += span;
    }
    if (span == 0) return -1; /* Invalid format. */

    next = HLL_SPARSE_IS_XZERO(p) ? p+2 : p+1;
    if (next >= end) next = NULL;

    /* Cache current opcode type to avoid using the macro again and
     * again for something that will not change.
     * Also cache the run-length of the opcode. */
    if (HLL_SPARSE_IS_ZERO(p)) {
        is_zero = 1;
        runlen = HLL_SPARSE_ZERO_LEN(p);
    } else if (HLL_SPARSE_IS_XZERO(p)) {
        is_xzero = 1;
        runlen = HLL_SPARSE_XZERO_LEN(p);
    } else {
        is_val = 1;
        runlen = HLL_SPARSE_VAL_LEN(p);
    }

    /* Step 2: After the loop:
     *
     * 'first' stores to the index of the first register covered
     *  by the current opcode, which is pointed by 'p'.
     *
     * 'next' ad 'prev' store respectively the next and previous opcode,
     *  or NULL if the opcode at 'p' is respectively the last or first.
     *
     * 'span' is set to the number of registers covered by the current
     *  opcode.
     *
     * There are different cases in order to update the data structure
     * in place without generating it from scratch:
     *
     * A) If it is a VAL opcode already set to a value >= our 'count'
     *    no update is needed, regardless of the VAL run-length field.
     *    In this case PFADD returns 0 since no changes are performed.
     *
     * B) If it is a VAL opcode with len = 1 (representing only our
     *    register) and the value is less than 'count', we just update it
     *    since this is a trivial case. */
    if (is_val) {
        oldcount = HLL_SPARSE_VAL_VALUE(p);
        /* Case A. */
        if (oldcount >= count) return 0;

        /* Case B. */
        if (runlen == 1) {
            HLL_SPARSE_VAL_SET(p,count,1);
            goto updated;
        }
    }

    /* C) Another trivial to handle case is a ZERO opcode with a len of 1.
     * We can just replace it with a VAL opcode with our value and len of 1. */
    if (is_zero && runlen == 1) {
        HLL_SPARSE_VAL_SET(p,count,1);
        goto updated;
    }

    /* D) General case.
     *
     * The other cases are more complex: our register requires to be updated
     * and is either currently represented by a VAL opcode with len > 1,
     * by a ZERO opcode with len > 1, or by an XZERO opcode.
     *
     * In those cases the original opcode must be split into muliple
     * opcodes. The worst case is an XZERO split in the middle resuling into
     * XZERO - VAL - XZERO, so the resulting sequence max length is
     * 5 bytes.
     *
     * We perform the split writing the new sequence into the 'new' buffer
     * with 'newlen' as length. Later the new sequence is inserted in place
     * of the old one, possibly moving what is on the right a few bytes
     * if the new sequence is longer than the older one. */
    uint8_t seq[5], *n = seq;
    int last = first+span-1; /* Last register covered by the sequence. */
    int len;

    if (is_zero || is_xzero) {
        /* Handle splitting of ZERO / XZERO. */
        if (index != first) {
            len = index-first;
            if (len > HLL_SPARSE_ZERO_MAX_LEN) {
                HLL_SPARSE_XZERO_SET(n,len);
                n += 2;
            } else {
                HLL_SPARSE_ZERO_SET(n,len);
                n++;
            }
        }
        HLL_SPARSE_VAL_SET(n,count,1);
        n++;
        if (index != last) {
            len = last-index;
            if (len > HLL_SPARSE_ZERO_MAX_LEN) {
                HLL_SPARSE_XZERO_SET(n,len);
                n += 2;
            } else {
                HLL_SPARSE_ZERO_SET(n,len);
                n++;
            }
        }
    } else {
        /* Handle splitting of VAL. */
        int curval = HLL_SPARSE_VAL_VALUE(p);

        if (index != first) {
            len = index-first;
            HLL_SPARSE_VAL_SET(n,curval,len);
            n++;
        }
        HLL_SPARSE_VAL_SET(n,count,1);
        n++;
        if (index != last) {
            len = last-index;
            HLL_SPARSE_VAL_SET(n,curval,len);
            n++;
        }
    }

    /* Step 3: substitute the new sequence with the old one.
     *
     * Note that we already allocated space on the sds string
     * calling sdsMakeRoomFor(). */
     int seqlen = n-seq;
     int oldlen = is_xzero ? 2 : 1;
     int deltalen = seqlen-oldlen;

     if (deltalen > 0 &&
         sdslen(o->ptr)+deltalen > server.hll_sparse_max_bytes) goto promote;
     if (deltalen && next) memmove(next+deltalen,next,end-next);
     sdsIncrLen(o->ptr,deltalen);
     memcpy(p,seq,seqlen);
     end += deltalen;

updated:
    /* Step 4: Merge adjacent values if possible.
     *
     * The representation was updated, however the resulting representation
     * may not be optimal: adjacent VAL opcodes can sometimes be merged into
     * a single one. */
    p = prev ? prev : sparse;
    int scanlen = 5; /* Scan up to 5 upcodes starting from prev. */
    while (p < end && scanlen--) {
        if (HLL_SPARSE_IS_XZERO(p)) {
            p += 2;
            continue;
        } else if (HLL_SPARSE_IS_ZERO(p)) {
            p++;
            continue;
        }
        /* We need two adjacent VAL opcodes to try a merge, having
         * the same value, and a len that fits the VAL opcode max len. */
        if (p+1 < end && HLL_SPARSE_IS_VAL(p+1)) {
            int v1 = HLL_SPARSE_VAL_VALUE(p);
            int v2 = HLL_SPARSE_VAL_VALUE(p+1);
            if (v1 == v2) {
                int len = HLL_SPARSE_VAL_LEN(p)+HLL_SPARSE_VAL_LEN(p+1);
                if (len <= HLL_SPARSE_VAL_MAX_LEN) {
                    HLL_SPARSE_VAL_SET(p+1,v1,len);
                    memmove(p,p+1,end-p);
                    sdsIncrLen(o->ptr,-1);
                    end--;
                    /* After a merge we reiterate without incrementing 'p'
                     * in order to try to merge the just merged value with
                     * a value on its right. */
                    continue;
                }
            }
        }
        p++;
    }

    /* Invalidate the cached cardinality. */
    hdr = o->ptr;
    HLL_INVALIDATE_CACHE(hdr);
    return 1;

promote: /* Promote to dense representation. */
    if (hllSparseToDense(o) == REDIS_ERR) return -1; /* Corrupted HLL. */
    hdr = o->ptr;

    /* We need to call hllDenseAdd() to perform the operation after the
     * conversion. However the result must be 1, since if we need to
     * convert from sparse to dense a register requires to be updated.
     *
     * Note that this in turn means that PFADD will make sure the command
     * is propagated to slaves / AOF, so if there is a sparse -> dense
     * convertion, it will be performed in all the slaves as well. */
    int dense_retval = hllDenseAdd(hdr->registers, ele, elesize);
    redisAssert(dense_retval == 1);
    return dense_retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rax
movq %rdi, %rbx
leaq 0x18(%rsp), %rdx
movq %rsi, 0x30(%rsp)
movq %rsi, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
callq 0x6104b
movl %eax, %ebp
movzbl %bpl, %eax
cmpl $0x20, %eax
jbe 0x615ae
movq %rbx, %rdi
callq 0x613d7
cmpl $-0x1, %eax
je 0x616ac
movq 0x8(%rbx), %rdi
addq $0x10, %rdi
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x61087
movl $0x1, %ecx
testl %eax, %eax
jne 0x61960
leaq 0x3eb3d(%rip), %rdi     # 0xa00d0
leaq 0x3eb48(%rip), %rsi     # 0xa00e2
movl $0x370, %edx            # imm = 0x370
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0
movl %eax, 0x24(%rsp)
movq 0x8(%rbx), %rdi
movl $0x3, %esi
callq 0x2823a
movq %rbx, 0x10(%rsp)
movq %rax, 0x8(%rbx)
leaq 0x10(%rax), %r14
movslq -0x8(%rax), %rcx
leaq (%rax,%rcx), %r12
xorl %r10d, %r10d
movl $0x0, %r8d
movl $0x0, %r15d
movq %r14, %r13
cmpq $0x11, %rcx
jl 0x61652
movq 0x18(%rsp), %rcx
xorl %r15d, %r15d
movq %r14, %rdx
xorl %r8d, %r8d
movzbl (%rdx), %esi
movl $0x1, %r13d
cmpl $0x40, %esi
jb 0x61625
testb %sil, %sil
js 0x61622
andl $0x3f, %esi
shll $0x8, %esi
movzbl 0x1(%rdx), %edi
orl %edi, %esi
movl $0x2, %r13d
jmp 0x61625
andl $0x3, %esi
movl %esi, %r10d
leaq (%r8,%r10), %rsi
incq %rsi
cmpq %rsi, %rcx
jge 0x61637
xorl %r13d, %r13d
incq %r10
addq %rdx, %r13
cmpq %rsi, %rcx
cmovgeq %rsi, %r8
cmovgeq %rdx, %r15
jl 0x61652
movq %r13, %rdx
cmpq %r12, %r13
jb 0x615fb
testq %r10, %r10
je 0x616ac
movzbl (%r13), %r9d
leaq 0x2(%r13), %rbx
leaq 0x1(%r13), %rcx
movl %r9d, %esi
andl $0xc0, %esi
cmpl $0x40, %esi
cmovneq %rcx, %rbx
cmpq %r12, %rbx
movl $0x0, %edx
cmovbq %rbx, %rdx
movq %rdx, 0x28(%rsp)
movl %r9d, %r11d
andl $0xc0, %r11d
je 0x616b6
cmpl $0x40, %r11d
jne 0x616bf
movl %r9d, %edx
andl $0x3f, %edx
shll $0x8, %edx
movzbl (%rcx), %edi
orl %edx, %edi
movb $0x1, %cl
movb $0x1, %sil
jmp 0x616c9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x61960
xorl %esi, %esi
movb $0x1, %cl
movl %r9d, %edi
jmp 0x616c9
movl %r9d, %edi
andl $0x3, %edi
xorl %ecx, %ecx
xorl %esi, %esi
testb %cl, %cl
jne 0x616eb
movl %r9d, %ecx
shrb $0x2, %cl
andb $0x1f, %cl
incb %cl
movzbl %cl, %edx
xorl %ecx, %ecx
cmpl %edx, 0x24(%rsp)
jbe 0x61960
testl %edi, %edi
je 0x616f0
orl %r11d, %edi
jne 0x61705
shlb $0x2, %bpl
addb $0x7c, %bpl
orb $-0x80, %bpl
movb %bpl, (%r13)
jmp 0x61890
testl %r11d, %r11d
setne %dl
leal (%r8,%r10), %ecx
decl %ecx
movq %rsi, %r10
movl %r10d, %edi
xorb $0x1, %dil
testb %dil, %dl
jne 0x61737
movq 0x18(%rsp), %r9
movq %r9, %rdi
subq %r8, %rdi
jne 0x61750
leaq 0x3(%rsp), %rsi
jmp 0x617c2
shrb $0x2, %r9b
andb $0x1f, %r9b
movq 0x18(%rsp), %rdi
cmpq %r8, %rdi
jne 0x6176f
leaq 0x3(%rsp), %rsi
jmp 0x6178c
cmpl $0x41, %edi
jl 0x617b6
leal -0x1(%rdi), %edx
movl %edx, %edi
shrl $0x8, %edi
orb $0x40, %dil
leaq 0x5(%rsp), %rsi
movb %dil, -0x2(%rsi)
movb %dl, -0x1(%rsi)
jmp 0x617c2
movl %edi, %edx
subl %r8d, %edx
leal (,%r9,4), %r8d
decb %dl
orb %r8b, %dl
orb $-0x80, %dl
leaq 0x4(%rsp), %rsi
movb %dl, -0x1(%rsi)
movq %rbx, %r8
shlb $0x2, %bpl
addb $0x7c, %bpl
orb $-0x80, %bpl
movb %bpl, (%rsi)
movslq %ecx, %rdx
cmpq %rdx, %rdi
je 0x617dc
shlb $0x2, %r9b
notl %edi
addl %edi, %ecx
orb %r9b, %cl
orb $-0x80, %cl
jmp 0x61801
decb %dil
leaq 0x4(%rsp), %rsi
movb %dil, -0x1(%rsi)
shlb $0x2, %bpl
addb $0x7c, %bpl
orb $-0x80, %bpl
movb %bpl, (%rsi)
movslq %ecx, %rdx
movq %rbx, %r8
cmpq %rdx, %r9
jne 0x617e1
incq %rsi
jmp 0x61808
subl %r9d, %ecx
cmpl $0x41, %ecx
jl 0x617ff
decl %ecx
movl %ecx, %edx
shrl $0x8, %edx
orb $0x40, %dl
movb %dl, 0x1(%rsi)
movb %cl, 0x2(%rsi)
addq $0x3, %rsi
jmp 0x61808
decb %cl
movb %cl, 0x1(%rsi)
addq $0x2, %rsi
leaq 0x3(%rsp), %rcx
subq %rcx, %rsi
movzbl %r10b, %ebp
notl %ebp
addl %esi, %ebp
testl %ebp, %ebp
movq 0x10(%rsp), %rbx
jle 0x6183e
movslq -0x8(%rax), %rax
movl %ebp, %ecx
addq %rax, %rcx
leaq 0x68777(%rip), %rax     # 0xc9fa8
cmpq 0x790(%rax), %rcx
ja 0x61557
movq %rsi, %rbx
movslq %ebp, %rax
movq %rax, 0x8(%rsp)
testl %ebp, %ebp
je 0x6186b
cmpq %r12, %r8
jae 0x6186b
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rax
leaq (%rsi,%rax), %rdi
movq %r12, %rdx
subq %rsi, %rdx
callq 0x1f780
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movl %ebp, %esi
callq 0x282c0
movslq %ebx, %rdx
leaq 0x3(%rsp), %rsi
movq %r13, %rdi
callq 0x1fbd0
addq 0x8(%rsp), %r12
testq %r15, %r15
cmovneq %r15, %r14
movl $0x5, %ebx
movl $0x1, %ebp
subl %ebx, %ebp
leaq 0x1(%r14), %r15
cmpq %r12, %r14
jae 0x6194e
movl %ebp, %ebx
leal 0x1(%rbx), %ebp
cmpl $0x2, %ebp
je 0x6194e
movzbl (%r14), %eax
movl %eax, %ecx
andl $0xc0, %ecx
je 0x61944
cmpl $0x40, %ecx
je 0x6193d
cmpq %r12, %r15
jae 0x61944
movzbl (%r15), %ecx
testb %cl, %cl
jns 0x61944
movl %ecx, %edx
xorl %eax, %edx
movb $0x1, %r13b
testb $0x7c, %dl
jne 0x61932
movl %eax, %edx
andl $0x3, %edx
andl $0x3, %ecx
addl %edx, %ecx
cmpl $0x3, %ecx
setae %r13b
jae 0x61932
andl $0x7c, %eax
incl %ecx
orl %eax, %ecx
orb $-0x80, %cl
movb %cl, 0x1(%r14)
movq %r12, %rdx
subq %r14, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1f780
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x282c0
decq %r12
testb %r13b, %r13b
je 0x618a7
jmp 0x61944
addq $0x2, %r14
movq %r14, %r15
negl %ebx
movq %r15, %r14
jmp 0x6189c
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
orb $-0x80, 0x8(%rax)
movl $0x1, %ecx
movl %ecx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq