// File: /workspace/github_c_makefile_star2_O1/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 $0xe8, %rsp
movq %rdi, %r12
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %r13
movq 0x8(%rcx), %r14
movzbl (%r14), %ebx
cmpl $0x60, %ebx
jg 0x59fcb
cmpl $0x4e, %ebx
jg 0x5a035
cmpl $0x41, %ebx
je 0x59fd9
cmpl $0x4e, %ebx
je 0x5a0a4
jmp 0x5a0b7
cmpl $0x6e, %ebx
jg 0x5a03c
cmpl $0x61, %ebx
jne 0x5a09f
leaq 0x3b213(%rip), %rsi     # 0x951f3
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x5a073
leal -0x4e(%rbx), %eax
cmpl $0x2a, %eax
ja 0x5a0b7
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x5a0a4
movabsq $0x200000002, %rcx      # imm = 0x200000002
btq %rax, %rcx
jb 0x5a041
movabsq $0x40000000400, %rcx    # imm = 0x40000000400
btq %rax, %rcx
jb 0x5a089
jmp 0x5a0b7
cmpl $0x4f, %ebx
jne 0x5a05e
jmp 0x5a041
cmpl $0x6f, %ebx
jne 0x5a084
leaq 0x48a7c(%rip), %rsi     # 0xa2ac4
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x5a065
cmpl $0x6d, %ebx
jg 0x5a07f
cmpl $0x4e, %ebx
je 0x5a0a4
cmpl $0x58, %ebx
je 0x5a089
jmp 0x5a0b7
movl $0x1, %ecx
movb $0x1, %dil
xorl %eax, %eax
xorl %edx, %edx
jmp 0x5a0e9
movb $0x1, %dl
xorl %eax, %eax
xorl %edi, %edi
xorl %esi, %esi
xorl %ecx, %ecx
jmp 0x5a0fb
cmpl $0x6e, %ebx
je 0x5a0a4
cmpl $0x78, %ebx
jne 0x5a0b7
leaq 0x44c1f(%rip), %rsi     # 0x9ecaf
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x5a0ed
orl $0x20, %ebx
cmpl $0x6e, %ebx
jne 0x5a0b7
leaq 0x44c08(%rip), %rsi     # 0x9ecb3
movq %r14, %rdi
callq 0x1f2e0
testl %eax, %eax
je 0x5a0de
leaq 0x70742(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq %r12, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d6f0
movl $0x3, %ecx
movb $0x1, %al
xorl %edx, %edx
xorl %edi, %edi
xorl %esi, %esi
jmp 0x5a0fb
movl $0x2, %ecx
movb $0x1, %sil
xorl %eax, %eax
xorl %edx, %edx
xorl %edi, %edi
testb %al, %al
je 0x5a10b
cmpl $0x4, 0x30(%r12)
jne 0x5a343
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %r13, 0x30(%rsp)
movq %r12, 0x18(%rsp)
movslq 0x30(%r12), %rbx
leaq -0x18(,%rbx,8), %r14
movq %r14, %rdi
callq 0x29709
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x29709
movq %rax, %r13
movq %r14, 0x48(%rsp)
movq %r14, %rdi
callq 0x29709
movq %rax, %rbp
cmpq $0x4, %rbx
movq %rbx, 0x10(%rsp)
jl 0x5a253
movl $0x3, %eax
subq %rbx, %rax
movq %rax, 0x58(%rsp)
movl $0x1, %r12d
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
movq %r14, %r15
movq %rbp, %r14
movq 0x18(%rsp), %rbp
movq 0x8(%rbp), %rdi
movq 0x38(%rbp), %rax
movq 0x10(%rax,%r12,8), %rsi
callq 0x3136a
testq %rax, %rax
je 0x5a21e
movq %rax, %rbx
movq %rbp, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x308e7
testl %eax, %eax
jne 0x5a377
movq %rbx, %rdi
callq 0x30ac3
movq %r14, %rbp
movq %rax, -0x8(%r14,%r12,8)
movq 0x8(%rax), %rax
movq (%rsp), %rcx
movq %rax, -0x8(%rcx,%r12,8)
movq -0x8(%r14,%r12,8), %rax
movq 0x8(%rax), %rax
movslq -0x8(%rax), %rax
movq 0x8(%rsp), %rcx
cmpq %rax, %rcx
cmovleq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq %r15, %r14
cmpq %rax, %r15
cmovgeq %rax, %r14
movq %rax, -0x8(%r13,%r12,8)
cmpq $0x1, %r12
cmoveq %rax, %r14
jmp 0x5a239
movq %r14, %rbp
xorl %ecx, %ecx
movq %rcx, -0x8(%r14,%r12,8)
movq (%rsp), %rax
movq %rcx, -0x8(%rax,%r12,8)
movq %rcx, -0x8(%r13,%r12,8)
xorl %r14d, %r14d
movq 0x58(%rsp), %rax
addq %r12, %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x5a190
jmp 0x5a25f
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x5a363
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x28148
movq %rax, %r12
movq 0x10(%rsp), %r11
cmpl $0x13, %r11d
jg 0x5a3c6
testq %r14, %r14
movq (%rsp), %r9
movq 0x20(%rsp), %r15
je 0x5a50a
leaq 0x60(%rsp), %rdi
movq %r9, %rsi
movq 0x48(%rsp), %rdx
movq %r9, %rbx
callq 0x1fbd0
movq (%rbx), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x1fbd0
cmpb $0x0, 0x50(%rsp)
je 0x5a3d4
cmpq $0x20, %r14
movq 0x10(%rsp), %r11
jb 0x5a504
leaq -0x4(%r11), %rax
xorl %ebx, %ebx
movq %r12, %rcx
cmpl $0x5, %r11d
jl 0x5a32c
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
xorl %r9d, %r9d
movq 0x68(%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, 0x68(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x5a2f8
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x5a2e0
jmp 0x5a506
leaq 0x4496d(%rip), %rsi     # 0x9ecb7
movq %r12, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d9d7
xorl %r12d, %r12d
movq %rsi, %r14
movq (%rsp), %r9
movq 0x10(%rsp), %r11
jmp 0x5a583
cmpq $0x1, %r12
movq (%rsp), %rax
je 0x5a39d
movq -0x10(%r14,%r12,8), %rdi
testq %rdi, %rdi
je 0x5a394
callq 0x30834
movq (%rsp), %rax
decq %r12
cmpq $0x1, %r12
jg 0x5a381
movq %rax, %rdi
callq 0x2984b
movq %r13, %rdi
callq 0x2984b
movq %r14, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2984b
movq (%rsp), %r9
movq 0x20(%rsp), %r15
jmp 0x5a50a
cmpb $0x0, 0x40(%rsp)
movq 0x10(%rsp), %r11
je 0x5a456
cmpq $0x20, %r14
jb 0x5a504
leaq -0x4(%r11), %rax
xorl %ebx, %ebx
movq %r12, %rcx
cmpl $0x5, %r11d
jl 0x5a43f
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
xorl %r9d, %r9d
movq 0x68(%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, 0x68(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x5a40b
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x5a3f3
jmp 0x5a506
cmpb $0x0, 0x38(%rsp)
je 0x5a4d0
cmpq $0x20, %r14
jb 0x5a504
leaq -0x4(%r11), %rax
xorl %ebx, %ebx
movq %r12, %rcx
cmpl $0x5, %r11d
jl 0x5a4bc
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
xorl %r9d, %r9d
movq 0x68(%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, 0x68(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x5a488
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x5a470
jmp 0x5a506
cmpq $0x20, %r14
setae %al
andb 0x28(%rsp), %al
cmpb $0x1, %al
jne 0x5a504
xorl %ebx, %ebx
notq (%r12,%rbx)
notq 0x8(%r12,%rbx)
notq 0x10(%r12,%rbx)
notq 0x18(%r12,%rbx)
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x5a4e1
jmp 0x5a506
xorl %ebx, %ebx
movq (%rsp), %r9
movq 0x8(%rsp), %r14
cmpq %r14, %rbx
movq 0x28(%rsp), %r10
jge 0x5a583
negb %r10b
leaq -0x4(%r11), %rax
leaq 0x44629(%rip), %rcx     # 0x9eb50
cmpq %rbx, (%r13)
jle 0x5a535
movq (%r9), %rdx
movb (%rdx,%rbx), %dl
jmp 0x5a537
xorl %edx, %edx
xorb %r10b, %dl
cmpl $0x5, %r11d
jl 0x5a577
xorl %esi, %esi
cmpq %rbx, 0x8(%r13,%rsi,8)
jle 0x5a554
movq 0x8(%r9,%rsi,8), %rdi
movb (%rdi,%rbx), %dil
jmp 0x5a556
xorl %edi, %edi
movslq (%rcx,%r15,4), %r8
addq %rcx, %r8
jmpq *%r8
andb %dl, %dil
jmp 0x5a56d
orb %dl, %dil
jmp 0x5a56d
xorb %dl, %dil
movl %edi, %edx
incq %rsi
cmpq %rsi, %rax
jne 0x5a542
movb %dl, (%r12,%rbx)
incq %rbx
cmpq %r14, %rbx
jne 0x5a527
cmpl $0x4, %r11d
jl 0x5a5b0
addq $-0x3, %r11
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %rdi
testq %rdi, %rdi
je 0x5a5a8
movq %r11, %r15
callq 0x30834
movq %r15, %r11
movq (%rsp), %r9
incq %rbx
cmpq %rbx, %r11
jne 0x5a58f
movq %r9, %rdi
callq 0x2984b
movq %r13, %rdi
callq 0x2984b
movq %rbp, %rdi
callq 0x2984b
testq %r14, %r14
je 0x5a61d
xorl %edi, %edi
movq %r12, %rsi
callq 0x302c4
movq %rax, %rbx
movq 0x18(%rsp), %r15
movq 0x8(%r15), %rdi
movq 0x30(%rsp), %r14
movq %r14, %rsi
movq %rax, %rdx
callq 0x315fb
movq 0x8(%r15), %rax
movl 0x30(%rax), %ecx
leaq 0x3aedd(%rip), %rsi     # 0x954de
movl $0x8, %edi
movq %r14, %rdx
movq 0x8(%rsp), %r14
callq 0x608b5
movq %rbx, %rdi
callq 0x30834
jmp 0x5a65e
movq 0x18(%rsp), %r15
movq 0x8(%r15), %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
callq 0x3174b
testl %eax, %eax
je 0x5a65e
movq 0x8(%r15), %rdi
movq %rbx, %rsi
callq 0x316b7
movq 0x8(%r15), %rax
movl 0x30(%rax), %ecx
leaq 0x39e3c(%rip), %rsi     # 0x9448d
movl $0x4, %edi
movq %rbx, %rdx
callq 0x608b5
leaq 0x6f943(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movq %r15, %rdi
movq %r14, %rsi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2dea3