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

    /* Parse the operation name. */
    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. */
    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;
    src = zmalloc(sizeof(unsigned char*) * numkeys);
    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,OBJ_STRING)) {
            unsigned long i;
            for (i = 0; i < j; i++) {
                if (objects[i])
                    decrRefCount(objects[i]);
            }
            zfree(src);
            zfree(len);
            zfree(objects);
            return;
        }
        objects[j] = getDecodedObject(o);
        src[j] = objects[j]->ptr;
        len[j] = sdslen(objects[j]->ptr);
        if (len[j] > maxlen) maxlen = len[j];
        if (j == 0 || len[j] < minlen) minlen = len[j];
    }

    /* Compute the bit operation, if at least one string is not empty. */
    if (maxlen) {
        res = (unsigned char*) sdsnewlen(NULL,maxlen);
        unsigned char output, byte;
        unsigned 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 >= sizeof(unsigned long)*4 && 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). */
            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;
            for (i = 1; i < numkeys; i++) {
                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(OBJ_STRING,res);
        setKey(c->db,targetkey,o);
        notifyKeyspaceEvent(NOTIFY_STRING,"set",targetkey,c->db->id);
        decrRefCount(o);
    } else if (dbDelete(c->db,targetkey)) {
        signalModifiedKey(c->db,targetkey);
        notifyKeyspaceEvent(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 0x40(%rdi), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %r13
movq 0x8(%rcx), %r14
movzbl (%r14), %ebx
cmpl $0x60, %ebx
jg 0x77dea
cmpl $0x4e, %ebx
jg 0x77e54
cmpl $0x41, %ebx
je 0x77df8
cmpl $0x4e, %ebx
je 0x77ec5
jmp 0x77ed8
cmpl $0x6e, %ebx
jg 0x77e5b
cmpl $0x61, %ebx
jne 0x77ec0
leaq 0x5d21d(%rip), %rsi     # 0xd501c
movq %r14, %rdi
callq 0x26300
testl %eax, %eax
je 0x77e93
leal -0x4e(%rbx), %eax
cmpl $0x2a, %eax
ja 0x77ed8
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x77ec5
movabsq $0x200000002, %rcx      # imm = 0x200000002
btq %rax, %rcx
jb 0x77e60
movabsq $0x40000000400, %rcx    # imm = 0x40000000400
btq %rax, %rcx
jb 0x77eaa
jmp 0x77ed8
cmpl $0x4f, %ebx
jne 0x77e7d
jmp 0x77e60
cmpl $0x6f, %ebx
jne 0x77ea5
leaq 0x5d1ba(%rip), %rsi     # 0xd5021
movq %r14, %rdi
callq 0x26300
testl %eax, %eax
je 0x77e84
cmpl $0x6d, %ebx
jg 0x77ea0
cmpl $0x4e, %ebx
je 0x77ec5
cmpl $0x58, %ebx
je 0x77eaa
jmp 0x77ed8
movl $0x1, %r15d
movb $0x1, %sil
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x77f0b
movb $0x1, %cl
xorl %eax, %eax
xorl %esi, %esi
xorl %edx, %edx
xorl %r15d, %r15d
jmp 0x77f1d
cmpl $0x6e, %ebx
je 0x77ec5
cmpl $0x78, %ebx
jne 0x77ed8
leaq 0x5d174(%rip), %rsi     # 0xd5025
movq %r14, %rdi
callq 0x26300
testl %eax, %eax
je 0x77f0f
orl $0x20, %ebx
cmpl $0x6e, %ebx
jne 0x77ed8
leaq 0x5d14b(%rip), %rsi     # 0xd5017
movq %r14, %rdi
callq 0x26300
testl %eax, %eax
je 0x77eff
leaq 0x8f3d1(%rip), %rax     # 0x1072b0
movq 0x80(%rax), %rsi
movq %r12, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3fc19
movl $0x3, %r15d
movb $0x1, %al
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
jmp 0x77f1d
movl $0x2, %r15d
movb $0x1, %dl
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
testb %al, %al
je 0x77f2d
cmpl $0x4, 0x38(%r12)
jne 0x781c9
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x58(%rsp)
movq %rax, 0x30(%rsp)
movq %r13, 0x38(%rsp)
movq %r12, 0x18(%rsp)
movslq 0x38(%r12), %rbx
leaq -0x18(,%rbx,8), %r14
addq $-0x3, %rbx
movq %r14, %rdi
callq 0x3b29d
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x3b29d
movq %rax, %r13
movq %r14, 0x50(%rsp)
movq %r14, %rdi
callq 0x3b29d
movq %rax, %rbp
movq %rbx, 0x8(%rsp)
testl %ebx, %ebx
je 0x780aa
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
movq %r15, %r13
movq %r14, %r15
movq %rbp, %r14
movq 0x18(%rsp), %rbp
movq 0x10(%rbp), %rdi
movq 0x40(%rbp), %rax
movq 0x18(%rax,%r12,8), %rsi
callq 0x44f5f
testq %rax, %rax
je 0x7802c
movq %rax, %rbx
movq %rbp, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x441ab
testl %eax, %eax
jne 0x781fe
movq %rbx, %rdi
callq 0x444b2
movq %r14, %rbp
movq %rax, (%r14,%r12,8)
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rcx
movq %rax, (%rcx,%r12,8)
movq (%r14,%r12,8), %rax
movq 0x8(%rax), %rcx
movzbl -0x1(%rcx), %eax
movl %eax, %edx
andl $0x7, %edx
cmpl $0x4, %edx
ja 0x78069
leaq 0x56cf1(%rip), %rsi     # 0xced0c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
movq %r15, %r14
jmpq *%rdx
shrl $0x3, %eax
jmp 0x7806e
movq %r14, %rbp
xorl %ecx, %ecx
movq %rcx, (%r14,%r12,8)
movq 0x20(%rsp), %rax
movq %rcx, (%rax,%r12,8)
movq 0x10(%rsp), %rax
movq %rcx, (%rax,%r12,8)
xorl %r14d, %r14d
movq %r13, %r15
movq %rax, %r13
jmp 0x7809a
movq -0x11(%rcx), %rax
jmp 0x7806e
movzwl -0x5(%rcx), %eax
jmp 0x7806e
movl -0x9(%rcx), %eax
jmp 0x7806e
movzbl -0x3(%rcx), %eax
jmp 0x7806e
xorl %eax, %eax
movq %r15, %r14
movq %r13, %r15
movq 0x28(%rsp), %rcx
cmpq %rcx, %rax
cmovaq %rax, %rcx
movq %rcx, 0x28(%rsp)
cmpq %r14, %rax
cmovbq %rax, %r14
movq 0x10(%rsp), %r13
movq %rax, (%r13,%r12,8)
testq %r12, %r12
cmoveq %rax, %r14
incq %r12
cmpq %r12, 0x8(%rsp)
jne 0x77fa5
jmp 0x780b6
movq $0x0, 0x28(%rsp)
xorl %r14d, %r14d
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x781e9
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x3910c
movq %rax, %r11
cmpl $0x10, 0x8(%rsp)
movq 0x18(%rsp), %r12
ja 0x783ee
cmpq $0x20, %r14
movq 0x20(%rsp), %r9
jb 0x783f3
movq %r13, 0x10(%rsp)
movq %r15, %r13
leaq 0x60(%rsp), %rdi
movq %r9, %rsi
movq 0x50(%rsp), %rdx
movq %r9, %rbx
movq %r14, %r15
movq %r11, %r14
callq 0x26c60
movq (%rbx), %rsi
movq %r14, %r12
movq %r14, %rdi
movq %r15, %r14
movq %r15, %rdx
callq 0x26c60
cmpb $0x0, 0x58(%rsp)
je 0x78253
cmpq $0x20, %r14
jb 0x782f6
movq 0x8(%rsp), %rcx
cmpq $0x3, %rcx
movl $0x2, %eax
cmovaeq %rcx, %rax
xorl %ebx, %ebx
movq %r12, %r11
movq %r12, %rcx
movq 0x18(%rsp), %r12
movq %r13, %r15
movq 0x10(%rsp), %r13
cmpl $0x2, 0x8(%rsp)
jb 0x781b2
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
movl $0x1, %r9d
movq 0x60(%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, 0x60(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x7817e
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x78162
jmp 0x783ee
leaq 0x56d39(%rip), %rsi     # 0xcef09
movq %r12, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3ffcb
xorl %r11d, %r11d
movq 0x18(%rsp), %r12
movq %rsi, %r14
movq 0x20(%rsp), %r9
jmp 0x7847d
testq %r12, %r12
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %r15
je 0x7822a
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x78222
callq 0x440f3
movq 0x20(%rsp), %rax
incq %rbx
cmpq %rbx, %r12
jne 0x7820f
movq %rax, %rdi
callq 0x3b3df
movq %r15, %rdi
callq 0x3b3df
movq %r14, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3b3df
cmpb $0x0, 0x48(%rsp)
je 0x78308
cmpq $0x20, %r14
movq %r12, %r11
movq %r13, %r15
jb 0x783a8
movq 0x8(%rsp), %rcx
cmpq $0x3, %rcx
movl $0x2, %eax
cmovaeq %rcx, %rax
xorl %ebx, %ebx
movq %r11, %rcx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
cmpl $0x2, 0x8(%rsp)
jb 0x782df
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
movl $0x1, %r9d
movq 0x60(%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, 0x60(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x782ab
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x7828f
jmp 0x783ee
xorl %ebx, %ebx
movq %r12, %r11
movq 0x18(%rsp), %r12
movq %r13, %r15
jmp 0x783af
cmpb $0x0, 0x40(%rsp)
movq %r12, %r11
movq %r13, %r15
je 0x783b6
cmpq $0x20, %r14
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
jb 0x783ec
movq 0x8(%rsp), %rcx
cmpq $0x3, %rcx
movl $0x2, %eax
cmovaeq %rcx, %rax
xorl %ebx, %ebx
movq %r11, %rcx
cmpl $0x2, 0x8(%rsp)
jb 0x78394
movq (%rcx), %rdx
movq 0x8(%rcx), %rsi
movq 0x10(%rcx), %rdi
movq 0x18(%rcx), %r8
movl $0x1, %r9d
movq 0x60(%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, 0x60(%rsp,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x78360
addq $0x20, %rcx
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x78344
jmp 0x783ee
xorl %ebx, %ebx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
jmp 0x783ee
cmpb $0x0, 0x30(%rsp)
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
je 0x783ec
xorl %ebx, %ebx
notq (%r11,%rbx)
notq 0x8(%r11,%rbx)
notq 0x10(%r11,%rbx)
notq 0x18(%r11,%rbx)
addq $0x20, %rbx
addq $-0x20, %r14
cmpq $0x1f, %r14
ja 0x783c9
jmp 0x783ee
xorl %ebx, %ebx
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r14
cmpq %r14, %rbx
movq 0x30(%rsp), %r10
jae 0x7847d
negb %r10b
movq 0x8(%rsp), %rcx
cmpq $0x3, %rcx
movl $0x2, %eax
cmovaeq %rcx, %rax
leaq 0x56902(%rip), %rcx     # 0xced20
cmpq %rbx, (%r13)
jbe 0x7842c
movq (%r9), %rdx
movb (%rdx,%rbx), %dl
jmp 0x7842e
xorl %edx, %edx
xorb %r10b, %dl
cmpl $0x2, 0x8(%rsp)
jb 0x78471
movl $0x1, %esi
cmpq %rbx, (%r13,%rsi,8)
jbe 0x7844e
movq (%r9,%rsi,8), %rdi
movb (%rdi,%rbx), %dil
jmp 0x78450
xorl %edi, %edi
movslq (%rcx,%r15,4), %r8
addq %rcx, %r8
jmpq *%r8
andb %dl, %dil
jmp 0x78467
orb %dl, %dil
jmp 0x78467
xorb %dl, %dil
movl %edi, %edx
incq %rsi
cmpq %rsi, %rax
jne 0x7843d
movb %dl, (%r11,%rbx)
incq %rbx
cmpq %r14, %rbx
jne 0x7841e
movq %r11, %r15
cmpl $0x0, 0x8(%rsp)
je 0x784a7
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %rdi
testq %rdi, %rdi
je 0x7849d
callq 0x440f3
movq 0x20(%rsp), %r9
incq %rbx
cmpq %rbx, 0x8(%rsp)
jne 0x78489
movq %r9, %rdi
callq 0x3b3df
movq %r13, %rdi
callq 0x3b3df
movq %rbp, %rdi
callq 0x3b3df
testq %r14, %r14
je 0x78511
xorl %edi, %edi
movq %r15, %rsi
callq 0x43ab4
movq %rax, %rbx
movq 0x10(%r12), %rdi
movq 0x38(%rsp), %r14
movq %r14, %rsi
movq %rax, %rdx
callq 0x45162
movq 0x10(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x4835e(%rip), %rsi     # 0xc0853
movl $0x8, %edi
movq %r14, %rdx
movq 0x28(%rsp), %r14
callq 0x800c9
movq %rbx, %rdi
callq 0x440f3
jmp 0x78550
movq 0x10(%r12), %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
callq 0x452f3
testl %eax, %eax
je 0x78550
movq 0x10(%r12), %rdi
movq %rbx, %rsi
callq 0x45220
movq 0x10(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x48f73(%rip), %rsi     # 0xc14b6
movl $0x4, %edi
movq %rbx, %rdx
callq 0x800c9
leaq 0x8e301(%rip), %rax     # 0x106858
incq 0x710(%rax)
movq %r12, %rdi
movq %r14, %rsi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40606