// File: /workspace/github_c_makefile_star2_O1/seojangho[P]clee/simclist.c
// Function: list_sort_quicksort
static void list_sort_quicksort(list_t *restrict l, int versus, 
        unsigned int first, struct list_entry_s *fel,
        unsigned int last, struct list_entry_s *lel) {
    unsigned int pivotid;
    unsigned int i;
    register struct list_entry_s *pivot;
    struct list_entry_s *left, *right;
    void *tmpdata;
#ifdef SIMCLIST_WITH_THREADS
    pthread_t tid;
    int traised;
#endif


    if (last <= first)      /* <= 1-element lists are always sorted */
        return;
    
    if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
        list_sort_selectionsort(l, versus, first, fel, last, lel);
        return;
    }
    
    /* base of iteration: one element list */
    if (! (last > first)) return;

    pivotid = (get_random() % (last - first + 1));
    /* pivotid = (last - first + 1) / 2; */

    /* find pivot */
    if (pivotid < (last - first + 1)/2) {
        for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
    } else {
        for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
    }

    /* smaller PIVOT bigger */
    left = fel;
    right = lel;
    /* iterate     --- left ---> PIV <--- right --- */
    while (left != pivot && right != pivot) {
        for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
        /* left points to a smaller element, or to pivot */
        for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
        /* right points to a bigger element, or to pivot */
        if (left != pivot && right != pivot) {
            /* swap, then move iterators */
            tmpdata = left->data;
            left->data = right->data;
            right->data = tmpdata;

            left = left->next;
            right = right->prev;
        }
    }

    /* now either left points to pivot (end run), or right */
    if (right == pivot) {    /* left part longer */
        while (left != pivot) {
            if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
                tmpdata = left->data;
                left->data = pivot->prev->data;
                pivot->prev->data = pivot->data;
                pivot->data = tmpdata;
                pivot = pivot->prev;
                pivotid--;
                if (pivot == left) break;
            } else {
                left = left->next;
            }
        }
    } else {                /* right part longer */
        while (right != pivot) {
            if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
                /* move current right before pivot */
                tmpdata = right->data;
                right->data = pivot->next->data;
                pivot->next->data = pivot->data;
                pivot->data = tmpdata;
                pivot = pivot->next;
                pivotid++;
                if (pivot == right) break;
            } else {
                right = right->prev;
            }
        }
    }

    /* sort sublists A and B :       |---A---| pivot |---B---| */

#ifdef SIMCLIST_WITH_THREADS
    traised = 0;
    if (pivotid > 0) {
        /* prepare wrapped args, then start thread */
        if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
            struct list_sort_wrappedparams *wp = (struct list_sort_wrappedparams *)malloc(sizeof(struct list_sort_wrappedparams));
            l->threadcount++;
            traised = 1;
            wp->l = l;
            wp->versus = versus;
            wp->first = first;
            wp->fel = fel;
            wp->last = first+pivotid-1;
            wp->lel = pivot->prev;
            if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
                free(wp);
                traised = 0;
                list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
            }
        } else {
            list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
        }
    }
    if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
    if (traised) {
        pthread_join(tid, (void **)NULL);
        l->threadcount--;
    }
#else
    if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
    if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %r12
movq %rcx, %r15
movl %esi, %ebp
movq %r9, 0x28(%rsp)
movq %rdi, 0x8(%rsp)
movl %r8d, 0x24(%rsp)
movl %r8d, %ebx
subl %edx, %ebx
jbe 0x4b5a
leal 0x1(%rbx), %r14d
cmpl $0x18, %r14d
jbe 0x4af2
movq %rdx, 0x30(%rsp)
callq 0x31e0
xorl %edx, %edx
divl %r14d
shrl %r14d
cmpl %r14d, %edx
movq %r15, 0x18(%rsp)
jae 0x4930
testl %edx, %edx
movq 0x8(%rsp), %r14
je 0x4946
xorl %eax, %eax
movq 0x18(%rsp), %r15
movq 0x8(%r15), %r15
incl %eax
cmpl %edx, %eax
jb 0x4924
jmp 0x4946
movq %r12, %r15
cmpl %edx, %ebx
movq 0x8(%rsp), %r14
jbe 0x4946
movq 0x10(%r15), %r15
decl %ebx
cmpl %edx, %ebx
ja 0x493c
movq %rdx, 0x10(%rsp)
movq 0x18(%rsp), %rdx
cmpq %rdx, %r15
sete %al
cmpq %r12, %r15
sete %cl
orb %al, %cl
movq %r12, %rbx
movq %rdx, %r12
jne 0x4a04
movq 0x28(%rsp), %rbx
movq 0x18(%rsp), %r12
movq (%r12), %rdi
movq (%r15), %rsi
callq *0x40(%r14)
imull %ebp, %eax
cmpl $0x80000000, %eax       # imm = 0x80000000
ja 0x49ae
movq 0x8(%r12), %r12
cmpq %r15, %r12
sete %r13b
je 0x49b7
movq (%r12), %rdi
movq (%r15), %rsi
callq *0x40(%r14)
imull %ebp, %eax
cmpl $0x80000000, %eax       # imm = 0x80000000
jbe 0x4989
jmp 0x49b7
xorl %r13d, %r13d
jmp 0x49b7
movq 0x10(%rbx), %rbx
cmpq %r15, %rbx
sete %r14b
je 0x49d5
movq (%rbx), %rdi
movq (%r15), %rsi
movq 0x8(%rsp), %rax
callq *0x40(%rax)
imull %ebp, %eax
decl %eax
js 0x49b3
orb %r14b, %r13b
jne 0x49f1
movq (%r12), %rax
movq (%rbx), %rcx
movq %rcx, (%r12)
movq %rax, (%rbx)
movq 0x8(%r12), %r12
movq 0x10(%rbx), %rbx
movq 0x8(%rsp), %r14
cmpq %r15, %r12
je 0x4a04
cmpq %r15, %rbx
jne 0x4974
cmpq %r15, %rbx
je 0x4aa7
movq (%rbx), %rdi
movq (%r15), %rsi
callq *0x40(%r14)
imull %ebp, %eax
decl %eax
js 0x4a4a
movq (%rbx), %rax
movq 0x8(%r15), %rcx
movq (%rcx), %rdx
movq %rdx, (%rbx)
movq (%r15), %rdx
movq %rdx, (%rcx)
movq %rax, (%r15)
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq %rcx, %r15
cmpq %rbx, %rcx
jne 0x4a4e
jmp 0x4a53
movq 0x10(%rbx), %rbx
cmpq %r15, %rbx
jne 0x4a0d
movq 0x10(%rsp), %rdx
testl %edx, %edx
movq 0x30(%rsp), %rbx
je 0x4a82
leal (%rbx,%rdx), %r8d
decl %r8d
movq 0x10(%r15), %r9
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
movq 0x18(%rsp), %rcx
callq 0x48ba
movq 0x10(%rsp), %rdx
addl %ebx, %edx
movl 0x24(%rsp), %r8d
cmpl %r8d, %edx
movq 0x28(%rsp), %r12
jae 0x4b5a
incl %edx
movq 0x8(%r15), %r15
jmp 0x48df
movq 0x8(%r12), %r12
cmpq %r15, %r12
je 0x4a53
movq (%r12), %rdi
movq (%r15), %rsi
callq *0x40(%r14)
imull %ebp, %eax
cmpl $0x80000001, %eax       # imm = 0x80000001
jb 0x4aa2
movq (%r12), %rax
movq 0x10(%r15), %rcx
movq (%rcx), %rdx
movq %rdx, (%r12)
movq (%r15), %rdx
movq %rdx, (%rcx)
movq %rax, (%r15)
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
movq %rcx, %r15
cmpq %r12, %rcx
jne 0x4aa7
jmp 0x4a53
cmpl %edx, %r8d
ja 0x4b55
jmp 0x4b5a
movq 0x8(%r15), %rbx
movq %r15, %r13
movq %r15, %rcx
cmpq 0x8(%r12), %rbx
je 0x4b3d
movq 0x8(%rsp), %rax
movq 0x40(%rax), %r14
movq %r13, %r15
movq (%r15), %rdi
movq (%rbx), %rsi
callq *%r14
imull %ebp, %eax
movq %rbx, %rcx
cmpl $0x80000001, %eax       # imm = 0x80000001
jae 0x4b2f
movq %r15, %rcx
movq 0x8(%rbx), %rbx
movq %rcx, %r15
cmpq 0x8(%r12), %rbx
jne 0x4b16
movq %r13, %r15
cmpq %r13, %rcx
je 0x4b51
movq (%r15), %rax
movq (%rcx), %rdx
movq %rdx, (%r15)
movq %rax, (%rcx)
movq 0x8(%r15), %r15
cmpq %r12, %r15
jne 0x4af9
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq