// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/t_set.c
// Function: sinterGenericCommand
void sinterGenericCommand(redisClient *c, robj **setkeys, unsigned long setnum, robj *dstkey) {

    // 集合数组
    robj **sets = zmalloc(sizeof(robj*)*setnum);

    setTypeIterator *si;
    robj *eleobj, *dstset = NULL;
    int64_t intobj;
    void *replylen = NULL;
    unsigned long j, cardinality = 0;
    int encoding;

    for (j = 0; j < setnum; j++) {

        // 取出对象
        // 第一次执行时，取出的是 dest 集合
        // 之后执行时，取出的都是 source 集合
        robj *setobj = dstkey ?
            lookupKeyWrite(c->db,setkeys[j]) :
            lookupKeyRead(c->db,setkeys[j]);

        // 对象不存在，放弃执行，进行清理
        if (!setobj) {
            zfree(sets);
            if (dstkey) {
                if (dbDelete(c->db,dstkey)) {
                    signalModifiedKey(c->db,dstkey);
                    server.dirty++;
                }
                addReply(c,shared.czero);
            } else {
                addReply(c,shared.emptymultibulk);
            }
            return;
        }
        
        // 检查对象的类型
        if (checkType(c,setobj,REDIS_SET)) {
            zfree(sets);
            return;
        }

        // 将数组指针指向集合对象
        sets[j] = setobj;
    }

    /* Sort sets from the smallest to largest, this will improve our
     * algorithm's performance */
    // 按基数对集合进行排序，这样提升算法的效率
    qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);

    /* The first thing we should output is the total number of elements...
     * since this is a multi-bulk write, but at this stage we don't know
     * the intersection set size, so we use a trick, append an empty object
     * to the output list and save the pointer to later modify it with the
     * right length */
    // 因为不知道结果集会有多少个元素，所有没有办法直接设置回复的数量
    // 这里使用了一个小技巧，直接使用一个 BUFF 列表，
    // 然后将之后的回复都添加到列表中
    if (!dstkey) {
        replylen = addDeferredMultiBulkLength(c);
    } else {
        /* If we have a target key where to store the resulting set
         * create this key with an empty set inside */
        dstset = createIntsetObject();
    }

    /* Iterate all the elements of the first (smallest) set, and test
     * the element against all the other sets, if at least one set does
     * not include the element it is discarded */
    // 遍历基数最小的第一个集合
    // 并将它的元素和所有其他集合进行对比
    // 如果有至少一个集合不包含这个元素，那么这个元素不属于交集
    si = setTypeInitIterator(sets[0]);
    while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) {
        // 遍历其他集合，检查元素是否在这些集合中存在
        for (j = 1; j < setnum; j++) {

            // 跳过第一个集合，因为它是结果集的起始值
            if (sets[j] == sets[0]) continue;

            // 元素的编码为 INTSET 
            // 在其他集合中查找这个对象是否存在
            if (encoding == REDIS_ENCODING_INTSET) {
                /* intset with intset is simple... and fast */
                if (sets[j]->encoding == REDIS_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,intobj))
                {
                    break;
                /* in order to compare an integer with an object we
                 * have to use the generic function, creating an object
                 * for this */
                } else if (sets[j]->encoding == REDIS_ENCODING_HT) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    if (!setTypeIsMember(sets[j],eleobj)) {
                        decrRefCount(eleobj);
                        break;
                    }
                    decrRefCount(eleobj);
                }

            // 元素的编码为 字典
            // 在其他集合中查找这个对象是否存在
            } else if (encoding == REDIS_ENCODING_HT) {
                /* Optimization... if the source object is integer
                 * encoded AND the target set is an intset, we can get
                 * a much faster path. */
                if (eleobj->encoding == REDIS_ENCODING_INT &&
                    sets[j]->encoding == REDIS_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr))
                {
                    break;
                /* else... object to object check is easy as we use the
                 * type agnostic API here. */
                } else if (!setTypeIsMember(sets[j],eleobj)) {
                    break;
                }
            }
        }

        /* Only take action when all sets contain the member */
        // 如果所有集合都带有目标元素的话，那么执行以下代码
        if (j == setnum) {

            // SINTER 命令，直接返回结果集元素
            if (!dstkey) {
                if (encoding == REDIS_ENCODING_HT)
                    addReplyBulk(c,eleobj);
                else
                    addReplyBulkLongLong(c,intobj);
                cardinality++;

            // SINTERSTORE 命令，将结果添加到结果集中
            } else {
                if (encoding == REDIS_ENCODING_INTSET) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    setTypeAdd(dstset,eleobj);
                    decrRefCount(eleobj);
                } else {
                    setTypeAdd(dstset,eleobj);
                }
            }
        }
    }
    setTypeReleaseIterator(si);

    // SINTERSTORE 命令，将结果集关联到数据库
    if (dstkey) {
        /* Store the resulting set into the target, if the intersection
         * is not an empty set. */
        // 删除现在可能有的 dstkey
        int deleted = dbDelete(c->db,dstkey);

        // 如果结果集非空，那么将它关联到数据库中
        if (setTypeSize(dstset) > 0) {
            dbAdd(c->db,dstkey,dstset);
            addReplyLongLong(c,setTypeSize(dstset));
            notifyKeyspaceEvent(REDIS_NOTIFY_SET,"sinterstore",
                dstkey,c->db->id);
        } else {
            decrRefCount(dstset);
            addReply(c,shared.czero);
            if (deleted)
                notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",
                    dstkey,c->db->id);
        }

        signalModifiedKey(c->db,dstkey);

        server.dirty++;

    // SINTER 命令，回复结果集的基数
    } else {
        setDeferredMultiBulkLength(c,replylen,cardinality);
    }

    zfree(sets);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2e130
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x4ca3c
cmpq $0x0, -0x20(%rbp)
je 0x4c940
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x3c090
movq %rax, -0x80(%rbp)
jmp 0x4c95d
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x3bed0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x4c9f6
movq -0x28(%rbp), %rdi
callq 0x2e3a0
cmpq $0x0, -0x20(%rbp)
je 0x4c9dd
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x3c4f0
cmpl $0x0, %eax
je 0x4c9c7
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x3c3d0
leaq 0xc6f4a(%rip), %rax     # 0x1138f8
movq 0x560(%rax), %rcx
addq $0x1, %rcx
leaq 0xc6f38(%rip), %rax     # 0x1138f8
movq %rcx, 0x560(%rax)
movq -0x8(%rbp), %rdi
leaq 0xc777e(%rip), %rax     # 0x114150
movq 0x20(%rax), %rsi
callq 0x35a70
jmp 0x4c9f1
movq -0x8(%rbp), %rdi
leaq 0xc7768(%rip), %rax     # 0x114150
movq 0x68(%rax), %rsi
callq 0x35a70
jmp 0x4cd9e
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl $0x2, %edx
callq 0x3abb0
cmpl $0x0, %eax
je 0x4ca1b
movq -0x28(%rbp), %rdi
callq 0x2e3a0
jmp 0x4cd9e
movq -0x70(%rbp), %rdx
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x4c90c
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x8, %edx
leaq -0x250(%rip), %rcx      # 0x4c800
callq 0x1f830
cmpq $0x0, -0x20(%rbp)
jne 0x4ca6b
movq -0x8(%rbp), %rdi
callq 0x36050
movq %rax, -0x50(%rbp)
jmp 0x4ca74
callq 0x3a720
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rdi
callq 0x4b1d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x4b2c0
movl %eax, -0x64(%rbp)
cmpl $-0x1, %eax
je 0x4cc7b
movq $0x1, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x4cbf9
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x4cad1
jmp 0x4cbe8
cmpl $0x6, -0x64(%rbp)
jne 0x4cb71
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x6, %eax
jne 0x4cb16
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x67990
cmpb $0x0, %al
jne 0x4cb16
jmp 0x4cbf9
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x4cb6d
movq -0x48(%rbp), %rdi
callq 0x3a330
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x38(%rbp), %rsi
callq 0x4b110
cmpl $0x0, %eax
jne 0x4cb64
movq -0x38(%rbp), %rdi
callq 0x3aaa0
jmp 0x4cbf9
movq -0x38(%rbp), %rdi
callq 0x3aaa0
jmp 0x4cb6f
jmp 0x4cbe6
cmpl $0x2, -0x64(%rbp)
jne 0x4cbe4
movq -0x38(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x1, %eax
jne 0x4cbc4
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x6, %eax
jne 0x4cbc4
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x67990
cmpb $0x0, %al
jne 0x4cbc4
jmp 0x4cbf9
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x38(%rbp), %rsi
callq 0x4b110
cmpl $0x0, %eax
jne 0x4cbe0
jmp 0x4cbf9
jmp 0x4cbe2
jmp 0x4cbe4
jmp 0x4cbe6
jmp 0x4cbe8
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x4caa9
movq -0x58(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x4cc76
cmpq $0x0, -0x20(%rbp)
jne 0x4cc3a
cmpl $0x2, -0x64(%rbp)
jne 0x4cc1f
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x36620
jmp 0x4cc2c
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x366c0
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x4cc74
cmpl $0x6, -0x64(%rbp)
jne 0x4cc65
movq -0x48(%rbp), %rdi
callq 0x3a330
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x4ad30
movq -0x38(%rbp), %rdi
callq 0x3aaa0
jmp 0x4cc72
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x4ad30
jmp 0x4cc74
jmp 0x4cc76
jmp 0x4ca84
movq -0x30(%rbp), %rdi
callq 0x4b280
cmpq $0x0, -0x20(%rbp)
je 0x4cd84
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x3c4f0
movl %eax, -0x74(%rbp)
movq -0x40(%rbp), %rdi
callq 0x4b4b0
cmpq $0x0, %rax
jbe 0x4cd0c
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x3c160
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rdi
callq 0x4b4b0
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0x36480
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x20, %edi
leaq 0x916b6(%rip), %rsi     # 0xde3bb
callq 0x814a0
jmp 0x4cd51
movq -0x40(%rbp), %rdi
callq 0x3aaa0
movq -0x8(%rbp), %rdi
leaq 0xc7430(%rip), %rax     # 0x114150
movq 0x20(%rax), %rsi
callq 0x35a70
cmpl $0x0, -0x74(%rbp)
je 0x4cd4f
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x4, %edi
leaq 0x917bb(%rip), %rsi     # 0xde505
callq 0x814a0
jmp 0x4cd51
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x3c3d0
leaq 0xc6b8f(%rip), %rax     # 0x1138f8
movq 0x560(%rax), %rcx
addq $0x1, %rcx
leaq 0xc6b7d(%rip), %rax     # 0x1138f8
movq %rcx, 0x560(%rax)
jmp 0x4cd95
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x360c0
movq -0x28(%rbp), %rdi
callq 0x2e3a0
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)