// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rbp
movq 0x8(%rcx), %r14
movzbl (%r14), %ebx
cmpl $0x60, %ebx
jg 0x59998
cmpl $0x4e, %ebx
jg 0x599ca
cmpl $0x41, %ebx
je 0x599a2
cmpl $0x4e, %ebx
je 0x59a16
jmp 0x59a29
cmpl $0x6e, %ebx
jg 0x599d1
cmpl $0x61, %ebx
jne 0x59a11
leaq 0x3b89a(%rip), %rsi     # 0x95243
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x59aca
cmpl $0x4e, %ebx
je 0x59a16
cmpl $0x58, %ebx
je 0x599fe
cmpl $0x4f, %ebx
jne 0x59a29
jmp 0x599dd
cmpl $0x4f, %ebx
jne 0x599f9
jmp 0x599dd
cmpl $0x6f, %ebx
je 0x599dd
cmpl $0x78, %ebx
je 0x599fe
jmp 0x59a29
leaq 0x49190(%rip), %rsi     # 0xa2b74
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x59aa5
cmpl $0x6e, %ebx
je 0x59a16
cmpl $0x58, %ebx
jne 0x59a29
leaq 0x4531a(%rip), %rsi     # 0x9ed1f
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x59a89
cmpl $0x6e, %ebx
jne 0x59a29
leaq 0x45306(%rip), %rsi     # 0x9ed23
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x59a50
leaq 0x70dd0(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq %r15, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d97c
cmpl $0x4, 0x30(%r15)
jne 0x59d05
movb $0x1, %al
movq %rax, 0x18(%rsp)
movl $0x3, %eax
movq %rax, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
jmp 0x59af5
movb $0x1, %al
movq %rax, 0x30(%rsp)
movl $0x2, %eax
movq %rax, 0x48(%rsp)
movq $0x0, 0x38(%rsp)
jmp 0x59abf
movb $0x1, %al
movq %rax, 0x38(%rsp)
movl $0x1, %eax
movq %rax, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
jmp 0x59aec
movb $0x1, %al
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movslq 0x30(%r15), %r14
leaq -0x18(,%r14,8), %rbx
movq %rbx, %rdi
callq 0x299b5
movq %rax, %r12
movq %rbx, %rdi
callq 0x299b5
movq %rax, %r13
movq %rbx, 0x60(%rsp)
movq %rbx, %rdi
callq 0x299b5
movq %rax, 0x10(%rsp)
cmpq $0x4, %r14
movq %rbp, 0x58(%rsp)
jl 0x59cf9
movq %r12, 0x20(%rsp)
leaq -0x3(%r14), %rax
movq %rax, 0x68(%rsp)
movl $0x3, %ebp
movq %r14, 0x28(%rsp)
subq %r14, %rbp
movl $0x1, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r15, 0x50(%rsp)
movq 0x8(%r15), %rdi
movq 0x38(%r15), %rax
movq 0x10(%rax,%rbx,8), %rsi
callq 0x3163a
testq %rax, %rax
je 0x59bdc
movq %r15, %rdi
movq %rax, %r15
movq %rax, %rsi
xorl %edx, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x59d25
movq %r15, %rdi
callq 0x30d93
movq 0x10(%rsp), %rcx
movq %rax, -0x8(%rcx,%rbx,8)
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rdx
movq %rax, -0x8(%rdx,%rbx,8)
movq -0x8(%rcx,%rbx,8), %rax
movq 0x8(%rax), %rax
movslq -0x8(%rax), %rax
cmpq %rax, %r12
cmovleq %rax, %r12
cmpq %rax, %r14
cmovgeq %rax, %r14
movq %rax, -0x8(%r13,%rbx,8)
cmpq $0x1, %rbx
cmoveq %rax, %r14
jmp 0x59bfa
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
movq %rcx, -0x8(%rax,%rbx,8)
movq 0x20(%rsp), %rax
movq %rcx, -0x8(%rax,%rbx,8)
movq %rcx, -0x8(%r13,%rbx,8)
xorl %r14d, %r14d
leaq (%rbx,%rbp), %rax
incq %rax
incq %rbx
cmpq $0x1, %rax
movq 0x50(%rsp), %r15
jne 0x59b63
testq %r12, %r12
sete 0xf(%rsp)
je 0x59d78
movq %r12, %rbx
xorl %r15d, %r15d
xorl %edi, %edi
movq %r12, %rsi
callq 0x283f4
movq %rax, %rbp
movq 0x28(%rsp), %r11
cmpl $0x13, %r11d
movq 0x20(%rsp), %r12
jg 0x59ed9
testq %r14, %r14
je 0x59ed9
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
movq 0x60(%rsp), %rdx
callq 0x1fbd0
movq (%r12), %rsi
movq %rbp, %rdi
movq %r14, %rdx
callq 0x1fbd0
cmpb $0x0, 0x40(%rsp)
je 0x59d8e
cmpq $0x20, %r14
movq 0x28(%rsp), %r11
jb 0x59ed6
leaq -0x4(%r11), %rax
xorl %r15d, %r15d
movq %rbp, %rcx
cmpl $0x4, %r11d
je 0x59ce2
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
xorl %r9d, %r9d
movq 0x78(%rsp,%r9,8), %r10
andq (%r10), %rdx
movq %rdx, (%rcx)
andq 0x8(%r10), %rsi
movq %rsi, 0x8(%rcx)
andq 0x10(%r10), %rdi
movq %rdi, 0x10(%rcx)
andq 0x18(%r10), %r8
movq %r8, 0x18(%rcx)
addq $0x20, %r10
movq %r10, 0x78(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x59cae
addq $0x20, %rcx
addq $0x20, %r15
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x59c96
jmp 0x59ed9
xorl %ebp, %ebp
movb $0x1, %r14b
xorl %ebx, %ebx
jmp 0x59fa4
leaq 0x4501b(%rip), %rsi     # 0x9ed27
movq %r15, %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2dc63
cmpq $0x1, %rbx
movq 0x20(%rsp), %r14
je 0x59d4d
movq 0x10(%rsp), %rax
movq -0x10(%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x59d44
callq 0x30b0c
decq %rbx
cmpq $0x1, %rbx
jg 0x59d30
movq %r14, %rdi
callq 0x29af7
movq %r13, %rdi
callq 0x29af7
movq 0x10(%rsp), %rdi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29af7
xorl %ebx, %ebx
movb $0x1, %r14b
xorl %ebp, %ebp
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %r11
jmp 0x59f6f
cmpb $0x0, 0x38(%rsp)
movq 0x28(%rsp), %r11
je 0x59e11
cmpq $0x20, %r14
jb 0x59ed6
leaq -0x4(%r11), %rax
xorl %r15d, %r15d
movq %rbp, %rcx
cmpl $0x4, %r11d
je 0x59dfa
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
xorl %r9d, %r9d
movq 0x78(%rsp,%r9,8), %r10
orq (%r10), %rdx
movq %rdx, (%rcx)
orq 0x8(%r10), %rsi
movq %rsi, 0x8(%rcx)
orq 0x10(%r10), %rdi
movq %rdi, 0x10(%rcx)
orq 0x18(%r10), %r8
movq %r8, 0x18(%rcx)
addq $0x20, %r10
movq %r10, 0x78(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x59dc6
addq $0x20, %rcx
addq $0x20, %r15
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x59dae
jmp 0x59ed9
cmpb $0x0, 0x30(%rsp)
je 0x59e8c
cmpq $0x20, %r14
jb 0x59ed6
leaq -0x4(%r11), %rax
xorl %r15d, %r15d
movq %rbp, %rcx
cmpl $0x4, %r11d
je 0x59e78
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
xorl %r9d, %r9d
movq 0x78(%rsp,%r9,8), %r10
xorq (%r10), %rdx
movq %rdx, (%rcx)
xorq 0x8(%r10), %rsi
movq %rsi, 0x8(%rcx)
xorq 0x10(%r10), %rdi
movq %rdi, 0x10(%rcx)
xorq 0x18(%r10), %r8
movq %r8, 0x18(%rcx)
addq $0x20, %r10
movq %r10, 0x78(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x59e44
addq $0x20, %rcx
addq $0x20, %r15
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x59e2c
jmp 0x59ed9
cmpq $0x20, %r14
setae %al
andb 0x18(%rsp), %al
cmpb $0x1, %al
jne 0x59ed6
xorl %r15d, %r15d
pcmpeqd %xmm0, %xmm0
movdqu (%rbp,%r15), %xmm1
movdqu 0x10(%rbp,%r15), %xmm2
pxor %xmm0, %xmm1
movdqu %xmm1, (%rbp,%r15)
pxor %xmm0, %xmm2
movdqu %xmm2, 0x10(%rbp,%r15)
addq $0x20, %r15
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x59ea2
jmp 0x59ed9
xorl %r15d, %r15d
cmpq %rbx, %r15
jge 0x59f65
movq 0x18(%rsp), %r10
negb %r10b
leaq -0x4(%r11), %rax
leaq 0x44ccb(%rip), %rcx     # 0x9ebc0
movq 0x48(%rsp), %r9
movb 0xf(%rsp), %r14b
cmpq %r15, (%r13)
jle 0x59f0f
movq (%r12), %rdx
movb (%rdx,%r15), %dl
jmp 0x59f11
xorl %edx, %edx
xorb %r10b, %dl
cmpl $0x4, %r11d
je 0x59f51
xorl %esi, %esi
cmpq %r15, 0x8(%r13,%rsi,8)
jle 0x59f2e
movq 0x8(%r12,%rsi,8), %rdi
movb (%rdi,%r15), %dil
jmp 0x59f30
xorl %edi, %edi
movslq (%rcx,%r9,4), %r8
addq %rcx, %r8
jmpq *%r8
andb %dl, %dil
jmp 0x59f47
orb %dl, %dil
jmp 0x59f47
xorb %dl, %dil
movl %edi, %edx
incq %rsi
cmpq %rsi, %rax
jne 0x59f1c
movb %dl, (%rbp,%r15)
incq %r15
cmpq %rbx, %r15
jne 0x59eff
movq 0x50(%rsp), %r15
jmp 0x59f6f
movq 0x50(%rsp), %r15
movb 0xf(%rsp), %r14b
cmpl $0x4, %r11d
jl 0x59fa4
movb %r14b, 0xf(%rsp)
movq %rbx, %r14
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x59f92
callq 0x30b0c
incq %rbx
cmpq %rbx, 0x68(%rsp)
jne 0x59f7f
movq %r14, %rbx
movb 0xf(%rsp), %r14b
movq %r12, %rdi
callq 0x29af7
movq %r13, %rdi
callq 0x29af7
movq 0x10(%rsp), %rdi
callq 0x29af7
testb %r14b, %r14b
je 0x5a001
movq 0x8(%r15), %rdi
movq 0x58(%rsp), %r14
movq %r14, %rsi
callq 0x31a36
testl %eax, %eax
je 0x5a045
movq 0x8(%r15), %rdi
movq %r14, %rsi
callq 0x319b8
movq 0x8(%r15), %rax
movl 0x30(%rax), %ecx
leaq 0x3a4eb(%rip), %rsi     # 0x944dd
movl $0x4, %edi
movq %r14, %rdx
callq 0x60371
jmp 0x5a045
xorl %edi, %edi
movq %rbp, %rsi
callq 0x3059c
movq %rax, %r14
movq 0x8(%r15), %rdi
movq 0x58(%rsp), %r12
movq %r12, %rsi
movq %rax, %rdx
callq 0x318fc
movq 0x8(%r15), %rax
movl 0x30(%rax), %ecx
leaq 0x3b4fe(%rip), %rsi     # 0x9552e
movl $0x8, %edi
movq %r12, %rdx
callq 0x60371
movq %r14, %rdi
callq 0x30b0c
leaq 0x6ff5c(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movq %r15, %rdi
movq %rbx, %rsi
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2e133