// File: /workspace/github_c_makefile_star2_O2/thegenemyers[P]DASCRUBBER/DB.c
// Function: Open_DB
int Open_DB(char* path, DAZZ_DB *db)
{ DAZZ_DB dbcopy;
  char   *root, *pwd, *bptr, *fptr, *cat;
  int     nreads;
  FILE   *index, *dbvis, *bases;
  int     status, plen, isdam;
  int     part, cutoff, all;
  int     ufirst, tfirst, ulast, tlast;

  status = -1;
  dbcopy = *db;

  plen = strlen(path);
  if (strcmp(path+(plen-4),".dam") == 0)
    { root = Root(path,".dam");
      isdam = 1;
    }
  else
    { if (strcmp(path+(plen-3),".db") == 0)
        isdam = -1;
      else
        isdam = 0;
      root = Root(path,".db");
    }
  pwd = PathTo(path);

  bptr = rindex(root,'.');
  if (bptr != NULL && bptr[1] != '\0' && bptr[1] != '-')
    { part = strtol(bptr+1,&fptr,10);
      if (*fptr != '\0' || part == 0)
        part = 0;
      else
        *bptr = '\0';
    }
  else
    part = 0;

  if (isdam > 0)
    cat = MyCatenate(pwd,"/",root,".dam");
  else
    cat = MyCatenate(pwd,"/",root,".db");
  if (cat == NULL)
    return (-1);
  if ((dbvis = fopen(cat,"r")) == NULL)
    { if (isdam < 0)
        { EPRINTF(EPLACE,"%s: Could not open DB %s\n",Prog_Name,path);
          goto error;
        }
      if (isdam > 0)
        { EPRINTF(EPLACE,"%s: Could not open DAM %s\n",Prog_Name,path);
          goto error;
        }
      cat = MyCatenate(pwd,"/",root,".dam");
      if (cat == NULL)
        return (-1);
      if ((dbvis = fopen(cat,"r")) == NULL)
        { EPRINTF(EPLACE,"%s: Could not open %s as a DB or a DAM\n",Prog_Name,path);
          goto error;
        }
      isdam = 1;
    }
  if (isdam < 0)
    isdam = 0;

  if ((index = Fopen(MyCatenate(pwd,PATHSEP,root,".idx"),"r")) == NULL)
    goto error1;
  if (fread(db,sizeof(DAZZ_DB),1,index) != 1)
    { EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
      goto error2;
    }

  { int   p, nblocks, nfiles;
    int64 size;
    char  fname[MAX_NAME], prolog[MAX_NAME];

    nblocks = 0;
    if (fscanf(dbvis,DB_NFILE,&nfiles) != 1)
      { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
        goto error2;
      }
    for (p = 0; p < nfiles; p++)
      if (fscanf(dbvis,DB_FDATA,&tlast,fname,prolog) != 3)
        { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
          goto error2;
        }
    if (fscanf(dbvis,DB_NBLOCK,&nblocks) != 1)
      if (part == 0)
        { cutoff = 0;
          all    = DB_ALL;
        }
      else
        { EPRINTF(EPLACE,"%s: DB %s has not yet been partitioned, cannot request a block !\n",
                         Prog_Name,root);
          goto error2;
        }
    else
      { if (fscanf(dbvis,DB_PARAMS,&size,&cutoff,&all) != 3)
          { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
            goto error2;
          }
        if (part > nblocks)
          { EPRINTF(EPLACE,"%s: DB %s has only %d blocks\n",Prog_Name,root,nblocks);
            goto error2;
          }
      }

    if (part > 0)
      { for (p = 1; p <= part; p++)
          if (fscanf(dbvis,DB_BDATA,&ufirst,&tfirst) != 2)
            { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
              goto error2;
            }
        if (fscanf(dbvis,DB_BDATA,&ulast,&tlast) != 2)
          { EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
            goto error2;
          }
      }
    else
      { ufirst = tfirst = 0;
        ulast  = db->ureads;
        tlast  = db->treads;
      }
  }

  db->trimmed = 0;
  db->tracks  = NULL;
  db->part    = part;
  db->cutoff  = cutoff;
  db->allarr |= all;
  db->ufirst  = ufirst;
  db->tfirst  = tfirst;

  nreads = ulast-ufirst;
  if (part <= 0)
    { db->reads = (DAZZ_READ *) Malloc(sizeof(DAZZ_READ)*(nreads+2),"Allocating Open_DB index");
      if (db->reads == NULL)
        goto error2;
        
      db->reads += 1;
      if (fread(db->reads,sizeof(DAZZ_READ),nreads,index) != (size_t) nreads)
        { EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
          free(db->reads-1);
          goto error2;
        }
    }
  else
    { DAZZ_READ *reads;
      int        i, r, maxlen;
      int64      totlen;

      reads = (DAZZ_READ *) Malloc(sizeof(DAZZ_READ)*(nreads+2),"Allocating Open_DB index");
      if (reads == NULL)
        goto error2;
      reads += 1;

      fseeko(index,sizeof(DAZZ_READ)*ufirst,SEEK_CUR);
      if (fread(reads,sizeof(DAZZ_READ),nreads,index) != (size_t) nreads)
        { EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
          free(reads-1);
          goto error2;
        }

      totlen = 0;
      maxlen = 0;
      for (i = 0; i < nreads; i++)
        { r = reads[i].rlen;
          totlen += r;
          if (r > maxlen)
            maxlen = r;
        }

      db->maxlen = maxlen;
      db->totlen = totlen;
      db->reads  = reads;
    }

  ((int *) (db->reads))[-1] = ulast - ufirst;   //  Kludge, need these for DB part
  ((int *) (db->reads))[-2] = tlast - tfirst;

  db->nreads = nreads;
  db->path   = Strdup(MyCatenate(pwd,PATHSEP,root,""),"Allocating Open_DB path");
  if (db->path == NULL)
    { free(db->reads-1);
      goto error2;
    }
  bases = Fopen(MyCatenate(db->path,"","",".bps"),"r");
  if (bases == NULL)
     { free(db->path);
       free(db->reads-1);
       goto error2;
     }
  db->bases = (void *) bases;
  db->loaded = 0;

  status = isdam;

error2:
  fclose(index);
error1:
  fclose(dbvis);
error:
  if (bptr != NULL)
    *bptr = '.';

  free(pwd);
  free(root);

  if (status < 0)
    *db = dbcopy;

  return (status);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f08, %rsp           # imm = 0x4F08
movq %rsi, %r12
movq %rdi, %rbp
pushq $0xe
popq %rcx
leaq 0x70(%rsp), %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %rbp, %rdi
callq 0x10e0
movq %rax, %r15
shlq $0x20, %r15
movabsq $-0x400000000, %rdi     # imm = 0xFFFFFFFC00000000
addq %r15, %rdi
sarq $0x20, %rdi
addq %rbp, %rdi
leaq 0x69e9(%rip), %r14      # 0x9485
movq %r14, %rsi
callq 0x1190
testl %eax, %eax
je 0x2ad7
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
addq %rax, %r15
sarq $0x20, %r15
addq %rbp, %r15
leaq 0x69c7(%rip), %r14      # 0x948a
movq %r15, %rdi
movq %r14, %rsi
callq 0x1190
xorl %ebx, %ebx
cmpl $0x1, %eax
sbbl %ebx, %ebx
jmp 0x2ada
pushq $0x1
popq %rbx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1d83
movq %rax, %r14
movq %rbp, %rdi
callq 0x1d2b
movq %rax, %r13
pushq $0x2e
popq %rsi
movq %r14, %rdi
callq 0x1250
testq %rax, %rax
movq %rax, 0x58(%rsp)
je 0x2b57
movq $0x0, 0x38(%rsp)
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x2b60
cmpl $0x2d, %ecx
je 0x2b60
movq %r14, %r15
leaq 0x1(%rax), %rdi
leaq 0x68(%rsp), %r14
pushq $0xa
popq %rdx
movq %r14, %rsi
callq 0x11d0
movq (%r14), %rcx
cmpb $0x0, (%rcx)
jne 0x2fb1
testl %eax, %eax
movq %r15, %r14
je 0x2b60
movq 0x58(%rsp), %rcx
movb $0x0, (%rcx)
movq %rax, 0x38(%rsp)
jmp 0x2b60
movq $0x0, 0x38(%rsp)
testl %ebx, %ebx
leaq 0x691c(%rip), %rax      # 0x9485
leaq 0x691a(%rip), %rcx      # 0x948a
cmovgq %rax, %rcx
leaq 0x6913(%rip), %rsi      # 0x948e
movq %r13, %rdi
movq %r14, %rdx
callq 0x31cf
pushq $-0x1
popq %r15
testq %rax, %rax
je 0x30f8
movq %r13, 0x30(%rsp)
leaq 0x71b2(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x2bb1
movq %rax, %r13
jmp 0x2c04
testl %ebx, %ebx
js 0x3047
movq 0x30(%rsp), %rbx
jg 0x3064
leaq 0x68c3(%rip), %rsi      # 0x948e
leaq 0x68b3(%rip), %rcx      # 0x9485
movq %rbx, %rdi
movq %r14, %rdx
callq 0x31cf
testq %rax, %rax
je 0x30f8
leaq 0x7164(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x310d
movq %rax, %r13
pushq $0x1
popq %rbx
xorl %ebp, %ebp
testl %ebx, %ebx
cmovgl %ebx, %ebp
leaq 0x68db(%rip), %rsi      # 0x94ed
leaq 0x68d7(%rip), %rcx      # 0x94f0
movq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x31cf
leaq 0x7124(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
testq %rax, %rax
je 0x2d76
pushq $0x70
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %rax, (%rsp)
movq %rax, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x302d
andl $0x0, 0x48(%rsp)
leaq 0x6790(%rip), %rsi      # 0x93f9
leaq 0x54(%rsp), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x2fdf
movl %ebp, 0x44(%rsp)
movq %r14, 0x8(%rsp)
movq %r12, 0x18(%rsp)
pushq $-0x1
popq %rbx
leaq 0x676d(%rip), %r15      # 0x9406
leaq 0x24(%rsp), %r12
leaq 0x27f0(%rsp), %rbp
leaq 0xe0(%rsp), %r14
incl %ebx
cmpl 0x54(%rsp), %ebx
jge 0x2cd6
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
je 0x2cae
jmp 0x2ffc
leaq 0x6750(%rip), %rsi      # 0x942d
leaq 0x48(%rsp), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x2d7f
leaq 0x673f(%rip), %rsi      # 0x943b
leaq 0x60(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x4c(%rsp), %r8
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %r14
movq 0x38(%rsp), %r15
jne 0x2fdf
movl 0x48(%rsp), %r8d
cmpl %r8d, %r15d
jg 0x3135
testl %r15d, %r15d
jle 0x2da4
leal 0x1(%r15), %ebx
leaq 0x6716(%rip), %r15      # 0x9461
leaq 0x14(%rsp), %r14
leaq 0x2c(%rsp), %r12
decl %ebx
je 0x2dc5
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1090
cmpl $0x2, %eax
je 0x2d55
jmp 0x2ffc
pushq $-0x1
popq %r15
jmp 0x30bb
movq 0x38(%rsp), %r15
testl %r15d, %r15d
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %r14
jne 0x308c
andl $0x0, 0x50(%rsp)
movl $0x1, 0x4c(%rsp)
andl $0x0, 0x2c(%rsp)
andl $0x0, 0x14(%rsp)
movl (%r12), %ebx
movl %ebx, 0x28(%rsp)
movl 0x4(%r12), %eax
movl %eax, 0x24(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x2e04
leaq 0x6695(%rip), %rsi      # 0x9461
leaq 0x28(%rsp), %rdx
leaq 0x24(%rsp), %rcx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x2, %eax
jne 0x2ffc
movl 0x14(%rsp), %ecx
movl 0x2c(%rsp), %eax
movl 0x28(%rsp), %ebx
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %r14
movq 0x38(%rsp), %r15
andl $0x0, 0x34(%r12)
andq $0x0, 0x68(%r12)
movl %r15d, 0x38(%r12)
movl 0x50(%rsp), %edx
movl %edx, 0x8(%r12)
movl 0x4c(%rsp), %edx
orl %edx, 0xc(%r12)
movl %ecx, 0x3c(%r12)
movl %eax, 0x40(%r12)
subl %ecx, %ebx
leal 0x2(%rbx), %eax
cltq
imulq $0x28, %rax, %rdi
leaq 0x675a(%rip), %rsi      # 0x959d
callq 0x1bad
movq %rax, %rbp
testl %r15d, %r15d
jle 0x2ec8
pushq $-0x1
popq %r15
testq %rbp, %rbp
je 0x30b2
movq %rbp, %r12
addq $0x28, %r12
movslq 0x14(%rsp), %rax
imulq $0x28, %rax, %rsi
pushq $0x1
popq %rdx
movq (%rsp), %rdi
callq 0x1230
movslq %ebx, %r14
pushq $0x28
popq %rsi
movq %r12, %rdi
movq %r14, %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq %r14, %rax
jne 0x315c
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
imulq $0x28, %rcx, %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0x44(%rsp), %r15d
cmpq %rax, %rcx
je 0x2f10
movslq 0x2c(%rbp,%rax), %rdi
addq %rdi, %rsi
cmpl %edx, %edi
cmovgl %edi, %edx
addq $0x28, %rax
jmp 0x2eb0
movq %rbp, 0x60(%r12)
pushq $-0x1
popq %r15
testq %rbp, %rbp
je 0x30b2
addq $0x28, %rbp
movq %rbp, 0x60(%r12)
movslq %ebx, %r14
pushq $0x28
popq %rsi
movq %rbp, %rdi
movq %r14, %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq %r14, %rax
jne 0x3195
movq %r12, %r14
movq 0x60(%r12), %r12
movl 0x44(%rsp), %r15d
jmp 0x2f21
movq 0x18(%rsp), %r14
movl %edx, 0x20(%r14)
movq %rsi, 0x28(%r14)
movq %r12, 0x60(%r14)
movl 0x28(%rsp), %eax
subl 0x14(%rsp), %eax
movl %eax, -0x4(%r12)
movl 0x24(%rsp), %eax
subl 0x2c(%rsp), %eax
movl %eax, -0x8(%r12)
movl %ebx, 0x30(%r14)
leaq 0x65a7(%rip), %rsi      # 0x94ed
leaq 0x6a0d(%rip), %rcx      # 0x995a
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0x31cf
leaq 0x6653(%rip), %rsi      # 0x95b6
movq %rax, %rdi
callq 0x1c68
movq %rax, 0x48(%r14)
testq %rax, %rax
movq %r14, %r12
je 0x2fc3
leaq 0x69dc(%rip), %rdx      # 0x995a
leaq 0x6649(%rip), %rcx      # 0x95ce
movq %rax, %rdi
movq %rdx, %rsi
callq 0x31cf
leaq 0x6dba(%rip), %rsi      # 0x9d51
movq %rax, %rdi
callq 0x1ccb
testq %rax, %rax
je 0x2fb9
movq %rax, 0x58(%r12)
andl $0x0, 0x50(%r12)
jmp 0x2fd5
movq %r15, %r14
jmp 0x2b60
movq 0x48(%r12), %rdi
callq 0x1030
movq 0x60(%r12), %rdi
addq $-0x28, %rdi
callq 0x1030
pushq $-0x1
popq %r15
movq 0x8(%rsp), %r14
jmp 0x30b2
movq 0xb012(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd8f8(%rip), %rdx      # 0x108e8
leaq 0x6523(%rip), %rsi      # 0x951a
jmp 0x30a4
movq 0xaff5(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd8db(%rip), %rdx      # 0x108e8
leaq 0x6506(%rip), %rsi      # 0x951a
movq 0x8(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
movq 0x18(%rsp), %r12
jmp 0x30ae
movq 0xafc4(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd8aa(%rip), %rdx      # 0x108e8
leaq 0x64b0(%rip), %rsi      # 0x94f5
jmp 0x30a4
movq 0xafaa(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd890(%rip), %rdx      # 0x108e8
leaq 0x6431(%rip), %rsi      # 0x9490
jmp 0x3125
movq 0xaf8d(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd873(%rip), %rdx      # 0x108e8
leaq 0x642e(%rip), %rsi      # 0x94aa
movq %rbp, %rcx
xorl %eax, %eax
callq 0x11a0
pushq $-0x1
popq %r15
jmp 0x30c8
movq 0xaf65(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd84b(%rip), %rdx      # 0x108e8
leaq 0x6499(%rip), %rsi      # 0x953d
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
pushq $-0x1
popq %r15
movq (%rsp), %rdi
callq 0x10c0
movq %r13, %rdi
callq 0x10c0
movq 0x30(%rsp), %rbx
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x30d5
movb $0x2e, (%rax)
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
testl %r15d, %r15d
jns 0x30f8
pushq $0xe
popq %rcx
leaq 0x70(%rsp), %rsi
movq %r12, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %eax
addq $0x4f08, %rsp           # imm = 0x4F08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xaee4(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd7ca(%rip), %rdx      # 0x108e8
leaq 0x63a0(%rip), %rsi      # 0x94c5
movq %rbp, %rcx
xorl %eax, %eax
callq 0x11a0
pushq $-0x1
popq %r15
jmp 0x30c3
movq 0xaebc(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd7a2(%rip), %rdx      # 0x108e8
leaq 0x6432(%rip), %rsi      # 0x957f
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x30ae
movq 0xae95(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd77b(%rip), %rdx      # 0x108e8
leaq 0x6381(%rip), %rsi      # 0x94f5
movq 0x8(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
movq %rbp, %rdi
callq 0x1030
movq 0x18(%rsp), %r12
jmp 0x30b2
movq 0xae5c(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xd742(%rip), %rdx      # 0x108e8
leaq 0x6348(%rip), %rsi      # 0x94f5
movq 0x8(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
movq 0x60(%r12), %rdi
addq $-0x28, %rdi
callq 0x1030
jmp 0x30b2