// File: /workspace/github_c_makefile_star2_O1/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 0x430c
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r11
movl 0x8(%rdi), %r12d
testl %r12d, %r12d
jg 0x4331
testb $0x1, 0xc(%r11)
jne 0x474e
movl 0x50(%r11), %eax
movq 0x68(%r11), %rcx
testq %rcx, %rcx
je 0x437d
leaq 0x7b11(%rip), %rdx      # 0xbe56
movl $0x1, %esi
leaq 0x72b2(%rip), %rdi      # 0xb603
movq 0x8(%rcx), %r9
cmpq %rdx, %r9
je 0x4367
movl $0x30, %r8d
cmpq %rdi, %r9
jne 0x436d
jmp 0x4375
movl $0x20, %r8d
cmpl $0x0, (%rcx,%r8)
cmovnel %esi, %eax
movq (%rcx), %rcx
testq %rcx, %rcx
jne 0x4351
testl %eax, %eax
jne 0x475d
movl 0xc(%r11), %r14d
movl 0x30(%r11), %eax
movq %rax, 0x20(%rsp)
notl %r14d
andl $0x1, %r14d
shll $0xb, %r14d
movq 0x60(%r11), %r13
movq 0x68(%r11), %r15
testq %r15, %r15
movq %r11, 0x10(%rsp)
je 0x4649
leaq 0x20(%r13), %rax
movq %rax, 0x18(%rsp)
leaq 0x4(%r13), %rax
movq %rax, 0x30(%rsp)
leaq 0x7237(%rip), %rbx      # 0xb603
movl $0x800, %r10d           # imm = 0x800
movl %r12d, 0xc(%rsp)
movq %r13, 0x38(%rsp)
movq 0x8(%r15), %rax
cmpq %rbx, %rax
je 0x446e
leaq 0x7a66(%rip), %rcx      # 0xbe56
cmpq %rcx, %rax
je 0x44b9
movq %r15, %rcx
movslq 0x10(%r15), %r15
cmpq $0x0, 0x28(%rcx)
je 0x4509
movq 0x18(%rcx), %rax
movq %rcx, %rsi
movq 0x20(%rcx), %rdi
movl 0x30(%r11), %ecx
cmpl $0x4, %r15d
jne 0x4580
testl %ecx, %ecx
jle 0x45c5
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
xorl %ebp, %ebp
movq %rsi, %r15
movl (%rdx), %esi
andl %r10d, %esi
cmpl %r14d, %esi
jb 0x4459
cmpl %r12d, -0x1c(%rdx)
jl 0x4459
movl (%rax,%rcx,4), %esi
movslq %ebp, %rbp
movl %esi, (%rax,%rbp,4)
movl (%rdi,%rcx,4), %esi
movl %esi, (%rdi,%rbp,4)
incl %ebp
incq %rcx
movslq 0x30(%r11), %rsi
addq $0x28, %rdx
cmpq %rsi, %rcx
jl 0x4438
jmp 0x45ca
movslq 0x30(%r11), %rax
testq %rax, %rax
jle 0x463d
movq 0x20(%r15), %rcx
xorl %edx, %edx
movq 0x30(%rsp), %rsi
xorl %edi, %edi
movl 0x1c(%rsi), %r8d
andl %r10d, %r8d
cmpl %r14d, %r8d
jb 0x44a8
cmpl %r12d, (%rsi)
jl 0x44a8
movzwl (%rcx,%rdx,2), %r8d
movslq %edi, %r9
incl %edi
movw %r8w, (%rcx,%r9,2)
incq %rdx
addq $0x28, %rsi
cmpq %rdx, %rax
jne 0x4488
jmp 0x463d
movq 0x10(%r15), %rdi
movq 0x20(%rsp), %r9
testl %r9d, %r9d
jle 0x45e9
xorl %eax, %eax
movq 0x30(%rsp), %rcx
xorl %edx, %edx
movl 0x1c(%rcx), %esi
andl %r10d, %esi
cmpl %r14d, %esi
jb 0x44f1
cmpl %r12d, (%rcx)
jl 0x44f1
movq (%rdi,%rax,8), %rsi
movslq %edx, %r8
incl %edx
movq %rsi, (%rdi,%r8,8)
incq %rax
addq $0x28, %rcx
cmpq %rax, %r9
jne 0x44d4
movslq %edx, %rsi
shlq $0x3, %rsi
jmp 0x45eb
movq %rcx, 0x28(%rsp)
cmpl $0x0, 0x30(%r11)
jle 0x45fd
movq 0x28(%rsp), %rax
movq 0x18(%rax), %r12
xorl %ebx, %ebx
movq 0x18(%rsp), %r13
movq %r12, 0x40(%rsp)
xorl %ebp, %ebp
movl (%r13), %eax
andl %r10d, %eax
cmpl %r14d, %eax
jb 0x456b
movl 0xc(%rsp), %eax
cmpl %eax, -0x1c(%r13)
jl 0x456b
movslq %ebp, %rbp
movq 0x40(%rsp), %rax
leaq (%rax,%rbp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1220
movl $0x800, %r10d           # imm = 0x800
movq 0x10(%rsp), %r11
addl %r15d, %ebp
incq %rbx
movslq 0x30(%r11), %rax
addq %r15, %r12
addq $0x28, %r13
cmpq %rax, %rbx
jl 0x4530
jmp 0x45ff
testl %ecx, %ecx
jle 0x45c5
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
xorl %ebp, %ebp
movq %rsi, %r15
movl (%rdx), %esi
andl %r10d, %esi
cmpl %r14d, %esi
jb 0x45b3
cmpl %r12d, -0x1c(%rdx)
jl 0x45b3
movq (%rax,%rcx,8), %rsi
movslq %ebp, %rbp
movq %rsi, (%rax,%rbp,8)
movl (%rdi,%rcx,4), %esi
movl %esi, (%rdi,%rbp,4)
incl %ebp
incq %rcx
movslq 0x30(%r11), %rsi
addq $0x28, %rdx
cmpq %rsi, %rcx
jl 0x4590
jmp 0x45ca
xorl %ebp, %ebp
movq %rsi, %r15
movslq %ebp, %rbx
leaq (,%rbx,4), %rsi
xorl %edx, %edx
callq 0x2bdd
movq %rax, 0x20(%r15)
incl %ebx
imull 0x10(%r15), %ebx
jmp 0x4615
xorl %esi, %esi
xorl %edx, %edx
callq 0x2bdd
movq %rax, 0x10(%r15)
movq 0x10(%rsp), %r11
jmp 0x4637
xorl %ebp, %ebp
movq 0x28(%rsp), %r15
movl 0x10(%r15), %ebx
imull %ebp, %ebx
movl 0xc(%rsp), %r12d
movq 0x38(%rsp), %r13
movq 0x18(%r15), %rdi
movslq %ebx, %rsi
xorl %edx, %edx
callq 0x2bdd
movq %rax, 0x18(%r15)
movl %ebp, 0x14(%r15)
movq 0x10(%rsp), %r11
leaq 0x6fcc(%rip), %rbx      # 0xb603
movl $0x800, %r10d           # imm = 0x800
movq (%r15), %r15
testq %r15, %r15
jne 0x43dc
movq 0x20(%rsp), %rax
testl %eax, %eax
jle 0x4707
leaq (,%rax,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(%r13,%rdi), %r10d
movl %r10d, %r8d
shll $0x15, %r8d
sarl $0x1f, %r8d
andl %r9d, %r8d
andl $0x800, %r10d           # imm = 0x800
cmpl %r14d, %r10d
jb 0x46f8
movl 0x4(%r13,%rdi), %r9d
cmpl %r12d, %r9d
jl 0x46f8
leaq (%rdi,%r13), %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(%r13,%r9,8)
movups (%r10), %xmm0
movups 0x10(%r10), %xmm1
movups %xmm1, 0x10(%r13,%r9,8)
movups %xmm0, (%r13,%r9,8)
xorl %r10d, %r10d
testl %r8d, %r8d
setne %r10b
andl $0xfffffbff, %r11d      # imm = 0xFFFFFBFF
shll $0xa, %r10d
orl %r11d, %r10d
movq 0x10(%rsp), %r11
movl %r10d, 0x20(%r13,%r9,8)
incl %eax
movl $0x1, %r8d
addq $0x28, %rdi
cmpq %rdi, %rsi
jne 0x466d
jmp 0x470d
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %rdx, 0x28(%r11)
movl %ecx, 0x20(%r11)
movl %eax, 0x30(%r11)
movl $0x1, 0x34(%r11)
cmpl 0x20(%rsp), %eax
jge 0x474e
addq $-0x28, %r13
cltq
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x50, %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r11, %rbx
callq 0x2bdd
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 0x474e