// File: /workspace/github_c_makefile_star2_O3/thegenemyers[P]DAMASKER/tandem.c
// Function: lex_sort
static Double *lex_sort(int bytes[16], Double *src, Double *trg, Lex_Arg *parmx)
{ THREAD  threads[NTHREADS];

  int64   len, x, y;
  Double *xch;
  int     i, j, k, z;
  int     b, c, fb;

  len       = parmx[NTHREADS-1].end;
  LEX_zsize = (len-1)/NTHREADS + 1;
  LEX_src   = src;
  LEX_trg   = trg;
  QMASK     = (BMASK << NSHIFT);

  for (c = 0; c < 16; c++)
    if (bytes[c])
      break;
  fb = c;
  for (b = c; b < 16; b = c)
    { for (c = b+1; c < 16; c++)
        if (bytes[c])
          break;
      LEX_last  = (c >= 16);
      LEX_shift = (b << 3);
      LEX_next  = (c << 3);
 
      if (b == fb)
        { for (i = 0; i < NTHREADS; i++)
            for (z = 0; z < NTHREADS*BPOWR; z++)
              parmx[i].sptr[z] = 0;
        }
      else
        { x = 0;
          for (i = 0; i < NTHREADS; i++)
            { parmx[i].beg = x;
              x = LEX_zsize*(i+1);
              if (x > len)
                x = len;
              parmx[i].end = x;
              for (j = 0; j < BPOWR; j++)
                parmx[i].tptr[j] = 0;
            }
          parmx[NTHREADS-1].end = len;

          for (j = 0; j < BPOWR; j++)
            { k = (j << NSHIFT);
              for (z = 0; z < NTHREADS; z++)
                for (i = 0; i < NTHREADS; i++)
                  { parmx[i].tptr[j] += parmx[z].sptr[k+i];
                    parmx[z].sptr[k+i] = 0;
                  }
            }
        }

      x = 0;
      for (j = 0; j < BPOWR; j++)
        for (i = 0; i < NTHREADS; i++)
          { y = parmx[i].tptr[j];
            parmx[i].tptr[j] = x;
            x += y;
          }

      for (i = 0; i < NTHREADS; i++)
        pthread_create(threads+i,NULL,lex_thread,parmx+i);

      for (i = 0; i < NTHREADS; i++)
        pthread_join(threads[i],NULL);

      xch     = LEX_src;
      LEX_src = LEX_trg;
      LEX_trg = xch;

#ifdef TEST_LSORT
      printf("\nLSORT %d\n",LEX_shift);
      if (LEX_shift >= 64)
        { x = (1 << ((LEX_shift-64)+BSHIFT))-1;
          for (i = 0; i < len; i++)
            { printf("%6d: %8llx %8llx %8llx %8llx : %4llx",
                     i,LEX_src[i].p2>>32,(LEX_src[i].p2)&0xffffffffll,LEX_src[i].p1>>32,
                     LEX_src[i].p1&0xffffffffll,LEX_src[i].p2&x);
              if (i > 0 && (LEX_src[i].p1 < LEX_src[i].p1 ||
                             (LEX_src[i].p1 == LEX_src[i].p1 && 
                             (LEX_src[i].p2 & x) < (LEX_src[i-1].p2 & x))))
                printf(" OO");
              printf("\n");
            }
        }
      else
        { x = (1 << (LEX_shift+BSHIFT))-1;
          for (i = 0; i < len; i++)
            { printf("%6d: %8llx %8llx %8llx %8llx : %4llx",
                     i,LEX_src[i].p2>>32,(LEX_src[i].p2)&0xffffffffll,LEX_src[i].p1>>32,
                     LEX_src[i].p1&0xffffffffll,LEX_src[i].p1&x);
              if (i > 0 && (LEX_src[i].p1 & x) < (LEX_src[i-1].p1 & x))
                printf(" OO");
              printf("\n");
            }
        }
#endif
    }

  return (LEX_src);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbx
movq %rdx, %r8
movslq 0x1f04a(%rip), %r9      # 0x241f8
movl %r9d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
imulq $0x818, %r9, %rax       # imm = 0x818
movq -0x810(%rbx,%rax), %rax
movq %rax, -0x48(%rbp)
decq %rax
cqto
idivq %r9
incq %rax
movq %rax, 0x1f041(%rip)     # 0x24230
movb 0x1f007(%rip), %cl      # 0x241fc
movl $0xff, %eax
shlq %cl, %rax
movq %rsi, 0x1f034(%rip)     # 0x24238
movq %r8, 0x1f035(%rip)      # 0x24240
movq %rax, 0x1f036(%rip)     # 0x24248
xorl %r8d, %r8d
cmpl $0x0, (%rdi,%r8,4)
jne 0x522a
incq %r8
cmpq $0x10, %r8
jne 0x5215
jmp 0x54e3
cmpq $0xf, %r8
ja 0x54e3
leaq -0x810(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq 0x10(%rbx), %r10
movl %r8d, %ecx
movq %r10, -0x30(%rbp)
movq %rdi, -0x60(%rbp)
movq %r8, -0x58(%rbp)
movl %ecx, %eax
movslq %ecx, %rcx
cmpl $0x10, %ecx
movl $0xf, %r11d
cmovgel %ecx, %r11d
leal 0x1(%r11), %esi
cmpq $0xe, %rcx
jg 0x5282
leaq 0x1(%rcx), %rdx
cmpl $0x0, 0x4(%rdi,%rcx,4)
movq %rdx, %rcx
je 0x5268
leal -0x1(%rdx), %r11d
movl %edx, %esi
xorl %ecx, %ecx
movq %r11, -0x68(%rbp)
cmpl $0xf, %r11d
setge %cl
movl %ecx, 0x1efbb(%rip)     # 0x24250
leal (,%rax,8), %ecx
movl %ecx, 0x1efb2(%rip)     # 0x24254
movq %rsi, -0x70(%rbp)
leal (,%rsi,8), %ecx
movl %ecx, 0x1efa5(%rip)     # 0x24258
movl %r9d, %r13d
cmpl %r8d, %eax
movq %r9, -0x38(%rbp)
jne 0x530e
testl %r9d, %r9d
jle 0x53f4
movq -0x38(%rbp), %r12
shll $0x8, %r12d
cmpl $0x2, %r12d
movl $0x1, %eax
cmovll %eax, %r12d
shlq $0x3, %r12
imulq $0x818, %r13, %r14      # imm = 0x818
xorl %r15d, %r15d
movq 0x810(%rbx,%r15), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2180
addq $0x818, %r15            # imm = 0x818
cmpq %r15, %r14
jne 0x52eb
jmp 0x53f4
testl %r9d, %r9d
movq -0x48(%rbp), %rcx
jle 0x5370
movq 0x1ef12(%rip), %r12     # 0x24230
imulq $0x818, %r13, %rax      # imm = 0x818
movq %rax, -0x78(%rbp)
movq %r12, -0x80(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
leaq (%r10,%r15), %rdi
movq %r14, -0x10(%r10,%r15)
cmpq %rcx, %r12
movq %rcx, %r14
cmovlq %r12, %r14
movq %r14, -0x8(%r10,%r15)
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x2180
movq -0x30(%rbp), %r10
movq -0x48(%rbp), %rcx
addq $0x818, %r15            # imm = 0x818
addq -0x80(%rbp), %r12
cmpq %r15, -0x78(%rbp)
jne 0x5333
movslq -0x38(%rbp), %rax
imulq $0x818, %rax, %rax      # imm = 0x818
movq -0x50(%rbp), %rdx
movq %rcx, (%rdx,%rax)
movl 0x1ee73(%rip), %ecx     # 0x241fc
imulq $0x103, %r13, %rax      # imm = 0x103
movq %r10, %rdx
xorl %esi, %esi
cmpl $0x0, -0x38(%rbp)
jle 0x53e4
movl %esi, %edi
shll %cl, %edi
movslq %edi, %rdi
shlq $0x3, %rdi
xorl %r8d, %r8d
imulq $0x818, %r8, %r9        # imm = 0x818
movq 0x810(%rbx,%r9), %r9
addq %rdi, %r9
xorl %r10d, %r10d
movq (%r9), %r11
addq %r11, (%rdx,%r10,8)
movq $0x0, (%r9)
addq $0x8, %r9
addq $0x103, %r10            # imm = 0x103
cmpq %r10, %rax
jne 0x53be
incq %r8
cmpq %r13, %r8
jne 0x53a9
incq %rsi
addq $0x8, %rdx
cmpq $0x100, %rsi            # imm = 0x100
jne 0x5395
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x38(%rbp), %r9
movq -0x30(%rbp), %r10
leaq 0xc14(%rip), %r15       # 0x601b
leaq (%r10,%rax,8), %rdx
testl %r9d, %r9d
jg 0x5424
incq %rax
addq $0x8, %rdx
cmpq $0x100, %rax            # imm = 0x100
jne 0x540b
jmp 0x54b1
movq %r13, %rsi
movq %rcx, %rdi
movq (%rdx), %rcx
addq %rdi, %rcx
movq %rdi, (%rdx)
addq $0x818, %rdx            # imm = 0x818
decq %rsi
jne 0x5427
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x5407
testl %r9d, %r9d
jle 0x54b1
movq -0x40(%rbp), %r12
movq %rbx, %r13
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r13, %rcx
callq 0x2280
incq %r14
movslq 0x1ed85(%rip), %r9      # 0x241f8
addq $0x818, %r13            # imm = 0x818
addq $0x8, %r12
cmpq %r9, %r14
jl 0x5459
testl %r9d, %r9d
movq -0x30(%rbp), %r10
jle 0x54b1
xorl %r14d, %r14d
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rdi
xorl %esi, %esi
callq 0x2300
movq -0x30(%rbp), %r10
incq %r14
movslq 0x1ed4c(%rip), %r9      # 0x241f8
cmpq %r9, %r14
jl 0x548f
movq 0x1ed80(%rip), %rax     # 0x24238
movq 0x1ed81(%rip), %rsi     # 0x24240
movq %rsi, 0x1ed72(%rip)     # 0x24238
movq %rax, 0x1ed73(%rip)     # 0x24240
cmpl $0xf, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %r8
movq -0x70(%rbp), %rcx
jl 0x5252
movq %rsi, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq