// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x46be
jmp 0x4e0b
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jg 0x46dc
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x46dc
jmp 0x4e0b
movq -0x8(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x4752
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
cmpq 0xdc38(%rip), %rax      # 0x12340
jne 0x471d
movq -0x40(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x471b
movl $0x1, -0x4c(%rbp)
jmp 0x4743
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
cmpq 0xdc1c(%rip), %rax      # 0x12348
je 0x4741
movq -0x40(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x473f
movl $0x1, -0x4c(%rbp)
jmp 0x4741
jmp 0x4743
jmp 0x4745
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x46f2
cmpl $0x0, -0x4c(%rbp)
je 0x477c
movq 0xd899(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movq 0x1031f(%rip), %rdx     # 0x14a88
leaq 0x9e63(%rip), %rsi      # 0xe5d3
movb $0x0, %al
callq 0x1180
jmp 0x4e0b
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x479e
movl $0x0, -0x1c(%rbp)
jmp 0x47a5
movl $0x800, -0x1c(%rbp)     # imm = 0x800
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x4c6a
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
cmpq 0xdb67(%rip), %rax      # 0x12348
jne 0x4874
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x486f
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl -0x1c(%rbp), %eax
jl 0x4862
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jl 0x4862
movq -0x58(%rbp), %rax
movslq -0xc(%rbp), %rcx
movw (%rax,%rcx,2), %dx
movq -0x58(%rbp), %rax
movl -0x10(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x10(%rbp)
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x4864
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x4801
jmp 0x4c58
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
cmpq 0xdabd(%rip), %rax      # 0x12340
jne 0x493a
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x4915
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl -0x1c(%rbp), %eax
jl 0x4908
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jl 0x4908
movq -0x68(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x68(%rbp), %rax
movl -0x10(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x10(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x490a
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x48ab
movq -0x68(%rbp), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x3, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x4c56
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4a20
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x49f1
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl -0x1c(%rbp), %eax
jl 0x49db
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jl 0x49db
movq -0x78(%rbp), %rdi
movslq -0x10(%rbp), %rax
addq %rax, %rdi
movq -0x78(%rbp), %rsi
movslq -0x14(%rbp), %rax
addq %rax, %rsi
movslq -0x6c(%rbp), %rdx
callq 0x1210
movl -0x6c(%rbp), %eax
addl -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x49dd
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
movl -0x6c(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x4974
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
imull -0x10(%rbp), %eax
movslq %eax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x4c4c
cmpl $0x4, -0x6c(%rbp)
jne 0x4b35
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x4add
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl -0x1c(%rbp), %eax
jl 0x4acd
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jl 0x4acd
movq -0x80(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x88(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x88(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x4acf
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x4a53
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x2, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movl -0x10(%rbp), %ecx
addl $0x1, %ecx
imull %ecx, %eax
movslq %eax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x4c4a
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x4bf7
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl -0x1c(%rbp), %eax
jl 0x4be7
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x20(%rbp), %eax
jl 0x4be7
movq -0x90(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x90(%rbp), %rax
movslq -0x10(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x98(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x98(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x4be9
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x4b61
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x2, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
movl -0x10(%rbp), %ecx
addl $0x1, %ecx
imull %ecx, %eax
movslq %eax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x4c4c
movl -0x10(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x4c58
jmp 0x4c5a
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x47c7
movl $0x0, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x4da0
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
jne 0x4cc3
movl $0x0, -0x24(%rbp)
movq -0x48(%rbp), %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
andl $0x800, %eax            # imm = 0x800
cmpl -0x1c(%rbp), %eax
jl 0x4d90
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0x4d90
movslq -0x14(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x4d0f
movl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rdi
movslq -0x10(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
movslq -0xc(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movl $0x28, %edx
callq 0x11c0
cmpl $0x0, -0x24(%rbp)
je 0x4d64
movq -0x48(%rbp), %rax
movl -0x10(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x10(%rbp)
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %ecx
orl $0x400, %ecx            # imm = 0x400
movl %ecx, 0x20(%rax)
jmp 0x4d89
movq -0x48(%rbp), %rax
movl -0x10(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x10(%rbp)
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %ecx
andl $0xfffffbff, %ecx       # imm = 0xFFFFFBFF
movl %ecx, 0x20(%rax)
movl $0x1, -0x24(%rbp)
jmp 0x4d92
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x4c8e
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x28(%rax)
movl -0x34(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20(%rax)
movl -0x10(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x34(%rax)
movl -0x10(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x4e0b
movq -0x48(%rbp), %rdi
addq $-0x28, %rdi
movl -0x10(%rbp), %eax
addl $0x2, %eax
cltq
imulq $0x28, %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2340
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rcx
addq $0x28, %rcx
movq %rcx, 0x60(%rax)
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)