// File: /workspace/github_c_makefile_star2_O3/theck01[P]offbrand_lib/src/classes/obdeque.c
// Function: obdeque_recursive_sort
obdeque obdeque_recursive_sort(obdeque deque, int8_t order, ob_compare_fptr funct){

  uint64_t i, half_length;
  obdeque left_sorted, right_sorted;
  obdeque_node *curnode;

  /* base case, a deque of size 0 or 1 is sorted */
  if(deque.length < 2) return deque;

  half_length = deque.length/2;

  left_sorted.head = deque.head;
  left_sorted.tail = deque.head;

  /* iterate through half of the deque to split it in half */
  for(i=1; i<half_length; i++) left_sorted.tail = left_sorted.tail->next;

  right_sorted.head = left_sorted.tail->next;
  right_sorted.tail = deque.tail;

  /* separate right and left deques */
  right_sorted.head->prev = NULL;
  left_sorted.tail->next = NULL;

  left_sorted.length = half_length;
  right_sorted.length = deque.length - half_length;

  /* sort deque halves recursively */
  right_sorted = obdeque_recursive_sort(right_sorted, order, funct);
  left_sorted = obdeque_recursive_sort(left_sorted, order, funct);

  /* set the head of the combined list appropriately */
  if(funct(right_sorted.head->stored, left_sorted.head->stored) == order){
    deque.head = right_sorted.head;
    right_sorted.head = right_sorted.head->next;
  }
  else{
    deque.head = left_sorted.head;
    left_sorted.head = left_sorted.head->next;
  }

  curnode = deque.head;

  /* merge lists with proper sequencing */
  while(right_sorted.head && left_sorted.head){

    if(funct(right_sorted.head->stored, left_sorted.head->stored)
        == order){
      curnode->next = right_sorted.head;
      right_sorted.head->prev = curnode;
      right_sorted.head = right_sorted.head->next;
    }
    else{
      curnode->next = left_sorted.head;
      left_sorted.head->prev = curnode;
      left_sorted.head = left_sorted.head->next;
    }

    curnode = curnode->next;
  }

  /* merge in remenants of lists, after one has been depleted */
  while(right_sorted.head){
    curnode->next = right_sorted.head;
    right_sorted.head->prev = curnode;
    right_sorted.head = right_sorted.head->next;
    curnode = curnode->next;
  }

  while(left_sorted.head){
    curnode->next = left_sorted.head;
    left_sorted.head->prev = curnode;
    left_sorted.head = left_sorted.head->next;
    curnode = curnode->next;
  }

  /* curnode should be the last node in the list, set appropriately */
  deque.tail = curnode;

  return deque;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
leaq 0xc0(%rsp), %rdi
movq 0x18(%rdi), %rax
cmpq $0x2, %rax
jb 0x295e
movq %rdx, %r14
movl %esi, %ebp
movq %rax, %rcx
shrq %rcx
movq 0x8(%rdi), %rdx
movq %rdx, 0x48(%rsp)
cmpq $0x4, %rax
movq %rbx, 0x60(%rsp)
jb 0x27d9
leaq -0x1(%rcx), %rsi
movq 0x10(%rdx), %rdx
decq %rsi
jne 0x27d0
movq %rdx, 0x50(%rsp)
movq 0x10(%rdx), %rsi
movq %rsi, 0x28(%rsp)
movq 0x10(%rdi), %rdi
movq %rdi, 0x30(%rsp)
xorl %edi, %edi
movq %rdi, 0x18(%rsi)
movq %rdi, 0x10(%rdx)
movq %rcx, 0x58(%rsp)
subq %rcx, %rax
movq %rax, 0x38(%rsp)
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movsbl %bpl, %r15d
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x2783
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps 0x40(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x2783
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x28(%rsp), %r12
movq 0x8(%r12), %rdi
movq 0x48(%rsp), %rbx
movq 0x8(%rbx), %rsi
callq *%r14
cmpb %r15b, %al
jne 0x28a6
movq 0x10(%r12), %r13
movq %rbx, %r15
jmp 0x28b0
movq 0x10(%rbx), %r15
movq %r12, %r13
movq %rbx, %r12
leaq 0xc0(%rsp), %rax
movq %r12, 0x8(%rax)
testq %r13, %r13
setne %al
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
movq 0x60(%rsp), %rbx
jne 0x290f
movq 0x8(%r13), %rdi
movq 0x8(%r15), %rsi
callq *%r14
cmpb %bpl, %al
jne 0x28f3
movq %r13, 0x10(%r12)
movq %r12, 0x18(%r13)
movq 0x10(%r13), %r13
jmp 0x2900
movq %r15, 0x10(%r12)
movq %r12, 0x18(%r15)
movq 0x10(%r15), %r15
movq 0x10(%r12), %r12
testq %r13, %r13
je 0x290f
testq %r15, %r15
jne 0x28d4
testq %r13, %r13
je 0x292e
movq %r13, %rax
movq %r13, 0x10(%r12)
movq %r12, 0x18(%r13)
movq 0x10(%r13), %r13
movq %rax, %r12
testq %r13, %r13
jne 0x2914
jmp 0x2931
movq %r12, %rax
testq %r15, %r15
je 0x294f
movq %r15, %rcx
movq %r15, 0x10(%rax)
movq %rax, 0x18(%r15)
movq 0x10(%r15), %r15
movq %rcx, %rax
testq %r15, %r15
jne 0x2936
jmp 0x2952
movq %rax, %rcx
leaq 0xc0(%rsp), %rdi
movq %rcx, 0x10(%rdi)
movups (%rdi), %xmm0
movq 0x10(%rdi), %rax
movq %rax, 0x10(%rbx)
movq 0x18(%rdi), %rax
movq %rax, 0x18(%rbx)
movups %xmm0, (%rbx)
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq