// File: /workspace/github_c_makefile_star2_O2/thegenemyers[P]DASCRUBBER/DB.c
// Function: Trim_DB
void Trim_DB(DAZZ_DB *db)
{ int         i, j, r, f;
  int         allflag, cutoff, css;
  int64       totlen;
  int         maxlen, nreads;
  DAZZ_TRACK *record;
  DAZZ_READ  *reads;

  if (db->trimmed) return;

  if (db->cutoff <= 0 && (db->allarr & DB_ALL) != 0) return;

  { int load_error;

    load_error = db->loaded;
    for (record = db->tracks; record != NULL; record = record->next)
      if (record->name == atrack_name)
        { if (((DAZZ_ARROW *) record)->loaded)
            load_error = 1;
        }
      else if (record->name != qtrack_name)
        { if (record->loaded)
            load_error = 1;
        }
    if (load_error)
      { EPRINTF(EPLACE,"%s: Cannot load anything before trim (Trim_DB)\n",Prog_Name);
        return;
      }
  }

  cutoff = db->cutoff;
  if ((db->allarr & DB_ALL) != 0)
    allflag = 0;
  else
    allflag = DB_BEST;

  reads  = db->reads;
  nreads = db->nreads;

  for (record = db->tracks; record != NULL; record = record->next)
    if (record->name == qtrack_name)
      { uint16 *table = ((DAZZ_QV *) record)->table;

        j = 0;
        for (i = 0; i < db->nreads; i++)
          if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
            table[j++] = table[i];
      }
    else if (record->name == atrack_name)
      { DAZZ_ARROW *atrack = (DAZZ_ARROW *) record;
        int64      *aoff   = atrack->aoff;

        for (j = i = 0; i < nreads; i++)
          if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
            aoff[j++] = aoff[i];
        atrack->aoff = Realloc(aoff,sizeof(int64)*j,NULL);
      }
    else
      { int size;

        size = record->size;
        if (record->data == NULL)
          { char *anno = (char *) record->anno;
            j = 0;
            for (i = r = 0; i < db->nreads; i++, r += size)
              if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
                { memmove(anno+j,anno+r,size);
                  j += size;
                }
            record->anno = Realloc(record->anno,record->size*j,NULL);
          }
        else if (size == 4)
          { int *anno4 = (int *) (record->anno);
            int *alen  = record->alen;

            j = 0;
            for (i = 0; i < db->nreads; i++)
              if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
                { anno4[j] = anno4[i];
                  alen[j]  = alen[i];
                  j += 1;
                }
            record->alen = Realloc(record->alen,sizeof(int)*j,NULL);
            record->anno = Realloc(record->anno,record->size*(j+1),NULL);
          }
        else // size == 8
          { int64 *anno8 = (int64 *) (record->anno);
            int *alen    = record->alen;

            j = 0;
            for (i = 0; i < db->nreads; i++)
              if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
                { anno8[j] = anno8[i];
                  alen[j]  = alen[i];
                  j += 1;
                }
            record->alen = Realloc(record->alen,sizeof(int)*j,NULL);
            record->anno = Realloc(record->anno,record->size*(j+1),NULL);
          }
        record->nreads = j;
      }

  css    = 0;
  totlen = maxlen = 0;
  for (j = i = 0; i < nreads; i++)
    { f = reads[i].flags;
      if ((f & DB_CCS) == 0)
        css = 0;
      r = reads[i].rlen;
      if ((f & DB_BEST) >= allflag && r >= cutoff)
        { totlen += r;
          if (r > maxlen)
            maxlen = r;
          reads[j] = reads[i];
          if (css)
            reads[j++].flags |= DB_CCS;
          else
            reads[j++].flags &= ~DB_CCS;
          css = 1;
        }
    }
  
  db->totlen  = totlen;
  db->maxlen  = maxlen;
  db->nreads  = j;
  db->trimmed = 1;

  if (j < nreads)
    { db->reads = Realloc(reads-1,sizeof(DAZZ_READ)*(j+2),NULL);
      db->reads += 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x0, 0x34(%rdi)
je 0x3319
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r11
movl 0x8(%rdi), %ebx
testl %ebx, %ebx
jg 0x332a
testb $0x1, 0xc(%r11)
jne 0x330a
movl 0x50(%r11), %eax
leaq 0x68(%r11), %r14
pushq $0x20
popq %rcx
leaq 0x6b1a(%rip), %r15      # 0x9e56
pushq $0x30
popq %rdx
leaq 0x62bd(%rip), %r12      # 0x9603
movq %r14, %rsi
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x3373
movq 0x8(%rsi), %rdi
cmpq %r15, %rdi
je 0x3364
cmpq %r12, %rdi
je 0x3349
movq %rdx, %rdi
jmp 0x3367
movq %rcx, %rdi
cmpl $0x0, (%rsi,%rdi)
pushq $0x1
popq %rdi
cmovnel %edi, %eax
jmp 0x3349
testl %eax, %eax
jne 0x36fa
movl 0xc(%r11), %r13d
movl 0x30(%r11), %eax
notl %r13d
andl $0x1, %r13d
shll $0xb, %r13d
xorl %ecx, %ecx
testl %eax, %eax
movl %eax, 0x1c(%rsp)
cmovgl %eax, %ecx
movq %rcx, 0x20(%rsp)
movq 0x60(%r11), %rbp
leaq 0x20(%rbp), %rax
movq %rax, 0x10(%rsp)
leaq 0x4(%rbp), %rax
movq %rax, 0x28(%rsp)
movl $0x800, %r10d           # imm = 0x800
movq %r11, 0x8(%rsp)
movl %ebx, 0x4(%rsp)
movq %rbp, 0x30(%rsp)
movq (%r14), %r14
testq %r14, %r14
je 0x3606
movq 0x8(%r14), %rax
cmpq %r12, %rax
je 0x3450
cmpq %r15, %rax
je 0x3497
movslq 0x10(%r14), %r15
cmpq $0x0, 0x28(%r14)
je 0x34f4
movq 0x18(%r14), %rax
movq %r14, %r12
movq 0x20(%r14), %rdi
xorl %ecx, %ecx
cmpl $0x4, %r15d
jne 0x356e
movq 0x10(%rsp), %rdx
xorl %r14d, %r14d
movslq 0x30(%r11), %rsi
movslq %r14d, %r15
cmpq %rsi, %rcx
jge 0x35af
movl (%rdx), %esi
andl %r10d, %esi
cmpl %r13d, %esi
jb 0x3447
cmpl %ebx, -0x1c(%rdx)
jl 0x3447
movl (%rax,%rcx,4), %esi
movl %esi, (%rax,%r15,4)
movl (%rdi,%rcx,4), %esi
movl %esi, (%rdi,%r15,4)
incl %r15d
movl %r15d, %r14d
incq %rcx
addq $0x28, %rdx
jmp 0x3414
movq 0x20(%r14), %rax
movl 0x30(%r11), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movq 0x28(%rsp), %rsi
xorl %edi, %edi
cmpq %rdx, %rcx
je 0x33c8
movl 0x1c(%rsi), %r8d
andl %r10d, %r8d
cmpl %r13d, %r8d
jb 0x348e
cmpl %ebx, (%rsi)
jl 0x348e
movzwl (%rax,%rdx,2), %r8d
movslq %edi, %r9
incl %edi
movw %r8w, (%rax,%r9,2)
incq %rdx
addq $0x28, %rsi
jmp 0x3466
movq 0x10(%r14), %rdi
xorl %eax, %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
movq 0x20(%rsp), %r9
movslq %edx, %rsi
cmpq %rax, %r9
je 0x34d5
movl 0x1c(%rcx), %r8d
andl %r10d, %r8d
cmpl %r13d, %r8d
jb 0x34cc
cmpl %ebx, (%rcx)
jl 0x34cc
movq (%rdi,%rax,8), %r8
leal 0x1(%rsi), %edx
movq %r8, (%rdi,%rsi,8)
incq %rax
addq $0x28, %rcx
jmp 0x34a9
shlq $0x3, %rsi
xorl %edx, %edx
callq 0x1c05
movl $0x800, %r10d           # imm = 0x800
movq 0x8(%rsp), %r11
movq %rax, 0x10(%r14)
jmp 0x33c8
movq %r14, 0x38(%rsp)
movq 0x18(%r14), %r12
xorl %ebp, %ebp
movq %r12, 0x40(%rsp)
movq 0x10(%rsp), %rbx
xorl %r14d, %r14d
movslq 0x30(%r11), %rax
cmpq %rax, %rbp
jge 0x3555
movl (%rbx), %eax
andl %r10d, %eax
cmpl %r13d, %eax
jb 0x3549
movl 0x4(%rsp), %eax
cmpl %eax, -0x1c(%rbx)
jl 0x3549
movslq %r14d, %rdi
addq 0x40(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1220
movl $0x800, %r10d           # imm = 0x800
movq 0x8(%rsp), %r11
addl %r15d, %r14d
incq %rbp
addq $0x28, %rbx
addq %r15, %r12
jmp 0x350c
movq 0x38(%rsp), %r12
movl 0x10(%r12), %r15d
imull %r14d, %r15d
movl 0x4(%rsp), %ebx
movq 0x30(%rsp), %rbp
jmp 0x35cc
movq 0x10(%rsp), %rdx
xorl %r14d, %r14d
movslq 0x30(%r11), %rsi
movslq %r14d, %r15
cmpq %rsi, %rcx
jge 0x35af
movl (%rdx), %esi
andl %r10d, %esi
cmpl %r13d, %esi
jb 0x35a6
cmpl %ebx, -0x1c(%rdx)
jl 0x35a6
movq (%rax,%rcx,8), %rsi
movq %rsi, (%rax,%r15,8)
movl (%rdi,%rcx,4), %esi
movl %esi, (%rdi,%r15,4)
incl %r15d
movl %r15d, %r14d
incq %rcx
addq $0x28, %rdx
jmp 0x3576
leaq (,%r15,4), %rsi
xorl %edx, %edx
callq 0x1c05
movq %rax, 0x20(%r12)
incl %r15d
imull 0x10(%r12), %r15d
movq 0x18(%r12), %rdi
movslq %r15d, %rsi
xorl %edx, %edx
callq 0x1c05
movq %rax, 0x18(%r12)
movl %r14d, 0x14(%r12)
movq 0x8(%rsp), %r11
movq %r12, %r14
leaq 0x6862(%rip), %r15      # 0x9e56
leaq 0x6008(%rip), %r12      # 0x9603
movl $0x800, %r10d           # imm = 0x800
jmp 0x33c8
imulq $0x28, 0x20(%rsp), %rcx
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
xorl %r9d, %r9d
xorl %eax, %eax
pushq $0x1
popq %r14
cmpq %rdx, %rcx
je 0x36b5
movl 0x20(%rbp,%rdx), %r10d
movl %r10d, %r8d
shll $0x15, %r8d
sarl $0x1f, %r8d
andl %r9d, %r8d
andl $0x800, %r10d           # imm = 0x800
cmpl %r13d, %r10d
jb 0x36a9
movl 0x4(%rbp,%rdx), %r9d
cmpl %ebx, %r9d
jl 0x36a9
leaq (%rdx,%rbp), %r10
movslq %r9d, %r11
addq %r11, %rdi
cmpl %esi, %r9d
cmovgl %r9d, %esi
cltq
imulq $0x28, %rax, %r9
movq 0x20(%r10), %r11
movq %r11, 0x20(%rbp,%r9)
movups (%r10), %xmm0
movups 0x10(%r10), %xmm1
movups %xmm1, 0x10(%rbp,%r9)
movups %xmm0, (%rbp,%r9)
xorl %r10d, %r10d
testl %r8d, %r8d
setne %r10b
andl $0xfffffbff, %r11d      # imm = 0xFFFFFBFF
shll $0xa, %r10d
orl %r11d, %r10d
movq 0x8(%rsp), %r11
movl %r10d, 0x20(%rbp,%r9)
incl %eax
movl %r14d, %r8d
addq $0x28, %rdx
movl %r8d, %r9d
jmp 0x361b
movq %rdi, 0x28(%r11)
movl %esi, 0x20(%r11)
movl %eax, 0x30(%r11)
movl $0x1, 0x34(%r11)
cmpl 0x1c(%rsp), %eax
jge 0x330a
addq $-0x28, %rbp
addl $0x2, %eax
cltq
imulq $0x28, %rax, %rsi
movq %rbp, %rdi
xorl %edx, %edx
movq %r11, %rbx
callq 0x1c05
addq $0x28, %rax
movq %rax, 0x60(%rbx)
jmp 0x330a
movq 0xa8f7(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd1dd(%rip), %rdx      # 0x108e8
leaq 0x5ec1(%rip), %rsi      # 0x95d3
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11a0