// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/networking.c
// Function: processMultibulkBuffer
int processMultibulkBuffer(redisClient *c) {
    char *newline = NULL;
    int pos = 0, ok;
    long long ll;

    // 读入命令的参数个数
    // 比如 *3\r\n$3\r\nSET\r\n... 将令 c->multibulklen = 3
    if (c->multibulklen == 0) {
        /* The client should have been reset */
        redisAssertWithInfo(c,NULL,c->argc == 0);

        /* Multi bulk length cannot be read without a \r\n */
        // 检查缓冲区的内容第一个 "\r\n"
        newline = strchr(c->querybuf,'\r');
        if (newline == NULL) {
            if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
                addReplyError(c,"Protocol error: too big mbulk count string");
                setProtocolError(c,0);
            }
            return REDIS_ERR;
        }
        /* Buffer should also contain \n */
        if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
            return REDIS_ERR;

        /* We know for sure there is a whole line since newline != NULL,
         * so go ahead and find out the multi bulk length. */
        // 协议的第一个字符必须是 '*'
        redisAssertWithInfo(c,NULL,c->querybuf[0] == '*');
        // 将参数个数，也即是 * 之后， \r\n 之前的数字取出并保存到 ll 中
        // 比如对于 *3\r\n ，那么 ll 将等于 3
        ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
        // 参数的数量超出限制
        if (!ok || ll > 1024*1024) {
            addReplyError(c,"Protocol error: invalid multibulk length");
            setProtocolError(c,pos);
            return REDIS_ERR;
        }

        // 参数数量之后的位置
        // 比如对于 *3\r\n$3\r\n$SET\r\n... 来说，
        // pos 指向 *3\r\n$3\r\n$SET\r\n...
        //                ^
        //                |
        //               pos
        pos = (newline-c->querybuf)+2;
        // 如果 ll <= 0 ，那么这个命令是一个空白命令
        // 那么将这段内容从查询缓冲区中删除，只保留未阅读的那部分内容
        // 为什么参数可以是空的呢？
        // processInputBuffer 中有注释到 "Multibulk processing could see a <= 0 length"
        // 但并没有详细说明原因
        if (ll <= 0) {
            sdsrange(c->querybuf,pos,-1);
            return REDIS_OK;
        }

        // 设置参数数量
        c->multibulklen = ll;

        /* Setup argv array on client structure */
        // 根据参数数量，为各个参数对象分配空间
        if (c->argv) zfree(c->argv);
        c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
    }

    redisAssertWithInfo(c,NULL,c->multibulklen > 0);

    // 从 c->querybuf 中读入参数，并创建各个参数对象到 c->argv
    while(c->multibulklen) {

        /* Read bulk length if unknown */
        // 读入参数长度
        if (c->bulklen == -1) {

            // 确保 "\r\n" 存在
            newline = strchr(c->querybuf+pos,'\r');
            if (newline == NULL) {
                if (sdslen(c->querybuf) > REDIS_INLINE_MAX_SIZE) {
                    addReplyError(c,
                        "Protocol error: too big bulk count string");
                    setProtocolError(c,0);
                    return REDIS_ERR;
                }
                break;
            }
            /* Buffer should also contain \n */
            if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
                break;

            // 确保协议符合参数格式，检查其中的 $...
            // 比如 $3\r\nSET\r\n
            if (c->querybuf[pos] != '$') {
                addReplyErrorFormat(c,
                    "Protocol error: expected '$', got '%c'",
                    c->querybuf[pos]);
                setProtocolError(c,pos);
                return REDIS_ERR;
            }

            // 读取长度
            // 比如 $3\r\nSET\r\n 将会让 ll 的值设置 3
            ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
            if (!ok || ll < 0 || ll > 512*1024*1024) {
                addReplyError(c,"Protocol error: invalid bulk length");
                setProtocolError(c,pos);
                return REDIS_ERR;
            }

            // 定位到参数的开头
            // 比如 
            // $3\r\nSET\r\n...
            //       ^
            //       |
            //      pos
            pos += newline-(c->querybuf+pos)+2;
            // 如果参数非常长，那么做一些预备措施来优化接下来的参数复制操作
            if (ll >= REDIS_MBULK_BIG_ARG) {
                size_t qblen;

                /* If we are going to read a large object from network
                 * try to make it likely that it will start at c->querybuf
                 * boundary so that we can optimize object creation
                 * avoiding a large copy of data. */
                sdsrange(c->querybuf,pos,-1);
                pos = 0;
                qblen = sdslen(c->querybuf);
                /* Hint the sds library about the amount of bytes this string is
                 * going to contain. */
                if (qblen < ll+2)
                    c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2-qblen);
            }
            // 参数的长度
            c->bulklen = ll;
        }

        /* Read bulk argument */
        // 读入参数
        if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
            // 确保内容符合协议格式
            // 比如 $3\r\nSET\r\n 就检查 SET 之后的 \r\n
            /* Not enough data (+2 == trailing \r\n) */
            break;
        } else {
            // 为参数创建字符串对象  
            /* Optimization: if the buffer contains JUST our bulk element
             * instead of creating a new object by *copying* the sds we
             * just use the current sds string. */
            if (pos == 0 &&
                c->bulklen >= REDIS_MBULK_BIG_ARG &&
                (signed) sdslen(c->querybuf) == c->bulklen+2)
            {
                c->argv[c->argc++] = createObject(REDIS_STRING,c->querybuf);
                sdsIncrLen(c->querybuf,-2); /* remove CRLF */
                c->querybuf = sdsempty();
                /* Assume that if we saw a fat argument we'll see another one
                 * likely... */
                c->querybuf = sdsMakeRoomFor(c->querybuf,c->bulklen+2);
                pos = 0;
            } else {
                c->argv[c->argc++] =
                    createStringObject(c->querybuf+pos,c->bulklen);
                pos += c->bulklen+2;
            }

            // 清空参数长度
            c->bulklen = -1;

            // 减少还需读入的参数个数
            c->multibulklen--;
        }
    }

    /* Trim to pos */
    // 从 querybuf 中删除已被读取的内容
    if (pos) sdsrange(c->querybuf,pos,-1);

    /* We're done when c->multibulk == 0 */
    // 如果本条命令的所有参数都已读取完，那么返回
    if (c->multibulklen == 0) return REDIS_OK;

    /* Still not read to process the command */
    // 如果还有参数未读取完，那么就协议内容有错
    return REDIS_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x54(%rdi), %eax
xorl %r14d, %r14d
testl %eax, %eax
je 0x2edb8
testl %eax, %eax
jle 0x2ef34
movabsq $-0x200000000, %r15     # imm = 0xFFFFFFFE00000000
movq 0x58(%rbx), %rsi
movq 0x20(%rbx), %r12
cmpq $-0x1, %rsi
jne 0x2ecda
movslq %r14d, %rbp
addq %r12, %rbp
movq %rbp, %rdi
movl $0xd, %esi
callq 0x1f790
testq %rax, %rax
je 0x2ee75
movq %rax, %r13
subq %r12, %rax
movl -0x8(%r12), %ecx
shlq $0x20, %rcx
addq %r15, %rcx
sarq $0x20, %rcx
cmpq %rcx, %rax
jg 0x2ed96
movsbl (%rbp), %edx
cmpl $0x24, %edx
jne 0x2ee9f
incq %rbp
movq %r13, %rsi
subq %rbp, %rsi
movq %rbp, %rdi
movq %rsp, %rdx
callq 0x30179
testl %eax, %eax
je 0x2eeb2
movq (%rsp), %rsi
cmpq $0x20000001, %rsi       # imm = 0x20000001
jae 0x2eeb2
movq 0x20(%rbx), %r12
subl %r12d, %r13d
subl %r14d, %r13d
addl %r13d, %r14d
addl $0x2, %r14d
cmpq $0x8000, %rsi           # imm = 0x8000
jb 0x2ecd6
movq %r12, %rdi
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x28f3c
movq 0x20(%rbx), %r12
movslq -0x8(%r12), %rcx
movq (%rsp), %rsi
leaq 0x2(%rsi), %rax
xorl %r14d, %r14d
subq %rcx, %rax
jbe 0x2ecd6
movq %r12, %rdi
movq %rax, %rsi
callq 0x28543
movq %rax, %r12
movq %rax, 0x20(%rbx)
movq (%rsp), %rsi
movq %rsi, 0x58(%rbx)
movslq -0x8(%r12), %rcx
movslq %r14d, %rax
movq %rcx, %rdi
subq %rax, %rdi
leaq 0x2(%rsi), %rdx
movl %edx, %r8d
cmpq %r8, %rdi
jb 0x2ed96
testl %r14d, %r14d
jne 0x2ed57
cmpq $0x8000, %rsi           # imm = 0x8000
jl 0x2ed57
cmpq %rcx, %rdx
jne 0x2ed57
xorl %r14d, %r14d
xorl %edi, %edi
movq %r12, %rsi
callq 0x3059c
movq 0x38(%rbx), %rcx
movslq 0x30(%rbx), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x30(%rbx)
movq %rax, (%rcx,%rdx,8)
movq 0x20(%rbx), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x285c9
callq 0x2845e
movq %rax, 0x20(%rbx)
movq 0x58(%rbx), %rsi
addq $0x2, %rsi
movq %rax, %rdi
callq 0x28543
movq %rax, 0x20(%rbx)
jmp 0x2ed7e
addq %rax, %r12
movq %r12, %rdi
callq 0x3068f
movq 0x38(%rbx), %rcx
movslq 0x30(%rbx), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0x30(%rbx)
movq %rax, (%rcx,%rdx,8)
movl 0x58(%rbx), %eax
addl %eax, %r14d
addl $0x2, %r14d
movq $-0x1, 0x58(%rbx)
decl 0x54(%rbx)
movq $-0x1, %rsi
jne 0x2ebfa
testl %r14d, %r14d
je 0x2edac
movq 0x20(%rbx), %rdi
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x28f3c
xorl %ebp, %ebp
cmpl 0x54(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x2eed6
cmpl $0x0, 0x30(%rbx)
jne 0x2ef4f
movq 0x20(%rbx), %r15
movq %r15, %rdi
movl $0xd, %esi
callq 0x1f790
testq %rax, %rax
je 0x2ee42
movq %rax, %r14
subq %r15, %rax
movl -0x8(%r15), %ecx
addl $-0x2, %ecx
movslq %ecx, %rcx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jg 0x2eed6
cmpb $0x2a, (%r15)
jne 0x2ef6a
incq %r15
movq %r14, %rsi
subq %r15, %rsi
movq %rsp, %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x30179
testl %eax, %eax
sete %cl
movq (%r12), %rax
cmpq $0x100001, %rax         # imm = 0x100001
setge %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x2eee7
leaq 0x68108(%rip), %rsi     # 0x96f43
movl $0x28, %edx
jmp 0x2ee61
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x10001, -0x8(%r15)    # imm = 0x10001
jb 0x2eed6
leaq 0x680a6(%rip), %rsi     # 0x96f02
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x2dc1d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2eb60
jmp 0x2eed6
cmpl $0x10001, -0x8(%r12)    # imm = 0x10001
jb 0x2ed96
leaq 0x680f5(%rip), %rsi     # 0x96f80
movl $0x29, %edx
movq %rbx, %rdi
callq 0x2dc1d
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x2eecc
leaq 0x68104(%rip), %rsi     # 0x96faa
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2dc8a
jmp 0x2eec6
leaq 0x68118(%rip), %rsi     # 0x96fd1
movl $0x23, %edx
movq %rbx, %rdi
callq 0x2dc1d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2eb60
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rbx), %rdi
subl %edi, %r14d
addl $0x2, %r14d
testq %rax, %rax
jle 0x2ef23
movl %eax, 0x54(%rbx)
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x2ef0b
callq 0x29af7
movl 0x54(%rbx), %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x299b5
movq %rax, 0x38(%rbx)
movl 0x54(%rbx), %eax
jmp 0x2ebe4
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x28f3c
xorl %ebp, %ebp
jmp 0x2eed6
leaq 0x68031(%rip), %rdx     # 0x96f6c
leaq 0x67e5c(%rip), %rcx     # 0x96d9e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x554, %r8d            # imm = 0x554
jmp 0x2ef83
leaq 0x67f9f(%rip), %rdx     # 0x96ef5
leaq 0x67e41(%rip), %rcx     # 0x96d9e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x51c, %r8d            # imm = 0x51C
jmp 0x2ef83
leaq 0x67fbc(%rip), %rdx     # 0x96f2d
leaq 0x67e26(%rip), %rcx     # 0x96d9e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x52f, %r8d            # imm = 0x52F
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0