// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/quicklist.c
// Function: quicklistDelRange
int quicklistDelRange(quicklist *quicklist, const long start,
                      const long count) {
    if (count <= 0)
        return 0;

    unsigned long extent = count; /* range is inclusive of start position */

    if (start >= 0 && extent > (quicklist->count - start)) {
        /* if requesting delete more elements than exist, limit to list size. */
        extent = quicklist->count - start;
    } else if (start < 0 && extent > (unsigned long)(-start)) {
        /* else, if at negative offset, limit max size to rest of list. */
        extent = -start; /* c.f. LREM -29 29; just delete until end. */
    }

    quicklistEntry entry;
    if (!quicklistIndex(quicklist, start, &entry))
        return 0;

    D("Quicklist delete request for start %ld, count %ld, extent: %ld", start,
      count, extent);
    quicklistNode *node = entry.node;

    /* iterate over next nodes until everything is deleted. */
    while (extent) {
        quicklistNode *next = node->next;

        unsigned long del;
        int delete_entire_node = 0;
        if (entry.offset == 0 && extent >= node->count) {
            /* If we are deleting more than the count of this node, we
             * can just delete the entire node without ziplist math. */
            delete_entire_node = 1;
            del = node->count;
        } else if (entry.offset >= 0 && extent >= node->count) {
            /* If deleting more nodes after this one, calculate delete based
             * on size of current node. */
            del = node->count - entry.offset;
        } else if (entry.offset < 0) {
            /* If offset is negative, we are in the first run of this loop
             * and we are deleting the entire range
             * from this start offset to end of list.  Since the Negative
             * offset is the number of elements until the tail of the list,
             * just use it directly as the deletion count. */
            del = -entry.offset;

            /* If the positive offset is greater than the remaining extent,
             * we only delete the remaining extent, not the entire offset.
             */
            if (del > extent)
                del = extent;
        } else {
            /* else, we are deleting less than the extent of this node, so
             * use extent directly. */
            del = extent;
        }

        D("[%ld]: asking to del: %ld because offset: %d; (ENTIRE NODE: %d), "
          "node count: %u",
          extent, del, entry.offset, delete_entire_node, node->count);

        if (delete_entire_node) {
            __quicklistDelNode(quicklist, node);
        } else {
            quicklistDecompressNodeForUse(node);
            node->zl = ziplistDeleteRange(node->zl, entry.offset, del);
            quicklistNodeUpdateSz(node);
            node->count -= del;
            quicklist->count -= del;
            quicklistDeleteIfEmpty(quicklist, node);
            if (node)
                quicklistRecompressOnly(quicklist, node);
        }

        extent -= del;

        node = next;

        entry.offset = 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
testq %rdx, %rdx
jle 0x2822f
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
xorl %r14d, %r14d
testq %rsi, %rsi
js 0x28237
movq 0x10(%rbx), %r13
jmp 0x2823a
xorl %r14d, %r14d
jmp 0x2833d
xorl %r13d, %r13d
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x27a60
testl %eax, %eax
je 0x2833d
subq %r12, %r13
cmpq %r15, %r13
cmovaeq %r15, %r13
movq 0x10(%rsp), %rbp
pushq $0x1
popq %r14
testq %r13, %r13
je 0x2833d
movq %rbp, %r15
movq 0x8(%rbp), %rbp
movl 0x34(%rsp), %esi
testl %esi, %esi
je 0x28289
js 0x282ac
movl 0x1c(%r15), %eax
movzwl %ax, %r12d
jmp 0x2829a
movl 0x1c(%r15), %eax
movzwl %ax, %r12d
cmpq %r12, %r13
jae 0x28325
cmpq %r12, %r13
jae 0x282a4
movq %r13, %r12
jmp 0x282bd
subl %esi, %r12d
movslq %r12d, %r12
jmp 0x282bd
movl %esi, %r12d
negl %r12d
cmpq %r12, %r13
cmovbq %r13, %r12
movl 0x1c(%r15), %eax
andl $0x30000, %eax          # imm = 0x30000
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x282da
movq %r15, %rdi
callq 0x2732d
orb $0x10, 0x1e(%r15)
movl 0x34(%rsp), %esi
movq 0x10(%r15), %rdi
movl %r12d, %edx
callq 0x35c9e
movq %rax, 0x10(%r15)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%r15)
movl 0x1c(%r15), %eax
subl %r12d, %eax
movw %ax, 0x1c(%r15)
subq %r12, 0x10(%rbx)
movl 0x1c(%r15), %eax
testw %ax, %ax
je 0x28325
andl $0x130000, %eax         # imm = 0x130000
cmpl $0x110000, %eax         # imm = 0x110000
jne 0x28330
movq %r15, %rdi
callq 0x272ac
jmp 0x28330
movq %rbx, %rdi
movq %r15, %rsi
callq 0x278b3
subq %r12, %r13
andl $0x0, 0x34(%rsp)
jmp 0x28265
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq