// File: /workspace/github_c_makefile_star2_O3/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
xorl %ebx, %ebx
testq %rdx, %rdx
jg 0x2c342
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r15
movq %rdi, %r14
xorl %ebx, %ebx
testq %rsi, %rsi
js 0x2c355
movq 0x10(%r14), %r12
jmp 0x2c358
xorl %r12d, %r12d
movq %r12, %r13
subq %r15, %r13
cmpq %rdx, %r13
cmovaeq %rdx, %r13
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x26fde
testl %eax, %eax
je 0x2c331
movl $0x1, %ebx
cmpq %r15, %r12
je 0x2c331
movq 0x10(%rsp), %rbp
movl 0x34(%rsp), %esi
movq %rbp, %r12
movq 0x8(%rbp), %rbp
testl %esi, %esi
je 0x2c3a4
js 0x2c3c8
movl 0x1c(%r12), %eax
movzwl %ax, %r15d
jmp 0x2c3b6
movl 0x1c(%r12), %eax
movzwl %ax, %r15d
cmpq %r15, %r13
jae 0x2c449
cmpq %r15, %r13
jae 0x2c3c0
movq %r13, %r15
jmp 0x2c3da
subl %esi, %r15d
movslq %r15d, %r15
jmp 0x2c3da
movl %esi, %r15d
negl %r15d
cmpq %r15, %r13
cmovbq %r13, %r15
movl 0x1c(%r12), %eax
andl $0x30000, %eax          # imm = 0x30000
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x2c3f8
movq %r12, %rdi
callq 0x2b72e
orb $0x10, 0x1e(%r12)
movl 0x34(%rsp), %esi
movq 0x10(%r12), %rdi
movl %r15d, %edx
callq 0x3b8b4
movq %rax, 0x10(%r12)
movq %rax, %rdi
callq 0x3bbc5
movl %eax, 0x18(%r12)
movl 0x1c(%r12), %eax
subl %r15d, %eax
movw %ax, 0x1c(%r12)
subq %r15, 0x10(%r14)
movl 0x1c(%r12), %eax
testw %ax, %ax
je 0x2c449
andl $0x130000, %eax         # imm = 0x130000
cmpl $0x110000, %eax         # imm = 0x110000
jne 0x2c454
movq %r12, %rdi
callq 0x2b6ab
jmp 0x2c454
movq %r14, %rdi
movq %r12, %rsi
callq 0x2bb96
xorl %esi, %esi
movl $0x0, 0x34(%rsp)
subq %r15, %r13
jne 0x2c38c
jmp 0x2c331