// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/t_zset.c
// Function: zaddGenericCommand
void zaddGenericCommand(client *c, int flags) {
    static char *nanerr = "resulting score is not a number (NaN)";
    robj *key = c->argv[1];
    robj *ele;
    robj *zobj;
    robj *curobj;
    double score = 0, *scores = NULL, curscore = 0.0;
    int j, elements;
    int scoreidx = 0;
    /* The following vars are used in order to track what the command actually
     * did during the execution, to reply to the client and to trigger the
     * notification of keyspace change. */
    int added = 0;      /* Number of new elements added. */
    int updated = 0;    /* Number of elements with updated score. */
    int processed = 0;  /* Number of elements processed, may remain zero with
                           options like XX. */

    /* Parse options. At the end 'scoreidx' is set to the argument position
     * of the score of the first score-element pair. */
    scoreidx = 2;
    while(scoreidx < c->argc) {
        char *opt = c->argv[scoreidx]->ptr;
        if (!strcasecmp(opt,"nx")) flags |= ZADD_NX;
        else if (!strcasecmp(opt,"xx")) flags |= ZADD_XX;
        else if (!strcasecmp(opt,"ch")) flags |= ZADD_CH;
        else if (!strcasecmp(opt,"incr")) flags |= ZADD_INCR;
        else break;
        scoreidx++;
    }

    /* Turn options into simple to check vars. */
    int incr = (flags & ZADD_INCR) != 0;
    int nx = (flags & ZADD_NX) != 0;
    int xx = (flags & ZADD_XX) != 0;
    int ch = (flags & ZADD_CH) != 0;

    /* After the options, we expect to have an even number of args, since
     * we expect any number of score-element pairs. */
    elements = c->argc-scoreidx;
    if (elements % 2 || !elements) {
        addReply(c,shared.syntaxerr);
        return;
    }
    elements /= 2; /* Now this holds the number of score-element pairs. */

    /* Check for incompatible options. */
    if (nx && xx) {
        addReplyError(c,
            "XX and NX options at the same time are not compatible");
        return;
    }

    if (incr && elements > 1) {
        addReplyError(c,
            "INCR option supports a single increment-element pair");
        return;
    }

    /* Start parsing all the scores, we need to emit any syntax error
     * before executing additions to the sorted set, as the command should
     * either execute fully or nothing at all. */
    scores = zmalloc(sizeof(double)*elements);
    for (j = 0; j < elements; j++) {
        if (getDoubleFromObjectOrReply(c,c->argv[scoreidx+j*2],&scores[j],NULL)
            != C_OK) goto cleanup;
    }

    /* Lookup the key and create the sorted set if does not exist. */
    zobj = lookupKeyWrite(c->db,key);
    if (zobj == NULL) {
        if (xx) goto reply_to_client; /* No key + XX option: nothing to do. */
        if (server.zset_max_ziplist_entries == 0 ||
            server.zset_max_ziplist_value < sdslen(c->argv[scoreidx+1]->ptr))
        {
            zobj = createZsetObject();
        } else {
            zobj = createZsetZiplistObject();
        }
        dbAdd(c->db,key,zobj);
    } else {
        if (zobj->type != OBJ_ZSET) {
            addReply(c,shared.wrongtypeerr);
            goto cleanup;
        }
    }

    for (j = 0; j < elements; j++) {
        score = scores[j];

        if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
            unsigned char *eptr;

            /* Prefer non-encoded element when dealing with ziplists. */
            ele = c->argv[scoreidx+1+j*2];
            if ((eptr = zzlFind(zobj->ptr,ele,&curscore)) != NULL) {
                if (nx) continue;
                if (incr) {
                    score += curscore;
                    if (isnan(score)) {
                        addReplyError(c,nanerr);
                        goto cleanup;
                    }
                }

                /* Remove and re-insert when score changed. */
                if (score != curscore) {
                    zobj->ptr = zzlDelete(zobj->ptr,eptr);
                    zobj->ptr = zzlInsert(zobj->ptr,ele,score);
                    server.dirty++;
                    updated++;
                }
                processed++;
            } else if (!xx) {
                /* Optimize: check if the element is too large or the list
                 * becomes too long *before* executing zzlInsert. */
                zobj->ptr = zzlInsert(zobj->ptr,ele,score);
                if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries)
                    zsetConvert(zobj,OBJ_ENCODING_SKIPLIST);
                if (sdslen(ele->ptr) > server.zset_max_ziplist_value)
                    zsetConvert(zobj,OBJ_ENCODING_SKIPLIST);
                server.dirty++;
                added++;
                processed++;
            }
        } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {
            zset *zs = zobj->ptr;
            zskiplistNode *znode;
            dictEntry *de;

            ele = c->argv[scoreidx+1+j*2] =
                tryObjectEncoding(c->argv[scoreidx+1+j*2]);
            de = dictFind(zs->dict,ele);
            if (de != NULL) {
                if (nx) continue;
                curobj = dictGetKey(de);
                curscore = *(double*)dictGetVal(de);

                if (incr) {
                    score += curscore;
                    if (isnan(score)) {
                        addReplyError(c,nanerr);
                        /* Don't need to check if the sorted set is empty
                         * because we know it has at least one element. */
                        goto cleanup;
                    }
                }

                /* Remove and re-insert when score changed. We can safely
                 * delete the key object from the skiplist, since the
                 * dictionary still has a reference to it. */
                if (score != curscore) {
                    serverAssertWithInfo(c,curobj,zslDelete(zs->zsl,curscore,curobj));
                    znode = zslInsert(zs->zsl,score,curobj);
                    incrRefCount(curobj); /* Re-inserted in skiplist. */
                    dictGetVal(de) = &znode->score; /* Update score ptr. */
                    server.dirty++;
                    updated++;
                }
                processed++;
            } else if (!xx) {
                znode = zslInsert(zs->zsl,score,ele);
                incrRefCount(ele); /* Inserted in skiplist. */
                serverAssertWithInfo(c,NULL,dictAdd(zs->dict,ele,&znode->score) == DICT_OK);
                incrRefCount(ele); /* Added to dictionary. */
                server.dirty++;
                added++;
                processed++;
            }
        } else {
            serverPanic("Unknown sorted set encoding");
        }
    }

reply_to_client:
    if (incr) { /* ZINCRBY or INCR option. */
        if (processed)
            addReplyDouble(c,score);
        else
            addReply(c,shared.nullbulk);
    } else { /* ZADD. */
        addReplyLongLong(c,ch ? added+updated : added);
    }

cleanup:
    zfree(scores);
    if (added || updated) {
        signalModifiedKey(c->db,key);
        notifyKeyspaceEvent(NOTIFY_ZSET,
            incr ? "zincr" : "zadd", key, c->db->id);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq 0x40(%rdi), %r14
movq 0x8(%r14), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq %rdi, (%rsp)
movl 0x38(%rdi), %r12d
movl $0x2, %r13d
cmpl $0x3, %r12d
jl 0x51f3f
movl $0x2, %r13d
leaq 0x6a935(%rip), %r15     # 0xbc7fd
movq (%r14,%r13,8), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x262f0
testl %eax, %eax
je 0x51f1f
movq %rbx, %rdi
leaq 0x7195d(%rip), %rsi     # 0xc3846
callq 0x262f0
testl %eax, %eax
je 0x51f26
movq %rbx, %rdi
leaq 0x7f46d(%rip), %rsi     # 0xd1369
callq 0x262f0
testl %eax, %eax
je 0x51f2d
movq %rbx, %rdi
leaq 0x719f8(%rip), %rsi     # 0xc3907
callq 0x262f0
movl $0x1, %ecx
testl %eax, %eax
je 0x51f32
jmp 0x51f3f
movl $0x2, %ecx
jmp 0x51f32
movl $0x4, %ecx
jmp 0x51f32
movl $0x8, %ecx
orl %ecx, %ebp
incq %r13
cmpq %r13, %r12
jne 0x51ec8
movl %r12d, %r13d
subl %r13d, %r12d
setne %al
testb $0x1, %r12b
sete %cl
testb %cl, %al
je 0x51f61
movl %ebp, %eax
notl %eax
testb $0x6, %al
jne 0x51f86
leaq 0x718ea(%rip), %rsi     # 0xc3849
jmp 0x51f9c
leaq 0xb03a8(%rip), %rax     # 0x102310
movq 0x80(%rax), %rsi
movq (%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3c854
sarl %r12d
testb $0x1, %bpl
je 0x51fb3
cmpl $0x2, %r12d
jl 0x51fb3
leaq 0x718e3(%rip), %rsi     # 0xc387f
movq (%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3cc06
movslq %r12d, %rdi
shlq $0x3, %rdi
callq 0x382d1
movq %rax, 0x38(%rsp)
movq %r12, 0x8(%rsp)
testl %r12d, %r12d
jle 0x52015
movl %r13d, %ebx
movl 0x8(%rsp), %r15d
movq 0x38(%rsp), %r14
movl $0x0, %r12d
movq (%rsp), %rdi
movq 0x40(%rdi), %rax
movq (%rax,%rbx,8), %rsi
movq $0x0, 0x18(%rsp)
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x41418
testl %eax, %eax
jne 0x524a5
addq $0x2, %rbx
addq $0x8, %r14
decq %r15
jne 0x51fe1
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x40(%rsp), %rsi
callq 0x41ad6
testq %rax, %rax
je 0x52067
movq %rax, %r14
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
movq 0x8(%rsp), %rbx
je 0x520d0
leaq 0xb02c7(%rip), %rax     # 0x102310
movq 0x70(%rax), %rsi
movq (%rsp), %rdi
callq 0x3c854
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
jmp 0x524a5
testb $0x4, %bpl
jne 0x52428
leaq 0xaf830(%rip), %rax     # 0x1018a8
cmpq $0x0, 0x958(%rax)
je 0x520a7
movq 0x960(%rax), %rbx
movq (%rsp), %rax
movq 0x40(%rax), %rax
movl %r13d, %ecx
movq 0x8(%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
callq 0x50a21
cmpq %rax, %rbx
jae 0x520ae
callq 0x40ad6
jmp 0x520b3
callq 0x40b2a
movq %rax, %r14
movq 0x8(%rsp), %rbx
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x41b77
testl %ebx, %ebx
jle 0x52428
incl %r13d
movl %ebx, %r15d
xorl %r12d, %r12d
movl $0x0, 0x24(%rsp)
movl $0x0, 0x14(%rsp)
movq $0x0, 0x18(%rsp)
movq %r14, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq 0x38(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
movsd %xmm0, 0x8(%rsp)
je 0x52185
cmpl $0x5, %eax
jne 0x52507
movq (%rsp), %rax
movq 0x40(%rax), %rax
movq (%rax,%r13,8), %rbx
movq 0x8(%r14), %rdi
movq %rbx, %rsi
leaq 0x48(%rsp), %rdx
callq 0x51551
testq %rax, %rax
je 0x5225e
testb $0x2, %bpl
jne 0x523fa
movsd 0x48(%rsp), %xmm0
testb $0x1, %bpl
je 0x52345
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jnp 0x5234b
jmp 0x52490
movq 0x8(%r14), %rbx
movq (%rsp), %r14
movq 0x40(%r14), %rax
movq (%rax,%r13,8), %rdi
callq 0x40e09
movq %rax, %r15
movq 0x40(%r14), %rax
movq %r15, (%rax,%r13,8)
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x2ebca
testq %rax, %rax
je 0x521f6
testb $0x2, %bpl
jne 0x52336
movq %rax, %r14
movq (%rax), %r15
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
testb $0x1, %bpl
je 0x522d3
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jnp 0x522d9
jmp 0x52490
testb $0x4, %bpl
jne 0x52336
movq 0x8(%rbx), %rdi
movsd 0x8(%rsp), %xmm0
movq %r15, %rsi
callq 0x4feda
movq %rax, %r14
movq %r15, %rdi
callq 0x407a8
movq (%rbx), %rdi
addq $0x8, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x2e9b8
testl %eax, %eax
jne 0x5253e
movq %r15, %rdi
callq 0x407a8
leaq 0xaf662(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
jmp 0x52332
testb $0x4, %bpl
jne 0x523fa
movq 0x8(%r14), %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x517e2
movq %rax, 0x8(%r14)
movq %rax, %rdi
callq 0x3bb78
shrl %eax
leaq 0xaf619(%rip), %rcx     # 0x1018a8
cmpq %rax, 0x958(%rcx)
jae 0x522a5
movq %r14, %rdi
movl $0x7, %esi
callq 0x51ad7
movq 0x8(%rbx), %rcx
movzbl -0x1(%rcx), %eax
movl %eax, %edx
andl $0x7, %edx
cmpl $0x4, %edx
ja 0x523dc
leaq 0x7135a(%rip), %rsi     # 0xc361c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
shrl $0x3, %eax
jmp 0x523bf
movsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
ucomisd %xmm0, %xmm1
jne 0x522e7
jnp 0x52332
movq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x501e2
testl %eax, %eax
je 0x52521
movq 0x8(%rbx), %rdi
movsd 0x8(%rsp), %xmm0
movq %r15, %rsi
callq 0x4feda
movq %rax, %rbx
movq %r15, %rdi
callq 0x407a8
addq $0x8, %rbx
movq %rbx, 0x8(%r14)
leaq 0xaf581(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
incl 0x14(%rsp)
incl 0x24(%rsp)
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %r15
jmp 0x523fa
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
jne 0x5235d
jnp 0x523f6
movq 0x8(%r14), %rdi
movq %rax, 0x50(%rsp)
leaq 0x50(%rsp), %rsi
callq 0x3b88c
movq %rax, %rdi
leaq 0x50(%rsp), %rsi
callq 0x3b88c
movq %rax, 0x8(%r14)
movq %rax, %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x517e2
movq %rax, 0x8(%r14)
leaq 0xaf50b(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
incl 0x14(%rsp)
jmp 0x523f6
movq -0x11(%rcx), %rax
jmp 0x523bf
movzwl -0x5(%rcx), %eax
jmp 0x523bf
movl -0x9(%rcx), %eax
jmp 0x523bf
movzbl -0x3(%rcx), %eax
leaq 0xaf4e2(%rip), %rcx     # 0x1018a8
cmpq 0x960(%rcx), %rax
jbe 0x523dc
movq %r14, %rdi
movl $0x7, %esi
callq 0x51ad7
leaq 0xaf4c5(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
incl 0x24(%rsp)
incq %r12
addq $0x2, %r13
cmpq %r12, %r15
jne 0x52104
testb $0x1, %bpl
movsd 0x8(%rsp), %xmm0
je 0x5243d
cmpl $0x0, 0x24(%rsp)
je 0x5246b
movq (%rsp), %rdi
callq 0x3d03c
jmp 0x524a0
xorl %eax, %eax
movl $0x0, %r12d
movl $0x0, %ebx
testb $0x1, %bpl
je 0x52449
jmp 0x52475
movq 0x18(%rsp), %rax
movl %eax, %ebx
movl 0x14(%rsp), %r12d
movl %ebp, %eax
shll $0x1c, %eax
sarl $0x1f, %eax
andl %r12d, %eax
addl %ebx, %eax
movslq %eax, %rsi
movq (%rsp), %rdi
callq 0x3d23f
movl %ebx, %eax
movq %rax, 0x18(%rsp)
jmp 0x524a5
movl 0x14(%rsp), %r12d
movq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xafe8f(%rip), %rax     # 0x102310
movq 0x50(%rax), %rsi
movq (%rsp), %rdi
callq 0x3c854
jmp 0x524a5
leaq 0x71389(%rip), %rsi     # 0xc3820
movq (%rsp), %rdi
callq 0x3cc06
movl 0x14(%rsp), %r12d
movq 0x38(%rsp), %rdi
callq 0x38413
movq 0x18(%rsp), %rax
orl %r12d, %eax
je 0x524f8
movq (%rsp), %rbx
movq 0x10(%rbx), %rdi
movq 0x40(%rsp), %r14
movq %r14, %rsi
callq 0x41d90
leaq 0x71431(%rip), %rax     # 0xc3906
leaq 0x6a1b4(%rip), %rsi     # 0xbc690
testb $0x1, %bpl
cmovneq %rax, %rsi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r14, %rdx
callq 0x7c665
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6e7b8(%rip), %rdi     # 0xc0cc6
leaq 0x711c1(%rip), %rsi     # 0xc36d6
movl $0x564, %edx            # imm = 0x564
callq 0x619b4
jmp 0x5255d
leaq 0x7138c(%rip), %rdx     # 0xc38b4
leaq 0x711a7(%rip), %rcx     # 0xc36d6
movq (%rsp), %rdi
movq %r15, %rsi
movl $0x552, %r8d            # imm = 0x552
jmp 0x52558
leaq 0x71392(%rip), %rdx     # 0xc38d7
leaq 0x7118a(%rip), %rcx     # 0xc36d6
movq (%rsp), %rdi
xorl %esi, %esi
movl $0x55d, %r8d            # imm = 0x55D
callq 0x627c3
movl $0x1, %edi
callq 0x263c0