// File: /workspace/github_c_makefile_star2_O2/thegenemyers[P]DASCRUBBER/DB.c
// Function: Open_Track
DAZZ_TRACK *Open_Track(DAZZ_DB *db, char *track)
{ FILE       *afile, *dfile;
  int         tracklen, size;
  int         nreads, ispart;
  int         treads, ureads;
  int64       dmax;
  void       *anno;
  int        *alen;
  void       *data;
  char       *name;
  DAZZ_TRACK *record;

  if (track[0] == '.')
    { EPRINTF(EPLACE,"%s: Track name, '%s', cannot begin with a .\n",Prog_Name,track);
      EXIT(NULL);
    }

  for (record = db->tracks; record != NULL; record = record->next)
    if (strcmp(record->name,track) == 0)
      return (record);

  afile = NULL;
  if (db->part)
    { afile  = fopen(MyCatenate(db->path,MyNumbered_Suffix(".",db->part,"."),track,".anno"),"r");
      ispart = 1;
    }
  if (afile == NULL)
    { afile = fopen(MyCatenate(db->path,".",track,".anno"),"r");
      ispart = 0;
    }
  if (afile == NULL)
    { EPRINTF(EPLACE,"%s: Track '%s' does not exist\n",Prog_Name,track);
      return (NULL);
    }

  dfile  = NULL;
  anno   = NULL;
  alen   = NULL;
  data   = NULL;
  record = NULL;

  if (ispart)
    name = MyCatenate(db->path,MyNumbered_Suffix(".",db->part,"."),track,".data");
  else
    name = MyCatenate(db->path,".",track,".data");
  if (name == NULL)
    goto error;
  dfile = fopen(name,"r");

  if (fread(&tracklen,sizeof(int),1,afile) != 1)
    { EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
      goto error;
    }
  if (fread(&size,sizeof(int),1,afile) != 1)
    { EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
      goto error;
    }

  if (size < 0)
    { EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
      goto error;
    }
  if (size == 0)
    size = 8;

  if (ispart)
    { ureads = ((int *) (db->reads))[-1];
      treads = ((int *) (db->reads))[-2];
    }
  else
    { ureads = db->ureads;
      treads = db->treads;
    }

  if (db->trimmed)
    { if (tracklen != treads && tracklen != ureads)
        { EPRINTF(EPLACE,"%s: Track '%s' not same size as database !\n",Prog_Name,track);
          goto error;
        }
      if ( ! ispart && db->part > 0)
        { if (tracklen == treads)
            fseeko(afile,size*db->tfirst,SEEK_CUR);
          else
            fseeko(afile,size*db->ufirst,SEEK_CUR);
        }
    }
  else
    { if (tracklen != ureads)
        { if (tracklen == treads)
            EPRINTF(EPLACE,"%s: Track '%s' is for a trimmed DB !\n",Prog_Name,track);
          else
            EPRINTF(EPLACE,"%s: Track '%s' not same size as database !\n",Prog_Name,track);
          goto error;
        }
      if ( ! ispart && db->part > 0)
        fseeko(afile,size*db->ufirst,SEEK_CUR);
    }
  if (tracklen == treads)
    nreads = ((int *) (db->reads))[-2];
  else
    nreads = ((int *) (db->reads))[-1];

  anno = (void *) Malloc(size*(nreads+1),"Allocating Track Anno Vector");
  if (anno == NULL)
    goto error;

  if (dfile != NULL)
    { int64 *anno8;
      int   *anno4;
      int64  x, y;
      int    i;

      alen = (int *)  Malloc(sizeof(int)*nreads,"Allocating Track Anno Lengths");
      if (alen == NULL)
        goto error;

      if (fread(anno,size,nreads+1,afile) != (size_t) (nreads+1))
        { EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
          goto error;
        }

      dmax = 0;
      if (size == 4)
        { anno4 = (int *) anno;
          y = anno4[0];
          for (i = 1; i <= nreads; i++)
            { x = anno4[i];
              y = x-y;
              if (y > dmax)
                dmax = y; 
              alen[i-1] = y;
              y = x;
            }
        }
      else
        { anno8 = (int64 *) anno;
          y = anno8[0];
          for (i = 1; i <= nreads; i++)
            { x = anno8[i];
              y = x-y;
              if (y > dmax)
                dmax = y; 
              alen[i-1] = y;
              y = x;
            }
        }
    }
  else
    { dmax = 0;
      if (fread(anno,size,nreads,afile) != (size_t) nreads)
        { EPRINTF(EPLACE,"%s: Track '%s' annotation file is junk\n",Prog_Name,track);
          goto error;
        }
    }

  fclose(afile);

  record = (DAZZ_TRACK *) Malloc(sizeof(DAZZ_TRACK),"Allocating Track Record");
  if (record == NULL)
    goto error;
  record->name = Strdup(track,"Allocating Track Name");
  if (record->name == NULL)
    goto error;
  if (dfile == NULL)
    record->data = NULL;
  else
    record->data = (void *) dfile;
  record->anno   = anno;
  record->alen   = alen;
  record->size   = size;
  record->nreads = nreads;
  record->loaded = 0;
  record->dmax   = dmax;

  if (db->trimmed && tracklen != treads)
    { if (Late_Track_Trim(db,record,ispart))
        goto error;
    }

  if (db->tracks != NULL && (db->tracks->name == qtrack_name || db->tracks->name == atrack_name))
    { record->next     = db->tracks->next;
      db->tracks->next = record;
    }
  else
    { record->next = db->tracks;
      db->tracks   = record;
    }

  return (record);

error:
  if (record != NULL)
    free(record);
  if (data != NULL)
    free(data);
  if (alen != NULL)
    free(alen);
  if (anno != NULL)
    free(anno);
  if (dfile != NULL)
    fclose(dfile);
  fclose(afile);
  EXIT (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
cmpb $0x2e, (%rsi)
je 0x5032
movq %rdi, %r13
leaq 0x68(%rdi), %r15
movq %r15, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x4855
movq 0x8(%rbp), %rdi
movq %r12, %rsi
callq 0x1190
testl %eax, %eax
jne 0x4837
jmp 0x4f60
movl 0x38(%r13), %esi
testl %esi, %esi
je 0x48cc
movq 0x48(%r13), %rbx
leaq 0x4c86(%rip), %rdi      # 0x94ee
movq %rdi, %rdx
callq 0x4744
leaq 0x4fec(%rip), %rcx      # 0x9863
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x31cf
leaq 0x54c5(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x48cc
movq %rax, %r14
movq 0x48(%r13), %rbx
movl 0x38(%r13), %esi
leaq 0x4c43(%rip), %rdi      # 0x94ee
movq %rdi, %rdx
callq 0x4744
leaq 0x5021(%rip), %rcx      # 0x98db
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x31cf
xorl %ebx, %ebx
jmp 0x491d
movq 0x48(%r13), %rdi
leaq 0x4c17(%rip), %rsi      # 0x94ee
leaq 0x4f85(%rip), %rcx      # 0x9863
movq %r12, %rdx
callq 0x31cf
leaq 0x5464(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x4f72
movq %rax, %r14
movq 0x48(%r13), %rdi
leaq 0x4be2(%rip), %rsi      # 0x94ee
leaq 0x4fc8(%rip), %rcx      # 0x98db
movq %r12, %rdx
callq 0x31cf
movb $0x1, %bl
testq %rax, %rax
je 0x5059
leaq 0x5424(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1240
movq %rax, 0x10(%rsp)
leaq 0x34(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x4cc0
leaq 0x30(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x4cc0
movl 0x30(%rsp), %ecx
testl %ecx, %ecx
js 0x4cc0
pushq $0x8
popq %rax
cmovel %eax, %ecx
movq %rcx, 0x20(%rsp)
testb %bl, %bl
movl %ebx, 0x1c(%rsp)
je 0x499c
leaq 0x4(%r13), %rax
movq %r13, %rcx
jmp 0x49a8
movq 0x60(%r13), %rax
leaq -0x4(%rax), %rcx
addq $-0x8, %rax
movl (%rcx), %ecx
movl (%rax), %ebx
cmpl $0x0, 0x34(%r13)
movl 0x34(%rsp), %ebp
movslq 0x20(%rsp), %rax
movq %r14, (%rsp)
je 0x4a01
cmpl %ebx, %ebp
sete %dl
cmpl %ecx, %ebp
sete %cl
orb %dl, %cl
je 0x4f98
cmpb $0x0, 0x1c(%rsp)
je 0x4a2c
cmpl $0x0, 0x38(%r13)
jle 0x4a2c
cmpl %ebx, %ebp
jne 0x4a19
movslq 0x40(%r13), %rsi
imulq %rax, %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x1230
movb $0x1, 0x28(%rsp)
pushq $-0x8
popq %rax
jmp 0x4a41
cmpl %ecx, %ebp
jne 0x4fd8
cmpb $0x0, 0x1c(%rsp)
je 0x4a2e
cmpl $0x0, 0x38(%r13)
movl %ecx, %ebp
jle 0x4a2e
movslq 0x3c(%r13), %rsi
imulq %rax, %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x1230
movl %ebp, %ecx
xorl %eax, %eax
cmpl %ebx, %ecx
sete 0x28(%rsp)
setne %al
shll $0x2, %eax
orq $-0x8, %rax
movq %r13, 0x40(%rsp)
movq 0x60(%r13), %rcx
movl (%rcx,%rax), %eax
movl %eax, 0xc(%rsp)
movslq %eax, %rbp
leaq 0x1(%rbp), %r14
movq 0x20(%rsp), %r13
movl %r13d, %eax
imull %r14d, %eax
movslq %eax, %rdi
leaq 0x4eed(%rip), %rsi      # 0x995b
callq 0x1bad
testq %rax, %rax
je 0x506c
movq %rax, %rbx
movq %r15, 0x38(%rsp)
movl %r13d, %r13d
movq 0x10(%rsp), %rax
testq %rax, %rax
movq %r13, 0x48(%rsp)
je 0x4b1c
shlq $0x2, %rbp
leaq 0x4ed3(%rip), %rsi      # 0x9978
movq %rbp, %rdi
callq 0x1bad
testq %rax, %rax
je 0x506e
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq %r14, %rax
jne 0x4fab
cmpl $0x4, 0x20(%rsp)
jne 0x4b77
movslq (%rbx), %rdx
xorl %eax, %eax
movl 0xc(%rsp), %esi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
xorl %r14d, %r14d
cmpq %rax, %rcx
je 0x4bb0
movslq 0x4(%rbx,%rax,4), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
cmpq %r14, %rdi
cmovgq %rdi, %r14
movl %edi, (%r15,%rax,4)
incq %rax
movq %rsi, %rdx
jmp 0x4af5
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq (%rsp), %r13
movq %r13, %rcx
callq 0x10b0
xorl %r14d, %r14d
movl $0x0, %r15d
cmpq %rbp, %rax
je 0x4bb4
movq 0x94b2(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xbd98(%rip), %rdx      # 0x108e8
leaq 0x4d8a(%rip), %rsi      # 0x98e1
xorl %r15d, %r15d
movq %r12, %rcx
xorl %eax, %eax
callq 0x11a0
xorl %ebp, %ebp
movq %r13, %r14
movq $0x0, 0x10(%rsp)
jmp 0x4ce9
movq (%rbx), %rdx
xorl %eax, %eax
movl 0xc(%rsp), %esi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
xorl %r14d, %r14d
cmpq %rax, %rcx
je 0x4bb0
movq 0x8(%rbx,%rax,8), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
cmpq %r14, %rdi
cmovgq %rdi, %r14
movl %edi, (%r15,%rax,4)
incq %rax
movq %rsi, %rdx
jmp 0x4b8d
movq (%rsp), %r13
movq %r13, %rdi
callq 0x10c0
leaq 0x4dd3(%rip), %rsi      # 0x9996
pushq $0x40
popq %rdi
callq 0x1bad
testq %rax, %rax
je 0x5071
movq %rax, %rbp
leaq 0x4dd0(%rip), %rsi      # 0x99ae
movq %r12, %rdi
callq 0x1c68
movq %rax, 0x8(%rbp)
testq %rax, %rax
movq 0x10(%rsp), %rdx
movq 0x40(%rsp), %r13
je 0x4fcf
movq %rdx, 0x28(%rbp)
movq %rbx, 0x18(%rbp)
movq %r15, 0x20(%rbp)
movq 0x20(%rsp), %rcx
movl %ecx, 0x10(%rbp)
movl 0xc(%rsp), %eax
movl %eax, 0x14(%rbp)
andl $0x0, 0x30(%rbp)
movq %r14, 0x38(%rbp)
cmpl $0x0, 0x34(%r13)
sete %al
orb 0x28(%rsp), %al
jne 0x4f1e
movl 0x8(%r13), %eax
movl 0xc(%r13), %r12d
movl %eax, 0xc(%rsp)
testl %eax, %eax
setg %al
testb $0x1, %r12b
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x4f1e
notl %r12d
andl $0x1, %r12d
movq 0x48(%r13), %r14
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x1250
movq %rax, 0x28(%rsp)
leaq 0x4ce3(%rip), %rdx      # 0x995a
leaq 0x4872(%rip), %rcx      # 0x94f0
movq %r14, %rdi
movq %rdx, %rsi
callq 0x31cf
leaq 0x50c1(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
movq %rax, %rdi
movslq 0x3c(%r13), %rax
imulq $0x28, %rax, %rsi
addq $0x70, %rsi
movq %rdi, (%rsp)
xorl %edx, %edx
callq 0x1230
cmpb $0x0, 0x1c(%rsp)
je 0x4d20
movq 0x10(%rsp), %rax
jmp 0x4d2d
movq 0x9331(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xbc17(%rip), %rdx      # 0x108e8
leaq 0x4c09(%rip), %rsi      # 0x98e1
xorl %ebx, %ebx
movq %r12, %rcx
xorl %eax, %eax
callq 0x11a0
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4d10
callq 0x10c0
movq %r14, %rdi
callq 0x10c0
pushq $0x1
popq %rdi
callq 0x1270
movq 0x10(%rsp), %rax
movq 0x60(%r13), %r13
addq $-0x4, %r13
shll $0xb, %r12d
movq 0x28(%rsp), %rcx
addq $0x2, %rcx
movl (%r13), %r13d
testq %rax, %rax
movq %rcx, 0x28(%rsp)
je 0x4dd3
movq %rbx, 0x10(%rsp)
xorl %ebx, %ebx
testl %r13d, %r13d
cmovlel %ebx, %r13d
cmpl $0x4, 0x20(%rsp)
movl %r12d, 0x1c(%rsp)
jne 0x4e5a
xorl %r14d, %r14d
xorl %r12d, %r12d
movslq %r12d, %rbx
cmpq %r14, %r13
je 0x4ebe
leaq 0x50(%rsp), %rdi
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x5005
movl 0x70(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
andl %ecx, %eax
cmpl 0x1c(%rsp), %eax
jb 0x4dce
movl 0xc(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jl 0x4dce
movq 0x10(%rsp), %rcx
movl (%rcx,%r14,4), %eax
movl %eax, (%rcx,%rbx,4)
movl (%r15,%r14,4), %eax
movl %eax, (%r15,%rbx,4)
incl %ebx
movl %ebx, %r12d
incq %r14
jmp 0x4d70
movq 0x20(%rsp), %rax
addl %eax, %eax
movl %eax, 0x10(%rsp)
xorl %r14d, %r14d
testl %r13d, %r13d
cmovlel %r14d, %r13d
xorl %r15d, %r15d
subl $0x1, %r13d
jb 0x4e4c
leaq 0x50(%rsp), %rdi
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x5005
movl 0x70(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
andl %ecx, %eax
cmpl %r12d, %eax
jb 0x4e45
movl 0xc(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jl 0x4e45
movslq %r15d, %r15
leaq (%rbx,%r15), %rdi
movslq %r14d, %rsi
addq %rbx, %rsi
movq 0x48(%rsp), %rdx
callq 0x1220
addl 0x20(%rsp), %r15d
addl 0x10(%rsp), %r14d
jmp 0x4deb
movq 0x20(%rsp), %rax
imull %r15d, %eax
jmp 0x4f04
xorl %r12d, %r12d
movslq %ebx, %r14
cmpq %r12, %r13
je 0x4edd
leaq 0x50(%rsp), %rdi
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x5005
movl 0x70(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
andl %ecx, %eax
cmpl 0x1c(%rsp), %eax
jb 0x4eb9
movl 0xc(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jl 0x4eb9
movq 0x10(%rsp), %rcx
movq (%rcx,%r12,8), %rax
movq %rax, (%rcx,%r14,8)
movl (%r15,%r12,4), %eax
movl %eax, (%r15,%r14,4)
incl %r14d
movl %r14d, %ebx
incq %r12
jmp 0x4e5d
leaq (,%rbx,4), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1c05
movq %rax, 0x20(%rbp)
leal 0x4(,%rbx,4), %eax
jmp 0x4eff
leaq (,%r14,4), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1c05
movq %rax, 0x20(%rbp)
incl %r14d
movq 0x20(%rsp), %rax
imull %r14d, %eax
movq 0x10(%rsp), %rbx
movslq %eax, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1c05
movq %rax, 0x18(%rbp)
movq (%rsp), %rdi
callq 0x10c0
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x4f59
movq 0x8(%rax), %rcx
leaq 0x46cd(%rip), %rdx      # 0x9603
cmpq %rdx, %rcx
setne %dl
leaq 0x4f13(%rip), %rsi      # 0x9e56
cmpq %rsi, %rcx
setne %cl
testb %cl, %dl
jne 0x4f59
movq (%rax), %rcx
movq %rcx, (%rbp)
movq %rbp, (%rax)
jmp 0x4f60
movq %rax, (%rbp)
movq %rbp, (%rdi)
movq %rbp, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x907f(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xb965(%rip), %rdx      # 0x108e8
leaq 0x4932(%rip), %rsi      # 0x98bc
xorl %ebp, %ebp
movq %r12, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x4f60
movq 0x9059(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xb93f(%rip), %rdx      # 0x108e8
jmp 0x4ff9
movq 0x9046(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xb92c(%rip), %rdx      # 0x108e8
leaq 0x491e(%rip), %rsi      # 0x98e1
xorl %ebp, %ebp
movq %r12, %rcx
xorl %eax, %eax
callq 0x11a0
movq (%rsp), %r14
jmp 0x4ce9
movq 0x9019(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xb8ff(%rip), %rdx      # 0x108e8
cmpl %ebx, %ebp
jne 0x4ff9
leaq 0x4941(%rip), %rsi      # 0x9935
jmp 0x4cd8
leaq 0x4909(%rip), %rsi      # 0x9909
jmp 0x4cd8
movq 0x8fec(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xb8d2(%rip), %rdx      # 0x108e8
leaq 0x44d8(%rip), %rsi      # 0x94f5
movq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0x11a0
movq (%rsp), %rdi
jmp 0x4d13
movq 0x8fbf(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xb8a5(%rip), %rdx      # 0x108e8
leaq 0x4845(%rip), %rsi      # 0x988f
movq %r12, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x4d18
xorl %ebx, %ebx
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
jmp 0x4ce7
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %ebp, %ebp
jmp 0x4fcf