// File: /workspace/github_c_makefile_star2_O1/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 0x7015b(%rip), %r12     # 0xca800
movq 0x20(%r12), %rdx
callq 0x31475
testq %rax, %rax
je 0x5a7ed
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x308e7
testl %eax, %eax
jne 0x5a7ed
movl $0xf0, %eax
andl (%r14), %eax
movq 0x8(%r14), %r14
cmpl $0x10, %eax
jne 0x5a6fb
leaq 0x10(%rsp), %r15
movl $0x20, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x2fe12
movq %r15, %r14
jmp 0x5a6ff
movl -0x8(%r14), %eax
movslq %eax, %r15
movl 0x30(%rbx), %eax
cmpl $0x2, %eax
je 0x5a79d
cmpl $0x4, %eax
jne 0x5a7c2
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x5a7ed
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x5a7ed
movq 0x8(%rsp), %rax
testq %rax, %rax
jns 0x5a765
addq %r15, %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %rax
testq %rax, %rax
jns 0x5a775
addq %r15, %rax
movq %rax, (%rsp)
cmpq $0x0, 0x8(%rsp)
jns 0x5a786
movq $0x0, 0x8(%rsp)
cmpq $0x0, (%rsp)
jns 0x5a795
movq $0x0, (%rsp)
cmpq %r15, (%rsp)
jge 0x5a7a6
jmp 0x5a7ad
movq $0x0, 0x8(%rsp)
decq %r15
movq %r15, (%rsp)
movq 0x8(%rsp), %rax
movq (%rsp), %rsi
subq %rax, %rsi
jge 0x5a7d4
movq 0x20(%r12), %rsi
jmp 0x5a7ca
movq 0x80(%r12), %rsi
movq %rbx, %rdi
callq 0x2d6f0
jmp 0x5a7ed
incq %rsi
addq %rax, %r14
movq %r14, %rdi
callq 0x59a30
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2dea3
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq