// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/bitops.c
// Function: bitcountCommand
void bitcountCommand(redisClient *c) {
    robj *o;
    long start, end, strlen;
    unsigned char *p;
    char llbuf[32];

    /* Lookup, check for type, and return 0 for non existing keys. */
    // 查找 key
    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
        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. */
    // 如果给定了 start 和 end 参数，那么读入它们
    if (c->argc == 4) {
        if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK)
            return;
        if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK)
            return;
        /* 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 == 2) {
        /* The whole string. */
        start = 0;
        end = strlen-1;
    } else {
        /* Syntax error. */
        addReply(c,shared.syntaxerr);
        return;
    }

    /* Precondition: end >= 0 && end < strlen, so the only condition where
     * zero can be returned is: start > end. */
    if (start > end) {
        // 超出范围的 case ，略过
        addReply(c,shared.czero);
    } else {
        long bytes = end-start+1;

        // 遍历数组，统计值为 1 的位的数量
        addReplyLongLong(c,redisPopcount(p+start,bytes));
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rsi
leaq 0x70774(%rip), %r12     # 0xca800
movq 0x20(%r12), %rdx
callq 0x31745
testq %rax, %rax
je 0x5a1de
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x5a1de
movl $0xf0, %eax
andl (%r14), %eax
movq 0x8(%r14), %r14
cmpl $0x10, %eax
jne 0x5a0e2
leaq 0x10(%rsp), %r15
movl $0x20, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x300c7
movq %r15, %r14
jmp 0x5a0e6
movl -0x8(%r14), %eax
movslq %eax, %r15
movl 0x30(%rbx), %eax
cmpl $0x2, %eax
je 0x5a192
cmpl $0x4, %eax
jne 0x5a1b3
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x5a1de
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x5a1de
movq 0x8(%rsp), %rax
testq %rax, %rax
jns 0x5a14c
addq %r15, %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
testq %rcx, %rcx
jns 0x5a15c
addq %r15, %rcx
movq %rcx, (%rsp)
testq %rax, %rax
jns 0x5a16c
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %edx, %edx
testq %rcx, %rcx
sets %dil
cmovgq %rcx, %rdx
leaq -0x1(%r15), %rsi
cmpq %r15, %rdx
setge %cl
cmovlq %rdx, %rsi
orb %dil, %cl
je 0x5a1a7
movq %rsi, (%rsp)
jmp 0x5a1a7
movq $0x0, 0x8(%rsp)
decq %r15
movq %r15, (%rsp)
xorl %eax, %eax
movq %r15, %rsi
subq %rax, %rsi
jge 0x5a1c5
movq 0x20(%r12), %rsi
jmp 0x5a1bb
movq 0x80(%r12), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x5a1de
incq %rsi
addq %rax, %r14
movq %r14, %rdi
callq 0x59470
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2e133
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq