// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
jne 0x37656
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x37475
jmp 0x374a0
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0xa9b3b(%rip), %rdx     # 0xe0fbf
leaq 0xa99dd(%rip), %rcx     # 0xe0e68
movl $0x51c, %r8d            # imm = 0x51C
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0xd, %esi
callq 0x1f790
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x374f9
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
cmpq $0x10000, %rax          # imm = 0x10000
jbe 0x374ed
movq -0x10(%rbp), %rdi
leaq 0xa9aef(%rip), %rsi     # 0xe0fcc
callq 0x35d40
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x373b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3a
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
movq %rax, %rcx
movq -0x38(%rbp), %rax
subl $0x2, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jle 0x37537
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3a
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbl (%rax), %eax
cmpl $0x2a, %eax
jne 0x37549
jmp 0x37574
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0xa9a9f(%rip), %rdx     # 0xe0ff7
leaq 0xa9909(%rip), %rcx     # 0xe0e68
movl $0x52f, %r8d            # imm = 0x52F
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x1, %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
addq $0x1, %rax
subq %rax, %rsi
leaq -0x28(%rbp), %rdx
callq 0x39780
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x375af
cmpq $0x100000, -0x28(%rbp)  # imm = 0x100000
jle 0x375d7
movq -0x10(%rbp), %rdi
leaq 0xa9a53(%rip), %rsi     # 0xe100d
callq 0x35d40
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x373b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3a
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
subq %rcx, %rax
addq $0x2, %rax
movl %eax, -0x1c(%rbp)
cmpq $0x0, -0x28(%rbp)
jg 0x37615
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x1c(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2d2e0
movl $0x0, -0x4(%rbp)
jmp 0x37a3a
movq -0x28(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x54(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x3763a
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x2e3a0
movq -0x10(%rbp), %rax
movslq 0x54(%rax), %rdi
shlq $0x3, %rdi
callq 0x2e130
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
jle 0x37662
jmp 0x3768d
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0xa99c5(%rip), %rdx     # 0xe1036
leaq 0xa97f0(%rip), %rcx     # 0xe0e68
movl $0x554, %r8d            # imm = 0x554
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
jmp 0x3768f
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
je 0x37a05
movq -0x10(%rbp), %rax
cmpq $-0x1, 0x58(%rax)
jne 0x378ad
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x1c(%rbp), %rax
addq %rax, %rdi
movl $0xd, %esi
callq 0x1f790
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x37711
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
cmpq $0x10000, %rax          # imm = 0x10000
jbe 0x3770c
movq -0x10(%rbp), %rdi
leaq 0xa995a(%rip), %rsi     # 0xe104a
callq 0x35d40
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x373b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3a
jmp 0x37a05
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
movq %rax, %rcx
movq -0x40(%rbp), %rax
subl $0x2, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jle 0x37748
jmp 0x37a05
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x24, %eax
je 0x37797
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
movsbl (%rax,%rcx), %edx
leaq 0xa98fc(%rip), %rsi     # 0xe1074
movb $0x0, %al
callq 0x35d80
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x373b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3a
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x1c(%rbp), %rax
addq %rax, %rdi
addq $0x1, %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
addq %rcx, %rax
addq $0x1, %rax
subq %rax, %rsi
leaq -0x28(%rbp), %rdx
callq 0x39780
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x377e7
cmpq $0x0, -0x28(%rbp)
jl 0x377e7
cmpq $0x20000000, -0x28(%rbp) # imm = 0x20000000
jle 0x3780f
movq -0x10(%rbp), %rdi
leaq 0xa98a9(%rip), %rsi     # 0xe109b
callq 0x35d40
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x373b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3a
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rdx
addq %rdx, %rax
subq %rax, %rcx
addq $0x2, %rcx
movslq -0x1c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x1c(%rbp)
cmpq $0x8000, -0x28(%rbp)    # imm = 0x8000
jl 0x378a1
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x1c(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2d2e0
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
addq $0x2, %rcx
cmpq %rcx, %rax
jae 0x3789f
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x28(%rbp), %rsi
addq $0x2, %rsi
subq -0x30(%rbp), %rsi
callq 0x2c220
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x378a1
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
movslq -0x1c(%rbp), %rcx
subq %rcx, %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
addq $0x2, %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x378d9
jmp 0x37a05
cmpl $0x0, -0x1c(%rbp)
jne 0x37994
movq -0x10(%rbp), %rax
cmpq $0x8000, 0x58(%rax)     # imm = 0x8000
jl 0x37994
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34800
cltq
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
addq $0x2, %rcx
cmpq %rcx, %rax
jne 0x37994
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
xorl %edi, %edi
callq 0x3a0d0
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rsi
movl 0x30(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x30(%rsi)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x2c370
callq 0x2c0c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
addq $0x2, %rsi
callq 0x2c220
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x0, -0x1c(%rbp)
jmp 0x379e7
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x1c(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x3a2e0
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rsi
movl 0x30(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x30(%rsi)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
addq $0x2, %rcx
movslq -0x1c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq $-0x1, 0x58(%rax)
movq -0x10(%rbp), %rax
movl 0x54(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x54(%rax)
jmp 0x3768f
cmpl $0x0, -0x1c(%rbp)
je 0x37a20
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x1c(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2d2e0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
jne 0x37a33
movl $0x0, -0x4(%rbp)
jmp 0x37a3a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)