// File: /workspace/github_c_makefile_star2_O1/thegenemyers[P]DASCRUBBER/DB.c
// Function: Open_QVs
int Open_QVs(DAZZ_DB *db)
{ FILE        *quiva, *istub, *indx;
  char        *root;
  uint16      *table;
  DAZZ_QV     *qvtrk;
  QVcoding    *coding, *nx;
  int          ncodes = 0;

  if (db->tracks != NULL && db->tracks->name == qtrack_name)
    return (0);

  if (db->trimmed)
    { EPRINTF(EPLACE,"%s: Cannot load QVs after trimming the DB\n",Prog_Name);
      EXIT(1);
    }

  if (db->reads[db->nreads-1].coff < 0)
    { if (db->part > 0)
        { EPRINTF(EPLACE,"%s: All QVs for this block have not been added to the DB!\n",Prog_Name);
          EXIT(1);
        }
      else
        { EPRINTF(EPLACE,"%s: All QVs for this DB have not been added!\n",Prog_Name);
          EXIT(1);
        }
    }

  //  Open .qvs, .idx, and .db files

  quiva = Fopen(MyCatenate(db->path,"","",".qvs"),"r");
  if (quiva == NULL)
    return (-1);

  istub  = NULL;
  indx   = NULL; 
  table  = NULL;
  coding = NULL;
  qvtrk  = NULL;

  root = rindex(db->path,'/');
  if (root[1] == '.')
    { *root = '\0';
      istub = Fopen(MyCatenate(db->path,"/",root+2,".db"),"r");
      *root = '/';
    }
  else
    istub = Fopen(MyCatenate(db->path,"","",".db"),"r");
  if (istub == NULL)
    goto error;

  { int   first, last, nfiles;
    char  prolog[MAX_NAME], fname[MAX_NAME];
    int   i, j;

    if (fscanf(istub,DB_NFILE,&nfiles) != 1)
      { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
        goto error;
      }

    if (db->part > 0)
      { int       pfirst, plast;
        int       fbeg, fend;
        int       n, k;
        FILE     *indx;

        //  Determine first how many and which files span the block (fbeg to fend)

        pfirst = db->ufirst;
        plast  = pfirst + db->nreads;

        first = 0;
        for (fbeg = 0; fbeg < nfiles; fbeg++)
          { if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
              { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
                goto error;
              }
            if (last > pfirst)
              break;
            first = last;
          }
        for (fend = fbeg+1; fend <= nfiles; fend++)
          { if (last >= plast)
              break;
            if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
              { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
                goto error;
              }
            first = last;
          }

        indx   = Fopen(MyCatenate(db->path,"","",".idx"),"r");
        ncodes = fend-fbeg;
        coding = (QVcoding *) Malloc(sizeof(QVcoding)*ncodes,"Allocating coding schemes");
        table  = (uint16 *) Malloc(sizeof(uint16)*db->nreads,"Allocating QV table indices");
        if (indx == NULL || coding == NULL || table == NULL)
          { ncodes = 0;
            goto error;
          }

        //  Carefully get the first coding scheme (its offset is most likely in a DAZZ_RECORD
        //    in .idx that is *not* in memory).  Get all the other coding schemes normally and
        //    assign the tables # for each read in the block in "tables".

        rewind(istub);
        (void) fscanf(istub,DB_NFILE,&nfiles);

        first = 0;
        for (n = 0; n < fbeg; n++)
          { (void) fscanf(istub,DB_FDATA,&last,fname,prolog);
            first = last;
          }

        for (n = fbeg; n < fend; n++)
          { (void) fscanf(istub,DB_FDATA,&last,fname,prolog);

            i = n-fbeg;
            if (first < pfirst)
              { DAZZ_READ read;

                fseeko(indx,sizeof(DAZZ_DB) + sizeof(DAZZ_READ)*first,SEEK_SET);
                if (fread(&read,sizeof(DAZZ_READ),1,indx) != 1)
                  { EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
                    ncodes = i;
                    goto error;
                  }
                fseeko(quiva,read.coff,SEEK_SET);
                nx = Read_QVcoding(quiva);
                if (nx == NULL)
                  { ncodes = i;
                    goto error;
                  }
                coding[i] = *nx;
              }
            else
              { fseeko(quiva,db->reads[first-pfirst].coff,SEEK_SET);
                nx = Read_QVcoding(quiva);
                if (nx == NULL)
                  { ncodes = i;
                    goto error;
                  }
                coding[i] = *nx;
                db->reads[first-pfirst].coff = ftello(quiva);
              }

            j = first-pfirst;
            if (j < 0)
              j = 0;
            k = last-pfirst;
            if (k > db->nreads)
              k = db->nreads;
            while (j < k)
              table[j++] = (uint16) i;

            first = last;
	  }

        fclose(indx);
        indx = NULL;
      }

    else
      { //  Load in coding scheme for each file, adjust .coff of first read in the file, and
        //    record which table each read uses

        ncodes = nfiles;
        coding = (QVcoding *) Malloc(sizeof(QVcoding)*nfiles,"Allocating coding schemes");
        table  = (uint16 *) Malloc(sizeof(uint16)*db->nreads,"Allocating QV table indices");
        if (coding == NULL || table == NULL)
          goto error;
  
        first = 0;
        for (i = 0; i < nfiles; i++)
          { if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
              { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
                goto error;
              }
  
            fseeko(quiva,db->reads[first].coff,SEEK_SET);
            nx = Read_QVcoding(quiva);
            if (nx == NULL)
              { ncodes = i;
                goto error;
              }
            coding[i] = *nx;
	    db->reads[first].coff = ftello(quiva);

            for (j = first; j < last; j++)
              table[j] = (uint16) i;

            first = last;
          }
      }

    //  Allocate and fill in the DAZZ_QV record and add it to the front of the
    //    track list

    qvtrk = (DAZZ_QV *) Malloc(sizeof(DAZZ_QV),"Allocating QV pseudo-track");
    if (qvtrk == NULL)
      goto error;
    qvtrk->name   = qtrack_name;
    if (qvtrk->name == NULL)
      goto error;
    qvtrk->next   = db->tracks;
    db->tracks    = (DAZZ_TRACK *) qvtrk;
    qvtrk->ncodes = ncodes;
    qvtrk->table  = table;
    qvtrk->coding = coding;
    qvtrk->quiva  = quiva;
  }

  fclose(istub);
  return (0);

error:
  if (qvtrk != NULL)
    free(qvtrk);
  if (table != NULL)
    free(table);
  if (coding != NULL)
    { int i;
      for (i = 0; i < ncodes; i++)
        Free_QVcoding(coding+i);
      free(coding);
    }
  if (indx != NULL)
    fclose(indx);
  if (istub != NULL)
    fclose(istub);
  fclose(quiva);
  EXIT(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4eb8, %rsp           # imm = 0x4EB8
movq %rdi, %r13
movq 0x68(%rdi), %rax
testq %rax, %rax
je 0x694c
leaq 0x4cc1(%rip), %rcx      # 0xb603
cmpq %rcx, 0x8(%rax)
je 0x7017
cmpl $0x0, 0x34(%r13)
jne 0x70e7
movq 0x60(%r13), %rax
movslq 0x30(%r13), %rcx
leaq (%rcx,%rcx,4), %rcx
cmpq $0x0, -0x10(%rax,%rcx,8)
js 0x70ec
movq 0x48(%r13), %rdi
leaq 0x4fe0(%rip), %rdx      # 0xb95a
leaq 0x5363(%rip), %rcx      # 0xbce4
movq %rdx, %rsi
callq 0x4215
leaq 0x53c1(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
testq %rax, %rax
je 0x69f4
movq %rax, %rbp
movq 0x48(%r13), %rbx
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x1250
movq %rax, %r15
cmpb $0x2e, 0x1(%rax)
jne 0x69fe
movb $0x0, (%r15)
movq 0x48(%r13), %rdi
movq %r15, %rdx
addq $0x2, %rdx
leaq 0x4abe(%rip), %rsi      # 0xb48e
leaq 0x4ab3(%rip), %rcx      # 0xb48a
callq 0x4215
leaq 0x536e(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
movq %rax, %r12
movb $0x2f, (%r15)
jmp 0x6a29
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x7019
leaq 0x4f55(%rip), %rdx      # 0xb95a
leaq 0x4a7e(%rip), %rcx      # 0xb48a
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x4215
leaq 0x5333(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
movq %rax, %r12
testq %r12, %r12
movq %r12, 0x20(%rsp)
je 0x7111
leaq 0x49bb(%rip), %rsi      # 0xb3f9
leaq 0x14(%rsp), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x703c
cmpl $0x0, 0x38(%r13)
movq %r13, 0x38(%rsp)
movq %rbp, 0x8(%rsp)
jle 0x6ae0
movq %r15, 0x48(%rsp)
movl 0x3c(%r13), %eax
movl 0x30(%r13), %r13d
movl %eax, 0x44(%rsp)
addl %eax, %r13d
cmpl $0x0, 0x14(%rsp)
jle 0x6c28
leaq 0x4979(%rip), %rbx      # 0xb406
leaq 0x4(%rsp), %r14
leaq 0x90(%rsp), %r15
leaq 0x27a0(%rsp), %r12
xorl %ebp, %ebp
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
jne 0x709c
movl 0x44(%rsp), %eax
cmpl %eax, 0x4(%rsp)
jg 0x6c2a
incl %ebp
cmpl 0x14(%rsp), %ebp
jl 0x6aa4
jmp 0x6c2a
movslq 0x14(%rsp), %rax
movq %rax, 0x18(%rsp)
shlq $0x3, %rax
leaq (%rax,%rax,8), %rdi
leaq 0x51f0(%rip), %rsi      # 0xbce9
callq 0x2b85
movq %rax, %r14
movslq 0x30(%r13), %rdi
addq %rdi, %rdi
leaq 0x51f4(%rip), %rsi      # 0xbd03
callq 0x2b85
movq %rax, %rbx
xorl %eax, %eax
movl $0x2, %edx
movq %r14, 0x28(%rsp)
testq %r14, %r14
je 0x6e08
testq %rbx, %rbx
je 0x6e08
cmpl $0x0, 0x14(%rsp)
jle 0x6c1e
movq %r15, 0x48(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rbx, 0x30(%rsp)
movq 0x8(%rsp), %rbp
movq %r12, %rdi
leaq 0x48a7(%rip), %rsi      # 0xb406
leaq 0x4(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x27a0(%rsp), %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
jne 0x70cc
movq 0x60(%r13), %rax
movq %r13, %rbx
movslq %r15d, %r13
leaq (,%r13,4), %r12
addq %r13, %r12
movq 0x18(%rax,%r12,8), %rsi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x1230
movq %rbp, %rdi
callq 0x8855
testq %rax, %rax
je 0x705a
leaq (%r14,%r14,8), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdi
movl $0x9, %ecx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbp, %rdi
callq 0x1080
movq 0x60(%rbx), %rcx
movq %rax, 0x18(%rcx,%r12,8)
movl 0x4(%rsp), %eax
cmpl %eax, %r15d
movq 0x30(%rsp), %rbx
jge 0x6c00
movslq %eax, %rcx
movw %r14w, (%rbx,%r13,2)
incq %r13
cmpq %r13, %rcx
jne 0x6bf3
incq %r14
movslq 0x14(%rsp), %rcx
movl %eax, %r15d
cmpq %rcx, %r14
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %r13
jl 0x6b55
movq 0x8(%rsp), %rbp
jmp 0x6db8
xorl %ebp, %ebp
movl %ebp, %r15d
movl $0x1, %r14d
leaq 0x90(%rsp), %r12
leaq 0x27a0(%rsp), %rbx
movq %r15, 0x58(%rsp)
cmpl 0x14(%rsp), %r15d
jge 0x6c8e
cmpl %r13d, 0x4(%rsp)
jge 0x6c8e
movq 0x20(%rsp), %rdi
leaq 0x47a4(%rip), %rsi      # 0xb406
leaq 0x4(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1090
incq %r15
incl %r14d
cmpl $0x3, %eax
je 0x6c48
movq 0x48(%rsp), %rdi
callq 0x1dfa
jmp 0x70a6
movq 0x38(%rsp), %r13
movq 0x48(%r13), %rdi
leaq 0x4cbc(%rip), %rdx      # 0xb95a
leaq 0x484b(%rip), %rcx      # 0xb4f0
movq %rdx, %rsi
callq 0x4215
leaq 0x509d(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
movq %rax, 0x50(%rsp)
movslq %r14d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,8), %rdi
leaq 0x5016(%rip), %rsi      # 0xbce9
callq 0x2b85
movq %rax, %r12
movslq 0x30(%r13), %rdi
addq %rdi, %rdi
leaq 0x501a(%rip), %rsi      # 0xbd03
callq 0x2b85
movq %rax, %rbx
xorl %esi, %esi
movl $0x2, %edx
cmpq $0x0, 0x50(%rsp)
movq %r12, 0x28(%rsp)
je 0x702d
testq %r12, %r12
movq 0x20(%rsp), %r12
je 0x6da3
testq %rbx, %rbx
je 0x6da3
movq %r12, %rdi
callq 0x1110
leaq 0x46ca(%rip), %rsi      # 0xb3f9
movl $0x0, 0x40(%rsp)
leaq 0x14(%rsp), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x1090
testl %ebp, %ebp
je 0x6d86
movq %r12, %r13
movl %ebp, %r12d
movq %r13, %rdi
leaq 0x46ac(%rip), %rsi      # 0xb406
leaq 0x4(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x27a0(%rsp), %r8
xorl %eax, %eax
callq 0x1090
decl %r12d
jne 0x6d50
movl 0x4(%rsp), %eax
movl %eax, 0x40(%rsp)
movq %r13, %r12
cmpl %r15d, %ebp
jbe 0x6e12
movq 0x50(%rsp), %rdi
callq 0x10c0
xorl %edx, %edx
movl %r14d, %esi
movq 0x38(%rsp), %r13
movq 0x8(%rsp), %rbp
testl %edx, %edx
je 0x6db3
xorl %eax, %eax
jmp 0x7001
movq %rsi, 0x18(%rsp)
leaq 0x4f60(%rip), %rsi      # 0xbd1f
movl $0x30, %edi
callq 0x2b85
xorl %edx, %edx
testq %rax, %rax
je 0x6ff5
leaq 0x4828(%rip), %rcx      # 0xb603
movq %rcx, 0x8(%rax)
movq 0x68(%r13), %rcx
movq %rcx, (%rax)
movq %rax, 0x68(%r13)
movq 0x18(%rsp), %rsi
movl %esi, 0x10(%rax)
movq %rbx, 0x20(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %rbp, 0x28(%rax)
jmp 0x7001
movq 0x8(%rsp), %rbp
jmp 0x6ffc
movslq %ebp, %rax
movq %rax, 0x60(%rsp)
incq %r15
movq %rbx, 0x30(%rsp)
movq 0x58(%rsp), %r13
movq %r12, %rdi
leaq 0x45d5(%rip), %rsi      # 0xb406
leaq 0x4(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x27a0(%rsp), %r8
xorl %eax, %eax
callq 0x1090
movq %r13, %rax
subq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x40(%rsp), %eax
movl %eax, %ebp
subl 0x44(%rsp), %ebp
movq %r13, 0x58(%rsp)
jge 0x6ef9
cltq
leaq (%rax,%rax,4), %rax
leaq 0x70(,%rax,8), %rsi
movq 0x50(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x1230
movl $0x28, %esi
movl $0x1, %edx
leaq 0x68(%rsp), %rdi
movq %r12, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x6fd9
movq 0x80(%rsp), %rsi
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1230
movq %rbx, %rdi
callq 0x8855
testq %rax, %rax
je 0x6f68
movq 0x18(%rsp), %r8
leaq (%r8,%r8,8), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdi
movl $0x9, %ecx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movb $0x1, %al
jmp 0x6f78
movq 0x38(%rsp), %r13
movq 0x60(%r13), %rax
movslq %ebp, %rcx
leaq (%rcx,%rcx,4), %r12
movq 0x18(%rax,%r12,8), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1230
movq %rbx, %rdi
callq 0x8855
testq %rax, %rax
je 0x7070
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,8), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdi
movl $0x9, %ecx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
callq 0x1080
movq 0x18(%rsp), %r8
movq 0x60(%r13), %rcx
movq %rax, 0x18(%rcx,%r12,8)
movq 0x30(%rsp), %rbx
jmp 0x6f8a
movl $0x2, %r12d
movq 0x18(%rsp), %r8
movl %r8d, %r14d
xorl %eax, %eax
movq 0x30(%rsp), %rbx
testb %al, %al
movq 0x38(%rsp), %r13
je 0x708c
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
movl 0x4(%rsp), %ecx
movl %ecx, 0x40(%rsp)
subl 0x44(%rsp), %ecx
movl 0x30(%r13), %eax
cmpl %eax, %ecx
cmovgel %eax, %ecx
cmpl %ecx, %ebp
movq 0x20(%rsp), %r12
jge 0x6fc3
movl %ebp, %eax
movl %ecx, %ecx
movw %r8w, (%rbx,%rax,2)
incq %rax
cmpq %rax, %rcx
jne 0x6fb6
movq 0x58(%rsp), %r13
incq %r13
cmpq %r13, %r15
jne 0x6e27
jmp 0x6d8f
movq 0x48(%rsp), %rdi
callq 0x1dd2
movl $0x2, %r12d
xorl %eax, %eax
movq 0x18(%rsp), %r8
movl %r8d, %r14d
jmp 0x6f7d
movl $0x2, %edx
xorl %eax, %eax
movq 0x18(%rsp), %rsi
testl %edx, %edx
je 0x700f
cmpl $0x2, %edx
jne 0x7019
jmp 0x7120
movq %r12, %rdi
callq 0x10c0
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x4eb8, %rsp           # imm = 0x4EB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rbp
movq 0x20(%rsp), %r12
jmp 0x6da8
movq %r15, %rdi
callq 0x1d5a
movl $0x2, %edx
xorl %ebx, %ebx
xorl %eax, %eax
movq $0x0, 0x28(%rsp)
xorl %esi, %esi
jmp 0x7001
xorl %eax, %eax
movl %r14d, %esi
movq 0x20(%rsp), %r12
movq 0x30(%rsp), %rbx
movl $0x2, %edx
jmp 0x7001
movq 0x20(%rsp), %r12
movq %rbx, %rbp
movq 0x30(%rsp), %rbx
movl $0x2, %edx
movq 0x18(%rsp), %rsi
jmp 0x6da8
movl %r14d, %esi
movl %r12d, %edx
movq 0x20(%rsp), %r12
jmp 0x6da3
movq 0x48(%rsp), %rdi
callq 0x1daa
movl $0x2, %edx
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
xorl %esi, %esi
movq 0x8(%rsp), %rbp
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %r13
jmp 0x6da8
movq 0x48(%rsp), %rdi
callq 0x1d82
xorl %eax, %eax
movq 0x8(%rsp), %rbp
movl $0x2, %edx
jmp 0x6ffc
callq 0x1d32
cmpl $0x0, 0x38(%r13)
movq 0x8f00(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0xb7e6(%rip), %rdx      # 0x128e8
jle 0x7189
leaq 0x4b6c(%rip), %rsi      # 0xbc7b
jmp 0x7190
xorl %ebx, %ebx
xorl %eax, %eax
movq $0x0, 0x28(%rsp)
xorl %esi, %esi
movq %rsi, %r14
testq %rax, %rax
je 0x7130
movq %rax, %rdi
callq 0x1030
testq %rbx, %rbx
je 0x713d
movq %rbx, %rdi
callq 0x1030
cmpq $0x0, 0x28(%rsp)
je 0x716d
testl %r14d, %r14d
jle 0x7163
movl %r14d, %r14d
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x8c9b
addq $0x48, %rbx
decq %r14
jne 0x7152
movq 0x28(%rsp), %rdi
callq 0x1030
cmpq $0x0, 0x20(%rsp)
je 0x717f
movq 0x20(%rsp), %rdi
callq 0x10c0
movq %rbp, %rdi
callq 0x10c0
jmp 0x7197
leaq 0x4b26(%rip), %rsi      # 0xbcb6
xorl %eax, %eax
callq 0x11a0
movl $0x1, %edi
callq 0x1270