// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %eax
subl $0x2, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x5054d
movq -0x8(%rbp), %rdi
leaq 0xc3c14(%rip), %rax     # 0x114150
movq 0x80(%rax), %rsi
callq 0x35a70
jmp 0x50b21
movslq -0x50(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2e130
movq %rax, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x505b7
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x4c(%rbp), %ecx
shll %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
movq -0x40(%rbp), %rdx
movslq -0x4c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b520
cmpl $0x0, %eax
je 0x505aa
jmp 0x50aca
jmp 0x505ac
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x50565
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x3c090
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x50642
leaq 0xc331e(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x780(%rax)
je 0x50617
leaq 0xc330d(%rip), %rax     # 0x1138f8
movq 0x788(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x4cfd0
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0x50622
callq 0x3a7a0
movq %rax, -0x28(%rbp)
jmp 0x5062b
callq 0x3a810
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x3c160
jmp 0x5066b
movq -0x28(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
je 0x50669
movq -0x8(%rbp), %rdi
leaq 0xc3af5(%rip), %rax     # 0x114150
movq 0x70(%rax), %rsi
callq 0x35a70
jmp 0x50aca
jmp 0x5066b
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x50aa5
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x5, %eax
jne 0x50843
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x4c(%rbp), %ecx
shll %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x4f990
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x5079c
cmpl $0x0, -0xc(%rbp)
je 0x50721
movsd -0x48(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x5070a
jmp 0x5071f
movq -0x8(%rbp), %rdi
movq 0xc23bb(%rip), %rsi     # 0x112ad0
callq 0x35d40
jmp 0x50aca
jmp 0x50721
movsd -0x38(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jne 0x50731
jp 0x50731
jmp 0x50797
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rsi
callq 0x4fab0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
movsd -0x38(%rbp), %xmm0
callq 0x4fce0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq 0xc3183(%rip), %rax     # 0x1138f8
movq 0x560(%rax), %rcx
addq $0x1, %rcx
leaq 0xc3171(%rip), %rax     # 0x1138f8
movq %rcx, 0x560(%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5083e
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
movsd -0x38(%rbp), %xmm0
callq 0x4fce0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4f0e0
movl %eax, %eax
leaq 0xc3125(%rip), %rcx     # 0x1138f8
cmpq 0x780(%rcx), %rax
jbe 0x507ea
movq -0x28(%rbp), %rdi
movl $0x7, %esi
callq 0x50110
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4cfd0
leaq 0xc30fa(%rip), %rcx     # 0x1138f8
cmpq 0x788(%rcx), %rax
jbe 0x50815
movq -0x28(%rbp), %rdi
movl $0x7, %esi
callq 0x50110
leaq 0xc30dc(%rip), %rax     # 0x1138f8
movq 0x560(%rax), %rcx
addq $0x1, %rcx
leaq 0xc30ca(%rip), %rax     # 0x1138f8
movq %rcx, 0x560(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x50a95
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
jne 0x50a71
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x4c(%rbp), %ecx
shll %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3acc0
movq -0x8(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x4c(%rbp), %edx
shll %edx
addl $0x3, %edx
movslq %edx, %rdx
movq %rax, (%rcx,%rdx,8)
movq %rax, -0x20(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x23aa0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x509d0
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x50914
movsd -0x48(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x508fd
jmp 0x50912
movq -0x8(%rbp), %rdi
movq 0xc21c8(%rip), %rsi     # 0x112ad0
callq 0x35d40
jmp 0x50aca
jmp 0x50914
movsd -0x38(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jne 0x50927
jp 0x50927
jmp 0x509cb
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rdi
movsd -0x48(%rbp), %xmm0
movq -0x30(%rbp), %rsi
callq 0x4d8b0
cmpl $0x0, %eax
je 0x50944
jmp 0x5096f
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq 0x92aab(%rip), %rdx     # 0xe33fe
leaq 0x92934(%rip), %rcx     # 0xe328e
movl $0x746, %r8d            # imm = 0x746
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rdi
movsd -0x38(%rbp), %xmm0
movq -0x30(%rbp), %rsi
callq 0x4d210
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3a3f0
movq -0x70(%rbp), %rcx
addq $0x8, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq 0xc2f4f(%rip), %rax     # 0x1138f8
movq 0x560(%rax), %rcx
addq $0x1, %rcx
leaq 0xc2f3d(%rip), %rax     # 0x1138f8
movq %rcx, 0x560(%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x50a6f
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rdi
movsd -0x38(%rbp), %xmm0
movq -0x20(%rbp), %rsi
callq 0x4d210
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3a3f0
movq -0x68(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x70(%rbp), %rdx
addq $0x8, %rdx
callq 0x236d0
cmpl $0x0, %eax
jne 0x50a12
jmp 0x50a3d
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x92a00(%rip), %rdx     # 0xe3421
leaq 0x92866(%rip), %rcx     # 0xe328e
movl $0x759, %r8d            # imm = 0x759
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x20(%rbp), %rdi
callq 0x3a3f0
leaq 0xc2eab(%rip), %rax     # 0x1138f8
movq 0x560(%rax), %rcx
addq $0x1, %rcx
leaq 0xc2e99(%rip), %rax     # 0x1138f8
movq %rcx, 0x560(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x50a93
leaq 0x90a23(%rip), %rdi     # 0xe149b
leaq 0x9280f(%rip), %rsi     # 0xe328e
movl $0x760, %edx            # imm = 0x760
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x50a95
jmp 0x50a97
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x50672
cmpl $0x0, -0xc(%rbp)
je 0x50abb
movq -0x8(%rbp), %rdi
movsd -0x38(%rbp), %xmm0
callq 0x36230
jmp 0x50ac8
movq -0x8(%rbp), %rdi
movslq -0x54(%rbp), %rsi
callq 0x36480
jmp 0x50aca
movq -0x40(%rbp), %rdi
callq 0x2e3a0
cmpl $0x0, -0x54(%rbp)
jne 0x50adf
cmpl $0x0, -0x58(%rbp)
je 0x50b21
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x3c3d0
movl -0xc(%rbp), %ecx
leaq 0x8d900(%rip), %rsi     # 0xde3fa
leaq 0x9294f(%rip), %rax     # 0xe3450
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movq -0x18(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
callq 0x814a0
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)