// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/bitops.c
// Function: bitposCommand
void bitposCommand(client *c) {
    robj *o;
    long bit, start, end, strlen;
    unsigned char *p;
    char llbuf[LONG_STR_SIZE];
    int end_given = 0;

    /* Parse the bit argument to understand what we are looking for, set
     * or clear bits. */
    if (getLongFromObjectOrReply(c,c->argv[2],&bit,NULL) != C_OK)
        return;
    if (bit != 0 && bit != 1) {
        addReplyError(c, "The bit argument must be 1 or 0.");
        return;
    }

    /* If the key does not exist, from our point of view it is an infinite
     * array of 0 bits. If the user is looking for the fist clear bit return 0,
     * If the user is looking for the first set bit, return -1. */
    if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) {
        addReplyLongLong(c, bit ? -1 : 0);
        return;
    }
    if (checkType(c,o,OBJ_STRING)) return;
    p = getObjectReadOnlyString(o,&strlen,llbuf);

    /* Parse start/end range if any. */
    if (c->argc == 4 || c->argc == 5) {
        if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != C_OK)
            return;
        if (c->argc == 5) {
            if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != C_OK)
                return;
            end_given = 1;
        } else {
            end = strlen-1;
        }
        /* Convert negative indexes */
        if (start < 0) start = strlen+start;
        if (end < 0) end = strlen+end;
        if (start < 0) start = 0;
        if (end < 0) end = 0;
        if (end >= strlen) end = strlen-1;
    } else if (c->argc == 3) {
        /* The whole string. */
        start = 0;
        end = strlen-1;
    } else {
        /* Syntax error. */
        addReply(c,shared.syntaxerr);
        return;
    }

    /* For empty ranges (start > end) we return -1 as an empty range does
     * not contain a 0 nor a 1. */
    if (start > end) {
        addReplyLongLong(c, -1);
    } else {
        long bytes = end-start+1;
        long pos = redisBitpos(p+start,bytes,bit);

        /* If we are looking for clear bits, and the user specified an exact
         * range with start-end, we can't consider the right of the range as
         * zero padded (as we do when no explicit end is given).
         *
         * So if redisBitpos() returns the first bit outside the range,
         * we return -1 to the caller, to mean, in the specified range there
         * is not a single "0" bit. */
        if (end_given && bit == 0 && pos == bytes*8) {
            addReplyLongLong(c,-1);
            return;
        }
        if (pos != -1) pos += start*8; /* Adjust for the bytes we skipped. */
        addReplyLongLong(c,pos);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3ab5e
testl %eax, %eax
jne 0x68d78
cmpq $0x2, 0x18(%rsp)
jb 0x68cdd
leaq 0x5413c(%rip), %rsi     # 0xbce0c
movq %rbx, %rdi
callq 0x36b6f
jmp 0x68d78
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3aebb
testq %rax, %rax
je 0x68d66
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x3a311
testl %eax, %eax
jne 0x68d78
leaq 0x8(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x68235
movq %rax, %r14
movl 0x38(%rbx), %eax
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
jae 0x68d83
movq 0x40(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x3ab5e
testl %eax, %eax
jne 0x68d78
cmpl $0x5, 0x38(%rbx)
jne 0x68db8
movq 0x40(%rbx), %rax
movq 0x20(%rax), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x3ab5e
testl %eax, %eax
jne 0x68d78
pushq $0x1
popq %rbp
jmp 0x68dc6
xorl %esi, %esi
cmpq 0x18(%rsp), %rsi
sbbq %rsi, %rsi
movq %rbx, %rdi
callq 0x370a6
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3, %eax
jne 0x68da0
andq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %r15
decq %r15
movq %r15, (%rsp)
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x68e1a
leaq 0x91569(%rip), %rax     # 0xfa310
movq 0x80(%rax), %rsi
movq %rbx, %rdi
callq 0x36987
jmp 0x68d78
movq 0x8(%rsp), %rax
decq %rax
movq %rax, (%rsp)
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
testq %rax, %rax
jns 0x68dda
addq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq (%rsp), %r15
testq %r15, %r15
jns 0x68dec
addq 0x8(%rsp), %r15
movq %r15, (%rsp)
testq %rax, %rax
jns 0x68df9
andq $0x0, 0x10(%rsp)
xorl %eax, %eax
testq %r15, %r15
jns 0x68e06
andq $0x0, (%rsp)
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
cmpq %rcx, %r15
jl 0x68e1a
decq %rcx
movq %rcx, (%rsp)
movq %rcx, %r15
subq %rax, %r15
jl 0x68e4b
leaq 0x1(%r15), %rsi
addq %rax, %r14
movl 0x18(%rsp), %edx
movq %r14, %rdi
callq 0x67cc2
testl %ebp, %ebp
je 0x68e53
cmpq $0x0, 0x18(%rsp)
jne 0x68e53
leaq 0x8(,%r15,8), %rcx
cmpq %rcx, %rax
jne 0x68e53
pushq $-0x1
popq %rsi
jmp 0x68d70
cmpq $-0x1, %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx,8), %rsi
cmoveq %rax, %rsi
jmp 0x68d70