// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 $0x68, %rsp
movl %esi, 0x18(%rsp)
movq 0x40(%rdi), %r14
movq 0x8(%r14), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
movq %rdi, 0x8(%rsp)
movl 0x38(%rdi), %ebp
movl $0x2, %r13d
leaq 0x6a1cc(%rip), %r15     # 0xbf7ad
leaq 0x711de(%rip), %r12     # 0xc67c6
cmpl %ebp, %r13d
jge 0x55667
movslq %r13d, %rax
movq (%r14,%rax,8), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x26300
testl %eax, %eax
je 0x55640
movq %rbx, %rdi
movq %r12, %rsi
callq 0x26300
testl %eax, %eax
je 0x55647
movq %rbx, %rdi
leaq 0x7ec14(%rip), %rsi     # 0xd4234
callq 0x26300
testl %eax, %eax
je 0x5564e
movq %rbx, %rdi
leaq 0x71254(%rip), %rsi     # 0xc6887
callq 0x26300
testl %eax, %eax
je 0x55655
xorl %eax, %eax
jmp 0x55663
movl $0x2, %eax
jmp 0x5565a
movl $0x4, %eax
jmp 0x5565a
movl $0x8, %eax
jmp 0x5565a
movl $0x1, %eax
orl %eax, 0x18(%rsp)
incl %r13d
movb $0x1, %al
testb %al, %al
jne 0x555e8
subl %r13d, %ebp
setne %al
testb $0x1, %bpl
sete %cl
testb %cl, %al
je 0x5568d
movl 0x18(%rsp), %ecx
movl %ecx, %eax
notl %eax
testb $0x6, %al
jne 0x556b3
leaq 0x7113e(%rip), %rsi     # 0xc67c9
jmp 0x556c6
leaq 0xb1c1c(%rip), %rax     # 0x1072b0
movq 0x80(%rax), %rsi
movq 0x8(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3fc19
sarl %ebp
testb $0x1, %cl
je 0x556de
cmpl $0x2, %ebp
jl 0x556de
leaq 0x71139(%rip), %rsi     # 0xc67ff
movq 0x8(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3ffcb
movslq %ebp, %rdi
shlq $0x3, %rdi
callq 0x3b29d
movq %rax, 0x48(%rsp)
testl %ebp, %ebp
jle 0x55732
movslq %r13d, %rbx
movl %ebp, %r15d
movq 0x48(%rsp), %r14
movq 0x8(%rsp), %rdi
movq 0x40(%rdi), %rax
movq (%rax,%rbx,8), %rsi
xorl %r12d, %r12d
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x448a1
movl $0x0, %ecx
testl %eax, %eax
jne 0x55c9d
addq $0x2, %rbx
addq $0x8, %r14
decq %r15
jne 0x556fe
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
callq 0x44f66
testq %rax, %rax
je 0x5577c
movq %rax, 0x28(%rsp)
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
je 0x557f5
leaq 0xb1b4c(%rip), %rax     # 0x1072b0
movq 0x70(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x3fc19
xorl %r12d, %r12d
xorl %ecx, %ecx
jmp 0x55c9d
movl 0x18(%rsp), %r15d
testb $0x4, %r15b
jne 0x557c5
leaq 0xb10ca(%rip), %rax     # 0x106858
cmpq $0x0, 0x948(%rax)
je 0x557be
movq 0x950(%rax), %rbx
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movslq %r13d, %rcx
movq 0x8(%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
callq 0x54164
cmpq %rax, %rbx
jae 0x557d5
callq 0x43f3a
jmp 0x557da
xorpd %xmm0, %xmm0
movb $0x1, %al
xorl %r12d, %r12d
xorl %ecx, %ecx
jmp 0x55c4f
callq 0x43f8e
movq %rax, %rdx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
movq %rdx, 0x28(%rsp)
callq 0x45007
testl %ebp, %ebp
jle 0x55c30
leal 0x1(%r13), %eax
cltq
movq %rax, 0x58(%rsp)
movl %ebp, %r12d
movslq %r13d, %rdx
incq %rdx
xorl %ebx, %ebx
movl $0x0, 0x1c(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x20(%rsp)
movl 0x18(%rsp), %r15d
movq 0x28(%rsp), %r13
movq %r12, 0x38(%rsp)
movl %ecx, 0x4(%rsp)
movq 0x48(%rsp), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movl (%r13), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
movq %rdx, 0x30(%rsp)
je 0x5592f
cmpl $0x5, %eax
jne 0x55d0f
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movq (%rax,%rdx,8), %r14
movq 0x8(%r13), %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
callq 0x54c26
testq %rax, %rax
je 0x55a72
movl $0xb, %ebp
testb $0x2, %r15b
movl 0x4(%rsp), %ecx
movsd 0x10(%rsp), %xmm0
jne 0x55bde
testb $0x1, %r15b
je 0x558c0
addsd 0x40(%rsp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x55c08
ucomisd 0x40(%rsp), %xmm0
jne 0x558ca
jnp 0x55924
movq 0x8(%r13), %rdi
movq %rax, 0x60(%rsp)
leaq 0x60(%rsp), %rbp
movq %rbp, %rsi
movsd %xmm0, 0x10(%rsp)
callq 0x3ebf5
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3ebf5
movq %rax, 0x8(%r13)
movq %rax, %rdi
movq %r14, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x54eba
movsd 0x10(%rsp), %xmm0
movl 0x4(%rsp), %ecx
movq %rax, 0x8(%r13)
leaq 0xb0f3d(%rip), %rax     # 0x106858
incq 0x710(%rax)
incl %ecx
incl 0x1c(%rsp)
xorl %ebp, %ebp
jmp 0x55bde
movl %r15d, %r12d
movq 0x8(%r13), %rbp
movq 0x8(%rsp), %r14
movq 0x40(%r14), %rax
movq 0x58(%rsp), %r15
movq (%rax,%r15,8), %rdi
callq 0x4426d
movq %rax, %r13
movq 0x40(%r14), %rax
movq %r13, (%rax,%r15,8)
movq (%rbp), %rdi
movq %r13, %rsi
callq 0x31cca
testq %rax, %rax
je 0x559d7
movq %rax, %r14
movl $0xb, %eax
movl %r12d, %r15d
testb $0x2, %r15b
jne 0x55a5a
movq (%r14), %r12
movq 0x8(%r14), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
testb $0x1, %r15b
movq 0x28(%rsp), %r13
je 0x55aef
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jnp 0x55af5
movsd %xmm1, 0x10(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x70ddc(%rip), %rsi     # 0xc67a0
callq 0x3ffcb
movl $0x7, %eax
movl 0x4(%rsp), %ecx
jmp 0x55b61
xorl %eax, %eax
movl %r12d, %r15d
testb $0x4, %r15b
jne 0x55a5a
movq 0x8(%rbp), %rdi
movsd 0x10(%rsp), %xmm0
movq %r13, %rsi
callq 0x5361a
movq %rax, %r14
movq %r13, %rdi
callq 0x43c0c
movq (%rbp), %rdi
addq $0x8, %r14
movq %r13, %rsi
movq %r14, %rdx
callq 0x31a95
testl %eax, %eax
movq 0x38(%rsp), %r12
jne 0x55d47
movq %r13, %rdi
callq 0x43c0c
leaq 0xb0e2a(%rip), %rax     # 0x106858
incq 0x710(%rax)
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
incl 0x1c(%rsp)
movl 0x4(%rsp), %ecx
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %rdx
xorl %eax, %eax
jmp 0x55b6b
movl 0x4(%rsp), %ecx
movq 0x28(%rsp), %r13
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %rdx
jmp 0x55b6b
xorl %ebp, %ebp
testb $0x4, %r15b
movsd 0x10(%rsp), %xmm0
jne 0x55bda
movq 0x8(%r13), %rdi
movq %r14, %rsi
callq 0x54eba
movq %rax, 0x8(%r13)
movq %rax, %rdi
callq 0x3ef20
shrl %eax
leaq 0xb0db3(%rip), %rcx     # 0x106858
cmpq %rax, 0x948(%rcx)
jae 0x55abb
movq %r13, %rdi
movl $0x7, %esi
callq 0x551f4
movq 0x8(%r14), %rcx
movzbl -0x1(%rcx), %eax
movl %eax, %edx
andl $0x7, %edx
cmpl $0x4, %edx
movsd 0x10(%rsp), %xmm0
ja 0x55b97
leaq 0x70abe(%rip), %rsi     # 0xc659c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
shrl $0x3, %eax
jmp 0x55b99
movsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm0, %xmm1
jne 0x55b03
jnp 0x55b57
movq 0x8(%rbp), %rdi
movq %r12, %rsi
callq 0x5390c
testl %eax, %eax
je 0x55d29
movq 0x8(%rbp), %rdi
movsd 0x10(%rsp), %xmm0
movq %r12, %rsi
callq 0x5361a
movq %rax, %r13
movq %r12, %rdi
callq 0x43c0c
addq $0x8, %r13
movq %r13, 0x8(%r14)
leaq 0xb0d15(%rip), %rax     # 0x106858
incq 0x710(%rax)
movl 0x4(%rsp), %ecx
incl %ecx
movq 0x28(%rsp), %r13
jmp 0x55b5b
movl 0x4(%rsp), %ecx
incl 0x1c(%rsp)
xorl %eax, %eax
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %r12
testl %eax, %eax
je 0x55b78
cmpl $0xb, %eax
jne 0x55d03
movsd 0x10(%rsp), %xmm0
jmp 0x55bf0
movq -0x11(%rcx), %rax
jmp 0x55b99
movzwl -0x5(%rcx), %eax
jmp 0x55b99
movl -0x9(%rcx), %eax
jmp 0x55b99
movzbl -0x3(%rcx), %eax
jmp 0x55b99
xorl %eax, %eax
leaq 0xb0cb8(%rip), %rcx     # 0x106858
cmpq 0x950(%rcx), %rax
jbe 0x55bbc
movq %r13, %rdi
movl $0x7, %esi
callq 0x551f4
movsd 0x10(%rsp), %xmm0
leaq 0xb0c95(%rip), %rax     # 0x106858
incq 0x710(%rax)
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
incl 0x1c(%rsp)
movl 0x4(%rsp), %ecx
testl %ebp, %ebp
je 0x55beb
cmpl $0xb, %ebp
jne 0x55cfe
movq 0x30(%rsp), %rdx
incq %rbx
addq $0x2, %rdx
addq $0x2, 0x58(%rsp)
cmpq %rbx, %r12
jne 0x55835
jmp 0x55c42
movq 0x8(%rsp), %rdi
leaq 0x70b8c(%rip), %rsi     # 0xc67a0
movsd %xmm0, 0x10(%rsp)
callq 0x3ffcb
movsd 0x10(%rsp), %xmm0
movl 0x4(%rsp), %ecx
movl $0x7, %ebp
jmp 0x55bde
xorpd %xmm0, %xmm0
movb $0x1, %al
xorl %r12d, %r12d
xorl %ecx, %ecx
movl 0x18(%rsp), %r15d
jmp 0x55c4f
cmpl $0x0, 0x1c(%rsp)
sete %al
movq 0x20(%rsp), %r12
testb $0x1, %r15b
je 0x55c72
movl %ecx, %ebp
testb %al, %al
je 0x55c91
leaq 0xb164e(%rip), %rax     # 0x1072b0
movq 0x50(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x3fc19
jmp 0x55c9b
movl %r15d, %eax
shll $0x1c, %eax
sarl $0x1f, %eax
andl %ecx, %eax
addl %r12d, %eax
movslq %eax, %rsi
movq 0x8(%rsp), %rdi
movl %ecx, %ebp
callq 0x40606
jmp 0x55c9b
movq 0x8(%rsp), %rdi
callq 0x40403
movl %ebp, %ecx
movq 0x48(%rsp), %rdi
movl %ecx, %ebx
callq 0x3b3df
orl %ebx, %r12d
je 0x55cef
movq 0x8(%rsp), %rbx
movq 0x10(%rbx), %rdi
movq 0x50(%rsp), %r14
movq %r14, %rsi
callq 0x45220
leaq 0x70bbb(%rip), %rax     # 0xc6886
leaq 0x6996e(%rip), %rsi     # 0xbf640
testb $0x1, 0x18(%rsp)
cmovneq %rax, %rsi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r14, %rdx
callq 0x800c9
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x7, %ebp
jmp 0x55d06
cmpl $0x7, %eax
movq 0x20(%rsp), %r12
je 0x55c9d
jmp 0x55cef
leaq 0x6df60(%rip), %rdi     # 0xc3c76
leaq 0x70939(%rip), %rsi     # 0xc6656
movl $0x564, %edx            # imm = 0x564
callq 0x6558d
jmp 0x55d67
leaq 0x70b04(%rip), %rdx     # 0xc6834
leaq 0x7091f(%rip), %rcx     # 0xc6656
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movl $0x552, %r8d            # imm = 0x552
jmp 0x55d62
leaq 0x70b09(%rip), %rdx     # 0xc6857
leaq 0x70901(%rip), %rcx     # 0xc6656
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0x55d, %r8d            # imm = 0x55D
callq 0x66380
movl $0x1, %edi
callq 0x263d0