// File: /workspace/github_c_makefile_star2_O1/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, %r15
cmpb $0x2e, (%rsi)
je 0x619d
movq %rdi, %r12
leaq 0x68(%rdi), %r14
movq %r14, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x59ed
movq 0x8(%rbp), %rdi
movq %r15, %rsi
callq 0x1190
testl %eax, %eax
jne 0x59cf
jmp 0x60dc
movl 0x38(%r12), %esi
testl %esi, %esi
je 0x5a33
movq 0x48(%r12), %rbx
leaq 0x5aec(%rip), %rdi      # 0xb4ee
movq %rdi, %rdx
callq 0x58f8
leaq 0x5e52(%rip), %rcx      # 0xb863
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x4215
leaq 0x632b(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x1240
movq %rax, %rbp
jmp 0x5a35
xorl %ebp, %ebp
movq %rbp, %r13
testq %rbp, %rbp
jne 0x5a6a
movq 0x48(%r12), %rdi
leaq 0x5aa5(%rip), %rsi      # 0xb4ee
leaq 0x5e13(%rip), %rcx      # 0xb863
movq %r15, %rdx
callq 0x4215
leaq 0x62f2(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x1240
movq %rax, %r13
testq %r13, %r13
je 0x6025
movq 0x48(%r12), %rbx
testq %rbp, %rbp
je 0x5aa0
movl 0x38(%r12), %esi
leaq 0x5a65(%rip), %rdi      # 0xb4ee
movq %rdi, %rdx
callq 0x58f8
leaq 0x5e43(%rip), %rcx      # 0xb8db
movq %rbx, %rdi
movq %rax, %rsi
jmp 0x5ab1
leaq 0x5a47(%rip), %rsi      # 0xb4ee
leaq 0x5e2d(%rip), %rcx      # 0xb8db
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4215
testq %rax, %rax
je 0x61d2
leaq 0x6288(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x1240
movq %rax, %rbx
leaq 0x34(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x10b0
cmpq $0x1, %rax
movq %rbx, 0x20(%rsp)
jne 0x6034
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x6041
movl 0x14(%rsp), %eax
testl %eax, %eax
js 0x604e
movl $0x8, %ecx
cmovel %ecx, %eax
movl %eax, 0x14(%rsp)
testq %rbp, %rbp
je 0x5b47
movq 0x60(%r12), %rcx
leaq -0x4(%rcx), %rdx
addq $-0x8, %rcx
jmp 0x5b4f
leaq 0x4(%r12), %rcx
movq %r12, %rdx
movl (%rdx), %edx
movl (%rcx), %edi
cmpl $0x0, 0x34(%r12)
movl 0x34(%rsp), %ecx
movl %edi, 0x38(%rsp)
je 0x5b8f
cmpl %edi, %ecx
sete %sil
cmpl %edx, %ecx
sete %dl
orb %sil, %dl
je 0x60ee
testq %rbp, %rbp
jne 0x5bc4
cmpl $0x0, 0x38(%r12)
jle 0x5bc4
cmpl %edi, %ecx
jne 0x5ba4
leaq 0x40(%r12), %rcx
jmp 0x5ba9
cmpl %edx, %ecx
jne 0x614c
testq %rbp, %rbp
jne 0x5bc4
cmpl $0x0, 0x38(%r12)
jle 0x5bc4
leaq 0x3c(%r12), %rcx
movslq (%rcx), %rcx
movslq %eax, %rsi
imulq %rcx, %rsi
movq %r13, %rdi
movl $0x1, %edx
callq 0x1230
movl 0x38(%rsp), %edi
movq %rbp, 0x48(%rsp)
movq %r13, 0x8(%rsp)
movq %r14, 0x40(%rsp)
xorl %eax, %eax
cmpl %edi, 0x34(%rsp)
setne %al
shll $0x2, %eax
orq $-0x8, %rax
movq %r12, 0x28(%rsp)
movq 0x60(%r12), %rcx
movl (%rcx,%rax), %eax
movl %eax, 0x18(%rsp)
movslq %eax, %r13
movslq 0x14(%rsp), %rbp
leaq 0x1(%r13), %r14
movq %rbp, %rdi
imulq %r14, %rdi
leaq 0x5d4d(%rip), %rsi      # 0xb95b
callq 0x2b85
testq %rax, %rax
je 0x61da
movq %rax, %r12
testq %rbx, %rbx
je 0x5cbe
shlq $0x2, %r13
leaq 0x5d45(%rip), %rsi      # 0xb978
movq %r13, %rdi
callq 0x2b85
movq %rax, %rbx
movl $0x5, %r8d
testq %rax, %rax
je 0x5cfc
movslq %r14d, %r13
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
callq 0x10b0
cmpq %r13, %rax
jne 0x616c
cmpl $0x4, 0x14(%rsp)
jne 0x5d03
cmpl $0x0, 0x18(%rsp)
movq 0x8(%rsp), %r13
jle 0x5d43
movslq (%r12), %rdx
movl %r14d, %eax
decq %rax
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %r14d, %r14d
movslq 0x4(%r12,%rcx,4), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
cmpq %r14, %rdi
cmovgq %rdi, %r14
movl %edi, (%rbx,%rcx,4)
incq %rcx
movq %rsi, %rdx
cmpq %rcx, %rax
jne 0x5c99
jmp 0x5d49
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq 0x8(%rsp), %rbp
movq %rbp, %rcx
callq 0x10b0
xorl %r14d, %r14d
movl $0x0, %ebx
cmpq %r13, %rax
movq %rbp, %r13
je 0x5d5d
movq %r15, %rdi
callq 0x195f
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
jmp 0x60fb
movq 0x8(%rsp), %r13
jmp 0x5d49
cmpl $0x0, 0x18(%rsp)
movq 0x8(%rsp), %r13
jle 0x5d43
movq (%r12), %rdx
movl %r14d, %eax
decq %rax
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %r14d, %r14d
movq 0x8(%r12,%rcx,8), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
cmpq %r14, %rdi
cmovgq %rdi, %r14
movl %edi, (%rbx,%rcx,4)
incq %rcx
movq %rsi, %rdx
cmpq %rcx, %rax
jne 0x5d21
jmp 0x5d49
xorl %r14d, %r14d
xorl %r8d, %r8d
testl %r8d, %r8d
je 0x5d5d
cmpl $0x5, %r8d
jne 0x60dc
jmp 0x60fb
movq %r13, %rdi
callq 0x10c0
leaq 0x5c2a(%rip), %rsi      # 0xb996
movl $0x40, %edi
callq 0x2b85
testq %rax, %rax
je 0x60fb
movq %rax, %rbp
leaq 0x5c25(%rip), %rsi      # 0xb9ae
movq %r15, %rdi
callq 0x2c42
movq %rax, 0x8(%rbp)
testq %rax, %rax
je 0x60fd
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rbp)
movq %r12, 0x18(%rbp)
movq %rbx, 0x20(%rbp)
movl 0x14(%rsp), %eax
movl %eax, 0x10(%rbp)
movl 0x18(%rsp), %eax
movl %eax, 0x14(%rbp)
movl $0x0, 0x30(%rbp)
movq %r14, 0x38(%rbp)
movq 0x28(%rsp), %r14
cmpl $0x0, 0x34(%r14)
movq 0x40(%rsp), %rdi
je 0x609f
movl 0x38(%rsp), %eax
cmpl %eax, 0x34(%rsp)
je 0x609f
movl 0x8(%r14), %eax
movl %eax, 0x20(%rsp)
testl %eax, %eax
jg 0x5e02
testb $0x1, 0xc(%r14)
jne 0x609f
movl 0xc(%r14), %r15d
notl %r15d
andl $0x1, %r15d
movq 0x48(%r14), %rbx
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x1250
movq %rax, %r13
leaq 0x5b32(%rip), %rdx      # 0xb95a
leaq 0x56c1(%rip), %rcx      # 0xb4f0
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x4215
leaq 0x5f10(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
movq %rax, %rdi
movslq 0x3c(%r14), %rax
leaq (%rax,%rax,4), %rax
leaq 0x70(,%rax,8), %rsi
movq %rdi, 0x8(%rsp)
xorl %edx, %edx
callq 0x1230
cmpq $0x0, 0x48(%rsp)
je 0x5e78
movq 0x60(%r14), %r14
addq $-0x4, %r14
shll $0xb, %r15d
addq $0x2, %r13
movl (%r14), %r12d
movslq 0x10(%rbp), %r14
cmpq $0x0, 0x28(%rbp)
je 0x5f22
movq 0x18(%rbp), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rbp), %rbx
cmpl $0x4, %r14d
jne 0x5faf
testl %r12d, %r12d
jle 0x605b
movq %r13, 0x28(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movl $0x28, %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x61b4
movl 0x70(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
andl %ecx, %eax
cmpl %r15d, %eax
jb 0x5f15
movl 0x20(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jl 0x5f15
movq 0x18(%rsp), %rcx
movl (%rcx,%r14,4), %eax
movslq %r13d, %r13
movl %eax, (%rcx,%r13,4)
movl (%rbx,%r14,4), %eax
movl %eax, (%rbx,%r13,4)
incl %r13d
incq %r14
cmpq %r14, %r12
jne 0x5ebd
jmp 0x605e
testl %r12d, %r12d
jle 0x6145
movq %r13, 0x28(%rsp)
movq 0x18(%rbp), %rax
movq %rax, 0x38(%rsp)
leal (%r14,%r14), %eax
movl %eax, 0x18(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movl $0x28, %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x61a5
movl 0x70(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
andl %ecx, %eax
cmpl %r15d, %eax
jb 0x5fa0
movl 0x20(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jl 0x5fa0
movslq %ebx, %rbx
movq 0x38(%rsp), %rax
leaq (%rax,%rbx), %rdi
movslq %r13d, %rsi
addq %rax, %rsi
movq %r14, %rdx
callq 0x1220
addl %r14d, %ebx
addl 0x18(%rsp), %r13d
decl %r12d
jne 0x5f46
jmp 0x607a
testl %r12d, %r12d
jle 0x605b
movq %r13, 0x28(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movl $0x28, %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x61c3
movl 0x70(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
andl %ecx, %eax
cmpl %r15d, %eax
jb 0x601b
movl 0x20(%rsp), %eax
cmpl %eax, 0x54(%rsp)
jl 0x601b
movq 0x18(%rsp), %rcx
movq (%rcx,%r14,8), %rax
movslq %r13d, %r13
movq %rax, (%rcx,%r13,8)
movl (%rbx,%r14,4), %eax
movl %eax, (%rbx,%r13,4)
incl %r13d
incq %r14
cmpq %r14, %r12
jne 0x5fc3
jmp 0x605e
movq %r15, %rdi
callq 0x1a45
xorl %ebp, %ebp
jmp 0x60dc
movq %r15, %rdi
callq 0x18d7
jmp 0x60f6
movq %r15, %rdi
callq 0x18f9
jmp 0x60f6
movq %r15, %rdi
callq 0x1a23
jmp 0x60f6
xorl %r13d, %r13d
movq 0x20(%rbp), %rdi
movslq %r13d, %rbx
leaq (,%rbx,4), %rsi
xorl %edx, %edx
callq 0x2bdd
movq %rax, 0x20(%rbp)
incl %ebx
imull 0x10(%rbp), %ebx
movq 0x18(%rbp), %rdi
movslq %ebx, %rsi
xorl %edx, %edx
callq 0x2bdd
movq %rax, 0x18(%rbp)
movq 0x8(%rsp), %rdi
callq 0x10c0
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x60d5
movq 0x8(%rax), %rcx
leaq 0x5551(%rip), %rdx      # 0xb603
cmpq %rdx, %rcx
setne %dl
leaq 0x5d97(%rip), %rsi      # 0xbe56
cmpq %rsi, %rcx
setne %cl
testb %cl, %dl
jne 0x60d5
movq (%rax), %rcx
movq %rcx, (%rbp)
movq %rbp, (%rax)
jmp 0x60dc
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 %r15, %rdi
callq 0x191b
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x610a
movq %rbp, %rdi
callq 0x1030
testq %rbx, %rbx
je 0x6117
movq %rbx, %rdi
callq 0x1030
testq %r12, %r12
je 0x6124
movq %r12, %rdi
callq 0x1030
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6133
callq 0x10c0
movq %r13, %rdi
callq 0x10c0
movl $0x1, %edi
callq 0x1270
xorl %ebx, %ebx
jmp 0x607a
movq 0x9ea5(%rip), %rax      # 0xfff8
movl %edi, %esi
movq (%rax), %rdi
movq 0xc789(%rip), %rdx      # 0x128e8
cmpl %esi, %ecx
jne 0x6184
leaq 0x57cb(%rip), %rsi      # 0xb935
jmp 0x618b
movq %r15, %rdi
callq 0x193d
movq 0x8(%rsp), %r13
movl $0x5, %r8d
jmp 0x5d49
leaq 0x577e(%rip), %rsi      # 0xb909
xorl %r12d, %r12d
movq %r15, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x60f9
movq %r15, %rdi
callq 0x1a67
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x19ed
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x19b7
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1981
xorl %r12d, %r12d
jmp 0x5cec
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
jmp 0x60fd