// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rdx, %rax
movq %rdi, %r15
leaq 0x18(%rsp), %rdx
movq %rsi, 0x20(%rsp)
movq %rsi, %rdi
movq %rax, 0x10(%rsp)
movq %rax, %rsi
callq 0x7cee9
movl %eax, %ebp
movzbl %bpl, %r14d
cmpl $0x20, %r14d
jbe 0x7d4c6
movq %r15, %rdi
callq 0x7d275
cmpl $-0x1, %eax
je 0x7d509
movq 0x8(%r15), %rdi
addq $0x10, %rdi
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x7cf25
movl $0x1, %ecx
testl %eax, %eax
jne 0x7d8de
leaq 0x50565(%rip), %rdi     # 0xcda10
leaq 0x50570(%rip), %rsi     # 0xcda22
movl $0x370, %edx            # imm = 0x370
callq 0x61908
movl $0x1, %edi
callq 0x263c0
movq 0x8(%r15), %rdi
movl $0x3, %esi
callq 0x363e2
movq %rax, 0x8(%r15)
movzbl -0x1(%rax), %ebx
movl %ebx, %edx
andl $0x7, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x4, %edx
ja 0x7d8de
leaq 0x5041e(%rip), %rsi     # 0xcd914
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
movq %r15, 0x8(%rsp)
jmpq *%rdx
shrl $0x3, %ebx
jmp 0x7d528
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x7d8de
movq -0x11(%rax), %rbx
jmp 0x7d528
movzwl -0x5(%rax), %ebx
jmp 0x7d528
movl -0x9(%rax), %ebx
jmp 0x7d528
movzbl -0x3(%rax), %ebx
cmpq $0x11, %rbx
jl 0x7d8de
movl %r14d, %r15d
leaq 0x10(%rax), %r14
addq %rax, %rbx
movq 0x18(%rsp), %rdx
xorl %r12d, %r12d
movq %r14, %r13
xorl %esi, %esi
movq %r13, %rcx
movzbl (%r13), %r9d
movl $0x1, %r13d
cmpl $0x40, %r9d
jae 0x7d562
movl %r9d, %edi
jmp 0x7d587
testb %r9b, %r9b
js 0x7d581
movl %r9d, %r8d
andl $0x3f, %r8d
shll $0x8, %r8d
movzbl 0x1(%rcx), %edi
orl %r8d, %edi
movl $0x2, %r13d
jmp 0x7d587
movl %r9d, %edi
andl $0x3, %edi
movl %edi, %r8d
leaq (%r8,%rsi), %r10
incq %r10
cmpq %r10, %rdx
jl 0x7d5b1
addq %rcx, %r13
movq %rcx, %r12
movq %r10, %rsi
cmpq %rbx, %r13
jb 0x7d549
movb (%r13), %r8b
movzbl %r8b, %r9d
movq %rcx, %r12
jmp 0x7d5ba
movl %r9d, %r8d
movq %rsi, %r10
movq %rcx, %r13
movl %r9d, %ecx
andl $0xc0, %ecx
xorl %edx, %edx
cmpl $0x40, %ecx
sete %dl
addq %r13, %rdx
incq %rdx
xorl %esi, %esi
cmpq %rbx, %rdx
cmovbq %rdx, %rsi
movl %r9d, %ecx
andl $0xc0, %ecx
je 0x7d626
cmpl $0x40, %ecx
je 0x7d62b
shrb $0x2, %r8b
andb $0x1f, %r8b
leal 0x1(%r8), %ecx
movzbl %cl, %r11d
xorl %ecx, %ecx
cmpl %r11d, %r15d
jbe 0x7d8de
testb $0x3, %r9b
je 0x7d665
addl %r10d, %edi
movq 0x18(%rsp), %rcx
cmpq %r10, %rcx
jne 0x7d6fc
leaq 0x3(%rsp), %r11
jmp 0x7d71d
testb %r8b, %r8b
je 0x7d665
addl %r10d, %edi
movq 0x18(%rsp), %r8
movq %r8, %r9
subq %r10, %r9
jne 0x7d642
leaq 0x3(%rsp), %r11
jmp 0x7d686
cmpl $0x41, %r9d
jl 0x7d67a
decl %r9d
movl %r9d, %r10d
shrl $0x8, %r10d
orb $0x40, %r10b
leaq 0x5(%rsp), %r11
movb %r10b, -0x2(%r11)
movb %r9b, -0x1(%r11)
jmp 0x7d686
shlb $0x2, %bpl
addb $0x7c, %bpl
orb $-0x80, %bpl
movb %bpl, (%r13)
jmp 0x7d816
decb %r9b
leaq 0x4(%rsp), %r11
movb %r9b, -0x1(%r11)
shlb $0x2, %bpl
addb $0x7c, %bpl
orb $-0x80, %bpl
movb %bpl, (%r11)
movslq %edi, %r9
cmpq %r9, %r8
jne 0x7d6aa
incq %r11
leaq 0x3(%rsp), %rdi
subq %rdi, %r11
jmp 0x7d6ee
subl %r8d, %edi
cmpl $0x41, %edi
jl 0x7d6d8
decl %edi
movl %edi, %r8d
shrl $0x8, %r8d
orb $0x40, %r8b
leaq 0x3(%rsp), %r9
negq %r9
movb %r8b, 0x1(%r11)
movb %dil, 0x2(%r11)
addq %r9, %r11
addq $0x3, %r11
jmp 0x7d6ee
decb %dil
leaq 0x3(%rsp), %r8
negq %r8
movb %dil, 0x1(%r11)
addq %r8, %r11
addq $0x2, %r11
testl %ecx, %ecx
je 0x7d758
movq %r11, %rbp
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
jmp 0x7d760
movl %ecx, %r9d
subl %r10d, %r9d
leal (,%r8,4), %r10d
decb %r9b
orb %r10b, %r9b
orb $-0x80, %r9b
leaq 0x4(%rsp), %r11
movb %r9b, -0x1(%r11)
shlb $0x2, %bpl
addb $0x7c, %bpl
orb $-0x80, %bpl
movb %bpl, (%r11)
movslq %edi, %r9
cmpq %r9, %rcx
jne 0x7d739
incq %r11
jmp 0x7d750
shlb $0x2, %r8b
notl %ecx
addl %ecx, %edi
orb %r8b, %dil
orb $-0x80, %dil
movb %dil, 0x1(%r11)
addq $0x2, %r11
leaq 0x3(%rsp), %rcx
subq %rcx, %r11
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r11, %rbp
addl %ecx, %r11d
testl %r11d, %r11d
jle 0x7d7c3
movzbl -0x1(%rax), %ecx
movl %ecx, %edi
andl $0x7, %edi
cmpl $0x4, %edi
ja 0x7d7a2
leaq 0x501ab(%rip), %r8      # 0xcd928
movslq (%r8,%rdi,4), %rdi
addq %r8, %rdi
jmpq *%rdi
shrl $0x3, %ecx
jmp 0x7d7a4
movq -0x11(%rax), %rcx
jmp 0x7d7a4
movzwl -0x5(%rax), %ecx
jmp 0x7d7a4
movl -0x9(%rax), %ecx
jmp 0x7d7a4
movzbl -0x3(%rax), %ecx
jmp 0x7d7a4
xorl %ecx, %ecx
movl %r11d, %edi
addq %rcx, %rdi
leaq 0x840f7(%rip), %rcx     # 0x1018a8
cmpq 0x968(%rcx), %rdi
movq 0x8(%rsp), %r15
ja 0x7d46f
movslq %r11d, %r15
testl %r11d, %r11d
je 0x7d7f8
cmpq %rbx, %rdx
jae 0x7d7f8
leaq (%rsi,%r15), %rdi
movq %rbx, %rdx
subq %rsi, %rdx
movq %r15, 0x10(%rsp)
movq %r11, %r15
callq 0x267e0
movq %r15, %r11
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, %rdi
movl %r11d, %esi
callq 0x36820
movslq %ebp, %rdx
leaq 0x3(%rsp), %rsi
movq %r13, %rdi
callq 0x26c50
addq %r15, %rbx
testq %r12, %r12
cmovneq %r12, %r14
cmpq %rbx, %r14
movq 0x8(%rsp), %r13
jae 0x7d8d1
movl $0x5, %ebp
testl %ebp, %ebp
je 0x7d8d1
leaq 0x1(%r14), %r15
movl $0x1, %eax
subl %ebp, %eax
movq %rbx, %r12
subq %r14, %r12
movl %eax, %ebp
movzbl (%r14), %eax
movl %eax, %ecx
andl $0xc0, %ecx
je 0x7d8c3
cmpl $0x40, %ecx
je 0x7d8bc
cmpq %rbx, %r15
jae 0x7d8c3
movzbl (%r15), %ecx
testb %cl, %cl
jns 0x7d8c3
movl %ecx, %edx
xorl %eax, %edx
testb $0x7c, %dl
jne 0x7d8c3
movl %eax, %edx
andl $0x3, %edx
andl $0x3, %ecx
addl %edx, %ecx
cmpl $0x2, %ecx
ja 0x7d8c3
andl $0x7c, %eax
incl %ecx
orl %eax, %ecx
orb $-0x80, %cl
movb %cl, 0x1(%r14)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x267e0
movq 0x8(%r13), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x36820
decq %rbx
incl %ebp
decq %r12
cmpl $0x1, %ebp
jne 0x7d84b
jmp 0x7d8d1
addq $0x2, %r14
movq %r14, %r15
negl %ebp
movq %r15, %r14
cmpq %rbx, %r15
jb 0x7d830
movq 0x8(%r13), %rax
orb $-0x80, 0xf(%rax)
movl $0x1, %ecx
movl %ecx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq