// File: /workspace/github_c_makefile_star2_O2/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
movq 0x40(%rdi), %r14
movq 0x8(%r14), %rax
movq %rax, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movl %esi, %r12d
movq %rdi, 0x50(%rsp)
movslq 0x38(%rdi), %r13
cmpl $0x3, %r13d
pushq $0x2
popq %rax
cmovgel %r13d, %eax
movq %rax, 0x10(%rsp)
pushq $0x2
popq %rbp
leaq 0x651d6(%rip), %r15     # 0xae6ed
cmpq %r13, %rbp
jge 0x49584
movq (%r14,%rbp,8), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x262f0
pushq $0x2
popq %rcx
testl %eax, %eax
je 0x4957c
movq %rbx, %rdi
leaq 0x6b782(%rip), %rsi     # 0xb4cc2
callq 0x262f0
pushq $0x4
popq %rcx
testl %eax, %eax
je 0x4957c
movq %rbx, %rdi
leaq 0x788d3(%rip), %rsi     # 0xc1e29
callq 0x262f0
pushq $0x8
popq %rcx
testl %eax, %eax
je 0x4957c
movq %rbx, %rdi
leaq 0x6b817(%rip), %rsi     # 0xb4d83
callq 0x262f0
pushq $0x1
popq %rcx
testl %eax, %eax
jne 0x49601
orl %ecx, %r12d
incq %rbp
jmp 0x49517
movq 0x10(%rsp), %r14
subl %r14d, %r13d
setne %al
testb $0x1, %r13b
sete %cl
testb %cl, %al
movq 0x50(%rsp), %rbp
je 0x495b1
movl %r12d, %eax
notl %eax
testb $0x6, %al
jne 0x495d5
leaq 0x6b716(%rip), %rsi     # 0xb4cc5
jmp 0x495eb
leaq 0xb0d58(%rip), %rax     # 0xfa310
movq 0x80(%rax), %rsi
movq %rbp, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36987
sarl %r13d
testb $0x1, %r12b
je 0x49606
cmpl $0x2, %r13d
jl 0x49606
leaq 0x6b710(%rip), %rsi     # 0xb4cfb
movq %rbp, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36b6f
movl %ebp, %r14d
jmp 0x49589
movl %r12d, 0x4(%rsp)
movslq %r13d, %rdi
shlq $0x3, %rdi
callq 0x327b9
movq %rax, 0x38(%rsp)
movq %r14, 0x10(%rsp)
movl %r14d, %r14d
xorl %edx, %edx
testl %r13d, %r13d
cmovlel %edx, %r13d
leaq (,%r14,8), %r15
movq %r13, 0x48(%rsp)
leaq 0x8(,%r13,8), %r13
leaq 0x8(%rdx), %rbx
cmpq %rbx, %r13
je 0x49679
movq 0x40(%rbp), %rax
addq %r15, %rax
movq (%rax,%rdx,2), %rsi
addq 0x38(%rsp), %rdx
xorl %r12d, %r12d
movq %rbp, %rdi
xorl %ecx, %ecx
callq 0x3a8ae
movq %rbx, %rdx
movl $0x0, %ebx
testl %eax, %eax
je 0x49642
jmp 0x49a71
movq 0x10(%rbp), %rdi
movq 0x30(%rsp), %rsi
callq 0x3aec2
testq %rax, %rax
je 0x496bf
movl (%rax), %ecx
andl $0xf, %ecx
cmpl $0x3, %ecx
movq 0x10(%rsp), %r15
movq %rax, %r14
je 0x49730
leaq 0xb0c67(%rip), %rax     # 0xfa310
movq 0x70(%rax), %rsi
movq %rbp, %rdi
callq 0x36987
xorl %r12d, %r12d
xorl %ebx, %ebx
jmp 0x49a71
movl 0x4(%rsp), %eax
testb $0x4, %al
movq 0x10(%rsp), %r15
jne 0x49702
leaq 0xb01d5(%rip), %rax     # 0xf98a8
cmpq $0x0, 0x958(%rax)
je 0x496fb
movq 0x960(%rax), %rbx
movq 0x40(%rbp), %rax
movq 0x8(%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x482f2
cmpq %rax, %rbx
jae 0x49717
callq 0x3a0df
jmp 0x4971c
xorl %r12d, %r12d
movl $0x0, %ebx
testb $0x1, %al
je 0x49a31
jmp 0x49a4d
callq 0x3a11e
movq %rax, %r14
movq 0x10(%rbp), %rdi
movq 0x30(%rsp), %rsi
movq %rax, %rdx
callq 0x3af45
incl %r15d
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebx, %ebx
movl $0x0, 0x1c(%rsp)
movq %r14, 0x20(%rsp)
cmpq %r13, 0x48(%rsp)
je 0x49a15
movq 0x38(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x49827
cmpl $0x5, %eax
jne 0x49acb
movq 0x40(%rbp), %rax
movq (%rax,%r15,8), %rsi
movq 0x8(%r14), %rdi
movq %rsi, 0x10(%rsp)
leaq 0x40(%rsp), %rdx
callq 0x48cbf
testq %rax, %rax
je 0x49984
movl 0x4(%rsp), %ecx
testb $0x2, %cl
movsd 0x8(%rsp), %xmm1
jne 0x49978
movsd 0x40(%rsp), %xmm0
testb $0x1, %cl
je 0x497d3
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jp 0x49a62
ucomisd %xmm0, %xmm1
jne 0x497db
jnp 0x4981e
movq 0x8(%r14), %rdi
movq %rax, %rsi
movsd %xmm1, 0x8(%rsp)
callq 0x48d7e
movq %rax, 0x8(%r14)
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movsd 0x8(%rsp), %xmm0
callq 0x48eac
movsd 0x8(%rsp), %xmm1
movq %rax, 0x8(%r14)
leaq 0xb0093(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
incl %ebx
incl 0x1c(%rsp)
jmp 0x49978
movq %r13, 0x28(%rsp)
movq 0x8(%r14), %r14
movq 0x40(%rbp), %rax
movq (%rax,%r15,8), %rdi
callq 0x3a3ad
movq %rax, %r13
movq 0x40(%rbp), %rax
movq %r15, 0x10(%rsp)
movq %r13, (%rax,%r15,8)
movq (%r14), %rdi
movq %r13, %rsi
callq 0x2a5f9
testq %rax, %rax
je 0x49908
movl 0x4(%rsp), %ecx
testb $0x2, %cl
jne 0x49963
movq %rax, %r15
movq (%rax), %r13
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
testb $0x1, %cl
movsd 0x8(%rsp), %xmm1
je 0x4989b
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jp 0x49a62
ucomisd %xmm0, %xmm1
jne 0x498a3
jnp 0x498f8
movsd %xmm1, 0x8(%rsp)
movq 0x8(%r14), %rdi
movq %r13, %rsi
callq 0x47c17
testl %eax, %eax
je 0x49ae5
movq 0x8(%r14), %rdi
movsd 0x8(%rsp), %xmm0
movq %r13, %rsi
callq 0x479ab
movq %r13, %rdi
movq %rax, %r13
callq 0x39e74
movsd 0x8(%rsp), %xmm1
addq $0x8, %r13
movq %r13, 0x8(%r15)
leaq 0xaffb9(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
incl %ebx
incl 0x1c(%rsp)
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r14
jmp 0x49973
testb $0x4, 0x4(%rsp)
jne 0x49963
movq 0x8(%r14), %rdi
movsd 0x8(%rsp), %xmm0
movq %r13, %rsi
callq 0x479ab
movq %rax, %r15
movq %r13, %rdi
callq 0x39e74
movq (%r14), %rdi
addq $0x8, %r15
movq %r13, %rsi
movq %r15, %rdx
callq 0x2a3ed
testl %eax, %eax
jne 0x49b01
movq %r13, %rdi
callq 0x39e74
leaq 0xaff53(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
incl %r12d
incl 0x1c(%rsp)
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r14
movsd 0x8(%rsp), %xmm1
movq 0x28(%rsp), %r13
incq %r13
addq $0x2, %r15
jmp 0x4974c
testb $0x4, 0x4(%rsp)
movsd 0x8(%rsp), %xmm1
jne 0x49978
movq 0x8(%r14), %rdi
movq 0x10(%rsp), %rsi
movapd %xmm1, %xmm0
callq 0x48eac
movq %rax, 0x8(%r14)
movq %rax, %rdi
callq 0x486e9
movl %eax, %eax
leaq 0xafef0(%rip), %rcx     # 0xf98a8
cmpq %rax, 0x958(%rcx)
jae 0x499cc
movq %r14, %rdi
pushq $0x7
popq %rsi
callq 0x4913d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x482f2
leaq 0xafec7(%rip), %rcx     # 0xf98a8
cmpq 0x960(%rcx), %rax
jbe 0x499f5
movq %r14, %rdi
pushq $0x7
popq %rsi
callq 0x4913d
leaq 0xafeac(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
incl %r12d
incl 0x1c(%rsp)
movsd 0x8(%rsp), %xmm1
jmp 0x49978
testb $0x1, 0x4(%rsp)
je 0x49a31
cmpl $0x0, 0x1c(%rsp)
je 0x49a4d
movq %rbp, %rdi
movapd %xmm1, %xmm0
callq 0x36eb0
jmp 0x49a71
movl 0x4(%rsp), %eax
shll $0x1c, %eax
sarl $0x1f, %eax
andl %ebx, %eax
addl %r12d, %eax
movslq %eax, %rsi
movq %rbp, %rdi
callq 0x370a6
jmp 0x49a71
leaq 0xb08bc(%rip), %rax     # 0xfa310
movq 0x50(%rax), %rsi
movq %rbp, %rdi
callq 0x36987
jmp 0x49a71
leaq 0x6b233(%rip), %rsi     # 0xb4c9c
movq %rbp, %rdi
callq 0x36b6f
movq 0x38(%rsp), %rdi
callq 0x328f9
orl %ebx, %r12d
je 0x49abc
movq 0x10(%rbp), %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
callq 0x3b125
leaq 0x6b2ea(%rip), %rax     # 0xb4d82
leaq 0x64ae1(%rip), %rsi     # 0xae580
testb $0x1, 0x4(%rsp)
cmovneq %rax, %rsi
movq 0x10(%rbp), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %rbx, %rdx
callq 0x6fb58
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68acc(%rip), %rdi     # 0xb259e
leaq 0x6b079(%rip), %rsi     # 0xb4b52
movl $0x564, %edx            # imm = 0x564
callq 0x57b45
jmp 0x49b1f
leaq 0x6b244(%rip), %rdx     # 0xb4d30
leaq 0x6b05f(%rip), %rcx     # 0xb4b52
movq %rbp, %rdi
movq %r13, %rsi
movl $0x552, %r8d            # imm = 0x552
jmp 0x49b1a
leaq 0x6b24b(%rip), %rdx     # 0xb4d53
leaq 0x6b043(%rip), %rcx     # 0xb4b52
movq %rbp, %rdi
xorl %esi, %esi
movl $0x55d, %r8d            # imm = 0x55D
callq 0x588a4
pushq $0x1
popq %rdi
callq 0x263c0