// File: /workspace/github_c_makefile_star2_O1/heltilda[P]cicada/lnklst.c
// Function: deleteElements
ccInt deleteElements(linkedlist *theList, ccInt firstDelete, ccInt lastDelete)
{
    sublistHeader *tempSublist, *currentSublist, **parentPtr, *tempPtr;
    ccInt bottom, top, firstCopy, copyBottom;
    
    if (theList->memory == NULL)  return init_err;
    if ((!elementExists(theList, firstDelete)) || (!elementExists(theList, lastDelete))) return out_of_range_err;
    if (firstDelete > lastDelete)  return passed;
    currentSublist = findSublist(theList, firstDelete, &bottom);
    
    
        // First, find the sublist in which we stop deleting, then copy what comes after back down towards the bottom.
    
    while (bottom <= lastDelete)
    {
        top = bottom + currentSublist->numElements - 1;
        copyBottom = bottom;
        if (bottom < firstDelete) copyBottom = firstDelete;      // i.e. if it's the first pass
        
        firstCopy = top + 1;
        if (lastDelete < top)  {
            firstCopy = lastDelete + 1;
            memmove((void *) (((char *) currentSublist) + (copyBottom-bottom)*theList->elementSize + sublistHeaderSize),
                    (void *) (((char *) currentSublist) + (firstCopy-bottom)*theList->elementSize + sublistHeaderSize),
                    (top + 1 - firstCopy)*theList->elementSize);        }
        
        bottom += currentSublist->numElements;         // bottom goes by the old indexing; before the elements were deleted
        currentSublist->numElements -= (firstCopy-copyBottom);
        currentSublist = currentSublist->nextSublist;
    }
    
    
        // Now get rid of null lists.
    
    parentPtr = &(theList->memory);
    while (*parentPtr != NULL)    {
        
        tempSublist = *parentPtr;
        
        if (tempSublist->numElements == 0)   {
            tempPtr = tempSublist->nextSublist;
            free((void *) *parentPtr);
            *parentPtr = tempPtr;        }
        else  {
            parentPtr = &((*parentPtr)->nextSublist);
    }   }
    
    
        // If we went too far and deleted the first sublist, recreate it
    
    if (theList->memory == NULL)  {
        tempPtr = newSublist(0, 1, theList->elementSize, ccFalse);
        theList->memory = tempPtr;
        if (tempPtr == NULL)  return out_of_memory_err;    }
    
    theList->elementNum -= lastDelete - firstDelete + 1;
    
    return passed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x8(%rdi), %r13
testq %r13, %r13
je 0x8802
movl %esi, %r15d
movl $0x2, %eax
testl %esi, %esi
jle 0x8807
cmpl %r15d, (%rdi)
jl 0x8807
movl %edx, %r14d
testl %edx, %edx
jle 0x8807
cmpl %r14d, (%rdi)
jl 0x8807
xorl %eax, %eax
cmpl %r14d, %r15d
jg 0x8807
movl (%r13), %eax
movl $0x1, %r12d
cmpl %r15d, %eax
jge 0x8703
movl %eax, %r12d
movq 0x8(%r13), %r13
movl (%r13), %eax
addl %r12d, %eax
cmpl %r15d, %eax
jl 0x86ed
incl %r12d
movq %rdi, 0x8(%rsp)
leaq 0x8(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpl %r14d, %r12d
jg 0x878b
leal 0x1(%r14), %ebx
movl (%r13), %eax
leal (%rax,%r12), %ecx
decl %ecx
addl %r12d, %eax
cmpl %r15d, %r12d
movl %r15d, %ebp
cmovgl %r12d, %ebp
cmpl %r14d, %ecx
jle 0x8773
movl %ebp, %ecx
subl %r12d, %ecx
movq 0x8(%rsp), %rdx
movl 0x10(%rdx), %edx
imull %edx, %ecx
movslq %ecx, %rcx
leaq (%rcx,%r13), %rdi
addq $0x18, %rdi
movl %ebx, %ecx
subl %r12d, %ecx
imull %edx, %ecx
movslq %ecx, %rcx
leaq (%rcx,%r13), %rsi
addq $0x18, %rsi
subl %ebx, %eax
imull %edx, %eax
movslq %eax, %rdx
callq 0x8220
movl %ebx, %eax
movl (%r13), %ecx
addl %ecx, %r12d
subl %eax, %ebp
addl %ecx, %ebp
movl %ebp, (%r13)
movq 0x8(%r13), %r13
cmpl %r14d, %r12d
jle 0x871a
movq 0x10(%rsp), %r13
movq (%r13), %rdi
testq %rdi, %rdi
je 0x87be
movq %r13, %rbx
cmpl $0x0, (%rdi)
je 0x87aa
addq $0x8, %rdi
movq %rdi, %rbx
jmp 0x87b6
movq 0x8(%rdi), %r12
callq 0x81c0
movq %r12, (%rbx)
movq (%rbx), %rdi
testq %rdi, %rdi
jne 0x879c
cmpq $0x0, (%r13)
movq 0x8(%rsp), %rbx
jne 0x87f5
movslq 0x10(%rbx), %rdi
addq $0x18, %rdi
callq 0x81d0
testq %rax, %rax
je 0x8816
movl $0x0, (%rax)
movl $0x1, 0x10(%rax)
movq $0x0, 0x8(%rax)
movq %rax, (%r13)
notl %r14d
addl %r15d, %r14d
addl %r14d, (%rbx)
xorl %eax, %eax
jmp 0x8807
movl $0x3, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, (%r13)
movl $0x1, %eax
jmp 0x8807