// File: /workspace/github_c_makefile_star2_O2/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 0x2d342
testl %eax, %eax
jle 0x2d49d
pushq $0x1
popq %rax
pushq $-0x1
popq %rbp
testl %eax, %eax
je 0x2d3f7
movq 0x20(%rbx), %r12
movq 0x58(%rbx), %rsi
cmpq $-0x1, %rsi
jne 0x2d28e
movslq %r14d, %r13
addq %r12, %r13
movq %r13, %rdi
pushq $0xd
popq %rsi
callq 0x1f790
testq %rax, %rax
je 0x2d3d6
movq %rax, %r15
subq %r12, %rax
movl -0x8(%r12), %ecx
shlq $0x20, %rcx
movabsq $-0x200000000, %rdx     # imm = 0xFFFFFFFE00000000
addq %rdx, %rcx
sarq $0x20, %rcx
cmpq %rcx, %rax
jg 0x2d3f7
movsbl (%r13), %edx
cmpl $0x24, %edx
jne 0x2d414
incq %r13
movq %r15, %rsi
subq %r13, %rsi
movq %r13, %rdi
movq %rsp, %rdx
callq 0x2e5a4
testl %eax, %eax
je 0x2d427
movq (%rsp), %rsi
cmpq $0x20000001, %rsi       # imm = 0x20000001
jae 0x2d427
movq 0x20(%rbx), %r12
subl %r12d, %r15d
subl %r14d, %r15d
addl %r15d, %r14d
addl $0x2, %r14d
cmpq $0x8000, %rsi           # imm = 0x8000
jb 0x2d28a
movq %r12, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x27a3e
movq 0x20(%rbx), %r12
movslq -0x8(%r12), %rcx
movq (%rsp), %rsi
leaq 0x2(%rsi), %rax
xorl %r14d, %r14d
subq %rcx, %rax
jbe 0x2d28a
movq %r12, %rdi
movq %rax, %rsi
callq 0x2722a
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 0x2d3f7
testl %r14d, %r14d
jne 0x2d309
cmpq $0x8000, %rsi           # imm = 0x8000
jl 0x2d309
cmpq %rcx, %rdx
jne 0x2d309
xorl %r14d, %r14d
xorl %edi, %edi
movq %r12, %rsi
callq 0x2e9b4
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
pushq $-0x2
popq %rsi
callq 0x272ad
callq 0x271bf
movq %rax, 0x20(%rbx)
movq 0x58(%rbx), %rsi
addq $0x2, %rsi
movq %rax, %rdi
callq 0x2722a
movq %rax, 0x20(%rbx)
jmp 0x2d330
addq %rax, %r12
movq %r12, %rdi
callq 0x2ea81
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
orq $-0x1, 0x58(%rbx)
movl 0x54(%rbx), %eax
decl %eax
movl %eax, 0x54(%rbx)
jmp 0x2d19c
cmpl $0x0, 0x30(%rbx)
jne 0x2d4b8
pushq $-0x1
popq %rbp
movq 0x20(%rbx), %r15
pushq $0xd
popq %rsi
movq %r15, %rdi
callq 0x1f790
testq %rax, %rax
je 0x2d3c3
movq %rax, %r14
subq %r15, %rax
movl -0x8(%r15), %ecx
addl $-0x2, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jg 0x2d441
cmpb $0x2a, (%r15)
jne 0x2d4d3
incq %r15
movq %r14, %rsi
subq %r15, %rsi
movq %rsp, %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x2e5a4
testl %eax, %eax
sete %cl
movq (%r12), %rax
cmpq $0x100001, %rax         # imm = 0x100001
setge %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x2d452
leaq 0x5fb52(%rip), %rsi     # 0x8cf13
jmp 0x2d3e8
cmpl $0x10001, -0x8(%r15)    # imm = 0x10001
jb 0x2d441
leaq 0x5fafe(%rip), %rsi     # 0x8ced2
jmp 0x2d3e8
cmpl $0x10001, -0x8(%r12)    # imm = 0x10001
jb 0x2d3f7
leaq 0x5fb68(%rip), %rsi     # 0x8cf50
movq %rbx, %rdi
callq 0x2c2da
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x2d43c
testl %r14d, %r14d
je 0x2d40b
movq 0x20(%rbx), %rdi
pushq $-0x1
popq %rdx
movl %r14d, %esi
callq 0x27a3e
xorl %ebp, %ebp
cmpl 0x54(%rbx), %ebp
sbbl %ebp, %ebp
jmp 0x2d441
leaq 0x5fb5f(%rip), %rsi     # 0x8cf7a
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2c301
jmp 0x2d436
leaq 0x5fb73(%rip), %rsi     # 0x8cfa1
movq %rbx, %rdi
callq 0x2c2da
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2d10e
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 0x2d48e
movl %eax, 0x54(%rbx)
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x2d476
callq 0x283cd
movl 0x54(%rbx), %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x2828d
movq %rax, 0x38(%rbx)
movl 0x54(%rbx), %eax
jmp 0x2d18e
pushq $-0x1
popq %rdx
movl %r14d, %esi
callq 0x27a3e
xorl %ebp, %ebp
jmp 0x2d441
leaq 0x5fa98(%rip), %rdx     # 0x8cf3c
leaq 0x5f8c3(%rip), %rcx     # 0x8cd6e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x554, %r8d            # imm = 0x554
jmp 0x2d4ec
leaq 0x5fa06(%rip), %rdx     # 0x8cec5
leaq 0x5f8a8(%rip), %rcx     # 0x8cd6e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x51c, %r8d            # imm = 0x51C
jmp 0x2d4ec
leaq 0x5fa23(%rip), %rdx     # 0x8cefd
leaq 0x5f88d(%rip), %rcx     # 0x8cd6e
movq %rbx, %rdi
xorl %esi, %esi
movl $0x52f, %r8d            # imm = 0x52F
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0