// File: /workspace/github_c_makefile_star2_O3/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:
cmpl $0x0, 0x34(%rdi)
je 0x438b
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl 0x8(%rdi), %r15d
testl %r15d, %r15d
jg 0x43af
testb $0x1, 0xc(%rbx)
jne 0x47d7
movl 0x50(%rbx), %eax
movq 0x68(%rbx), %rbp
testq %rbp, %rbp
je 0x43fd
leaq 0x7af4(%rip), %rcx      # 0xbeb6
movl $0x1, %edx
leaq 0x7295(%rip), %rsi      # 0xb663
movq %rbp, %rdi
movq 0x8(%rdi), %r9
cmpq %rcx, %r9
je 0x43e7
movl $0x30, %r8d
cmpq %rsi, %r9
jne 0x43ed
jmp 0x43f5
movl $0x20, %r8d
cmpl $0x0, (%rdi,%r8)
cmovnel %edx, %eax
movq (%rdi), %rdi
testq %rdi, %rdi
jne 0x43d1
testl %eax, %eax
jne 0x47e6
movl 0xc(%rbx), %r14d
movl 0x30(%rbx), %r13d
notl %r14d
andl $0x1, %r14d
shll $0xb, %r14d
movq 0x60(%rbx), %r12
testq %rbp, %rbp
je 0x46e3
leaq 0x20(%r12), %rax
movq %rax, 0x10(%rsp)
leaq 0x4(%r12), %rax
movq %rax, 0x20(%rsp)
movl $0x800, %r10d           # imm = 0x800
movq %rbx, 0x28(%rsp)
movl %r15d, 0x4(%rsp)
movq %r12, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movl %r14d, 0x1c(%rsp)
movq 0x8(%rbp), %rax
leaq 0x7200(%rip), %rcx      # 0xb663
cmpq %rcx, %rax
je 0x44ef
leaq 0x7a43(%rip), %rcx      # 0xbeb6
cmpq %rcx, %rax
je 0x453a
movl 0x10(%rbp), %r8d
cmpq $0x0, 0x28(%rbp)
movq %rbp, 0x8(%rsp)
je 0x4585
movq 0x18(%rbp), %rax
movq 0x20(%rbp), %rdi
movl 0x30(%rbx), %ecx
cmpl $0x4, %r8d
jne 0x462a
testl %ecx, %ecx
jle 0x466f
xorl %edx, %edx
movq 0x10(%rsp), %rsi
xorl %ebp, %ebp
movl (%rsi), %r8d
andl %r10d, %r8d
cmpl %r14d, %r8d
jb 0x44db
cmpl %r15d, -0x1c(%rsi)
jl 0x44db
movl (%rax,%rdx,4), %ecx
movslq %ebp, %rbp
movl %ecx, (%rax,%rbp,4)
movl (%rdi,%rdx,4), %ecx
movl %ecx, (%rdi,%rbp,4)
incl %ebp
movl 0x30(%rbx), %ecx
incq %rdx
movslq %ecx, %r8
addq $0x28, %rsi
cmpq %r8, %rdx
jl 0x44b6
jmp 0x4671
movslq 0x30(%rbx), %rax
testq %rax, %rax
jle 0x46d6
movq 0x20(%rbp), %rcx
xorl %edx, %edx
movq 0x20(%rsp), %rsi
xorl %edi, %edi
movl 0x1c(%rsi), %r8d
andl %r10d, %r8d
cmpl %r14d, %r8d
jb 0x4529
cmpl %r15d, (%rsi)
jl 0x4529
movzwl (%rcx,%rdx,2), %r8d
movslq %edi, %r9
incl %edi
movw %r8w, (%rcx,%r9,2)
incq %rdx
addq $0x28, %rsi
cmpq %rdx, %rax
jne 0x4509
jmp 0x46d6
movq 0x10(%rbp), %rdi
testl %r13d, %r13d
jle 0x469d
xorl %eax, %eax
movq 0x20(%rsp), %rcx
xorl %edx, %edx
movl 0x1c(%rcx), %esi
andl %r10d, %esi
cmpl %r14d, %esi
jb 0x456d
cmpl %r15d, (%rcx)
jl 0x456d
movq (%rdi,%rax,8), %rsi
movslq %edx, %r8
incl %edx
movq %rsi, (%rdi,%r8,8)
incq %rax
addq $0x28, %rcx
cmpq %rax, %r13
jne 0x4550
movslq %edx, %rsi
shlq $0x3, %rsi
jmp 0x469f
movq 0x18(%rbp), %rdi
movl 0x30(%rbx), %eax
testl %eax, %eax
jle 0x46ac
movslq %r8d, %rdx
xorl %r15d, %r15d
movq 0x10(%rsp), %rbx
movq %rdi, %r13
xorl %ebp, %ebp
movq %r8, 0x40(%rsp)
movl (%rbx), %ecx
andl %r10d, %ecx
cmpl %r14d, %ecx
jb 0x45f7
movl 0x4(%rsp), %ecx
cmpl %ecx, -0x1c(%rbx)
jl 0x45f7
movslq %ebp, %rbp
movq %rbp, %r12
movq %rdi, %rbp
addq %r12, %rdi
movq %r13, %rsi
movq %rdx, %r14
callq 0x1220
movq %r14, %rdx
movq %rbp, %rdi
movq %r12, %rbp
movq 0x40(%rsp), %r8
movl 0x1c(%rsp), %r14d
movl $0x800, %r10d           # imm = 0x800
addl %r8d, %ebp
movq 0x28(%rsp), %rax
movl 0x30(%rax), %eax
incq %r15
movslq %eax, %rcx
addq %rdx, %r13
addq $0x28, %rbx
cmpq %rcx, %r15
jl 0x45a9
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0x10(%rax), %r8d
movl 0x4(%rsp), %r15d
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %r13
jmp 0x46ae
testl %ecx, %ecx
jle 0x466f
xorl %edx, %edx
movq 0x10(%rsp), %rsi
xorl %ebp, %ebp
movl (%rsi), %r8d
andl %r10d, %r8d
cmpl %r14d, %r8d
jb 0x465e
cmpl %r15d, -0x1c(%rsi)
jl 0x465e
movq (%rax,%rdx,8), %rcx
movslq %ebp, %rbp
movq %rcx, (%rax,%rbp,8)
movl (%rdi,%rdx,4), %ecx
movl %ecx, (%rdi,%rbp,4)
incl %ebp
movl 0x30(%rbx), %ecx
incq %rdx
movslq %ecx, %r8
addq $0x28, %rsi
cmpq %r8, %rdx
jl 0x4637
jmp 0x4671
xorl %ebp, %ebp
movslq %ebp, %rbx
leaq (,%rbx,4), %rsi
xorl %edx, %edx
callq 0x2b4d
movq %rbx, %r8
movq 0x8(%rsp), %rbx
movq %rax, 0x20(%rbx)
movq 0x18(%rbx), %rdi
incl %r8d
imull 0x10(%rbx), %r8d
jmp 0x46b7
xorl %esi, %esi
xorl %edx, %edx
callq 0x2b4d
movq %rax, 0x10(%rbp)
jmp 0x46d0
xorl %ebp, %ebp
imull %ebp, %r8d
movq 0x8(%rsp), %rbx
movslq %r8d, %rsi
xorl %edx, %edx
callq 0x2b4d
movq %rax, 0x18(%rbx)
movl %ebp, 0x14(%rbx)
movq %rbx, %rbp
movq 0x28(%rsp), %rbx
movl $0x800, %r10d           # imm = 0x800
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x4458
testl %r13d, %r13d
jle 0x4797
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %rsi
xorl %edi, %edi
xorl %eax, %eax
xorl %r8d, %r8d
xorl %edx, %edx
xorl %ecx, %ecx
movl %r8d, %r9d
movl 0x20(%r12,%rdi), %r10d
movl %r10d, %r8d
shll $0x15, %r8d
sarl $0x1f, %r8d
andl %r9d, %r8d
andl $0x800, %r10d           # imm = 0x800
cmpl %r14d, %r10d
jb 0x4788
movl 0x4(%r12,%rdi), %r9d
cmpl %r15d, %r9d
jl 0x4788
leaq (%r12,%rdi), %r10
movslq %r9d, %r11
addq %r11, %rdx
cmpl %ecx, %r9d
cmovgl %r9d, %ecx
cltq
leaq (%rax,%rax,4), %r9
movq 0x20(%r10), %r11
movq %r11, 0x20(%r12,%r9,8)
movups (%r10), %xmm0
movups 0x10(%r10), %xmm1
movups %xmm1, 0x10(%r12,%r9,8)
movups %xmm0, (%r12,%r9,8)
xorl %r10d, %r10d
testl %r8d, %r8d
setne %r10b
andl $0xfffffbff, %r11d      # imm = 0xFFFFFBFF
shll $0xa, %r10d
orl %r11d, %r10d
movl %r10d, 0x20(%r12,%r9,8)
incl %eax
movl $0x1, %r8d
addq $0x28, %rdi
cmpq %rdi, %rsi
jne 0x4703
jmp 0x479d
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %rdx, 0x28(%rbx)
movl %ecx, 0x20(%rbx)
movl %eax, 0x30(%rbx)
movl $0x1, 0x34(%rbx)
cmpl %r13d, %eax
jge 0x47d7
addq $-0x28, %r12
cltq
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x50, %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x2b4d
addq $0x28, %rax
movq %rax, 0x60(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x15dd
jmp 0x47d7