// File: /workspace/github_c_makefile_star2_O3/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 0x313b2
testl %eax, %eax
jne 0x3a933
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
je 0x3a942
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 0x8feaf(%rip), %rbp     # 0xca800
movq 0x8(%rbp), %rdx
movq %rbx, %rdi
callq 0x317a6
testq %rax, %rax
je 0x3a933
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x3a933
movq %r14, %rdi
callq 0x39979
movq %rax, %r15
movq 0x10(%rsp), %rax
testq %rax, %rax
jns 0x3a996
addq %r15, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
jns 0x3a9a8
addq %r15, %rcx
movq %rcx, 0x8(%rsp)
testq %rax, %rax
jns 0x3a9b8
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
cmpq %rcx, %rax
jg 0x3a9df
cmpq %r15, %rax
jge 0x3a9df
cmpq %r15, %rcx
jb 0x3a9d3
leaq -0x1(%r15), %rcx
movq %rcx, 0x8(%rsp)
notq %rcx
movq %r15, %r12
addq %rcx, %r12
movq %rax, %r15
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3aa1e
cmpl $0x5, %eax
jne 0x3aadc
movq 0x8(%r14), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x2c8cb
movq %rax, 0x8(%r14)
movl %r12d, %esi
negl %esi
movq %rax, %rdi
movl %r12d, %edx
callq 0x2c8cb
movq %rax, 0x8(%r14)
jmp 0x3aa4e
movq 0x8(%r14), %r13
testq %r15, %r15
jle 0x3aa38
movq (%r13), %rsi
movq %r13, %rdi
callq 0x205b1
decq %r15
jne 0x3aa27
testq %r12, %r12
jle 0x3aa4e
movq 0x8(%r13), %rsi
movq %r13, %rdi
callq 0x205b1
decq %r12
jne 0x3aa3d
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x598eb(%rip), %rsi     # 0x9434f
movl $0x10, %edi
callq 0x60371
movq %r14, %rdi
callq 0x39979
testq %rax, %rax
jne 0x3aaac
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x31a36
movq 0x8(%rbx), %rax
movq 0x38(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x59a3b(%rip), %rsi     # 0x944dd
movl $0x4, %edi
callq 0x60371
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x319b8
leaq 0x8f4e4(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movq 0x8(%rbp), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x3a933
leaq 0x5dffd(%rip), %rdi     # 0x98ae0
leaq 0x5e4c4(%rip), %rsi     # 0x98fae
movl $0x3a1, %edx            # imm = 0x3A1
callq 0x4cae0
movl $0x1, %edi
callq 0x1f3b0