// File: /workspace/github_c_makefile_star2_O1/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 0x310df
testl %eax, %eax
jne 0x3a8c7
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
je 0x3a8d6
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 0x8ff1b(%rip), %rbp     # 0xca800
movq 0x8(%rbp), %rdx
movq %rbx, %rdi
callq 0x314a5
testq %rax, %rax
je 0x3a8c7
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3a8c7
movq %r14, %rdi
callq 0x398e9
movq %rax, %r15
movq 0x10(%rsp), %rax
testq %rax, %rax
jns 0x3a92a
addq %r15, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
testq %rax, %rax
jns 0x3a93c
addq %r15, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x10(%rsp)
jns 0x3a94d
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
xorl %r12d, %r12d
cmpq %rcx, %rax
jg 0x3a983
cmpq %r15, %rax
jge 0x3a983
cmpq %r15, %rcx
jl 0x3a972
leaq -0x1(%r15), %rcx
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rcx
notq %rcx
movq %r15, %r12
addq %rcx, %r12
movq %rax, %r15
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3a9c2
cmpl $0x5, %eax
jne 0x3aa80
movq 0x8(%r14), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x2c61d
movq %rax, 0x8(%r14)
movl %r12d, %esi
negl %esi
movq %rax, %rdi
movl %r12d, %edx
callq 0x2c61d
movq %rax, 0x8(%r14)
jmp 0x3a9f2
movq 0x8(%r14), %r13
testq %r15, %r15
jle 0x3a9dc
movq (%r13), %rsi
movq %r13, %rdi
callq 0x20544
decq %r15
jne 0x3a9cb
testq %r12, %r12
jle 0x3a9f2
movq 0x8(%r13), %rsi
movq %r13, %rdi
callq 0x20544
decq %r12
jne 0x3a9e1
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x598f7(%rip), %rsi     # 0x942ff
movl $0x10, %edi
callq 0x608b5
movq %r14, %rdi
callq 0x398e9
testq %rax, %rax
jne 0x3aa50
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3174b
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x59a47(%rip), %rsi     # 0x9448d
movl $0x4, %edi
callq 0x608b5
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x316b7
leaq 0x8f540(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movq 0x8(%rbp), %rsi
movq %rbx, %rdi
callq 0x2d6f0
jmp 0x3a8c7
leaq 0x5dffd(%rip), %rdi     # 0x98a84
leaq 0x5e4d7(%rip), %rsi     # 0x98f65
movl $0x3a1, %edx            # imm = 0x3A1
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0