// File: /workspace/github_c_makefile_star2_O2/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 0x2634
movq %rdx, %r14
movl %esi, %ebp
movq %rax, %rcx
shrq %rcx
movq 0x8(%rdi), %rdx
movq %rdx, 0x48(%rsp)
leaq -0x1(%rcx), %rsi
subq $0x1, %rsi
jb 0x24cc
movq 0x10(%rdx), %rdx
jmp 0x24c0
movq %rbx, 0x60(%rsp)
movq %rdx, 0x50(%rsp)
movq 0x10(%rdx), %rsi
movq %rsi, 0x28(%rsp)
movq 0x10(%rdi), %rdi
movq %rdi, 0x30(%rsp)
andq $0x0, 0x18(%rsi)
andq $0x0, 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 0x247e
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 0x247e
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 0x259e
movq 0x10(%r12), %r13
movq %rbx, %r15
jmp 0x25a8
movq 0x10(%rbx), %r15
movq %r12, %r13
movq %rbx, %r12
leaq 0xc0(%rsp), %rax
movq %r12, 0x8(%rax)
movq 0x60(%rsp), %rbx
testq %r13, %r13
je 0x2609
testq %r15, %r15
je 0x2609
movq 0x8(%r13), %rdi
movq 0x8(%r15), %rsi
callq *%r14
cmpb %bpl, %al
jne 0x25e2
movq %r13, 0x10(%r12)
movq %r12, 0x18(%r13)
movq 0x10(%r13), %r13
jmp 0x25ef
movq %r15, 0x10(%r12)
movq %r12, 0x18(%r15)
movq 0x10(%r15), %r15
movq 0x10(%r12), %r12
jmp 0x25b9
movq %r13, %rax
movq %r13, 0x10(%r12)
movq %r12, 0x18(%r13)
movq 0x10(%r13), %r13
movq %rax, %r12
testq %r13, %r13
jne 0x25f6
jmp 0x2623
movq %r15, %rax
movq %r15, 0x10(%r12)
movq %r12, 0x18(%r15)
movq 0x10(%r15), %r15
movq %rax, %r12
testq %r15, %r15
jne 0x2610
leaq 0xc0(%rsp), %rdi
movq %r12, 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