// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/bitops.c
// Function: bitopCommand
void bitopCommand(redisClient *c) {
    char *opname = c->argv[1]->ptr;
    robj *o, *targetkey = c->argv[2];
    long op, j, numkeys;
    robj **objects;      /* Array of source objects. */
    unsigned char **src; /* Array of source strings pointers. */
    long *len, maxlen = 0; /* Array of length of src strings, and max len. */
    long minlen = 0;    /* Min len among the input keys. */
    unsigned char *res = NULL; /* Resulting string. */

    /* Parse the operation name. */
    // 读入 op 参数，确定要执行的操作
    if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,"and"))
        op = BITOP_AND;
    else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,"or"))
        op = BITOP_OR;
    else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,"xor"))
        op = BITOP_XOR;
    else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,"not"))
        op = BITOP_NOT;
    else {
        addReply(c,shared.syntaxerr);
        return;
    }

    /* Sanity check: NOT accepts only a single key argument. */
    // NOT 操作只能接受单个 key 输入
    if (op == BITOP_NOT && c->argc != 4) {
        addReplyError(c,"BITOP NOT must be called with a single source key.");
        return;
    }

    /* Lookup keys, and store pointers to the string objects into an array. */
    // 查找输入键，并将它们放入一个数组里面
    numkeys = c->argc - 3;
    // 字符串数组，保存 sds 值
    src = zmalloc(sizeof(unsigned char*) * numkeys);
    // 长度数组，保存 sds 的长度
    len = zmalloc(sizeof(long) * numkeys);
    // 对象数组，保存字符串对象
    objects = zmalloc(sizeof(robj*) * numkeys);
    for (j = 0; j < numkeys; j++) {

        // 查找对象
        o = lookupKeyRead(c->db,c->argv[j+3]);

        /* Handle non-existing keys as empty strings. */
        // 不存在的键被视为空字符串
        if (o == NULL) {
            objects[j] = NULL;
            src[j] = NULL;
            len[j] = 0;
            minlen = 0;
            continue;
        }

        /* Return an error if one of the keys is not a string. */
        // 键不是字符串类型，返回错误，放弃执行操作
        if (checkType(c,o,REDIS_STRING)) {
            for (j = j-1; j >= 0; j--) {
                if (objects[j])
                    decrRefCount(objects[j]);
            }
            zfree(src);
            zfree(len);
            zfree(objects);
            return;
        }

        // 记录对象
        objects[j] = getDecodedObject(o);
        // 记录 sds
        src[j] = objects[j]->ptr;
        // 记录 sds 长度
        len[j] = sdslen(objects[j]->ptr);
        
        // 记录目前最长 sds 的长度
        if (len[j] > maxlen) maxlen = len[j];

        // 记录目前最短 sds 的长度
        if (j == 0 || len[j] < minlen) minlen = len[j];
    }

    /* Compute the bit operation, if at least one string is not empty. */
    // 如果有至少一个非空字符串，那么执行计算
    if (maxlen) {

        // 根据最大长度，创建一个 sds ，该 sds 的所有位都被设置为 0
        res = (unsigned char*) sdsnewlen(NULL,maxlen);

        unsigned char output, byte;
        long i;

        /* Fast path: as far as we have data for all the input bitmaps we
         * can take a fast path that performs much better than the
         * vanilla algorithm. */
        // 在键的数量比较少时，进行优化
        j = 0;
        if (minlen && numkeys <= 16) {
            unsigned long *lp[16];
            unsigned long *lres = (unsigned long*) res;

            /* Note: sds pointer is always aligned to 8 byte boundary. */
            memcpy(lp,src,sizeof(unsigned long*)*numkeys);
            memcpy(res,src[0],minlen);

            /* Different branches per different operations for speed (sorry). */
            // 当要处理的位大于等于 32 位时
            // 每次载入 4*8 = 32 个位，然后对这些位进行计算，利用缓存，进行加速
            if (op == BITOP_AND) {
                while(minlen >= sizeof(unsigned long)*4) {
                    for (i = 1; i < numkeys; i++) {
                        lres[0] &= lp[i][0];
                        lres[1] &= lp[i][1];
                        lres[2] &= lp[i][2];
                        lres[3] &= lp[i][3];
                        lp[i]+=4;
                    }
                    lres+=4;
                    j += sizeof(unsigned long)*4;
                    minlen -= sizeof(unsigned long)*4;
                }
            } else if (op == BITOP_OR) {
                while(minlen >= sizeof(unsigned long)*4) {
                    for (i = 1; i < numkeys; i++) {
                        lres[0] |= lp[i][0];
                        lres[1] |= lp[i][1];
                        lres[2] |= lp[i][2];
                        lres[3] |= lp[i][3];
                        lp[i]+=4;
                    }
                    lres+=4;
                    j += sizeof(unsigned long)*4;
                    minlen -= sizeof(unsigned long)*4;
                }
            } else if (op == BITOP_XOR) {
                while(minlen >= sizeof(unsigned long)*4) {
                    for (i = 1; i < numkeys; i++) {
                        lres[0] ^= lp[i][0];
                        lres[1] ^= lp[i][1];
                        lres[2] ^= lp[i][2];
                        lres[3] ^= lp[i][3];
                        lp[i]+=4;
                    }
                    lres+=4;
                    j += sizeof(unsigned long)*4;
                    minlen -= sizeof(unsigned long)*4;
                }
            } else if (op == BITOP_NOT) {
                while(minlen >= sizeof(unsigned long)*4) {
                    lres[0] = ~lres[0];
                    lres[1] = ~lres[1];
                    lres[2] = ~lres[2];
                    lres[3] = ~lres[3];
                    lres+=4;
                    j += sizeof(unsigned long)*4;
                    minlen -= sizeof(unsigned long)*4;
                }
            }
        }

        /* j is set to the next byte to process by the previous loop. */
        // 以正常方式执行位运算
        for (; j < maxlen; j++) {
            output = (len[0] <= j) ? 0 : src[0][j];
            if (op == BITOP_NOT) output = ~output;
            // 遍历所有输入键，对所有输入的 scr[i][j] 字节进行运算
            for (i = 1; i < numkeys; i++) {
                // 如果数组的长度不足，那么相应的字节被假设为 0
                byte = (len[i] <= j) ? 0 : src[i][j];
                switch(op) {
                case BITOP_AND: output &= byte; break;
                case BITOP_OR:  output |= byte; break;
                case BITOP_XOR: output ^= byte; break;
                }
            }
            // 保存输出
            res[j] = output;
        }
    }

    // 释放资源
    for (j = 0; j < numkeys; j++) {
        if (objects[j])
            decrRefCount(objects[j]);
    }
    zfree(src);
    zfree(len);
    zfree(objects);

    /* Store the computed value into the target key */
    if (maxlen) {
        // 保存结果到指定键
        o = createObject(REDIS_STRING,res);
        setKey(c->db,targetkey,o);
        notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"set",targetkey,c->db->id);
        decrRefCount(o);
    } else if (dbDelete(c->db,targetkey)) {
        // 输入为空，没有产生结果，仅仅删除指定键
        signalModifiedKey(c->db,targetkey);
        notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",targetkey,c->db->id);
    }
    server.dirty++;
    addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %r12
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %r15
movq 0x8(%rcx), %r14
movzbl (%r14), %ebx
cmpl $0x41, %ebx
je 0x551ac
cmpl $0x4e, %ebx
je 0x5520b
cmpl $0x4f, %ebx
je 0x551d2
cmpl $0x58, %ebx
je 0x551f3
cmpl $0x78, %ebx
je 0x551f3
cmpl $0x6e, %ebx
je 0x5520b
cmpl $0x6f, %ebx
je 0x551d2
cmpl $0x61, %ebx
jne 0x5521e
leaq 0x36050(%rip), %rsi     # 0x8b203
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x552bd
cmpl $0x4e, %ebx
je 0x5520b
cmpl $0x58, %ebx
je 0x551f3
cmpl $0x4f, %ebx
jne 0x5521e
leaq 0x43917(%rip), %rsi     # 0x98af0
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x55299
cmpl $0x6e, %ebx
je 0x5520b
cmpl $0x58, %ebx
jne 0x5521e
leaq 0x3fae5(%rip), %rsi     # 0x94cdf
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x5527e
cmpl $0x6e, %ebx
jne 0x5521e
leaq 0x3fad1(%rip), %rsi     # 0x94ce3
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x55245
leaq 0x725db(%rip), %rax     # 0xc7800
movq 0x80(%rax), %rsi
movq %r12, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c10d
cmpl $0x4, 0x30(%r12)
jne 0x55641
movq %r15, 0x20(%rsp)
movb $0x1, %al
movq %rax, 0x10(%rsp)
pushq $0x3
popq %r15
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
jmp 0x552e7
movq %r15, 0x20(%rsp)
movb $0x1, %al
movq %rax, 0x28(%rsp)
pushq $0x2
popq %r15
movq $0x0, 0x30(%rsp)
jmp 0x552b2
movq %r15, 0x20(%rsp)
pushq $0x1
popq %r15
movb $0x1, %al
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
jmp 0x552de
movq %r15, 0x20(%rsp)
movb $0x1, %al
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, 0x58(%rsp)
movslq 0x30(%r12), %r12
leaq -0x18(,%r12,8), %rbx
movq %r12, 0x40(%rsp)
addq $-0x3, %r12
movq %rbx, %rdi
callq 0x2828d
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x2828d
movq %rax, %r13
movq %rbx, 0x68(%rsp)
movq %rbx, %rdi
callq 0x2828d
movq %rax, %rbp
xorl %r14d, %r14d
testq %r12, %r12
movl $0x0, %eax
cmovgq %r12, %rax
movq %rax, 0x60(%rsp)
movq $0x0, 0x48(%rsp)
xorl %ecx, %ecx
movq %rbp, 0x50(%rsp)
cmpq %r14, 0x60(%rsp)
movq %rcx, 0x18(%rsp)
je 0x5540e
movq 0x58(%rsp), %rbp
movq 0x8(%rbp), %rdi
movq 0x38(%rbp), %rax
movq 0x18(%rax,%r14,8), %rsi
callq 0x2f89e
testq %rax, %rax
je 0x553e9
movq %rax, %rbx
movq %rbp, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2eec6
testl %eax, %eax
jne 0x554f3
movq %rbx, %rdi
callq 0x2f087
movq 0x50(%rsp), %rbp
movq %rax, (%rbp,%r14,8)
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
movq (%rbp,%r14,8), %rax
movq 0x8(%rax), %rax
movslq -0x8(%rax), %rax
movq 0x48(%rsp), %rcx
cmpq %rax, %rcx
cmovleq %rax, %rcx
movq %rcx, 0x48(%rsp)
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
cmovgeq %rax, %rcx
movq %rax, (%r13,%r14,8)
testq %r14, %r14
cmoveq %rax, %rcx
jmp 0x55406
movq 0x50(%rsp), %rbp
andq $0x0, (%rbp,%r14,8)
movq 0x8(%rsp), %rax
andq $0x0, (%rax,%r14,8)
andq $0x0, (%r13,%r14,8)
xorl %ecx, %ecx
incq %r14
jmp 0x5534e
movq 0x48(%rsp), %r14
testq %r14, %r14
je 0x55544
xorl %ebx, %ebx
xorl %edi, %edi
movq %r14, %rsi
callq 0x27158
cmpl $0x13, 0x40(%rsp)
jg 0x55661
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
je 0x5566b
leaq 0x70(%rsp), %rdi
movq %r9, %rsi
movq 0x68(%rsp), %rdx
movq %rax, %rbx
callq 0x1fbd0
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %rbx, 0x40(%rsp)
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
callq 0x1fbd0
cmpb $0x0, 0x38(%rsp)
je 0x556d6
xorl %ebx, %ebx
pushq $0x1
popq %r8
movq 0x40(%rsp), %rax
movq %rax, %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r11
cmpq $0x20, %r11
jb 0x5566b
movq %r8, %rdx
cmpq %r12, %rdx
jge 0x554e5
movq 0x70(%rsp,%rdx,8), %rsi
movq (%rsi), %rdi
andq %rdi, (%rcx)
movq 0x8(%rsi), %rdi
andq %rdi, 0x8(%rcx)
movq 0x10(%rsi), %rdi
andq %rdi, 0x10(%rcx)
movq 0x18(%rsi), %rdi
andq %rdi, 0x18(%rcx)
addq $0x20, %rsi
movq %rsi, 0x70(%rsp,%rdx,8)
incq %rdx
jmp 0x554af
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r11
jmp 0x554a2
movq 0x50(%rsp), %rbx
movq 0x8(%rsp), %rax
testq %r14, %r14
jle 0x5551b
movq -0x8(%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x55516
callq 0x2ee1f
movq 0x8(%rsp), %rax
decq %r14
jmp 0x554fd
movq %rax, %rdi
callq 0x283cd
movq %r13, %rdi
callq 0x283cd
movq %rbx, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x283cd
xorl %eax, %eax
movq 0x8(%rsp), %r9
xorl %ebx, %ebx
movq %rax, %r15
cmpq %rbx, 0x60(%rsp)
je 0x55570
movq (%rbp,%rbx,8), %rdi
testq %rdi, %rdi
je 0x5556b
callq 0x2ee1f
movq 0x8(%rsp), %r9
incq %rbx
jmp 0x55550
movq %r9, %rdi
callq 0x283cd
movq %r13, %rdi
callq 0x283cd
movq %rbp, %rdi
callq 0x283cd
testq %r14, %r14
je 0x555d6
xorl %edi, %edi
movq %r15, %rsi
callq 0x2e9b4
movq %rax, %rbx
movq 0x58(%rsp), %r14
movq 0x8(%r14), %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
movq %rax, %rdx
callq 0x2fb11
movq 0x8(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x35f2d(%rip), %rsi     # 0x8b4ee
pushq $0x8
popq %rdi
movq %r15, %rdx
callq 0x5b488
movq %rbx, %rdi
callq 0x2ee1f
jmp 0x55615
movq 0x58(%rsp), %r14
movq 0x8(%r14), %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x2fc3b
testl %eax, %eax
je 0x55615
movq 0x8(%r14), %rdi
movq %rbx, %rsi
callq 0x2fbc0
movq 0x8(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x34e93(%rip), %rsi     # 0x8a49d
pushq $0x4
popq %rdi
movq %rbx, %rdx
callq 0x5b488
leaq 0x7198c(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
movq %r14, %rdi
movq 0x48(%rsp), %rsi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c794
leaq 0x3f69f(%rip), %rsi     # 0x94ce7
movq %r12, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c2da
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
negb %r10b
pushq $0x1
popq %r11
leaq 0x3f507(%rip), %rcx     # 0x94b80
cmpq %r14, %rbx
jge 0x5554b
cmpq %rbx, (%r13)
jle 0x55690
movq (%r9), %rdx
movb (%rdx,%rbx), %dl
jmp 0x55692
xorl %edx, %edx
xorb %r10b, %dl
movq %r11, %rsi
cmpq %r12, %rsi
jge 0x556ce
cmpq %rbx, (%r13,%rsi,8)
jle 0x556ae
movq (%r9,%rsi,8), %rdi
movb (%rdi,%rbx), %dil
jmp 0x556b0
xorl %edi, %edi
movslq (%rcx,%r15,4), %r8
addq %rcx, %r8
jmpq *%r8
andb %dl, %dil
jmp 0x556c7
orb %dl, %dil
jmp 0x556c7
xorb %dl, %dil
movl %edi, %edx
incq %rsi
jmp 0x55698
movb %dl, (%rax,%rbx)
incq %rbx
jmp 0x55679
movq 0x40(%rsp), %rax
cmpb $0x0, 0x30(%rsp)
je 0x5574b
xorl %ebx, %ebx
pushq $0x1
popq %r8
movq %rax, %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r11
cmpq $0x20, %r11
jb 0x5566b
movq %r8, %rdx
cmpq %r12, %rdx
jge 0x5573d
movq 0x70(%rsp,%rdx,8), %rsi
movq (%rsi), %rdi
orq %rdi, (%rcx)
movq 0x8(%rsi), %rdi
orq %rdi, 0x8(%rcx)
movq 0x10(%rsi), %rdi
orq %rdi, 0x10(%rcx)
movq 0x18(%rsi), %rdi
orq %rdi, 0x18(%rcx)
addq $0x20, %rsi
movq %rsi, 0x70(%rsp,%rdx,8)
incq %rdx
jmp 0x55707
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r11
jmp 0x556fa
cmpb $0x0, 0x28(%rsp)
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r11
je 0x557bb
xorl %ebx, %ebx
pushq $0x1
popq %r8
movq %rax, %rcx
cmpq $0x20, %r11
jb 0x5566b
movq %r8, %rdx
cmpq %r12, %rdx
jge 0x557ad
movq 0x70(%rsp,%rdx,8), %rsi
movq (%rsi), %rdi
xorq %rdi, (%rcx)
movq 0x8(%rsi), %rdi
xorq %rdi, 0x8(%rcx)
movq 0x10(%rsi), %rdi
xorq %rdi, 0x10(%rcx)
movq 0x18(%rsi), %rdi
xorq %rdi, 0x18(%rcx)
addq $0x20, %rsi
movq %rsi, 0x70(%rsp,%rdx,8)
incq %rdx
jmp 0x55777
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r11
jmp 0x5576a
cmpq $0x20, %r11
setae %cl
andb %r10b, %cl
xorl %ebx, %ebx
cmpb $0x1, %cl
jne 0x5566b
pcmpeqd %xmm0, %xmm0
movdqu (%rax,%rbx), %xmm1
movdqu 0x10(%rax,%rbx), %xmm2
pxor %xmm0, %xmm1
movdqu %xmm1, (%rax,%rbx)
pxor %xmm0, %xmm2
movdqu %xmm2, 0x10(%rax,%rbx)
addq $0x20, %rbx
addq $-0x20, %r11
cmpq $0x1f, %r11
ja 0x557d4
jmp 0x5566b