// File: /workspace/github_c_makefile_star2_O1/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
xorl %r14d, %r14d
cmpl $0x0, 0x54(%rdi)
je 0x2eb1e
cmpl $0x0, 0x54(%rbx)
jle 0x2ec8a
cmpl $0x0, 0x54(%rbx)
je 0x2eaf0
movabsq $-0x200000000, %rbp     # imm = 0xFFFFFFFE00000000
cmpq $-0x1, 0x58(%rbx)
jne 0x2ea34
movq 0x20(%rbx), %r15
movslq %r14d, %r13
addq %r15, %r13
movq %r13, %rdi
movl $0xd, %esi
callq 0x1f790
testq %rax, %rax
je 0x2ec11
movq %rax, %r12
subq %r15, %rax
movl -0x8(%r15), %ecx
shlq $0x20, %rcx
addq %rbp, %rcx
sarq $0x20, %rcx
cmpq %rcx, %rax
jg 0x2eaf0
movsbl (%r13), %edx
cmpl $0x24, %edx
jne 0x2ec3a
incq %r13
movq %r12, %rsi
subq %r13, %rsi
movq %r13, %rdi
movq %rsp, %rdx
callq 0x2fec4
testl %eax, %eax
je 0x2ec4d
movq (%rsp), %rax
cmpq $0x20000001, %rax       # imm = 0x20000001
jae 0x2ec4d
movq 0x20(%rbx), %rdi
subl %edi, %r12d
subl %r14d, %r12d
addl %r12d, %r14d
addl $0x2, %r14d
cmpq $0x8000, %rax           # imm = 0x8000
jl 0x2ea2c
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x28c31
movq 0x20(%rbx), %rdi
movslq -0x8(%rdi), %rax
movq (%rsp), %rsi
addq $0x2, %rsi
xorl %r14d, %r14d
subq %rax, %rsi
jbe 0x2ea2c
callq 0x2823a
movq %rax, 0x20(%rbx)
movq (%rsp), %rax
movq %rax, 0x58(%rbx)
movq 0x20(%rbx), %rax
movq 0x58(%rbx), %rsi
movslq -0x8(%rax), %rdx
movslq %r14d, %rcx
movq %rdx, %r8
subq %rcx, %r8
leaq 0x2(%rsi), %rdi
movl %edi, %r9d
cmpq %r9, %r8
jb 0x2eaf0
testl %r14d, %r14d
jne 0x2eab8
cmpq $0x8000, %rsi           # imm = 0x8000
jl 0x2eab8
cmpq %rdx, %rdi
jne 0x2eab8
xorl %r14d, %r14d
xorl %edi, %edi
movq %rax, %rsi
callq 0x302c4
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 0x282c0
callq 0x281b2
movq %rax, 0x20(%rbx)
movq 0x58(%rbx), %rsi
addq $0x2, %rsi
movq %rax, %rdi
callq 0x2823a
movq %rax, 0x20(%rbx)
jmp 0x2eadf
addq %rcx, %rax
movq %rax, %rdi
callq 0x303b7
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)
jne 0x2e962
testl %r14d, %r14d
je 0x2eb06
movq 0x20(%rbx), %rdi
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x28c31
xorl %ebp, %ebp
cmpl 0x54(%rbx), %ebp
sbbl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x30(%rbx)
jne 0x2eca5
movq 0x20(%rbx), %r15
movq %r15, %rdi
movl $0xd, %esi
callq 0x1f790
testq %rax, %rax
je 0x2eba0
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 0x2eb0d
cmpb $0x2a, (%r15)
jne 0x2ecc0
incq %r15
movq %r14, %rsi
subq %r15, %rsi
movq %rsp, %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x2fec4
testl %eax, %eax
sete %cl
movq (%r12), %rax
cmpq $0x100001, %rax         # imm = 0x100001
setge %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x2ebd6
leaq 0x6834a(%rip), %rsi     # 0x96ee3
movl $0x28, %edx
jmp 0x2ebbf
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x10001, -0x8(%r15)    # imm = 0x10001
jb 0x2eb0d
leaq 0x682e8(%rip), %rsi     # 0x96ea2
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x2d991
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2e8c1
jmp 0x2eb0d
movq 0x20(%rbx), %rdi
subl %edi, %r14d
addl $0x2, %r14d
testq %rax, %rax
jle 0x2ec76
movl %eax, 0x54(%rbx)
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x2ebfb
callq 0x2984b
movslq 0x54(%rbx), %rdi
shlq $0x3, %rdi
callq 0x29709
movq %rax, 0x38(%rbx)
jmp 0x2e944
cmpl $0x10001, -0x8(%r15)    # imm = 0x10001
jb 0x2eaf0
leaq 0x682fa(%rip), %rsi     # 0x96f20
movl $0x29, %edx
movq %rbx, %rdi
callq 0x2d991
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x2ec67
leaq 0x68309(%rip), %rsi     # 0x96f4a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2d9fe
jmp 0x2ec61
leaq 0x6831d(%rip), %rsi     # 0x96f71
movl $0x23, %edx
movq %rbx, %rdi
callq 0x2d991
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2e8c1
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2eb0d
movl %r14d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x28c31
xorl %ebp, %ebp
jmp 0x2eb0d
leaq 0x6827b(%rip), %rdx     # 0x96f0c
leaq 0x680a6(%rip), %rcx     # 0x96d3e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x554, %r8d            # imm = 0x554
jmp 0x2ecd9
leaq 0x681e9(%rip), %rdx     # 0x96e95
leaq 0x6808b(%rip), %rcx     # 0x96d3e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x51c, %r8d            # imm = 0x51C
jmp 0x2ecd9
leaq 0x68206(%rip), %rdx     # 0x96ecd
leaq 0x68070(%rip), %rcx     # 0x96d3e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x52f, %r8d            # imm = 0x52F
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0