// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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), %r15
movq 0x8(%rcx), %r14
movzbl (%r14), %ebx
cmpl $0x41, %ebx
je 0x684f7
cmpl $0x4e, %ebx
je 0x68556
cmpl $0x4f, %ebx
je 0x6851d
cmpl $0x58, %ebx
je 0x6853e
cmpl $0x78, %ebx
je 0x6853e
cmpl $0x6e, %ebx
je 0x68556
cmpl $0x6f, %ebx
je 0x6851d
cmpl $0x61, %ebx
jne 0x68569
leaq 0x5a746(%rip), %rsi     # 0xc2c44
movq %r14, %rdi
callq 0x262f0
testl %eax, %eax
je 0x68608
cmpl $0x4e, %ebx
je 0x68556
cmpl $0x58, %ebx
je 0x6853e
cmpl $0x4f, %ebx
jne 0x68569
leaq 0x5a725(%rip), %rsi     # 0xc2c49
movq %r14, %rdi
callq 0x262f0
testl %eax, %eax
je 0x685e4
cmpl $0x6e, %ebx
je 0x68556
cmpl $0x58, %ebx
jne 0x68569
leaq 0x5a708(%rip), %rsi     # 0xc2c4d
movq %r14, %rdi
callq 0x262f0
testl %eax, %eax
je 0x685c9
cmpl $0x6e, %ebx
jne 0x68569
leaq 0x5a6e2(%rip), %rsi     # 0xc2c3f
movq %r14, %rdi
callq 0x262f0
testl %eax, %eax
je 0x68590
leaq 0x91da0(%rip), %rax     # 0xfa310
movq 0x80(%rax), %rsi
movq %r12, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36987
cmpl $0x4, 0x38(%r12)
jne 0x68973
movq %r15, 0x18(%rsp)
movb $0x1, %al
movq %rax, 0x10(%rsp)
pushq $0x3
popq %r15
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
jmp 0x68632
movq %r15, 0x18(%rsp)
movb $0x1, %al
movq %rax, 0x28(%rsp)
pushq $0x2
popq %r15
movq $0x0, 0x30(%rsp)
jmp 0x685fd
movq %r15, 0x18(%rsp)
pushq $0x1
popq %r15
movb $0x1, %al
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x40(%rsp)
jmp 0x68629
movq %r15, 0x18(%rsp)
movb $0x1, %al
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, 0x58(%rsp)
movslq 0x38(%r12), %r12
leaq -0x18(,%r12,8), %rbx
addq $-0x3, %r12
movq %rbx, %rdi
callq 0x327b9
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x327b9
movq %rax, %r13
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0x327b9
movq %rax, %rbp
xorl %r14d, %r14d
movq $0x0, 0x48(%rsp)
xorl %ecx, %ecx
movq %rax, 0x50(%rsp)
cmpq %r14, %r12
movq %rcx, 0x20(%rsp)
je 0x68742
movq 0x58(%rsp), %rbp
movq 0x10(%rbp), %rdi
movq 0x40(%rbp), %rax
movq 0x18(%rax,%r14,8), %rsi
callq 0x3aebb
testq %rax, %rax
je 0x6871d
movq %rax, %rbx
movq %rbp, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x3a311
testl %eax, %eax
jne 0x68826
movq %rbx, %rdi
callq 0x3a57d
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), %rdi
callq 0x680a1
movq 0x48(%rsp), %rcx
cmpq %rcx, %rax
cmovaq %rax, %rcx
movq %rcx, 0x48(%rsp)
movq 0x20(%rsp), %rcx
cmpq %rcx, %rax
cmovbq %rax, %rcx
movq %rax, (%r13,%r14,8)
testq %r14, %r14
cmoveq %rax, %rcx
jmp 0x6873a
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 0x68683
movq 0x48(%rsp), %r14
testq %r14, %r14
je 0x68878
xorl %ebx, %ebx
xorl %edi, %edi
movq %r14, %rsi
callq 0x31018
cmpl $0x10, %r12d
ja 0x68993
movq 0x20(%rsp), %rcx
cmpq $0x20, %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
jb 0x6899d
leaq 0x60(%rsp), %rdi
movq %r9, %rsi
movq 0x38(%rsp), %rdx
movq %rax, %rbx
callq 0x26c50
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
movq %rdx, %rbx
callq 0x26c50
cmpb $0x0, 0x40(%rsp)
je 0x68a08
movq %rbx, %r11
andq $-0x20, %rbx
pushq $0x1
popq %r8
movq 0x38(%rsp), %rax
movq %rax, %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
cmpq $0x20, %r11
jb 0x6899d
movq %r8, %rdx
cmpq %r12, %rdx
jae 0x6881c
movq 0x60(%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, 0x60(%rsp,%rdx,8)
incq %rdx
jmp 0x687e6
addq $0x20, %rcx
addq $-0x20, %r11
jmp 0x687d9
xorl %ebx, %ebx
movq 0x50(%rsp), %r15
movq 0x8(%rsp), %rax
cmpq %rbx, %r14
je 0x6884f
movq (%r15,%rbx,8), %rdi
testq %rdi, %rdi
je 0x6884a
callq 0x3a265
movq 0x8(%rsp), %rax
incq %rbx
jmp 0x68832
movq %rax, %rdi
callq 0x328f9
movq %r13, %rdi
callq 0x328f9
movq %r15, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x328f9
xorl %eax, %eax
movq 0x8(%rsp), %r9
xorl %ebx, %ebx
movq %rax, %r15
cmpq %rbx, %r12
je 0x688a2
movq (%rbp,%rbx,8), %rdi
testq %rdi, %rdi
je 0x6889d
callq 0x3a265
movq 0x8(%rsp), %r9
incq %rbx
jmp 0x68884
movq %r9, %rdi
callq 0x328f9
movq %r13, %rdi
callq 0x328f9
movq %rbp, %rdi
callq 0x328f9
testq %r14, %r14
je 0x68908
xorl %edi, %edi
movq %r15, %rsi
callq 0x39d58
movq %rax, %rbx
movq 0x58(%rsp), %r14
movq 0x10(%r14), %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %rax, %rdx
callq 0x3b076
movq 0x10(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x46ec2(%rip), %rsi     # 0xaf7b5
pushq $0x8
popq %rdi
movq %r15, %rdx
callq 0x6fb58
movq %rbx, %rdi
callq 0x3a265
jmp 0x68947
movq 0x58(%rsp), %r14
movq 0x10(%r14), %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x3b1df
testl %eax, %eax
je 0x68947
movq 0x10(%r14), %rdi
movq %rbx, %rsi
callq 0x3b125
movq 0x10(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x47adc(%rip), %rsi     # 0xb0418
pushq $0x4
popq %rdi
movq %rbx, %rdx
callq 0x6fb58
leaq 0x90f5a(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
movq %r14, %rdi
movq 0x48(%rsp), %rsi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x370a6
leaq 0x5445f(%rip), %rsi     # 0xbcdd9
movq %r12, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36b6f
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
negb %r10b
pushq $0x1
popq %r11
leaq 0x54239(%rip), %rcx     # 0xbcbe4
cmpq %r14, %rbx
jae 0x6887f
cmpq %rbx, (%r13)
jbe 0x689c2
movq (%r9), %rdx
movb (%rdx,%rbx), %dl
jmp 0x689c4
xorl %edx, %edx
xorb %r10b, %dl
movq %r11, %rsi
cmpq %r12, %rsi
jae 0x68a00
cmpq %rbx, (%r13,%rsi,8)
jbe 0x689e0
movq (%r9,%rsi,8), %rdi
movb (%rdi,%rbx), %dil
jmp 0x689e2
xorl %edi, %edi
movslq (%rcx,%r15,4), %r8
addq %rcx, %r8
jmpq *%r8
andb %dl, %dil
jmp 0x689f9
orb %dl, %dil
jmp 0x689f9
xorb %dl, %dil
movl %edi, %edx
incq %rsi
jmp 0x689ca
movb %dl, (%rax,%rbx)
incq %rbx
jmp 0x689ab
movq 0x38(%rsp), %rax
cmpb $0x0, 0x30(%rsp)
je 0x68a7e
movq 0x20(%rsp), %r11
movq %r11, %rbx
andq $-0x20, %rbx
pushq $0x1
popq %r8
movq %rax, %rcx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
cmpq $0x20, %r11
jb 0x6899d
movq %r8, %rdx
cmpq %r12, %rdx
jae 0x68a74
movq 0x60(%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, 0x60(%rsp,%rdx,8)
incq %rdx
jmp 0x68a3e
addq $0x20, %rcx
addq $-0x20, %r11
jmp 0x68a31
cmpb $0x0, 0x28(%rsp)
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
movq 0x20(%rsp), %r11
je 0x68aef
movq %r11, %rbx
andq $-0x20, %rbx
pushq $0x1
popq %r8
movq %rax, %rcx
cmpq $0x20, %r11
jb 0x6899d
movq %r8, %rdx
cmpq %r12, %rdx
jae 0x68ae5
movq 0x60(%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, 0x60(%rsp,%rdx,8)
incq %rdx
jmp 0x68aaf
addq $0x20, %rcx
addq $-0x20, %r11
jmp 0x68aa2
xorl %ebx, %ebx
testb %r10b, %r10b
je 0x6899d
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 0x68afe
jmp 0x6899d