// File: /workspace/github_c_makefile_star2_O1/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 0x90467(%rip), %r13     # 0xca800
movq 0x78(%r13), %rdx
callq 0x314a5
testq %rax, %rax
je 0x3a4ff
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3a4ff
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rdi
callq 0x30971
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 0x310df
testl %eax, %eax
jne 0x3a4ff
movq %r14, %rdi
movq %r15, %rsi
callq 0x395f4
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3a479
cmpl $0x5, %eax
jne 0x3a50d
movq 0x8(%r14), %rdi
movl (%rsp), %esi
callq 0x2bff5
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x3a4f0
movq 0x8(%r14), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x2c46e
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x30ac3
movq %rax, %r15
movq 0x8(%r14), %rdi
movq (%r12), %rsi
movq 0x8(%rax), %rdx
movl -0x8(%rdx), %ecx
callq 0x2c469
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x30834
jmp 0x3a4a3
movq 0x8(%r14), %rdi
movq (%rsp), %rsi
callq 0x20750
testq %rax, %rax
je 0x3a4f0
movq %rax, %r14
movq 0x10(%rax), %rdi
callq 0x30834
movq %r15, 0x10(%r14)
movq %r15, %rdi
callq 0x3041b
movq 0x8(%r13), %rsi
movq %rbx, %rdi
callq 0x2d6f0
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x316b7
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x59e1d(%rip), %rsi     # 0x942f3
movl $0x10, %edi
callq 0x608b5
leaq 0x8fac1(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
jmp 0x3a4ff
movq 0x90(%r13), %rsi
movq %rbx, %rdi
callq 0x2d6f0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x5e570(%rip), %rdi     # 0x98a84
leaq 0x5ea4a(%rip), %rsi     # 0x98f65
movl $0x2f9, %edx            # imm = 0x2F9
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0