// File: /workspace/github_c_makefile_star2_O1/jsh58[P]Genrich/Genrich.c
// Function: johnPartition
uint32_t johnPartition(uint16_t* qual, uint32_t* order,
    uint32_t low, uint32_t high, uint16_t* qual0,
    uint16_t* qual1, uint16_t* qual2, uint32_t* order0,
    uint32_t* order1, uint32_t* order2,
    uint32_t* idxHigh) {

  // separate qual values into temp arrays --
  //   qual0 for higher values, qual1 equal, qual2 lower
  uint16_t pivot = qual[high - 1];  // pivot value: last elt
  uint32_t idx0 = 0, idx1 = 0, idx2 = 0; // indexes into temp arrays
  for (uint32_t j = low; j < high; j++) {
    if (qual[j] > pivot) {
      qual0[idx0] = qual[j];
      order0[idx0] = order[j];
      idx0++;
    } else if (qual[j] == pivot) {
      qual1[idx1] = qual[j];
      order1[idx1] = order[j];
      idx1++;
    } else {
      qual2[idx2] = qual[j];
      order2[idx2] = order[j];
      idx2++;
    }
  }

  if (! idx0 && ! idx2)
    return 0; // all equal values, no need to shuffle

  // recombine temp arrays back into qual/order
  uint32_t i = 0;
  bool val0 = (bool) idx0, val1 = true;
  for (uint32_t j = low; j < high; j++) {
    if (val0) {
      qual[j] = qual0[i];
      order[j] = order0[i];
      if (++i == idx0) {
        val0 = false;
        i = 0;
      }
    } else if (val1) {
      qual[j] = qual1[i];
      order[j] = order1[i];
      if (++i == idx1) {
        val1 = false;
        i = 0;
      }
    } else {
      qual[j] = qual2[i];
      order[j] = order2[i];
      i++;
    }
  }

  // return low/high indexes
  *idxHigh = low + idx0 + idx1;
  return low + idx0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorl %eax, %eax
movl $0x0, %ebp
movl $0x0, %r15d
movl $0x0, %r10d
cmpl %ecx, %edx
movl %edx, -0x4(%rsp)
jae 0xa24c
leal -0x1(%rcx), %r10d
movzwl (%rdi,%r10,2), %r10d
movl %edx, %r11d
movl %ecx, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x50(%rsp), %rax
movzwl (%rdi,%r11,2), %r14d
cmpw %r10w, %r14w
jbe 0xa203
movl %ebp, %ebx
movw %r14w, (%r8,%rbx,2)
movl (%rsi,%r11,4), %r14d
movq 0x40(%rsp), %rdx
movl %r14d, (%rdx,%rbx,4)
incl %ebp
jmp 0xa237
jne 0xa21f
movl %r15d, %ebx
movw %r14w, (%r9,%rbx,2)
movl (%rsi,%r11,4), %r14d
movq 0x48(%rsp), %rdx
movl %r14d, (%rdx,%rbx,4)
incl %r15d
jmp 0xa237
movl %r13d, %ebx
movq 0x38(%rsp), %rdx
movw %r14w, (%rdx,%rbx,2)
movl (%rsi,%r11,4), %r14d
movl %r14d, (%rax,%rbx,4)
incl %r13d
incq %r11
cmpq %r11, %r12
jne 0xa1e0
testl %r13d, %r13d
setne %r10b
movl -0x4(%rsp), %edx
xorl %eax, %eax
testl %ebp, %ebp
setne %r11b
orb %r10b, %r11b
testb $0x1, %r11b
je 0xa31a
cmpl %ecx, %edx
movq 0x50(%rsp), %rdx
jae 0xa309
testl %ebp, %ebp
setne %r12b
movl -0x4(%rsp), %r13d
movl %ecx, %ecx
xorl %eax, %eax
movb $0x1, %r10b
xorl %r11d, %r11d
movl %r11d, %r14d
testb $0x1, %r12b
je 0xa2b5
movzwl (%r8,%r14,2), %ebx
movw %bx, (%rdi,%r13,2)
movq 0x40(%rsp), %rbx
movl (%rbx,%r14,4), %ebx
incl %r11d
cmpl %ebp, %r11d
cmovel %eax, %r11d
movl %ebx, (%rsi,%r13,4)
movzbl %r12b, %r12d
cmovel %eax, %r12d
jmp 0xa2fd
incl %r11d
testb $0x1, %r10b
je 0xa2e6
movzwl (%r9,%r14,2), %ebx
movw %bx, (%rdi,%r13,2)
movq 0x48(%rsp), %rbx
movl (%rbx,%r14,4), %ebx
cmpl %r15d, %r11d
cmovel %eax, %r11d
movl %ebx, (%rsi,%r13,4)
movzbl %r10b, %r10d
cmovel %eax, %r10d
jmp 0xa2fd
movq 0x38(%rsp), %rbx
movzwl (%rbx,%r14,2), %ebx
movw %bx, (%rdi,%r13,2)
movl (%rdx,%r14,4), %ebx
movl %ebx, (%rsi,%r13,4)
incq %r13
cmpq %r13, %rcx
jne 0xa281
addl -0x4(%rsp), %ebp
addl %ebp, %r15d
movq 0x58(%rsp), %rax
movl %r15d, (%rax)
movl %ebp, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq