// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/bitops.c
// Function: bitposCommand
void bitposCommand(redisClient *c) {
    robj *o;
    long bit, start, end, strlen;
    unsigned char *p;
    char llbuf[32];
    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) != REDIS_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,REDIS_STRING)) return;

    /* Set the 'p' pointer to the string, that can be just a stack allocated
     * array if our string was integer encoded. */
    if (o->encoding == REDIS_ENCODING_INT) {
        p = (unsigned char*) llbuf;
        strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
    } else {
        p = (unsigned char*) o->ptr;
        strlen = sdslen(o->ptr);
    }

    /* Parse start/end range if any. */
    if (c->argc == 4 || c->argc == 5) {
        if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != REDIS_OK)
            return;
        if (c->argc == 5) {
            if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != REDIS_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 %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x5a295
cmpq $0x2, 0x18(%rsp)
jb 0x5a22e
leaq 0x44b36(%rip), %rsi     # 0x9ed5a
movq %rbx, %rdi
callq 0x2dc63
jmp 0x5a295
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3163a
testq %rax, %rax
je 0x5a283
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x5a295
movl $0xf0, %eax
andl (%r14), %eax
movq 0x8(%r14), %r14
cmpl $0x10, %eax
jne 0x5a2a2
leaq 0x20(%rsp), %r15
movl $0x20, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x300c7
movq %r15, %r14
jmp 0x5a2a6
xorl %esi, %esi
cmpq 0x18(%rsp), %rsi
sbbq %rsi, %rsi
movq %rbx, %rdi
callq 0x2e133
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl -0x8(%r14), %eax
movslq %eax, %r15
movl 0x30(%rbx), %eax
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
jae 0x5a2f7
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x5a295
cmpl $0x5, 0x30(%rbx)
jne 0x5a331
movq 0x38(%rbx), %rax
movq 0x20(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x5a295
movl $0x1, %ebp
jmp 0x5a33c
cmpl $0x3, %eax
jne 0x5a316
movq $0x0, 0x10(%rsp)
decq %r15
movq %r15, 0x8(%rsp)
xorl %eax, %eax
movq %r15, %r12
xorl %ebp, %ebp
jmp 0x5a395
leaq 0x704e3(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x5a295
leaq -0x1(%r15), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
testq %rax, %rax
jns 0x5a34e
addq %r15, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
jns 0x5a360
addq %r15, %rcx
movq %rcx, 0x8(%rsp)
testq %rax, %rax
jns 0x5a370
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
xorl %edx, %edx
testq %rcx, %rcx
sets %sil
cmovgq %rcx, %rdx
leaq -0x1(%r15), %r12
cmpq %r15, %rdx
setge %cl
cmovlq %rdx, %r12
orb %sil, %cl
je 0x5a395
movq %r12, 0x8(%rsp)
subq %rax, %r12
jl 0x5a3c7
leaq 0x1(%r12), %rsi
addq %rax, %r14
movl 0x18(%rsp), %edx
movq %r14, %rdi
callq 0x5957f
testl %ebp, %ebp
je 0x5a3d6
cmpq $0x0, 0x18(%rsp)
jne 0x5a3d6
leaq 0x8(,%r12,8), %rcx
cmpq %rcx, %rax
jne 0x5a3d6
movq %rbx, %rdi
movq $-0x1, %rsi
jmp 0x5a290
cmpq $-0x1, %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx,8), %rsi
cmoveq %rax, %rsi
jmp 0x5a28d