// File: /workspace/github_c_makefile_star2_O2/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 0x8fd86(%rip), %r13     # 0xc7800
movq 0x78(%r13), %rdx
callq 0x2f9d9
testq %rax, %rax
je 0x37bdc
movq %rax, %r14
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
jne 0x37bdc
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rdi
callq 0x2ef4c
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 0x2f672
testl %eax, %eax
jne 0x37bdc
movq %r14, %rdi
movq %r15, %rsi
callq 0x36d34
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x37b58
cmpl $0x5, %eax
jne 0x37bea
movq 0x8(%r14), %rdi
movl (%rsp), %esi
callq 0x2abfb
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x37bcd
movq 0x8(%r14), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x2afb7
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x2f087
movq %rax, %r15
movq 0x8(%r14), %rdi
movq (%r12), %rsi
movq 0x8(%rax), %rdx
movl -0x8(%rdx), %ecx
callq 0x2afb2
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x2ee1f
jmp 0x37b82
movq 0x8(%r14), %rdi
movq (%rsp), %rsi
callq 0x200c3
testq %rax, %rax
je 0x37bcd
movq %rax, %r14
movq 0x10(%rax), %rdi
callq 0x2ee1f
movq %r15, 0x10(%r14)
movq %r15, %rdi
callq 0x2eacc
movq 0x8(%r13), %rsi
movq %rbx, %rdi
callq 0x2c10d
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x2fbc0
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x5274e(%rip), %rsi     # 0x8a303
pushq $0x10
popq %rdi
callq 0x5b488
leaq 0x8f3e4(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
jmp 0x37bdc
movq 0x90(%r13), %rsi
movq %rbx, %rdi
callq 0x2c10d
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x56eb3(%rip), %rdi     # 0x8eaa4
leaq 0x5737a(%rip), %rsi     # 0x8ef72
movl $0x2f9, %edx            # imm = 0x2F9
callq 0x490e8
pushq $0x1
popq %rdi
callq 0x1f3b0