// File: /workspace/github_c_makefile_star2_O2/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 0x71fde(%rip), %r12     # 0xc7800
movq 0x20(%r12), %rdx
callq 0x2f9a9
testq %rax, %rax
je 0x5596c
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2eec6
testl %eax, %eax
jne 0x5596c
movl $0xf0, %eax
andl (%r14), %eax
movq 0x8(%r14), %r14
cmpl $0x10, %eax
jne 0x55876
leaq 0x10(%rsp), %r15
pushq $0x20
popq %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x2e511
movq %r15, %r14
jmp 0x5587a
movl -0x8(%r14), %eax
movslq %eax, %r15
movl 0x30(%rbx), %eax
cmpl $0x2, %eax
je 0x55923
cmpl $0x4, %eax
jne 0x55941
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x5596c
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x5596c
movq 0x8(%rsp), %rax
testq %rax, %rax
jns 0x558e0
addq %r15, %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
testq %rcx, %rcx
jns 0x558f0
addq %r15, %rcx
movq %rcx, (%rsp)
testq %rax, %rax
jns 0x558fd
andq $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 0x55935
movq %rsi, (%rsp)
jmp 0x55935
andq $0x0, 0x8(%rsp)
decq %r15
movq %r15, (%rsp)
xorl %eax, %eax
movq %r15, %rsi
subq %rax, %rsi
jge 0x55953
movq 0x20(%r12), %rsi
jmp 0x55949
movq 0x80(%r12), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x5596c
incq %rsi
addq %rax, %r14
movq %r14, %rdi
callq 0x54cc0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c794
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq