// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, %rbx
movq %rdx, %r8
movq %rdi, -0x40(%rbp)
movslq 0x1e621(%rip), %rdi     # 0x221f8
movl %edi, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
imulq $0x818, %rdi, %rax      # imm = 0x818
movq -0x810(%rbx,%rax), %rax
movq %rax, -0x38(%rbp)
decq %rax
cqto
idivq %rdi
incq %rax
movq %rax, 0x1e619(%rip)     # 0x22230
movq %rsi, 0x1e61a(%rip)     # 0x22238
movb 0x1e5d8(%rip), %cl      # 0x221fc
movl $0xff, %eax
shlq %cl, %rax
movq %r8, 0x1e60d(%rip)      # 0x22240
movq %rax, 0x1e60e(%rip)     # 0x22248
xorl %eax, %eax
cmpq $0x10, %rax
je 0x3c57
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpl $0x0, (%rax,%rcx,4)
movq %rcx, %rax
jne 0x3c5a
incq %rax
jmp 0x3c3c
pushq $0x10
popq %rax
leaq -0x810(%rbx), %rcx
movq %rcx, -0x50(%rbp)
leaq 0x10(%rbx), %rcx
movq %rcx, -0x30(%rbp)
movq %rax, -0x58(%rbp)
cmpl $0xf, %eax
jg 0x3ee4
movslq %eax, %rcx
decq %rcx
movl %eax, %r8d
cmpq $0xe, %rcx
je 0x3ca9
leaq 0x1(%rcx), %rdx
incl %r8d
movq -0x40(%rbp), %rsi
cmpl $0x0, 0x8(%rsi,%rcx,4)
movq %rdx, %rcx
je 0x3c83
xorl %ecx, %ecx
cmpq $0xf, %rdx
setge %cl
jmp 0x3cb2
pushq $0x1
popq %rcx
pushq $0x10
popq %rdx
movl %edx, %r8d
movl %ecx, 0x1e598(%rip)     # 0x22250
leal (,%rax,8), %ecx
movl %ecx, 0x1e58f(%rip)     # 0x22254
movq %r8, -0x60(%rbp)
leal (,%r8,8), %ecx
movl %ecx, 0x1e581(%rip)     # 0x22258
cmpl -0x58(%rbp), %eax
jne 0x3d2b
movl %edi, %ecx
shll $0x8, %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
testl %edi, %edi
movl $0x0, %eax
cmovgl %edi, %eax
xorl %edx, %edx
cmpq %rax, %rdx
je 0x3e1c
imulq $0x818, %rdx, %rsi      # imm = 0x818
addq %rbx, %rsi
addq $0x810, %rsi            # imm = 0x810
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x3d26
movq (%rsi), %r9
andq $0x0, (%r9,%r8,8)
incq %r8
jmp 0x3d14
incq %rdx
jmp 0x3cf7
testl %edi, %edi
movl $0x0, %eax
cmovgl %edi, %eax
movq 0x1e4f4(%rip), %rcx     # 0x22230
movq -0x30(%rbp), %rdx
xorl %esi, %esi
xorl %r8d, %r8d
cmpq %rax, %rsi
je 0x3d8e
imulq $0x818, %rsi, %r9       # imm = 0x818
movq %r8, (%rbx,%r9)
incq %rsi
movq %rcx, %r8
imulq %rsi, %r8
movq -0x38(%rbp), %r10
cmpq %r10, %r8
cmovgeq %r10, %r8
movq %r8, 0x8(%rbx,%r9)
xorl %r9d, %r9d
cmpq $0x100, %r9             # imm = 0x100
je 0x3d85
andq $0x0, (%rdx,%r9,8)
incq %r9
jmp 0x3d72
addq $0x818, %rdx            # imm = 0x818
jmp 0x3d45
movslq %edi, %rcx
imulq $0x818, %rcx, %rcx      # imm = 0x818
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %rsi
movq %rdx, (%rsi,%rcx)
movl 0x1e452(%rip), %ecx     # 0x221fc
movl %edi, %edx
imulq $0x818, %rdx, %rdx      # imm = 0x818
movq -0x30(%rbp), %rsi
xorl %r8d, %r8d
cmpq $0x100, %r8             # imm = 0x100
je 0x3e1c
movl %r8d, %r9d
shll %cl, %r9d
movslq %r9d, %r9
xorl %r10d, %r10d
cmpq %rax, %r10
je 0x3e13
imulq $0x818, %r10, %r11      # imm = 0x818
addq %rbx, %r11
addq $0x810, %r11            # imm = 0x810
xorl %r14d, %r14d
movq %r9, %r12
cmpq %r14, %rdx
je 0x3e0e
movq (%r11), %r13
movq (%r13,%r12,8), %r15
addq %r15, (%rsi,%r14)
andq $0x0, (%r13,%r12,8)
incq %r12
addq $0x818, %r14            # imm = 0x818
jmp 0x3deb
incq %r10
jmp 0x3dcf
incq %r8
addq $0x8, %rsi
jmp 0x3dba
movl %eax, %eax
imulq $0x818, %rax, %rax      # imm = 0x818
movq -0x30(%rbp), %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq $0x100, %rdx            # imm = 0x100
je 0x3e5e
xorl %r8d, %r8d
cmpq %r8, %rax
je 0x3e55
movq (%rcx,%r8), %r9
addq %rsi, %r9
movq %rsi, (%rcx,%r8)
addq $0x818, %r8             # imm = 0x818
movq %r9, %rsi
jmp 0x3e39
incq %rdx
addq $0x8, %rcx
jmp 0x3e2d
movq -0x48(%rbp), %r12
movq %rbx, %r13
xorl %r14d, %r14d
movslq %edi, %rax
cmpq %rax, %r14
jge 0x3e9a
movq %r12, %rdi
xorl %esi, %esi
leaq 0xad5(%rip), %rdx       # 0x4951
movq %r13, %rcx
callq 0x2280
incq %r14
movl 0x1e36b(%rip), %edi     # 0x221f8
addq $0x818, %r13            # imm = 0x818
addq $0x8, %r12
jmp 0x3e68
xorl %r14d, %r14d
movslq %edi, %rax
cmpq %rax, %r14
jge 0x3ebf
movq -0x48(%rbp), %rax
movq (%rax,%r14,8), %rdi
xorl %esi, %esi
callq 0x2300
incq %r14
movl 0x1e33b(%rip), %edi     # 0x221f8
jmp 0x3e9d
movq 0x1e372(%rip), %rax     # 0x22238
movq 0x1e373(%rip), %rsi     # 0x22240
movq %rsi, 0x1e364(%rip)     # 0x22238
movq %rax, 0x1e365(%rip)     # 0x22240
movq -0x60(%rbp), %rax
jmp 0x3c71
movq %rsi, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq