// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rdi, %rbx
leaq 0xd0(%rsp), %rdi
movq 0x18(%rdi), %rax
cmpq $0x2, %rax
jb 0x292c
movq %rdx, %r14
movl %esi, %ebp
movq %rax, %rcx
shrq %rcx
movq 0x8(%rdi), %rdx
movq %rdx, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
cmpq $0x4, %rax
movq %rbx, 0x28(%rsp)
jb 0x2773
leaq -0x1(%rcx), %rsi
movq 0x10(%rdx), %rdx
decq %rsi
jne 0x276a
leaq 0x30(%rsp), %r13
movq %rdx, 0x10(%r13)
movq 0x10(%rdx), %rsi
leaq 0x50(%rsp), %rbx
movq %rsi, 0x8(%rbx)
movq 0x10(%rdi), %rdi
movq %rdi, 0x10(%rbx)
xorl %edi, %edi
movq %rdi, 0x18(%rsi)
movq %rdi, 0x10(%rdx)
movq %rcx, 0x18(%r13)
subq %rcx, %rax
movq %rax, 0x18(%rbx)
movaps (%rbx), %xmm0
movaps 0x10(%rbx), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movsbl %bpl, %r15d
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x2718
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movaps %xmm1, 0x10(%rbx)
movaps %xmm0, (%rbx)
movaps (%r13), %xmm0
movaps 0x10(%r13), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x2718
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movq %r13, %rax
movaps %xmm1, 0x10(%r13)
movaps %xmm0, (%r13)
movq 0x8(%rbx), %r13
movq 0x8(%r13), %rdi
movq 0x8(%rax), %r12
movq 0x8(%r12), %rsi
callq *%r14
cmpb %r15b, %al
cmoveq %r13, %r12
leaq 0x30(%rsp), %rdx
movq %rdx, %rax
cmoveq %rbx, %rax
leaq 0xd0(%rsp), %rcx
movq %r12, 0x8(%rcx)
movq 0x10(%r12), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rbx), %r15
movq 0x8(%rdx), %r13
testq %r15, %r15
setne %al
testq %r13, %r13
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x28b6
movq 0x28(%rsp), %rbx
movq 0x8(%r15), %rdi
movq 0x8(%r13), %rsi
callq *%r14
cmpb %bpl, %al
jne 0x2898
movq %r15, 0x10(%r12)
movq %r12, 0x18(%r15)
movq 0x10(%r15), %r15
jmp 0x28a5
movq %r13, 0x10(%r12)
movq %r12, 0x18(%r13)
movq 0x10(%r13), %r13
movq 0x10(%r12), %r12
testq %r15, %r15
je 0x28bb
testq %r13, %r13
jne 0x2879
jmp 0x28bb
movq 0x28(%rsp), %rbx
movq %r15, 0x58(%rsp)
movq %r13, 0x38(%rsp)
testq %r15, %r15
je 0x28e4
movq %r15, 0x10(%r12)
movq %r12, 0x18(%r15)
movq 0x10(%r15), %rax
movq %r15, %r12
movq %rax, %r15
testq %rax, %rax
jne 0x28ca
jmp 0x28e7
movq %r15, %rax
movq %rax, 0x58(%rsp)
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x2918
leaq 0xd0(%rsp), %rdi
movq %rax, %rcx
movq %rax, 0x10(%r12)
movq %r12, 0x18(%rax)
movq 0x10(%rax), %rax
movq %rcx, %r12
testq %rax, %rax
jne 0x28fe
jmp 0x2923
movq %r12, %rcx
leaq 0xd0(%rsp), %rdi
movq %rax, 0x38(%rsp)
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 $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq