// File: /workspace/github_c_makefile_star2_O3/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, 0x1c(%rsp)
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rax
movq $0x0, 0x38(%rsp)
movl 0x30(%rdi), %r14d
testb $0x1, %r14b
jne 0x3eee7
movq %rax, 0x28(%rsp)
leal -0x2(%r14), %eax
shrl $0x1f, %eax
leal (%r14,%rax), %ebp
addl $-0x2, %ebp
sarl %ebp
movslq %ebp, %rdi
shlq $0x3, %rdi
callq 0x299b5
movq %rax, %r13
cmpl $0x4, %r14d
jl 0x3eea3
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 0x310bf
testl %eax, %eax
jne 0x3ef3c
addq $0x8, %r12
cmpq %r12, %r15
jne 0x3ee7b
movq 0x8(%rbx), %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0x31724
testq %rax, %rax
je 0x3ef0b
movq %rax, %r12
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
je 0x3ef62
leaq 0x8b92f(%rip), %rax     # 0xca800
movq 0x70(%rax), %rsi
movq %rbx, %rdi
callq 0x2d97c
xorl %r14d, %r14d
xorl %ebp, %ebp
jmp 0x3f2ae
leaq 0x8b912(%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 0x2d97c
leaq 0x8b096(%rip), %rax     # 0xc9fa8
cmpq $0x0, 0x780(%rax)
je 0x3ef35
movq 0x38(%rbx), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rcx), %rcx
movslq -0x8(%rcx), %rcx
cmpq %rcx, 0x788(%rax)
jae 0x3ef4b
callq 0x30946
jmp 0x3ef50
xorl %ebp, %ebp
movq 0x28(%rsp), %r15
xorl %r14d, %r14d
jmp 0x3f2ae
callq 0x3099a
movq %rax, %r12
movq 0x8(%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x317f0
cmpl $0x4, %r14d
jl 0x3f246
cmpl $0x2, %ebp
movl $0x1, %r14d
cmovgel %ebp, %r14d
shlq $0x3, %r14
xorl %r15d, %r15d
leaq 0x8b021(%rip), %rbp     # 0xc9fa8
movl $0x0, 0x4(%rsp)
movq $0x0, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movsd (%r13,%r15), %xmm0
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
movsd %xmm0, 0x8(%rsp)
je 0x3f075
cmpl $0x5, %eax
jne 0x3f301
movq 0x38(%rbx), %rax
movq 0x18(%rax,%r15,2), %r13
movq 0x8(%r12), %rdi
movq %r13, %rsi
leaq 0x38(%rsp), %rdx
callq 0x3e6af
testq %rax, %rax
je 0x3f1b0
movsd 0x38(%rsp), %xmm0
cmpl $0x0, 0x1c(%rsp)
movsd 0x8(%rsp), %xmm1
je 0x3f010
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jp 0x3f28c
ucomisd %xmm0, %xmm1
jne 0x3f018
jnp 0x3f06b
movq 0x8(%r12), %rdi
movq %rax, 0x40(%rsp)
leaq 0x40(%rsp), %rsi
movsd %xmm1, 0x8(%rsp)
callq 0x2c713
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2c713
movq %rax, 0x8(%r12)
movq %rax, %rdi
movq %r13, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x3e887
movsd 0x8(%rsp), %xmm1
movq %rax, 0x8(%r12)
incq 0x560(%rbp)
incl 0x4(%rsp)
movq 0x20(%rsp), %r13
jmp 0x3f237
movq 0x8(%r12), %rbp
movq 0x38(%rbx), %rax
movq 0x18(%rax,%r15,2), %rdi
callq 0x30c49
movq %rax, %r14
movq 0x38(%rbx), %rax
movq %r14, 0x18(%rax,%r15,2)
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x226ac
testq %rax, %rax
je 0x3f14a
movq %rax, %r13
movq (%rax), %r14
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
cmpl $0x0, 0x1c(%rsp)
movsd 0x8(%rsp), %xmm1
je 0x3f0d8
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
jp 0x3f28c
ucomisd %xmm0, %xmm1
jne 0x3f0e4
jnp 0x3f226
movsd %xmm1, 0x8(%rsp)
movq 0x8(%rbp), %rdi
movq %r14, %rsi
callq 0x3d412
testl %eax, %eax
je 0x3f31b
movq 0x8(%rbp), %rdi
movsd 0x8(%rsp), %xmm0
movq %r14, %rsi
callq 0x3d10a
movq %rax, %rbp
movq %r14, %rdi
callq 0x306f3
movsd 0x8(%rsp), %xmm1
addq $0x8, %rbp
movq %rbp, 0x8(%r13)
leaq 0x8ae78(%rip), %rbp     # 0xc9fa8
incq 0x560(%rbp)
incl 0x4(%rsp)
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %r14
jmp 0x3f237
movq 0x8(%rbp), %rdi
movsd 0x8(%rsp), %xmm0
movq %r14, %rsi
callq 0x3d10a
movq %rax, %r13
movq %r14, %rdi
callq 0x306f3
movq (%rbp), %rdi
addq $0x8, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x2249c
testl %eax, %eax
jne 0x3f337
movq %r14, %rdi
callq 0x306f3
leaq 0x8ae17(%rip), %rbp     # 0xc9fa8
incq 0x560(%rbp)
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %r14
jmp 0x3f21e
movq 0x8(%r12), %rdi
movq %r13, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x3e887
movq %rax, 0x8(%r12)
movq %rax, %rdi
callq 0x2cc17
shrl %eax
cmpq %rax, 0x780(%rbp)
jae 0x3f1e8
movq %r12, %rdi
movl $0x7, %esi
callq 0x3eb2c
movq 0x8(%r13), %rax
movslq -0x8(%rax), %rax
cmpq %rax, 0x788(%rbp)
jae 0x3f206
movq %r12, %rdi
movl $0x7, %esi
callq 0x3eb2c
incq 0x560(%rbp)
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %r13
movsd 0x8(%rsp), %xmm1
jmp 0x3f237
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %r14
leaq 0x8ad71(%rip), %rbp     # 0xc9fa8
addq $0x8, %r15
cmpq %r15, %r14
jne 0x3efa2
jmp 0x3f25b
xorpd %xmm1, %xmm1
movq $0x0, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x3f275
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x2df9b
movq 0x28(%rsp), %r15
jmp 0x3f2a5
movq 0x10(%rsp), %r14
movslq %r14d, %rsi
movq %rbx, %rdi
callq 0x2e133
movq 0x28(%rsp), %r15
jmp 0x3f2aa
leaq 0x5a0b6(%rip), %rsi     # 0x99349
movq %rbx, %rdi
callq 0x2dc63
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %r14
movl 0x4(%rsp), %ebp
movq %r13, %rdi
callq 0x29af7
orl %ebp, %r14d
je 0x3f2f2
movq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x319b8
cmpl $0x0, 0x1c(%rsp)
leaq 0x550ff(%rip), %rax     # 0x943d2
leaq 0x5a0e7(%rip), %rsi     # 0x993c1
cmoveq %rax, %rsi
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r15, %rdx
callq 0x60371
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x580ab(%rip), %rdi     # 0x973b3
leaq 0x59ef0(%rip), %rsi     # 0x991ff
movl $0x760, %edx            # imm = 0x760
callq 0x4cae0
jmp 0x3f355
leaq 0x5a04d(%rip), %rdx     # 0x9936f
leaq 0x59ed6(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq %r14, %rsi
movl $0x746, %r8d            # imm = 0x746
jmp 0x3f350
leaq 0x5a054(%rip), %rdx     # 0x99392
leaq 0x59eba(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
xorl %esi, %esi
movl $0x759, %r8d            # imm = 0x759
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0