// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/t_list.c
// Function: ltrimCommand
void ltrimCommand(redisClient *c) {
    robj *o;
    long start, end, llen, j, ltrim, rtrim;
    list *list;
    listNode *ln;

    // 取出索引值 start 和 end
    if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
        (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;

    // 取出列表对象
    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
        checkType(c,o,REDIS_LIST)) return;

    // 列表长度
    llen = listTypeLength(o);

    /* convert negative indexes */
    // 将负数索引转换成正数索引
    if (start < 0) start = llen+start;
    if (end < 0) end = llen+end;
    if (start < 0) start = 0;

    /* Invariant: start >= 0, so this test will be true when end < 0.
     * The range is empty when start > end or start >= length. */
    if (start > end || start >= llen) {
        /* Out of range start or start > end result in empty list */
        ltrim = llen;
        rtrim = 0;
    } else {
        if (end >= llen) end = llen-1;
        ltrim = start;
        rtrim = llen-end-1;
    }

    /* Remove list elements to perform the trim */
    // 删除指定列表两端的元素

    if (o->encoding == REDIS_ENCODING_ZIPLIST) {
        // 删除左端元素
        o->ptr = ziplistDeleteRange(o->ptr,0,ltrim);
        // 删除右端元素
        o->ptr = ziplistDeleteRange(o->ptr,-rtrim,rtrim);

    } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) {
        list = o->ptr;
        // 删除左端元素
        for (j = 0; j < ltrim; j++) {
            ln = listFirst(list);
            listDelNode(list,ln);
        }
        // 删除右端元素
        for (j = 0; j < rtrim; j++) {
            ln = listLast(list);
            listDelNode(list,ln);
        }

    } else {
        redisPanic("Unknown list encoding");
    }

    // 发送通知
    notifyKeyspaceEvent(REDIS_NOTIFY_LIST,"ltrim",c->argv[1],c->db->id);

    // 如果列表已经为空，那么删除它
    if (listTypeLength(o) == 0) {
        dbDelete(c->db,c->argv[1]);
        notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
    }

    signalModifiedKey(c->db,c->argv[1]);

    server.dirty++;

    addReply(c,shared.ok);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x37f6c
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
je 0x37f7b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x8f876(%rip), %r13     # 0xc7800
movq 0x8(%r13), %rdx
movq %rbx, %rdi
callq 0x2f9d9
testq %rax, %rax
je 0x37f6c
movq %rax, %r14
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
jne 0x37f6c
movq %r14, %rdi
callq 0x3700e
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
jns 0x37fca
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
jns 0x37fdc
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
testq %rcx, %rcx
jns 0x37fe9
andq $0x0, 0x10(%rsp)
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpq %rdx, %rcx
jg 0x38010
cmpq %rax, %rcx
jge 0x38010
cmpq %rax, %rdx
jb 0x38004
leaq -0x1(%rax), %rdx
movq %rdx, 0x8(%rsp)
notq %rdx
movq %rax, %r15
addq %rdx, %r15
movq %rcx, %rax
movl (%r14), %ecx
shrl $0x4, %ecx
andl $0xf, %ecx
cmpl $0x4, %ecx
je 0x380da
cmpl $0x5, %ecx
jne 0x3811d
movq 0x8(%r14), %rdi
xorl %esi, %esi
movl %eax, %edx
callq 0x2b132
movq %rax, 0x8(%r14)
movl %r15d, %esi
negl %esi
movq %rax, %rdi
movl %r15d, %edx
callq 0x2b132
movq %rax, 0x8(%r14)
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x522a9(%rip), %rsi     # 0x8a30f
pushq $0x10
popq %rdi
callq 0x5b488
movq %r14, %rdi
callq 0x3700e
testq %rax, %rax
jne 0x380aa
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x2fc3b
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x523fb(%rip), %rsi     # 0x8a49d
pushq $0x4
popq %rdi
callq 0x5b488
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x2fbc0
leaq 0x8eee6(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
movq 0x8(%r13), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x37f6c
movq 0x8(%r14), %r12
xorl %ebp, %ebp
testq %rax, %rax
cmovgq %rax, %rbp
subq $0x1, %rbp
jb 0x380fb
movq (%r12), %rsi
movq %r12, %rdi
callq 0x1ff28
jmp 0x380e7
xorl %ebp, %ebp
testq %r15, %r15
cmovgq %r15, %rbp
subq $0x1, %rbp
jb 0x38050
movq 0x8(%r12), %rsi
movq %r12, %rdi
callq 0x1ff28
jmp 0x38104
leaq 0x56980(%rip), %rdi     # 0x8eaa4
leaq 0x56e47(%rip), %rsi     # 0x8ef72
movl $0x3a1, %edx            # imm = 0x3A1
callq 0x490e8
pushq $0x1
popq %rdi
callq 0x1f3b0