// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, %r13
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rcx
andq $0x0, 0x40(%rsp)
movl %esi, 0x24(%rsp)
movl 0x30(%rdi), %eax
testb $0x1, %al
jne 0x3bfc5
movq %rcx, 0x48(%rsp)
addl $-0x2, %eax
sarl %eax
movslq %eax, %rbx
leaq (,%rbx,8), %rdi
callq 0x2828d
movq %rax, %rbp
xorl %edx, %edx
testl %ebx, %ebx
cmovlel %edx, %ebx
movq %rbx, 0x28(%rsp)
leaq 0x8(,%rbx,8), %rbx
movl $0x0, %r12d
leaq 0x8(%rdx), %r14
cmpq %r14, %rbx
je 0x3bfe9
movq 0x38(%r13), %rax
movq 0x10(%rax,%rdx,2), %rsi
addq %rbp, %rdx
xorl %r15d, %r15d
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x2f399
movq %r14, %rdx
testl %eax, %eax
je 0x3bf97
jmp 0x3c3e6
leaq 0x8b834(%rip), %rax     # 0xc7800
movq 0x80(%rax), %rsi
movq %r13, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c10d
movq 0x8(%r13), %rdi
movq 0x48(%rsp), %rbx
movq %rbx, %rsi
callq 0x2f988
testq %rax, %rax
je 0x3c02f
movq %rax, %rcx
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
movq 0x28(%rsp), %r14
je 0x3c07f
leaq 0x8b7e8(%rip), %rax     # 0xc7800
movq 0x70(%rax), %rsi
movq %r13, %rdi
callq 0x2c10d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x3c3e6
leaq 0x8af72(%rip), %rax     # 0xc6fa8
cmpq $0x0, 0x780(%rax)
movq 0x28(%rsp), %r14
je 0x3c05e
movq 0x38(%r13), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rcx), %rcx
movslq -0x8(%rcx), %rcx
cmpq %rcx, 0x788(%rax)
jae 0x3c065
callq 0x2ec8c
jmp 0x3c06a
callq 0x2eccb
movq 0x8(%r13), %rdi
movq %rbx, %rsi
movq %rax, %rbx
movq %rax, %rdx
callq 0x2fa09
movq %rbx, %rcx
shlq $0x3, %r14
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r13, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rbp, 0x30(%rsp)
movq %r14, 0x28(%rsp)
cmpq %rbx, %r14
je 0x3c395
movsd (%rbp,%rbx), %xmm0
movl (%rcx), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
movsd %xmm0, 0x8(%rsp)
je 0x3c130
cmpl $0x5, %eax
jne 0x3c43e
movq 0x38(%r13), %rax
movq %rbx, 0x38(%rsp)
movq 0x18(%rax,%rbx,2), %rbx
movq 0x8(%rcx), %rdi
movq %rbx, %rsi
movq %rcx, %r13
leaq 0x40(%rsp), %rdx
callq 0x3b828
testq %rax, %rax
je 0x3c201
movsd 0x40(%rsp), %xmm0
cmpl $0x0, 0x24(%rsp)
movsd 0x8(%rsp), %xmm1
je 0x3c315
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
movq 0x10(%rsp), %rcx
movq %rbx, %r13
movq 0x38(%rsp), %rbx
jnp 0x3c322
jmp 0x3c3d2
movq 0x8(%rcx), %rbp
movq 0x38(%r13), %rax
movq 0x18(%rax,%rbx,2), %rdi
callq 0x2ef4c
movq %rax, %r14
movq 0x38(%r13), %rax
movq %r14, 0x18(%rax,%rbx,2)
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x21b64
testq %rax, %rax
je 0x3c19c
movq %rax, %r13
movq (%rax), %r14
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
cmpl $0x0, 0x24(%rsp)
movsd 0x8(%rsp), %xmm1
je 0x3c298
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm1
movq 0x10(%rsp), %rcx
jnp 0x3c29d
jmp 0x3c3b7
movq 0x8(%rbp), %rdi
movsd 0x8(%rsp), %xmm0
movq %r14, %rsi
callq 0x3a577
movq %rax, %r13
movq %r14, %rdi
callq 0x2eacc
movq (%rbp), %rdi
addq $0x8, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x21956
testl %eax, %eax
jne 0x3c476
movq %r14, %rdi
callq 0x2eacc
leaq 0x8adc5(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
incl %r15d
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rbp
movq 0x28(%rsp), %r14
jmp 0x3c288
movq 0x8(%r13), %rdi
movq %rbx, 0x50(%rsp)
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x3b9e9
movq %rax, 0x8(%r13)
movq %rax, %rdi
callq 0x3b252
movl %eax, %eax
leaq 0x8ad7b(%rip), %rcx     # 0xc6fa8
cmpq %rax, 0x780(%rcx)
jae 0x3c243
movq 0x10(%rsp), %rdi
pushq $0x7
popq %rsi
callq 0x3bc66
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movslq -0x8(%rax), %rax
leaq 0x8ad51(%rip), %rcx     # 0xc6fa8
cmpq %rax, 0x788(%rcx)
movq 0x38(%rsp), %rbx
jae 0x3c272
movq 0x10(%rsp), %rdi
pushq $0x7
popq %rsi
callq 0x3bc66
leaq 0x8ad2f(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
incl %r15d
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rcx
movsd 0x8(%rsp), %xmm1
jmp 0x3c38c
movq 0x10(%rsp), %rcx
ucomisd %xmm0, %xmm1
jne 0x3c2a9
jnp 0x3c37d
movsd %xmm1, 0x8(%rsp)
movq 0x8(%rbp), %rdi
movq %r14, %rsi
callq 0x3a7e5
testl %eax, %eax
je 0x3c458
movq 0x8(%rbp), %rdi
movsd 0x8(%rsp), %xmm0
movq %r14, %rsi
callq 0x3a577
movq %rax, %rbp
movq %r14, %rdi
callq 0x2eacc
movsd 0x8(%rsp), %xmm1
addq $0x8, %rbp
movq %rbp, 0x8(%r13)
leaq 0x8acb3(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
incl %r12d
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rbp
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %rcx
jmp 0x3c38c
movq 0x10(%rsp), %rcx
movq %rbx, %r13
movq 0x38(%rsp), %rbx
ucomisd %xmm0, %xmm1
jne 0x3c32a
jnp 0x3c376
movq 0x8(%rcx), %rdi
movq %rax, %rsi
movsd %xmm1, 0x8(%rsp)
callq 0x3b8d6
movq 0x10(%rsp), %rcx
movq %rax, 0x8(%rcx)
movq %rax, %rdi
movq %r13, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x3b9e9
movsd 0x8(%rsp), %xmm1
movq 0x10(%rsp), %rcx
movq %rax, 0x8(%rcx)
leaq 0x8ac3c(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
incl %r12d
movq 0x18(%rsp), %r13
jmp 0x3c38c
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rbp
movq 0x28(%rsp), %r14
addq $0x8, %rbx
jmp 0x3c0a3
cmpl $0x0, 0x24(%rsp)
je 0x3c3aa
movq %r13, %rdi
movapd %xmm1, %xmm0
callq 0x2c607
jmp 0x3c3e6
movslq %r15d, %rsi
movq %r13, %rdi
callq 0x2c794
jmp 0x3c3e6
leaq 0x52f4f(%rip), %rsi     # 0x8f30d
movq 0x18(%rsp), %r13
movq %r13, %rdi
callq 0x2c2da
movq 0x30(%rsp), %rbp
jmp 0x3c3e6
leaq 0x52f34(%rip), %rsi     # 0x8f30d
movq 0x18(%rsp), %r13
movq %r13, %rdi
callq 0x2c2da
movq %rbp, %rdi
callq 0x283cd
orl %r12d, %r15d
je 0x3c42f
movq 0x8(%r13), %rdi
movq 0x48(%rsp), %rbx
movq %rbx, %rsi
callq 0x2fbc0
cmpl $0x0, 0x24(%rsp)
leaq 0x4df82(%rip), %rax     # 0x8a392
leaq 0x52f6e(%rip), %rsi     # 0x8f385
cmoveq %rax, %rsi
movq 0x8(%r13), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %rbx, %rdx
callq 0x5b488
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50f62(%rip), %rdi     # 0x8d3a7
leaq 0x52d77(%rip), %rsi     # 0x8f1c3
movl $0x760, %edx            # imm = 0x760
callq 0x490e8
jmp 0x3c496
leaq 0x52ed4(%rip), %rdx     # 0x8f333
leaq 0x52d5d(%rip), %rcx     # 0x8f1c3
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movl $0x746, %r8d            # imm = 0x746
jmp 0x3c491
leaq 0x52ed9(%rip), %rdx     # 0x8f356
leaq 0x52d3f(%rip), %rcx     # 0x8f1c3
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x759, %r8d            # imm = 0x759
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0