// File: /workspace/github_c_makefile_star2_O0/thegenemyers[P]DAMASKER/tandem.c
// Function: Sort_Kmers
static KmerPos *Sort_Kmers(DAZZ_DB *block, int *len, KmerPos **buffer)
{ THREAD    threads[NTHREADS];
  Tuple_Arg parmt[NTHREADS];
  Lex_Arg   parmx[NTHREADS];
  int       mersort[16];

  KmerPos  *src, *trg, *rez;
  int       kmers, nreads;
  int       i, j, x;

  for (i = 0; i < NTHREADS; i++)
    parmx[i].sptr = (int64 *) alloca(NTHREADS*BPOWR*sizeof(int64));

  for (i = 0; i < 16; i++)
    mersort[i] = 0;
  for (i = 0; i < Kshift; i += 8)
    mersort[i>>3] = 1;

  nreads = block->nreads;
  kmers  = block->reads[nreads].boff - Kmer * nreads;

  if (kmers <= 0)
    goto no_mers;

  if (((Kshift-1)/BSHIFT) & 0x1)
    { trg = (KmerPos *) Malloc(sizeof(KmerPos)*(kmers+1),"Allocating Sort_Kmers vectors");
      src = (KmerPos *) Malloc(sizeof(KmerPos)*(kmers+1),"Allocating Sort_Kmers vectors");
    }
  else
    { src = (KmerPos *) Malloc(sizeof(KmerPos)*(kmers+1),"Allocating Sort_Kmers vectors");
      trg = (KmerPos *) Malloc(sizeof(KmerPos)*(kmers+1),"Allocating Sort_Kmers vectors");
    }
  if (src == NULL || trg == NULL)
    Clean_Exit(1);

  if (VERBOSE)
    { printf("\n   Kmer count = ");
      Print_Number((int64) kmers,0,stdout);
      printf("\n   Using %.2fGb of space\n",(1. * kmers) / 33554432);
      fflush(stdout);
    }

  TA_block = block;
  TA_list  = src;

  for (i = 0; i < NTHREADS; i++)
    { parmt[i].tnum = i;
      parmt[i].kptr = parmx[i].tptr;
      for (j = 0; j < BPOWR; j++)
        parmt[i].kptr[j] = 0;
    }

  for (i = 0; i < NTHREADS; i++)
    pthread_create(threads+i,NULL,tuple_thread,parmt+i);

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

  x = 0;
  for (i = 0; i < NTHREADS; i++)
    { parmx[i].beg = x;
      j = (int) ((((int64) nreads) * (i+1)) >> NSHIFT);
      parmx[i].end = x = block->reads[j].boff - j*Kmer;
    }

  rez = (KmerPos *) lex_sort(mersort,(Double *) src,(Double *) trg,parmx);

  if (rez[kmers-1].code == 0xffffffffffffffffllu)
    rez[kmers].code = 0;
  else
    rez[kmers].code = 0xffffffffffffffffllu;
  rez[kmers].read = nreads;
 
  if (src != rez)
    *buffer = src; 
  else
    *buffer = trg; 

#ifdef TEST_KSORT
  { int i;

    printf("\nKMER SORT:\n");
    for (i = 0; i < HOW_MANY && i < kmers; i++)
      { KmerPos *c = rez+i;
        printf(" %5d / %5d / %10lld\n",c->read,c->rpos,c->code);
      }
    fflush(stdout);
  }
#endif

  if (VERBOSE)
    { printf("   Index occupies %.2fGb\n",(1. * kmers) / 67108864);
      fflush(stdout);
    }

  if (kmers <= 0)
    { free(rez);
      goto no_mers;
    }

  *len = kmers;
  return (rez);

no_mers:
  *len = 0;
  return (NULL);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl 0x29e3b(%rip), %eax     # 0x2e448
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl 0x29e0e(%rip), %eax     # 0x2e448
movq %rax, %rdx
shlq $0x4, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl 0x29ded(%rip), %eax     # 0x2e448
imulq $0x818, %rax, %rdx      # imm = 0x818
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x29db4(%rip), %eax     # 0x2e448
jge 0x46df
movq -0xb8(%rbp), %rax
movl 0x29da5(%rip), %ecx     # 0x2e448
shll $0x8, %ecx
movslq %ecx, %rdx
shlq $0x3, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, %rsp
movslq -0xa4(%rbp), %rdx
imulq $0x818, %rdx, %rdx      # imm = 0x818
addq %rdx, %rax
movq %rcx, 0x810(%rax)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x4688
movl $0x0, -0xa4(%rbp)
cmpl $0x10, -0xa4(%rbp)
jge 0x4712
movslq -0xa4(%rbp), %rax
movl $0x0, -0x80(%rbp,%rax,4)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x46e9
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x29d14(%rip), %eax     # 0x2e43c
jge 0x474e
movl -0xa4(%rbp), %eax
sarl $0x3, %eax
cltq
movl $0x1, -0x80(%rbp,%rax,4)
movl -0xa4(%rbp), %eax
addl $0x8, %eax
movl %eax, -0xa4(%rbp)
jmp 0x471c
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movslq -0xa0(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl 0x29cb5(%rip), %ecx     # 0x2e430
imull -0xa0(%rbp), %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movl %eax, -0x9c(%rbp)
cmpl $0x0, -0x9c(%rbp)
jg 0x479c
jmp 0x4c80
movl 0x29c9a(%rip), %eax     # 0x2e43c
subl $0x1, %eax
movl $0x8, %ecx
cltd
idivl %ecx
andl $0x1, %eax
cmpl $0x0, %eax
je 0x47fd
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x4, %rdi
leaq 0x24f5c(%rip), %rsi     # 0x29728
callq 0x1d780
movq %rax, -0x90(%rbp)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x4, %rdi
leaq 0x24f39(%rip), %rsi     # 0x29728
callq 0x1d780
movq %rax, -0x88(%rbp)
jmp 0x4843
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x4, %rdi
leaq 0x24f14(%rip), %rsi     # 0x29728
callq 0x1d780
movq %rax, -0x88(%rbp)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x4, %rdi
leaq 0x24ef1(%rip), %rsi     # 0x29728
callq 0x1d780
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x4857
cmpq $0x0, -0x90(%rbp)
jne 0x4861
movl $0x1, %edi
callq 0x2420
leaq 0x29bbc(%rip), %rax     # 0x2e424
cmpl $0x0, (%rax)
je 0x48d0
leaq 0x24ed2(%rip), %rdi     # 0x29746
movb $0x0, %al
callq 0x2130
movslq -0x9c(%rbp), %rdi
movq 0x2974f(%rip), %rax     # 0x2dfd8
movq (%rax), %rdx
xorl %esi, %esi
callq 0x1ddf0
cvtsi2sdl -0x9c(%rbp), %xmm1
movsd 0x2477d(%rip), %xmm0    # 0x29020
mulsd %xmm1, %xmm0
movsd 0x24da1(%rip), %xmm1    # 0x29650
divsd %xmm1, %xmm0
leaq 0x24e9e(%rip), %rdi     # 0x29758
movb $0x1, %al
callq 0x2130
movq 0x29710(%rip), %rax     # 0x2dfd8
movq (%rax), %rdi
callq 0x2240
movq -0x10(%rbp), %rax
movq %rax, 0x29b95(%rip)     # 0x2e470
movq -0x88(%rbp), %rax
movq %rax, 0x29b8f(%rip)     # 0x2e478
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x29b49(%rip), %eax     # 0x2e448
jge 0x49b8
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rcx
movl -0xa4(%rbp), %esi
movslq -0xa4(%rbp), %rdi
shlq $0x4, %rdi
movq %rax, %rdx
addq %rdi, %rdx
movl %esi, (%rdx)
movslq -0xa4(%rbp), %rdx
imulq $0x818, %rdx, %rdx      # imm = 0x818
addq %rdx, %rcx
addq $0x10, %rcx
movslq -0xa4(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0xa8(%rbp)
cmpl $0x100, -0xa8(%rbp)     # imm = 0x100
jge 0x49a2
movq -0xc0(%rbp), %rax
movslq -0xa4(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq -0xa8(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x495d
jmp 0x49a4
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x48f3
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x29a7a(%rip), %eax     # 0x2e448
jge 0x4a1b
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rdi
movslq -0xa4(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movslq -0xa4(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0x15ab(%rip), %rdx      # 0x5fb0
callq 0x2270
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x49c2
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x29a17(%rip), %eax     # 0x2e448
jge 0x4a5f
movq -0xc8(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22f0
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x4a25
movl $0x0, -0xac(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x299c9(%rip), %eax     # 0x2e448
jge 0x4b32
movq -0xb8(%rbp), %rax
movslq -0xac(%rbp), %rdx
movslq -0xa4(%rbp), %rcx
imulq $0x818, %rcx, %rsi      # imm = 0x818
movq %rax, %rcx
addq %rsi, %rcx
movq %rdx, (%rcx)
movslq -0xa0(%rbp), %rdx
movl -0xa4(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq %rcx, %rdx
movl 0x29985(%rip), %ecx     # 0x2e44c
sarq %cl, %rdx
movq %rdx, %rcx
movl %ecx, -0xa8(%rbp)
movq -0x10(%rbp), %rcx
movq 0x60(%rcx), %rcx
movslq -0xa8(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
movl -0xa8(%rbp), %edx
imull 0x29936(%rip), %edx     # 0x2e430
movslq %edx, %rdx
subq %rdx, %rcx
movl %ecx, -0xac(%rbp)
movslq %ecx, %rcx
movslq -0xa4(%rbp), %rdx
imulq $0x818, %rdx, %rdx      # imm = 0x818
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x4a73
movq -0xb8(%rbp), %rcx
leaq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x90(%rbp), %rdx
callq 0x4e30
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $-0x1, (%rax)
jne 0x4b95
movq -0x98(%rbp), %rax
movslq -0x9c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, (%rax)
jmp 0x4bb1
movq -0x98(%rbp), %rax
movslq -0x9c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq $-0x1, (%rax)
movl -0xa0(%rbp), %ecx
movq -0x98(%rbp), %rax
movslq -0x9c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x88(%rbp), %rax
cmpq -0x98(%rbp), %rax
je 0x4bef
movq -0x88(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0x4bfd
movq -0x90(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
leaq 0x29820(%rip), %rax     # 0x2e424
cmpl $0x0, (%rax)
je 0x4c46
cvtsi2sdl -0x9c(%rbp), %xmm1
movsd 0x24407(%rip), %xmm0    # 0x29020
mulsd %xmm1, %xmm0
movsd 0x24a33(%rip), %xmm1    # 0x29658
divsd %xmm1, %xmm0
leaq 0x24b43(%rip), %rdi     # 0x29773
movb $0x1, %al
callq 0x2130
movq 0x2939a(%rip), %rax     # 0x2dfd8
movq (%rax), %rdi
callq 0x2240
cmpl $0x0, -0x9c(%rbp)
jg 0x4c5d
movq -0x98(%rbp), %rdi
callq 0x2030
jmp 0x4c80
movl -0x9c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x98(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0xb0(%rbp)
jmp 0x4c9c
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
movq $0x0, -0x8(%rbp)
movl $0x1, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq -0x8(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)