// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rax), %rsi
leaq -0x18(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
je 0x7861d
jmp 0x788ad
cmpq $0x0, -0x18(%rbp)
je 0x78640
cmpq $0x1, -0x18(%rbp)
je 0x78640
movq -0x8(%rbp), %rdi
leaq 0x70734(%rip), %rsi     # 0xe8d6a
callq 0x35d40
jmp 0x788ad
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x3bed0
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
jne 0x78686
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq $0x0, %rdx
cmovnel %ecx, %eax
movslq %eax, %rsi
callq 0x36480
jmp 0x788ad
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x3abb0
cmpl $0x0, %eax
je 0x7869f
jmp 0x788ad
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x1, %eax
jne 0x786d6
leaq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movl $0x20, %esi
callq 0x39660
cltq
movq %rax, -0x30(%rbp)
jmp 0x786f3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x770a0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x4, 0x30(%rax)
je 0x7870b
movq -0x8(%rbp), %rax
cmpl $0x5, 0x30(%rax)
jne 0x787d4
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rsi
leaq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
je 0x78732
jmp 0x788ad
movq -0x8(%rbp), %rax
cmpl $0x5, 0x30(%rax)
jne 0x7876c
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x20(%rax), %rsi
leaq -0x28(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
je 0x78763
jmp 0x788ad
movl $0x1, -0x64(%rbp)
jmp 0x78778
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
jge 0x7878b
movq -0x30(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x28(%rbp)
jge 0x7879e
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
jge 0x787ad
movq $0x0, -0x20(%rbp)
cmpq $0x0, -0x28(%rbp)
jge 0x787bc
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x787d2
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x78812
movq -0x8(%rbp), %rax
cmpl $0x3, 0x30(%rax)
jne 0x787f4
movq $0x0, -0x20(%rbp)
movq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x78810
movq -0x8(%rbp), %rdi
leaq 0x9b951(%rip), %rax     # 0x114150
movq 0x80(%rax), %rsi
callq 0x35a70
jmp 0x788ad
jmp 0x78812
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jle 0x7882e
movq -0x8(%rbp), %rdi
movq $-0x1, %rsi
callq 0x36480
jmp 0x788ad
movq -0x28(%rbp), %rax
subq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rdi
addq -0x20(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x18(%rbp), %rax
movl %eax, %edx
callq 0x77340
movq %rax, -0x78(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x78889
cmpq $0x0, -0x18(%rbp)
jne 0x78889
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jne 0x78889
movq -0x8(%rbp), %rdi
movq $-0x1, %rsi
callq 0x36480
jmp 0x788ad
cmpq $-0x1, -0x78(%rbp)
je 0x788a0
movq -0x20(%rbp), %rax
shlq $0x3, %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x36480
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)