// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zaddGenericCommand
void zaddGenericCommand(redisClient *c, int incr) {

    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 = (c->argc-2)/2;
    int added = 0, updated = 0;

    // 输入的 score - member 参数必须是成对出现的
    if (c->argc % 2) {
        addReply(c,shared.syntaxerr);
        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. */
    // 取出所有输入的 score 分值
    scores = zmalloc(sizeof(double)*elements);
    for (j = 0; j < elements; j++) {
        if (getDoubleFromObjectOrReply(c,c->argv[2+j*2],&scores[j],NULL)
            != REDIS_OK) goto cleanup;
    }

    /* Lookup the key and create the sorted set if does not exist. */
    // 取出有序集合对象
    zobj = lookupKeyWrite(c->db,key);
    if (zobj == NULL) {
        // 有序集合不存在，创建新有序集合
        if (server.zset_max_ziplist_entries == 0 ||
            server.zset_max_ziplist_value < sdslen(c->argv[3]->ptr))
        {
            zobj = createZsetObject();
        } else {
            zobj = createZsetZiplistObject();
        }
        // 关联对象到数据库
        dbAdd(c->db,key,zobj);
    } else {
        // 对象存在，检查类型
        if (zobj->type != REDIS_ZSET) {
            addReply(c,shared.wrongtypeerr);
            goto cleanup;
        }
    }

    // 处理所有元素
    for (j = 0; j < elements; j++) {
        score = scores[j];

        // 有序集合为 ziplist 编码
        if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
            unsigned char *eptr;

            /* Prefer non-encoded element when dealing with ziplists. */
            // 查找成员
            ele = c->argv[3+j*2];
            if ((eptr = zzlFind(zobj->ptr,ele,&curscore)) != NULL) {

                // 成员已存在

                // ZINCRYBY 命令时使用
                if (incr) {
                    score += curscore;
                    if (isnan(score)) {
                        addReplyError(c,nanerr);
                        goto cleanup;
                    }
                }

                /* Remove and re-insert when score changed. */
                // 执行 ZINCRYBY 命令时，
                // 或者用户通过 ZADD 修改成员的分值时执行
                if (score != curscore) {
                    // 删除已有元素
                    zobj->ptr = zzlDelete(zobj->ptr,eptr);
                    // 重新插入元素
                    zobj->ptr = zzlInsert(zobj->ptr,ele,score);
                    // 计数器
                    server.dirty++;
                    updated++;
                }
            } else {
                /* Optimize: check if the element is too large or the list
                 * becomes too long *before* executing zzlInsert. */
                // 元素不存在，直接添加
                zobj->ptr = zzlInsert(zobj->ptr,ele,score);

                // 查看元素的数量，
                // 看是否需要将 ZIPLIST 编码转换为有序集合
                if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries)
                    zsetConvert(zobj,REDIS_ENCODING_SKIPLIST);

                // 查看新添加元素的长度
                // 看是否需要将 ZIPLIST 编码转换为有序集合
                if (sdslen(ele->ptr) > server.zset_max_ziplist_value)
                    zsetConvert(zobj,REDIS_ENCODING_SKIPLIST);

                server.dirty++;
                added++;
            }

        // 有序集合为 SKIPLIST 编码
        } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
            zset *zs = zobj->ptr;
            zskiplistNode *znode;
            dictEntry *de;

            // 编码对象
            ele = c->argv[3+j*2] = tryObjectEncoding(c->argv[3+j*2]);

            // 查看成员是否存在
            de = dictFind(zs->dict,ele);
            if (de != NULL) {

                // 成员存在

                // 取出成员
                curobj = dictGetKey(de);
                // 取出分值
                curscore = *(double*)dictGetVal(de);

                // ZINCRYBY 时执行
                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. */
                // 执行 ZINCRYBY 命令时，
                // 或者用户通过 ZADD 修改成员的分值时执行
                if (score != curscore) {
                    // 删除原有元素
                    redisAssertWithInfo(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++;
                }
            } else {

                // 元素不存在，直接添加到跳跃表
                znode = zslInsert(zs->zsl,score,ele);
                incrRefCount(ele); /* Inserted in skiplist. */

                // 将元素关联到字典
                redisAssertWithInfo(c,NULL,dictAdd(zs->dict,ele,&znode->score) == DICT_OK);
                incrRefCount(ele); /* Added to dictionary. */

                server.dirty++;
                added++;
            }
        } else {
            redisPanic("Unknown sorted set encoding");
        }
    }

    if (incr) /* ZINCRBY */
        addReplyDouble(c,score);
    else /* ZADD */
        addReplyLongLong(c,added);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rax
movq $0x0, 0x30(%rsp)
movl 0x30(%rdi), %r14d
testb $0x1, %r14b
jne 0x3ee60
movq %rax, 0x20(%rsp)
leal -0x2(%r14), %eax
shrl $0x1f, %eax
leal (%r14,%rax), %ebp
addl $-0x2, %ebp
sarl %ebp
movslq %ebp, %rdi
shlq $0x3, %rdi
callq 0x29709
movq %rax, %r13
cmpl $0x4, %r14d
jl 0x3ee19
cmpl $0x2, %ebp
movl $0x1, %r15d
cmovgel %ebp, %r15d
shlq $0x3, %r15
xorl %r12d, %r12d
movq 0x38(%rbx), %rax
movq 0x10(%rax,%r12,2), %rsi
leaq (%r12,%r13), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x30dec
testl %eax, %eax
jne 0x3eeb5
addq $0x8, %r12
cmpq %r12, %r15
jne 0x3edf1
movq 0x8(%rbx), %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x31454
testq %rax, %rax
je 0x3ee84
movq %rax, 0x18(%rsp)
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
je 0x3eede
leaq 0x8b9b7(%rip), %rax     # 0xca800
movq 0x70(%rax), %rsi
movq %rbx, %rdi
callq 0x2d6f0
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x3f25f
leaq 0x8b999(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d6f0
leaq 0x8b11d(%rip), %rax     # 0xc9fa8
cmpq $0x0, 0x780(%rax)
je 0x3eeae
movq 0x38(%rbx), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rcx), %rcx
movslq -0x8(%rcx), %rcx
cmpq %rcx, 0x788(%rax)
jae 0x3eec5
callq 0x3066e
jmp 0x3eeca
xorl %r14d, %r14d
movq 0x20(%rsp), %r15
xorl %r12d, %r12d
jmp 0x3f25f
callq 0x306c2
movq 0x8(%rbx), %rdi
movq %r15, %rsi
movq %rax, 0x18(%rsp)
movq %rax, %rdx
callq 0x314ef
cmpl $0x4, %r14d
jl 0x3f217
cmpl $0x2, %ebp
movl $0x1, %eax
cmovgel %ebp, %eax
shlq $0x3, %rax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
movq 0x18(%rsp), %rbp
movsd (%r13,%r15), %xmm0
movl (%rbp), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
movsd %xmm0, (%rsp)
je 0x3eff3
cmpl $0x5, %eax
jne 0x3f2b2
movq 0x38(%rbx), %rax
movq 0x18(%rax,%r15,2), %r13
movq 0x8(%rbp), %rdi
movq %r13, %rsi
leaq 0x30(%rsp), %rdx
callq 0x3e5cc
testq %rax, %rax
je 0x3f141
cmpl $0x0, 0xc(%rsp)
movsd (%rsp), %xmm0
je 0x3ef75
addsd 0x30(%rsp), %xmm0
ucomisd %xmm0, %xmm0
jp 0x3f1dd
xorl %ecx, %ecx
ucomisd 0x30(%rsp), %xmm0
movsd %xmm0, (%rsp)
jne 0x3ef8a
jnp 0x3f1b5
movq %r12, 0x28(%rsp)
movq 0x18(%rsp), %r12
movq 0x8(%r12), %rdi
movq %rax, 0x40(%rsp)
movl %r14d, %ebp
leaq 0x40(%rsp), %r14
movq %r14, %rsi
callq 0x2c46e
movq %rax, %rdi
movq %r14, %rsi
movl %ebp, %r14d
callq 0x2c46e
movq %rax, 0x8(%r12)
movq %rax, %rdi
movq %r13, %rsi
movsd (%rsp), %xmm0
callq 0x3e7a3
xorl %ecx, %ecx
movq %rax, 0x8(%r12)
movq 0x28(%rsp), %r12
leaq 0x8afc4(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
incl %r14d
jmp 0x3f1b5
movl %r14d, 0x8(%rsp)
movq 0x8(%rbp), %rbp
movq 0x38(%rbx), %rax
movq 0x18(%rax,%r15,2), %rdi
callq 0x30971
movq %rax, %r14
movq 0x38(%rbx), %rax
movq %r14, 0x18(%rax,%r15,2)
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x224dc
testq %rax, %rax
je 0x3f0c7
movq %rax, %r13
movq (%rax), %r14
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
cmpl $0x0, 0xc(%rsp)
movsd (%rsp), %xmm1
movq %r12, 0x28(%rsp)
je 0x3f05e
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jp 0x3f1f8
xorl %r12d, %r12d
movsd %xmm1, (%rsp)
ucomisd %xmm0, %xmm1
jne 0x3f072
jnp 0x3f11f
movq 0x8(%rbp), %rdi
movq %r14, %rsi
callq 0x3d388
testl %eax, %eax
je 0x3f2cc
movq 0x8(%rbp), %rdi
movsd (%rsp), %xmm0
movq %r14, %rsi
callq 0x3d096
movq %rax, %rbp
movq %r14, %rdi
callq 0x3041b
addq $0x8, %rbp
movq %rbp, 0x8(%r13)
leaq 0x8aef7(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movl 0x8(%rsp), %r14d
incl %r14d
movq 0x10(%rsp), %r13
jmp 0x3f129
movq 0x8(%rbp), %rdi
movsd (%rsp), %xmm0
movq %r14, %rsi
callq 0x3d096
movq %rax, %r13
movq %r14, %rdi
callq 0x3041b
movq (%rbp), %rdi
addq $0x8, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x222b6
testl %eax, %eax
jne 0x3f2e8
movq %r14, %rdi
callq 0x3041b
leaq 0x8ae9b(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
incl %r12d
movq %r12, 0x28(%rsp)
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
movl 0x8(%rsp), %r14d
movq 0x18(%rsp), %rbp
testl %r12d, %r12d
jne 0x3f24f
movq 0x28(%rsp), %r12
jmp 0x3f1c7
movq 0x8(%rbp), %rdi
movq %r13, %rsi
movsd (%rsp), %xmm0
callq 0x3e7a3
movq %rax, 0x8(%rbp)
movq %rax, %rdi
callq 0x2c9a8
shrl %eax
leaq 0x8ae41(%rip), %rcx     # 0xc9fa8
cmpq %rax, 0x780(%rcx)
jae 0x3f17d
movq %rbp, %rdi
movl $0x7, %esi
callq 0x3ea98
movq 0x8(%r13), %rax
movslq -0x8(%rax), %rax
leaq 0x8ae1c(%rip), %rcx     # 0xc9fa8
cmpq %rax, 0x788(%rcx)
jae 0x3f1a2
movq %rbp, %rdi
movl $0x7, %esi
callq 0x3ea98
leaq 0x8adff(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
incl %r12d
xorl %ecx, %ecx
movq 0x10(%rsp), %r13
testl %ecx, %ecx
jne 0x3f243
movq 0x18(%rsp), %rbp
addq $0x8, %r15
cmpq %r15, 0x38(%rsp)
movsd (%rsp), %xmm0
jne 0x3ef0f
jmp 0x3f220
movsd %xmm0, (%rsp)
movq %rbx, %rdi
leaq 0x5a114(%rip), %rsi     # 0x99300
callq 0x2d9d7
movl $0x5, %ecx
jmp 0x3f1b5
movsd %xmm1, (%rsp)
movq %rbx, %rdi
leaq 0x5a0f9(%rip), %rsi     # 0x99300
callq 0x2d9d7
movl $0x5, %r12d
jmp 0x3f11f
xorps %xmm0, %xmm0
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %r15
je 0x3f236
movq %rbx, %rdi
callq 0x2dd0b
jmp 0x3f25f
movslq %r12d, %rsi
movq %rbx, %rdi
callq 0x2dea3
jmp 0x3f25f
cmpl $0x5, %ecx
movq 0x20(%rsp), %r15
je 0x3f25f
jmp 0x3f2a3
cmpl $0x5, %r12d
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r12
jne 0x3f2a3
movq %r13, %rdi
callq 0x2984b
orl %r14d, %r12d
je 0x3f2a3
movq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x316b7
cmpl $0x0, 0xc(%rsp)
leaq 0x550fe(%rip), %rax     # 0x94382
leaq 0x5a0ed(%rip), %rsi     # 0x99378
cmoveq %rax, %rsi
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r15, %rdx
callq 0x608b5
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5809a(%rip), %rdi     # 0x97353
leaq 0x59ef6(%rip), %rsi     # 0x991b6
movl $0x760, %edx            # imm = 0x760
callq 0x4d0e5
jmp 0x3f306
leaq 0x5a053(%rip), %rdx     # 0x99326
leaq 0x59edc(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r14, %rsi
movl $0x746, %r8d            # imm = 0x746
jmp 0x3f301
leaq 0x5a05a(%rip), %rdx     # 0x99349
leaq 0x59ec0(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
xorl %esi, %esi
movl $0x759, %r8d            # imm = 0x759
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0