// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_list.c
// Function: lsetCommand
void lsetCommand(redisClient *c) {

    // 取出列表对象
    robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);

    if (o == NULL || checkType(c,o,REDIS_LIST)) return;
    long index;

    // 取出值对象 value
    robj *value = (c->argv[3] = tryObjectEncoding(c->argv[3]));

    // 取出整数值对象 index
    if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != REDIS_OK))
        return;

    // 查看保存 value 值是否需要转换列表的底层编码
    listTypeTryConversion(o,value);

    // 设置到 ziplist
    if (o->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *p, *zl = o->ptr;
        // 查找索引
        p = ziplistIndex(zl,index);
        if (p == NULL) {
            addReply(c,shared.outofrangeerr);
        } else {
            // 删除现有的值
            o->ptr = ziplistDelete(o->ptr,&p);
            // 插入新值到指定索引
            value = getDecodedObject(value);
            o->ptr = ziplistInsert(o->ptr,p,value->ptr,sdslen(value->ptr));
            decrRefCount(value);

            addReply(c,shared.ok);
            signalModifiedKey(c->db,c->argv[1]);
            notifyKeyspaceEvent(REDIS_NOTIFY_LIST,"lset",c->argv[1],c->db->id);
            server.dirty++;
        }

    // 设置到双端链表
    } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) {

        listNode *ln = listIndex(o->ptr,index);

        if (ln == NULL) {
            addReply(c,shared.outofrangeerr);
        } else {
            // 删除旧值对象
            decrRefCount((robj*)listNodeValue(ln));
            // 指向新对象
            listNodeValue(ln) = value;
            incrRefCount(value);

            addReply(c,shared.ok);
            signalModifiedKey(c->db,c->argv[1]);
            notifyKeyspaceEvent(REDIS_NOTIFY_LIST,"lset",c->argv[1],c->db->id);
            server.dirty++;
        }
    } else {
        redisPanic("Unknown list encoding");
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rsi
leaq 0x903e0(%rip), %r13     # 0xca800
movq 0x78(%r13), %rdx
callq 0x317a6
testq %rax, %rax
je 0x3a586
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x3a586
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rdi
callq 0x30c49
movq %rax, %r15
movq 0x38(%rbx), %rax
movq %r15, 0x18(%rax)
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x3a586
movq %r14, %rdi
movq %r15, %rsi
callq 0x39684
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3a500
cmpl $0x5, %eax
jne 0x3a594
movq 0x8(%r14), %rdi
movl (%rsp), %esi
callq 0x2c28b
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x3a577
movq 0x8(%r14), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x2c713
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x30d93
movq %rax, %r15
movq 0x8(%r14), %rdi
movq (%r12), %rsi
movq 0x8(%rax), %rdx
movl -0x8(%rdx), %ecx
callq 0x2c70e
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x30b0c
jmp 0x3a52a
movq 0x8(%r14), %rdi
movq (%rsp), %rsi
callq 0x207b0
testq %rax, %rax
je 0x3a577
movq %rax, %r14
movq 0x10(%rax), %rdi
callq 0x30b0c
movq %r15, 0x10(%r14)
movq %r15, %rdi
callq 0x306f3
movq 0x8(%r13), %rsi
movq %rbx, %rdi
callq 0x2d97c
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x319b8
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x59de6(%rip), %rsi     # 0x94343
movl $0x10, %edi
callq 0x60371
leaq 0x8fa3a(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
jmp 0x3a586
movq 0x90(%r13), %rsi
movq %rbx, %rdi
callq 0x2d97c
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x5e545(%rip), %rdi     # 0x98ae0
leaq 0x5ea0c(%rip), %rsi     # 0x98fae
movl $0x2f9, %edx            # imm = 0x2F9
callq 0x4cae0
movl $0x1, %edi
callq 0x1f3b0