// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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) == C_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);
    serverAssert(dense_retval == 1);
    return dense_retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %rax
movq %rdi, %r15
leaq 0x40(%rsp), %rdx
movq %rsi, 0x30(%rsp)
movq %rsi, %rdi
movq %rax, 0x38(%rsp)
movq %rax, %rsi
callq 0x7038b
movl %eax, %r12d
movzbl %r12b, %eax
cmpl $0x20, %eax
jbe 0x70910
movq %r15, %rdi
callq 0x706fa
pushq $-0x1
popq %r12
cmpl $-0x1, %eax
je 0x70ce8
movq 0x8(%r15), %rdi
addq $0x10, %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x703c1
testl %eax, %eax
je 0x70cfa
pushq $0x1
jmp 0x70a79
movl %eax, 0x8(%rsp)
movq 0x8(%r15), %rdi
pushq $0x3
popq %rsi
callq 0x31243
movq %rax, %rbx
movq %rax, 0x8(%r15)
leaq 0x10(%rax), %r14
movq %rax, %rdi
callq 0x70863
movq %rbx, %rdi
addq %rax, %rbx
addq $0x10, %rbx
addq $-0x10, %rbx
xorl %r11d, %r11d
movq 0x40(%rsp), %rax
pushq $0x1
popq %rdx
pushq $0x2
popq %r10
xorl %ecx, %ecx
xorl %ebp, %ebp
movq %r14, %r13
cmpq %rbx, %r13
jae 0x709a4
movzbl (%r13), %esi
cmpl $0x40, %esi
jb 0x70981
testb %sil, %sil
js 0x7097e
andl $0x3f, %esi
shll $0x8, %esi
movzbl 0x1(%r13), %r8d
orl %r8d, %esi
movq %r10, %r8
jmp 0x70984
andl $0x3, %esi
movq %rdx, %r8
movl %esi, %r11d
leaq (%rcx,%r11), %r9
incq %r9
incq %r11
cmpq %r9, %rax
jl 0x709ad
addq %r13, %r8
movq %r9, %rcx
movq %r13, %rbp
movq %r8, %r13
jmp 0x70957
testq %r11, %r11
je 0x70a77
movzbl (%r13), %r9d
movl %r9d, %edx
andl $0xc0, %edx
xorl %r8d, %r8d
cmpl $0x40, %edx
sete %r8b
leaq (%r8,%r13), %r10
incq %r10
xorl %esi, %esi
cmpq %rbx, %r10
cmovbq %r10, %rsi
movl %r9d, %edx
andl $0xc0, %edx
je 0x70a2e
cmpl $0x40, %edx
je 0x70a33
movl %r12d, 0x18(%rsp)
movl %r9d, %edx
shrl $0x2, %edx
andl $0x1f, %edx
leal 0x1(%rdx), %r8d
xorl %r12d, %r12d
cmpl %r8d, 0x8(%rsp)
jbe 0x70ce8
movq %r15, 0x10(%rsp)
testb $0x3, %r9b
je 0x70b20
leal (%r11,%rcx), %r9d
decl %r9d
cmpq %rcx, %rax
jne 0x70b34
leaq 0x3(%rsp), %r12
jmp 0x70b55
testl %r9d, %r9d
je 0x70a80
leal (%r11,%rcx), %r9d
decl %r9d
movq %rax, %r8
subq %rcx, %r8
movq %r15, 0x10(%rsp)
movl %r12d, %r11d
jne 0x70a51
leaq 0x3(%rsp), %r12
jmp 0x70aa7
cmpl $0x41, %r8d
jl 0x70a9a
leal -0x1(%r8), %ecx
movl %ecx, %r8d
shrl $0x8, %r8d
orb $0x40, %r8b
leaq 0x5(%rsp), %r12
movb %r8b, -0x2(%r12)
movb %cl, -0x1(%r12)
jmp 0x70aa7
pushq $-0x1
popq %r12
jmp 0x70ce8
movq %r15, 0x10(%rsp)
shlb $0x2, %r12b
addb $0x7c, %r12b
orb $-0x80, %r12b
movb %r12b, (%r13)
jmp 0x70c27
decb %r8b
leaq 0x4(%rsp), %r12
movb %r8b, -0x1(%r12)
shlb $0x2, %r11b
addb $0x7c, %r11b
orb $-0x80, %r11b
movb %r11b, (%r12)
movslq %r9d, %rcx
cmpq %rcx, %rax
jne 0x70acc
incq %r12
leaq 0x3(%rsp), %rax
subq %rax, %r12
jmp 0x70b12
subl %eax, %r9d
cmpl $0x41, %r9d
jl 0x70afb
decl %r9d
movl %r9d, %eax
shrl $0x8, %eax
orb $0x40, %al
leaq 0x3(%rsp), %rcx
negq %rcx
movb %al, 0x1(%r12)
movb %r9b, 0x2(%r12)
addq %rcx, %r12
addq $0x3, %r12
jmp 0x70b12
decb %r9b
leaq 0x3(%rsp), %rax
negq %rax
movb %r9b, 0x1(%r12)
addq %rax, %r12
addq $0x2, %r12
testl %edx, %edx
je 0x70b93
movq %r12, 0x8(%rsp)
pushq $-0x2
popq %rax
jmp 0x70b9b
movl 0x18(%rsp), %eax
shlb $0x2, %al
addb $0x7c, %al
orb $-0x80, %al
movb %al, (%r13)
jmp 0x70c27
movl %eax, %r8d
subl %ecx, %r8d
leal (,%rdx,4), %ecx
decb %r8b
orb %cl, %r8b
orb $-0x80, %r8b
leaq 0x4(%rsp), %r12
movb %r8b, -0x1(%r12)
movl 0x18(%rsp), %ecx
shlb $0x2, %cl
addb $0x7c, %cl
orb $-0x80, %cl
movb %cl, (%r12)
movslq %r9d, %rcx
cmpq %rcx, %rax
jne 0x70b73
incq %r12
jmp 0x70b8b
shlb $0x2, %dl
notl %eax
addl %eax, %r9d
orb %dl, %r9b
orb $-0x80, %r9b
movb %r9b, 0x1(%r12)
addq $0x2, %r12
leaq 0x3(%rsp), %rax
subq %rax, %r12
pushq $-0x1
popq %rax
movq %r12, 0x8(%rsp)
addl %eax, %r12d
testl %r12d, %r12d
movq 0x10(%rsp), %r15
jle 0x70be5
movq %rdi, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movq %r10, 0x20(%rsp)
callq 0x70863
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl %r12d, %ecx
addq %rax, %rcx
leaq 0x88cd0(%rip), %rax     # 0xf98a8
cmpq 0x968(%rax), %rcx
ja 0x708d5
movslq %r12d, %r15
testl %r12d, %r12d
je 0x70c0a
cmpq %rbx, %r10
jae 0x70c0a
leaq (%rsi,%r15), %rdi
movq %rbx, %rdx
subq %rsi, %rdx
callq 0x267e0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movl %r12d, %esi
callq 0x31514
movslq 0x8(%rsp), %rdx
leaq 0x3(%rsp), %rsi
movq %r13, %rdi
callq 0x26c50
addq %r15, %rbx
testq %rbp, %rbp
cmovneq %rbp, %r14
pushq $0x5
popq %rbp
pushq $0x1
popq %r12
leaq 0x1(%r14), %r15
movl %r12d, %eax
subl %ebp, %eax
movq %rbx, %r13
subq %r14, %r13
movl %eax, %ebp
cmpq %rbx, %r14
jae 0x70cdb
cmpl $0x1, %ebp
je 0x70cdb
movzbl (%r14), %eax
movl %eax, %ecx
andl $0xc0, %ecx
je 0x70cd1
cmpl $0x40, %ecx
je 0x70cca
cmpq %rbx, %r15
jae 0x70cd1
movzbl (%r15), %ecx
testb %cl, %cl
jns 0x70cd1
movl %ecx, %edx
xorl %eax, %edx
testb $0x7c, %dl
jne 0x70cd1
movl %eax, %edx
andl $0x3, %edx
andl $0x3, %ecx
addl %edx, %ecx
cmpl $0x2, %ecx
ja 0x70cd1
andl $0x7c, %eax
incl %ecx
orl %eax, %ecx
orb $-0x80, %cl
movb %cl, 0x1(%r14)
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x267e0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
pushq $-0x1
popq %rsi
callq 0x31514
decq %rbx
incl %ebp
decq %r13
jmp 0x70c46
addq $0x2, %r14
movq %r14, %r15
negl %ebp
movq %r15, %r14
jmp 0x70c35
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
orb $-0x80, 0xf(%rax)
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4d8b7(%rip), %rdi     # 0xbe5b8
leaq 0x4d8c2(%rip), %rsi     # 0xbe5ca
movl $0x370, %edx            # imm = 0x370
callq 0x57ac8
pushq $0x1
popq %rdi
callq 0x263c0