// File: /workspace/github_c_makefile_star2_O1/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 0x1f362(%rip), %rcx     # 0x241f8
movl %ecx, %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
imulq $0x818, %rcx, %rax      # imm = 0x818
movq -0x810(%rbx,%rax), %rax
movq %rax, -0x40(%rbp)
decq %rax
cqto
idivq %rcx
incq %rax
movq %rax, 0x1f35e(%rip)     # 0x24230
movb 0x1f324(%rip), %cl      # 0x241fc
movl $0xff, %eax
shlq %cl, %rax
movq %rsi, 0x1f351(%rip)     # 0x24238
movq %r8, 0x1f352(%rip)      # 0x24240
movq %rax, 0x1f353(%rip)     # 0x24248
xorl %esi, %esi
cmpl $0x0, (%rdi,%rsi,4)
jne 0x4f0b
incq %rsi
cmpq $0x10, %rsi
jne 0x4ef7
movl $0x10, %esi
cmpl $0xf, %esi
jg 0x51c5
leaq -0x810(%rbx), %rax
movq %rax, -0x48(%rbp)
leaq 0x10(%rbx), %r8
movl %esi, %eax
movq %r8, -0x30(%rbp)
movq %rdi, -0x58(%rbp)
movq %r15, -0x38(%rbp)
movq %rsi, -0x50(%rbp)
movslq %eax, %rcx
cmpl $0x10, %ecx
movl $0xf, %r10d
cmovgel %ecx, %r10d
leal 0x1(%r10), %r9d
cmpq $0xe, %rcx
jg 0x4f64
leaq 0x1(%rcx), %rdx
cmpl $0x0, 0x4(%rdi,%rcx,4)
movq %rdx, %rcx
je 0x4f49
leal -0x1(%rdx), %r10d
movl %edx, %r9d
xorl %ecx, %ecx
movq %r10, -0x60(%rbp)
cmpl $0xf, %r10d
setge %cl
movl %ecx, 0x1f2d9(%rip)     # 0x24250
leal (,%rax,8), %ecx
movl %ecx, 0x1f2d0(%rip)     # 0x24254
movq %r9, -0x68(%rbp)
leal (,%r9,8), %ecx
movl %ecx, 0x1f2c2(%rip)     # 0x24258
movslq 0x1f25b(%rip), %r13     # 0x241f8
cmpl %esi, %eax
jne 0x4fef
testl %r13d, %r13d
jle 0x50d3
movl %r13d, %r12d
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 0x4fcc
jmp 0x50d3
testl %r13d, %r13d
movq -0x40(%rbp), %rcx
jle 0x5051
movq 0x1f231(%rip), %r12     # 0x24230
imulq $0x818, %r13, %rax      # imm = 0x818
movq %rax, -0x70(%rbp)
movq %r12, -0x78(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
leaq (%r8,%r14), %rdi
movq %r15, -0x10(%r8,%r14)
cmpq %rcx, %r12
movq %rcx, %r15
cmovlq %r12, %r15
movq %r15, -0x8(%r8,%r14)
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x2180
movq -0x30(%rbp), %r8
movq -0x40(%rbp), %rcx
addq $0x818, %r14            # imm = 0x818
addq -0x78(%rbp), %r12
cmpq %r14, -0x70(%rbp)
jne 0x5014
imulq $0x818, %r13, %rax      # imm = 0x818
movq -0x48(%rbp), %rdx
movq %rcx, (%rdx,%rax)
movl 0x1f196(%rip), %ecx     # 0x241fc
movl %r13d, %eax
imulq $0x103, %rax, %rax      # imm = 0x103
movq %r8, %rdx
xorl %esi, %esi
testl %r13d, %r13d
jle 0x50c3
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 0x509d
incq %r8
cmpq %r13, %r8
jne 0x5088
incq %rsi
addq $0x8, %rdx
cmpq $0x100, %rsi            # imm = 0x100
jne 0x5075
movl 0x1f11f(%rip), %eax     # 0x241f8
imulq $0x818, %rax, %rcx      # imm = 0x818
movq -0x30(%rbp), %rdx
xorl %esi, %esi
xorl %edi, %edi
testl %eax, %eax
jle 0x510b
xorl %r9d, %r9d
movq (%rdx,%r9), %r8
addq %rdi, %r8
movq %rdi, (%rdx,%r9)
addq $0x818, %r9             # imm = 0x818
movq %r8, %rdi
cmpq %r9, %rcx
jne 0x50ef
jmp 0x510e
movq %rdi, %r8
incq %rsi
addq $0x8, %rdx
movq %r8, %rdi
cmpq $0x100, %rsi            # imm = 0x100
jne 0x50e8
cmpl $0x0, 0x1f0d0(%rip)     # 0x241f8
leaq 0xb73(%rip), %r15       # 0x5ca2
jle 0x5165
movq -0x38(%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 0x1f0a3(%rip), %rax     # 0x241f8
addq $0x818, %r13            # imm = 0x818
addq $0x8, %r12
cmpq %rax, %r14
jl 0x513b
cmpl $0x0, 0x1f08c(%rip)     # 0x241f8
movq -0x38(%rbp), %r15
jle 0x518f
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
xorl %esi, %esi
callq 0x2300
incq %r14
movslq 0x1f06e(%rip), %rax     # 0x241f8
cmpq %rax, %r14
jl 0x5175
movq 0x1f0a2(%rip), %rax     # 0x24238
movq 0x1f0a3(%rip), %rcx     # 0x24240
movq %rcx, 0x1f094(%rip)     # 0x24238
movq %rax, 0x1f095(%rip)     # 0x24240
cmpl $0xf, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %r8
movq -0x68(%rbp), %rax
jl 0x4f35
movq 0x1f06c(%rip), %rax     # 0x24238
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq