// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rdi, %rbp
leaq 0x70(%rsp), %rdi
movl $0xe, %ecx
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 0x78f7(%rip), %r14      # 0xb485
movq %r14, %rsi
callq 0x1190
testl %eax, %eax
je 0x3bcb
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
addq %rax, %r15
sarq $0x20, %r15
addq %rbp, %r15
leaq 0x78d5(%rip), %r14      # 0xb48a
movq %r15, %rdi
movq %r14, %rsi
callq 0x1190
xorl %r12d, %r12d
cmpl $0x1, %eax
sbbl %r12d, %r12d
jmp 0x3bd1
movl $0x1, %r12d
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2d58
movq %rax, %r14
movq %rbp, %rdi
callq 0x2cfe
movq %rax, %r13
movq %r14, %rdi
movl $0x2e, %esi
callq 0x1250
movl $0x0, 0x20(%rsp)
testq %rax, %rax
movq %rax, 0x58(%rsp)
je 0x3c4f
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x3c4f
cmpl $0x2d, %ecx
je 0x3c4f
movq %r14, %r15
leaq 0x1(%rax), %rdi
leaq 0x68(%rsp), %r14
movq %r14, %rsi
movl $0xa, %edx
callq 0x11d0
movq (%r14), %rcx
cmpb $0x0, (%rcx)
jne 0x3c4c
testl %eax, %eax
movq %r15, %r14
je 0x3c4f
movq 0x58(%rsp), %rcx
movb $0x0, (%rcx)
movl %eax, 0x20(%rsp)
jmp 0x3c4f
movq %r15, %r14
testl %r12d, %r12d
leaq 0x782c(%rip), %rax      # 0xb485
leaq 0x782a(%rip), %rcx      # 0xb48a
cmovgq %rax, %rcx
leaq 0x7823(%rip), %rsi      # 0xb48e
movq %r13, %rdi
movq %r14, %rdx
callq 0x4215
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x416a
movq %r13, 0x38(%rsp)
leaq 0x80c0(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x3ca3
movq %rax, %r13
jmp 0x3cfa
testl %r12d, %r12d
js 0x4193
movq 0x38(%rsp), %r12
jg 0x419d
leaq 0x77d0(%rip), %rsi      # 0xb48e
leaq 0x77c0(%rip), %rcx      # 0xb485
movq %r12, %rdi
movq %r14, %rdx
callq 0x4215
testq %rax, %rax
je 0x416a
leaq 0x8071(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x41ed
movq %rax, %r13
movl $0x1, %r12d
xorl %ebp, %ebp
testl %r12d, %r12d
cmovgl %r12d, %ebp
leaq 0x77e3(%rip), %rsi      # 0xb4ed
leaq 0x77df(%rip), %rcx      # 0xb4f0
movq 0x38(%rsp), %rdi
movq %r14, %rdx
callq 0x4215
leaq 0x802c(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
testq %rax, %rax
je 0x3e5d
movl $0x70, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rax, (%rsp)
movq %rax, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x417f
movl $0x0, 0x48(%rsp)
leaq 0x7691(%rip), %rsi      # 0xb3f9
leaq 0x44(%rsp), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x4189
movl %ebp, 0x54(%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x18(%rsp)
cmpl $0x0, 0x44(%rsp)
jle 0x3dda
leaq 0x766a(%rip), %r15      # 0xb406
leaq 0x2c(%rsp), %r12
leaq 0x27f0(%rsp), %rbp
leaq 0xe0(%rsp), %r14
xorl %ebx, %ebx
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
jne 0x41ad
incl %ebx
cmpl 0x44(%rsp), %ebx
jl 0x3db3
leaq 0x764c(%rip), %rsi      # 0xb42d
leaq 0x48(%rsp), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x3e68
leaq 0x763f(%rip), %rsi      # 0xb43b
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), %rbx
movq 0x8(%rsp), %r14
movl 0x20(%rsp), %ebp
jne 0x41d3
movl 0x48(%rsp), %r8d
cmpl %r8d, %ebp
jle 0x3e8e
movq 0xc1bb(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0xeaa1(%rip), %rdx      # 0x128e8
leaq 0x7731(%rip), %rsi      # 0xb57f
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x411c
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x412b
movl 0x20(%rsp), %ebp
testl %ebp, %ebp
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r14
jne 0x41e0
movl $0x0, 0x50(%rsp)
movl $0x1, 0x4c(%rsp)
testl %ebp, %ebp
jle 0x3efc
leaq 0x75c8(%rip), %r15      # 0xb461
leaq 0x14(%rsp), %r14
leaq 0x34(%rsp), %r12
movl %ebp, %ebx
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1090
cmpl $0x2, %eax
jne 0x41bc
decl %ebx
jne 0x3ea5
leaq 0x7595(%rip), %rsi      # 0xb461
leaq 0x30(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x2, %eax
movq 0x18(%rsp), %rbx
je 0x3f13
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x14c9
jmp 0x411c
xorl %eax, %eax
movl %eax, 0x34(%rsp)
movl %eax, 0x14(%rsp)
movl (%rbx), %eax
movl %eax, 0x30(%rsp)
movl 0x4(%rbx), %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x34(%rbx)
movq $0x0, 0x68(%rbx)
movl %ebp, 0x38(%rbx)
movl 0x50(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0x4c(%rsp), %eax
orl %eax, 0xc(%rbx)
movl 0x14(%rsp), %eax
movl %eax, 0x3c(%rbx)
movl 0x34(%rsp), %ecx
movl %ecx, 0x40(%rbx)
movl 0x30(%rsp), %r14d
subl %eax, %r14d
movslq %r14d, %r12
leaq (,%r12,8), %rax
leaq (%rax,%rax,4), %rdi
addq $0x50, %rdi
leaq 0x763a(%rip), %rsi      # 0xb59d
callq 0x2b85
movl %ebp, %ecx
movq %rax, %rbp
testl %ecx, %ecx
jle 0x4004
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rbp, %rbp
je 0x4053
movl %r14d, 0x18(%rsp)
movq %rbp, %r14
addq $0x28, %r14
movslq 0x14(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
movq (%rsp), %rdi
movl $0x1, %edx
callq 0x1230
movl $0x28, %esi
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq %r12, %rax
jne 0x4200
movl 0x18(%rsp), %r14d
testl %r14d, %r14d
jle 0x405d
movl %r14d, %eax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x2c(%rbp,%rsi), %rdi
addq %rdi, %rcx
cmpl %eax, %edi
cmovgl %edi, %eax
addq $0x28, %rsi
cmpq %rsi, %rdx
jne 0x3fec
jmp 0x4061
movq %rbp, 0x60(%rbx)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rbp, %rbp
je 0x4053
leaq 0x60(%rbx), %rax
addq $0x28, %rbp
movq %rax, 0x20(%rsp)
movq %rbp, (%rax)
movl $0x28, %esi
movq %rbp, %rdi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x10b0
cmpq %r12, %rax
je 0x4071
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x1521
jmp 0x4122
movq 0x8(%rsp), %r14
jmp 0x4122
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, 0x20(%rbx)
movq %rcx, 0x28(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rbx)
movl 0x30(%rsp), %eax
subl 0x14(%rsp), %eax
movq 0x60(%rbx), %rcx
movl %eax, -0x4(%rcx)
movl 0x2c(%rsp), %eax
subl 0x34(%rsp), %eax
movl %eax, -0x8(%rcx)
movl %r14d, 0x30(%rbx)
leaq 0x7457(%rip), %rsi      # 0xb4ed
leaq 0x78bd(%rip), %rcx      # 0xb95a
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x4215
leaq 0x7500(%rip), %rsi      # 0xb5b6
movq %rax, %rdi
callq 0x2c42
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x410f
leaq 0x788c(%rip), %rdx      # 0xb95a
leaq 0x74f9(%rip), %rcx      # 0xb5ce
movq %rax, %rdi
movq %rdx, %rsi
callq 0x4215
leaq 0x7c6a(%rip), %rsi      # 0xbd51
movq %rax, %rdi
callq 0x2ca1
testq %rax, %rax
je 0x4106
movq %rax, 0x58(%rbx)
movl $0x0, 0x50(%rbx)
movl 0x54(%rsp), %r15d
jmp 0x4122
movq 0x48(%rbx), %rdi
callq 0x1030
movq 0x60(%rbx), %rdi
addq $-0x28, %rdi
callq 0x1030
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq (%rsp), %rdi
callq 0x10c0
movq %r13, %rdi
callq 0x10c0
movq 0x38(%rsp), %r12
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x4145
movb $0x2e, (%rax)
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
testl %r15d, %r15d
jns 0x416a
leaq 0x70(%rsp), %rsi
movl $0xe, %ecx
movq %rbx, %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 %r14, %rdi
callq 0x13df
jmp 0x411c
movq %r14, %rdi
callq 0x1401
jmp 0x411c
movq %rbp, %rdi
callq 0x1599
jmp 0x41f5
movq %rbp, %rdi
callq 0x1577
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4138
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x1429
jmp 0x41c9
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x14a1
movq 0x18(%rsp), %rbx
jmp 0x411c
movq %r14, %rdi
callq 0x1479
jmp 0x411c
movq %r14, %rdi
callq 0x1451
jmp 0x411c
movq %rbp, %rdi
callq 0x1555
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4133
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x14f1
jmp 0x4122