// File: /workspace/github_c_makefile_star2_O3/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
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 0x791c(%rip), %r14      # 0xb4e5
movq %r14, %rsi
callq 0x1190
testl %eax, %eax
je 0x3c04
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
addq %rax, %r15
sarq $0x20, %r15
addq %rbp, %r15
leaq 0x78fa(%rip), %r14      # 0xb4ea
movq %r15, %rdi
movq %r14, %rsi
callq 0x1190
xorl %ebx, %ebx
cmpl $0x1, %eax
sbbl %ebx, %ebx
jmp 0x3c09
movl $0x1, %ebx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2ccf
movq %rax, %r14
movq %rbp, %rdi
callq 0x2c75
movq %rax, %r13
movq %r14, %rdi
movl $0x2e, %esi
callq 0x1250
movl $0x0, 0x38(%rsp)
testq %rax, %rax
movq %rax, 0x58(%rsp)
je 0x3c87
movzbl 0x1(%rax), %ecx
testl %ecx, %ecx
je 0x3c87
cmpl $0x2d, %ecx
je 0x3c87
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 0x3c84
testl %eax, %eax
movq %r15, %r14
je 0x3c87
movq 0x58(%rsp), %rcx
movb $0x0, (%rcx)
movl %eax, 0x38(%rsp)
jmp 0x3c87
movq %r15, %r14
testl %ebx, %ebx
leaq 0x7855(%rip), %rax      # 0xb4e5
leaq 0x7853(%rip), %rcx      # 0xb4ea
cmovgq %rax, %rcx
leaq 0x784c(%rip), %rsi      # 0xb4ee
movq %r13, %rdi
movq %r14, %rdx
callq 0x427d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x4252
movq %r13, 0x30(%rsp)
leaq 0x80e9(%rip), %rsi      # 0xbdb1
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x3cda
movq %rax, %r13
jmp 0x3d2f
testl %ebx, %ebx
js 0x415d
movq 0x30(%rsp), %rbx
jg 0x4167
leaq 0x77fa(%rip), %rsi      # 0xb4ee
leaq 0x77ea(%rip), %rcx      # 0xb4e5
movq %rbx, %rdi
movq %r14, %rdx
callq 0x427d
testq %rax, %rax
je 0x4252
leaq 0x809b(%rip), %rsi      # 0xbdb1
movq %rax, %rdi
callq 0x1240
testq %rax, %rax
je 0x419c
movq %rax, %r13
movl $0x1, %ebx
xorl %ebp, %ebp
testl %ebx, %ebx
cmovgl %ebx, %ebp
leaq 0x7810(%rip), %rsi      # 0xb54d
leaq 0x780c(%rip), %rcx      # 0xb550
movq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x427d
leaq 0x8059(%rip), %rsi      # 0xbdb1
movq %rax, %rdi
callq 0x2c15
testq %rax, %rax
je 0x3ee2
movl $0x70, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rcx
callq 0x10b0
cmpq $0x1, %rax
jne 0x4143
movl $0x0, 0x48(%rsp)
leaq 0x76bd(%rip), %rsi      # 0xb459
leaq 0x44(%rsp), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x4150
movl %ebp, 0x20(%rsp)
movq %r14, (%rsp)
movq %r12, 0x10(%rsp)
cmpl $0x0, 0x44(%rsp)
jle 0x3e0d
leaq 0x7697(%rip), %r14      # 0xb466
leaq 0x24(%rsp), %r15
leaq 0x27f0(%rsp), %r12
leaq 0xe0(%rsp), %rbp
xorl %ebx, %ebx
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
jne 0x417a
incl %ebx
cmpl 0x44(%rsp), %ebx
jl 0x3de6
leaq 0x7679(%rip), %rsi      # 0xb48d
leaq 0x48(%rsp), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x1, %eax
jne 0x3eed
leaq 0x7668(%rip), %rsi      # 0xb49b
leaq 0x60(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x4c(%rsp), %r8
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x3, %eax
movq 0x10(%rsp), %r12
movq (%rsp), %r14
movl 0x38(%rsp), %ebp
jne 0x4188
movl 0x48(%rsp), %r8d
cmpl %r8d, %ebp
jg 0x41ac
testl %ebp, %ebp
jle 0x3f12
leaq 0x7642(%rip), %r14      # 0xb4c1
leaq 0x1c(%rsp), %r15
leaq 0x2c(%rsp), %r12
movl %ebp, %ebx
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1090
cmpl $0x2, %eax
jne 0x41e4
decl %ebx
jne 0x3e8b
leaq 0x760f(%rip), %rsi      # 0xb4c1
leaq 0x28(%rsp), %rdx
leaq 0x24(%rsp), %rcx
movq %r13, %rdi
xorl %eax, %eax
callq 0x1090
cmpl $0x2, %eax
jne 0x41f2
movl 0x1c(%rsp), %ecx
movl 0x2c(%rsp), %eax
movl 0x28(%rsp), %ebx
movq 0x10(%rsp), %r12
jmp 0x3f2f
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4213
movl 0x38(%rsp), %ebp
testl %ebp, %ebp
movq 0x10(%rsp), %r12
movq (%rsp), %r14
jne 0x4192
movl $0x0, 0x50(%rsp)
movl $0x1, 0x4c(%rsp)
xorl %eax, %eax
movl %eax, 0x2c(%rsp)
movl %eax, 0x1c(%rsp)
movl (%r12), %ebx
movl %ebx, 0x28(%rsp)
movl 0x4(%r12), %ecx
movl %ecx, 0x24(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x34(%r12)
movq $0x0, 0x68(%r12)
movl %ebp, 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
movslq %ebx, %r14
leaq (,%r14,8), %rax
leaq (%rax,%rax,4), %rdi
addq $0x50, %rdi
leaq 0x767f(%rip), %rsi      # 0xb5fd
callq 0x2af5
movl %ebp, %ecx
movq %rax, %rbp
testl %ecx, %ecx
jle 0x401f
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rbp, %rbp
je 0x413a
movl %ebx, 0x54(%rsp)
movq %rbp, %rbx
addq $0x28, %rbx
movslq 0x1c(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
movq 0x8(%rsp), %r12
movq %r12, %rdi
movl $0x1, %edx
callq 0x1230
movl $0x28, %esi
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0x10b0
cmpq %r14, %rax
jne 0x41d0
movl 0x54(%rsp), %ebx
testl %ebx, %ebx
jle 0x406d
movl %ebx, %eax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
xorl %eax, %eax
movl 0x20(%rsp), %r15d
movslq 0x2c(%rbp,%rsi), %rdi
addq %rdi, %rcx
cmpl %eax, %edi
cmovgl %edi, %eax
addq $0x28, %rsi
cmpq %rsi, %rdx
jne 0x4007
jmp 0x4076
movq %rbp, 0x60(%r12)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rbp, %rbp
je 0x413a
addq $0x60, %r12
addq $0x28, %rbp
movq %rbp, (%r12)
movl $0x28, %esi
movq %rbp, %rdi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
callq 0x10b0
cmpq %r14, %rax
jne 0x4267
movq (%r12), %rcx
movq 0x10(%rsp), %r12
movl 0x20(%rsp), %r15d
jmp 0x408f
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x20(%rsp), %r15d
movq 0x10(%rsp), %r12
movl %eax, 0x20(%r12)
movq %rcx, 0x28(%r12)
movq 0x38(%rsp), %rcx
movq %rcx, 0x60(%r12)
movl 0x28(%rsp), %eax
subl 0x1c(%rsp), %eax
movl %eax, -0x4(%rcx)
movl 0x24(%rsp), %eax
subl 0x2c(%rsp), %eax
movl %eax, -0x8(%rcx)
movl %ebx, 0x30(%r12)
leaq 0x749c(%rip), %rsi      # 0xb54d
leaq 0x7902(%rip), %rcx      # 0xb9ba
movq 0x30(%rsp), %rdi
movq (%rsp), %rdx
callq 0x427d
leaq 0x7549(%rip), %rsi      # 0xb616
movq %rax, %rdi
callq 0x2bb2
movq %rax, 0x48(%r12)
testq %rax, %rax
je 0x4126
leaq 0x78d4(%rip), %rdx      # 0xb9ba
leaq 0x7541(%rip), %rcx      # 0xb62e
movq %rax, %rdi
movq %rdx, %rsi
callq 0x427d
leaq 0x7cb2(%rip), %rsi      # 0xbdb1
movq %rax, %rdi
callq 0x2c15
testq %rax, %rax
je 0x411c
movq %rax, 0x58(%r12)
movl $0x0, 0x50(%r12)
jmp 0x413a
movq 0x48(%r12), %rdi
callq 0x1030
movq 0x60(%r12), %rdi
addq $-0x28, %rdi
callq 0x1030
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq (%rsp), %r14
jmp 0x4209
movq %r14, %rdi
callq 0x13df
jmp 0x4203
movq %r14, %rdi
callq 0x1401
jmp 0x4203
movq %rbp, %rdi
callq 0x1599
jmp 0x41a4
movq %rbp, %rdi
callq 0x1577
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4220
movq (%rsp), %r14
movq %r14, %rdi
callq 0x1429
jmp 0x41fe
movq %r14, %rdi
callq 0x1479
jmp 0x4203
movq %r14, %rdi
callq 0x1451
jmp 0x4203
movq %rbp, %rdi
callq 0x1555
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x421b
movq 0xbe45(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0xe72b(%rip), %rdx      # 0x128e8
leaq 0x741b(%rip), %rsi      # 0xb5df
movq %r14, %rcx
xorl %eax, %eax
callq 0x11a0
jmp 0x4203
movq (%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x14f1
jmp 0x4276
movq (%rsp), %r14
movq %r14, %rdi
callq 0x14a1
jmp 0x41fe
movq (%rsp), %r14
movq %r14, %rdi
callq 0x14c9
movq 0x10(%rsp), %r12
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdi
callq 0x10c0
movq %r13, %rdi
callq 0x10c0
movq 0x30(%rsp), %rbx
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x422d
movb $0x2e, (%rax)
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
testl %r15d, %r15d
jns 0x4252
leaq 0x70(%rsp), %rsi
movl $0xe, %ecx
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 (%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x1521
movq 0x10(%rsp), %r12
jmp 0x4209