// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq 0x68(%rdi), %rax
testq %rax, %rax
je 0x582c
leaq 0x3de1(%rip), %rcx      # 0x9603
cmpq %rcx, 0x8(%rax)
je 0x5e0a
cmpl $0x0, 0x34(%r14)
jne 0x5efb
movq 0x60(%r14), %rax
movslq 0x30(%r14), %rcx
imulq $0x28, %rcx, %rcx
cmpq $0x0, -0x10(%rax,%rcx)
js 0x5f15
movq 0x48(%r14), %rdi
leaq 0x4100(%rip), %rdx      # 0x995a
leaq 0x4483(%rip), %rcx      # 0x9ce4
movq %rdx, %rsi
callq 0x31cf
leaq 0x44e1(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
testq %rax, %rax
je 0x58d1
movq %rax, %r15
movq 0x48(%r14), %rbx
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x1250
movq %rax, %r12
cmpb $0x2e, 0x1(%rax)
jne 0x58d9
movb $0x0, (%r12)
movq 0x48(%r14), %rdi
movq %r12, %rdx
addq $0x2, %rdx
leaq 0x3bdf(%rip), %rsi      # 0x948e
leaq 0x3bd4(%rip), %rcx      # 0x948a
callq 0x31cf
leaq 0x448f(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
movb $0x2f, (%r12)
jmp 0x5901
pushq $-0x1
popq %rax
jmp 0x5e0c
leaq 0x407a(%rip), %rdx      # 0x995a
leaq 0x3ba3(%rip), %rcx      # 0x948a
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x31cf
leaq 0x4458(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
testq %rax, %rax
movq %rax, 0x18(%rsp)
je 0x5f36
movq %rax, %rdi
leaq 0x3ae0(%rip), %rsi      # 0x93f9
xorl %ebx, %ebx
leaq 0x14(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x5e1e
movq %r12, 0x40(%rsp)
cmpl $0x0, 0x38(%r14)
movq %r15, 0x8(%rsp)
movq %r14, 0x30(%rsp)
jle 0x5cb5
pushq $0x1
popq %rbx
movl 0x3c(%r14), %eax
movl 0x30(%r14), %r15d
movl %eax, 0x24(%rsp)
addl %eax, %r15d
leaq 0x3aa3(%rip), %r14      # 0x9406
leaq 0x4(%rsp), %r12
leaq 0x90(%rsp), %r13
leaq 0x27a0(%rsp), %rbp
xorl %eax, %eax
movq %rax, 0x38(%rsp)
cmpl 0x14(%rsp), %eax
jge 0x59b6
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
jne 0x5a10
movl 0x24(%rsp), %eax
cmpl %eax, 0x4(%rsp)
jg 0x59b6
movq 0x38(%rsp), %rax
incq %rax
jmp 0x597a
leaq 0x3a49(%rip), %r12      # 0x9406
leaq 0x4(%rsp), %r13
leaq 0x90(%rsp), %rbp
leaq 0x27a0(%rsp), %r14
movq 0x38(%rsp), %rcx
movq %rcx, 0x48(%rsp)
cmpl 0x14(%rsp), %ecx
jge 0x5a45
cmpl %r15d, 0x4(%rsp)
jge 0x5a45
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x1090
movq 0x48(%rsp), %rcx
incq %rcx
incl %ebx
cmpl $0x3, %eax
je 0x59d7
movq 0x85e1(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xaec7(%rip), %rdx      # 0x108e8
leaq 0x3af2(%rip), %rsi      # 0x951a
xorl %ebp, %ebp
movq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x11a0
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
jmp 0x5ea3
movq %rbx, %r14
movq 0x30(%rsp), %rbx
movq 0x48(%rbx), %rdi
leaq 0x3f02(%rip), %rdx      # 0x995a
leaq 0x3a91(%rip), %rcx      # 0x94f0
movq %rdx, %rsi
callq 0x31cf
leaq 0x42e3(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
movq %rax, %r15
movq %r14, 0x28(%rsp)
movslq %r14d, %rax
imulq $0x48, %rax, %rdi
leaq 0x425d(%rip), %rsi      # 0x9ce9
callq 0x1bad
movq %rax, %r13
movslq 0x30(%rbx), %rdi
addq %rdi, %rdi
leaq 0x4261(%rip), %rsi      # 0x9d03
callq 0x1bad
movq %rax, %rbp
movq %r15, 0x50(%rsp)
testq %r15, %r15
je 0x5a39
testq %r13, %r13
je 0x5a39
testq %rbp, %rbp
movq 0x28(%rsp), %rbx
je 0x5a39
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x1110
leaq 0x391e(%rip), %rsi      # 0x93f9
leaq 0x14(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x27a0(%rsp), %r12
movq 0x38(%rsp), %rax
movl %eax, %r14d
xorl %r15d, %r15d
subl $0x1, %r14d
jb 0x5b2d
movq 0x18(%rsp), %rdi
leaq 0x38f7(%rip), %rsi      # 0x9406
leaq 0x4(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x1090
movl 0x4(%rsp), %r15d
jmp 0x5afd
movq 0x38(%rsp), %r14
movq %r14, %r12
movq 0x48(%rsp), %rcx
cmpq %rcx, %r12
ja 0x5ca1
movq 0x18(%rsp), %rdi
leaq 0x38b7(%rip), %rsi      # 0x9406
leaq 0x4(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x27a0(%rsp), %r8
xorl %eax, %eax
callq 0x1090
movq %r12, 0x60(%rsp)
subq %r14, %r12
movl %r15d, %r14d
subl 0x24(%rsp), %r14d
jge 0x5bf3
movslq %r15d, %rax
imulq $0x28, %rax, %rsi
addq $0x70, %rsi
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1230
leaq 0x68(%rsp), %rdi
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x5e77
movq 0x80(%rsp), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x1230
movq %r15, %rdi
callq 0x76a9
testq %rax, %rax
je 0x5ea0
imulq $0x48, %r12, %rdi
addq %r13, %rdi
pushq $0x9
popq %rcx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x30(%rsp), %rbx
jmp 0x5c53
movq 0x30(%rsp), %rbx
movq 0x60(%rbx), %rax
movslq %r14d, %rcx
imulq $0x28, %rcx, %rcx
movq %rcx, 0x58(%rsp)
movq 0x18(%rax,%rcx), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x1230
movq %r15, %rdi
callq 0x76a9
testq %rax, %rax
je 0x5ea0
imulq $0x48, %r12, %rdi
addq %r13, %rdi
pushq $0x9
popq %rcx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x1080
movq 0x60(%rbx), %rcx
movq 0x58(%rsp), %rdx
movq %rax, 0x18(%rcx,%rdx)
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
movl 0x4(%rsp), %r15d
movl %r15d, %eax
subl 0x24(%rsp), %eax
movl 0x30(%rbx), %ecx
cmpl %ecx, %eax
cmovgel %ecx, %eax
cltq
movq 0x28(%rsp), %rbx
movq 0x48(%rsp), %rcx
cmpq %rax, %r14
jge 0x5c8f
movw %r12w, (%rbp,%r14,2)
incq %r14
jmp 0x5c7f
movq 0x60(%rsp), %r12
incq %r12
movq 0x38(%rsp), %r14
jmp 0x5b3a
movq 0x50(%rsp), %rdi
callq 0x10c0
movq 0x30(%rsp), %r14
jmp 0x5dbe
movslq 0x14(%rsp), %rbx
imulq $0x48, %rbx, %rdi
leaq 0x4024(%rip), %rsi      # 0x9ce9
callq 0x1bad
movq %rax, %r13
movslq 0x30(%r14), %rdi
addq %rdi, %rdi
leaq 0x4028(%rip), %rsi      # 0x9d03
callq 0x1bad
movq %rax, %rbp
testq %r13, %r13
je 0x5ea3
movq %rbx, 0x28(%rsp)
testq %rbp, %rbp
je 0x5e6b
xorl %ebx, %ebx
xorl %r15d, %r15d
movslq 0x14(%rsp), %rax
cmpq %rax, %rbx
jge 0x5db9
movq 0x18(%rsp), %rdi
leaq 0x36ed(%rip), %rsi      # 0x9406
leaq 0x4(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x27a0(%rsp), %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
jne 0x5e47
movq 0x60(%r14), %rax
movslq %r15d, %r14
imulq $0x28, %r14, %r12
movq 0x18(%rax,%r12), %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x1230
movq %r15, %rdi
callq 0x76a9
testq %rax, %rax
je 0x5ef9
imulq $0x48, %rbx, %rdi
addq %r13, %rdi
pushq $0x9
popq %rcx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x1080
movq 0x30(%rsp), %rcx
movq 0x60(%rcx), %rcx
movq %rax, 0x18(%rcx,%r12)
movl 0x4(%rsp), %r15d
movslq %r15d, %rax
cmpq %rax, %r14
jge 0x5dac
movw %bx, (%rbp,%r14,2)
incq %r14
jmp 0x5d9c
incq %rbx
movq 0x30(%rsp), %r14
jmp 0x5cff
movq 0x28(%rsp), %rbx
leaq 0x3f5a(%rip), %rsi      # 0x9d1f
pushq $0x30
popq %rdi
callq 0x1bad
testq %rax, %rax
movq 0x8(%rsp), %r15
je 0x5ea3
leaq 0x3821(%rip), %rcx      # 0x9603
movq %rcx, 0x8(%rax)
movq 0x68(%r14), %rcx
movq %rcx, (%rax)
movq %rax, 0x68(%r14)
movl %ebx, 0x10(%rax)
movq %rbp, 0x20(%rax)
movq %r13, 0x18(%rax)
movq %r15, 0x28(%rax)
movq 0x18(%rsp), %rdi
callq 0x10c0
xorl %eax, %eax
addq $0x4eb8, %rsp           # imm = 0x4EB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x81d3(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xaab9(%rip), %rdx      # 0x108e8
leaq 0x36e4(%rip), %rsi      # 0x951a
movq %r12, %rcx
xorl %eax, %eax
callq 0x11a0
xorl %ebp, %ebp
xorl %r13d, %r13d
jmp 0x5ea3
movq 0x81aa(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xaa90(%rip), %rdx      # 0x108e8
leaq 0x36bb(%rip), %rsi      # 0x951a
movq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x11a0
movq 0x8(%rsp), %r15
movq 0x28(%rsp), %rbx
jmp 0x5ea3
movq 0x817a(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xaa60(%rip), %rdx      # 0x108e8
leaq 0x3666(%rip), %rsi      # 0x94f5
movq 0x40(%rsp), %rcx
xorl %eax, %eax
callq 0x11a0
movq 0x8(%rsp), %r15
movl %r12d, %ebx
movq %rbp, %rdi
callq 0x1030
testq %r13, %r13
je 0x5eda
movq %rbx, %rax
xorl %ebx, %ebx
testl %eax, %eax
cmovlel %ebx, %eax
imulq $0x48, %rax, %r14
cmpq %rbx, %r14
je 0x5ed2
leaq (%rbx,%r13), %rdi
callq 0x7b3e
addq $0x48, %rbx
jmp 0x5ebe
movq %r13, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5ee9
callq 0x10c0
movq %r15, %rdi
callq 0x10c0
pushq $0x1
popq %rdi
callq 0x1270
jmp 0x5ea3
movq 0x80f6(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xa9dc(%rip), %rdx      # 0x108e8
leaq 0x3d3d(%rip), %rsi      # 0x9c50
jmp 0x5f49
cmpl $0x0, 0x38(%r14)
movq 0x80d7(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xa9bd(%rip), %rdx      # 0x108e8
jle 0x5f42
leaq 0x3d47(%rip), %rsi      # 0x9c7b
jmp 0x5f49
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %ebx, %ebx
jmp 0x5ea3
leaq 0x3d6d(%rip), %rsi      # 0x9cb6
xorl %eax, %eax
callq 0x11a0
jmp 0x5ef1