// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %r9, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movl %r8d, 0x1c(%rsp)
movl %r8d, %r15d
subl %edx, %r15d
jbe 0x476b
movq %rcx, 0x8(%rsp)
leal 0x1(%r15), %r12d
cmpl $0x18, %r12d
jbe 0x4702
movq %rdx, 0x20(%rsp)
callq 0x31e0
xorl %edx, %edx
divl %r12d
movl %edx, %r13d
shrl %r12d
movq 0x10(%rsp), %rbx
cmpl %r12d, %edx
movq 0x8(%rsp), %rcx
jae 0x45b0
xorl %eax, %eax
movq %rcx, %rbx
cmpl %r13d, %eax
jae 0x45be
movq 0x8(%rbx), %rbx
incl %eax
jmp 0x45a3
cmpl %r13d, %r15d
jbe 0x45be
movq 0x10(%rbx), %rbx
decl %r15d
jmp 0x45b0
movq %rcx, %r15
movq 0x10(%rsp), %r12
cmpq %rbx, %r15
je 0x4629
cmpq %rbx, %r12
je 0x4629
cmpq %rbx, %r15
je 0x45f4
movq (%r15), %rdi
movq (%rbx), %rsi
callq *0x40(%r14)
imull %ebp, %eax
cmpl $0x80000001, %eax       # imm = 0x80000001
jae 0x45f4
movq 0x8(%r15), %r15
jmp 0x45d0
movq 0x10(%r12), %r12
cmpq %rbx, %r12
je 0x45c6
movq (%r12), %rdi
movq (%rbx), %rsi
callq *0x40(%r14)
imull %ebp, %eax
decl %eax
js 0x45ef
cmpq %rbx, %r15
je 0x45c6
movq (%r15), %rax
movq (%r12), %rcx
movq %rcx, (%r15)
movq %rax, (%r12)
movq 0x8(%r15), %r15
movq 0x10(%r12), %r12
jmp 0x45c6
cmpq %rbx, %r12
je 0x4675
cmpq %rbx, %r12
je 0x46b7
movq (%r12), %rdi
movq (%rbx), %rsi
callq *0x40(%r14)
imull %ebp, %eax
decl %eax
jns 0x4650
movq 0x10(%r12), %r12
jmp 0x462e
movq (%r12), %rax
movq 0x8(%rbx), %rdx
movq (%rdx), %rcx
movq %rcx, (%r12)
movq (%rbx), %rcx
movq %rcx, (%rdx)
movq %rax, (%rbx)
incl %r13d
movq %rdx, %rbx
cmpq %r12, %rdx
jne 0x462e
jmp 0x46ba
cmpq %rbx, %r15
je 0x46b7
movq (%r15), %rdi
movq (%rbx), %rsi
callq *0x40(%r14)
imull %ebp, %eax
cmpl $0x80000001, %eax       # imm = 0x80000001
jae 0x4694
movq 0x8(%r15), %r15
jmp 0x4675
movq (%r15), %rax
movq 0x10(%rbx), %rdx
movq (%rdx), %rcx
movq %rcx, (%r15)
movq (%rbx), %rcx
movq %rcx, (%rdx)
movq %rax, (%rbx)
decl %r13d
movq %rdx, %rbx
cmpq %r15, %rdx
jne 0x4675
jmp 0x46ba
movq %rbx, %rdx
testl %r13d, %r13d
movq 0x20(%rsp), %rbx
je 0x46e6
leal (%rbx,%r13), %r8d
decl %r8d
movq 0x10(%rdx), %r9
movq %r14, %rdi
movl %ebp, %esi
movq %rdx, %r15
movl %ebx, %edx
movq 0x8(%rsp), %rcx
callq 0x453e
movq %r15, %rdx
addl %ebx, %r13d
movl 0x1c(%rsp), %r8d
cmpl %r8d, %r13d
jae 0x476b
incl %r13d
movq 0x8(%rdx), %rcx
movl %r13d, %edx
jmp 0x455b
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %r15
cmpq %r13, %r15
je 0x476b
leaq 0x8(%r15), %rbx
movq %r15, 0x8(%rsp)
movq %rbx, %r12
movq 0x10(%rsp), %r13
movq (%r12), %r12
cmpq 0x8(%r13), %r12
je 0x4750
movq (%r15), %rdi
movq (%r12), %rsi
callq *0x40(%r14)
imull %ebp, %eax
movq %r12, %rcx
cmpl $0x80000001, %eax       # imm = 0x80000001
jae 0x4747
movq %r15, %rcx
addq $0x8, %r12
movq %rcx, %r15
jmp 0x4722
movq 0x8(%rsp), %rdx
cmpq %rdx, %r15
je 0x4766
movq (%rdx), %rax
movq (%r15), %rcx
movq %rcx, (%rdx)
movq %rax, (%r15)
movq (%rbx), %r15
jmp 0x470c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq