// File: /workspace/github_c_makefile_star2_O0/jflatow[P]jfdb/src/db.c
// Function: JFDB_open
JFDB *JFDB_open(const char *path, int flags) {
  JFDB *db = calloc(1, sizeof(JFDB));
  int len = strlen(path);
  if (db == NULL)
    return NULL;
  if (len == 0 || len > JFDB_MAX_PATH)
    return JFDB_set_error(db, JFDB_EPATH, "invalid path ('%.32s')", path);
  if ((db->path = strdup(path)) == NULL)
    return JFDB_set_error(db, JFDB_ENOMEM, "out of memory");
  if ((db->scratch[0] = JFT_membuf()) == NULL)
    return JFDB_set_error(db, JFDB_ENOMEM, "out of memory");
  if ((db->scratch[1] = JFT_membuf()) == NULL)
    return JFDB_set_error(db, JFDB_ENOMEM, "out of memory");

  char fp[JFDB_MAX_PATH + 6];
  db->flags = flags;

  // load vals before keys, because we want to keep the tip from keys
  snprintf(fp, sizeof(fp), "%s.vals", path);
  db->vmap.fd = JFDB_file_open(db, fp);
  if (JFDB_has_error(db))
    return db;

  snprintf(fp, sizeof(fp), "%s.keys", path);
  db->kmap.fd = JFDB_file_open(db, fp);
  if (JFDB_has_error(db))
    return db;

  // we'll need the *actual* size of the keys file to ensure correctness
  struct stat kstat;
  if (fstat(db->kmap.fd, &kstat))
    return JFDB_set_error(db, JFDB_EFILE, "failed to fstat keys");

  // load the checkpoint
  JFDB_CheckPoint *cp = &db->tip.cp;

  // map the files accordingly
  cp->lengthVals = MAX(cp->lengthVals, sizeof(JFDB_Header));
  JFDB_mmap_file(db, JFDB_VALS, cp->lengthVals, 1);
  if (JFDB_has_error(db))
    return db;

  cp->lengthKeys = MAX(cp->lengthKeys, sizeof(JFDB_Header));
  JFDB_mmap_file(db, JFDB_KEYS, cp->lengthKeys, 1);
  if (JFDB_has_error(db))
    return db;

  // ensure that the last checkpoint was written correctly, otherwise recompact
  if (kstat.st_size > cp->lengthKeys)
    return JFDB_compact(db, NULL);
  if (cp->offset && db->kmap.map[cp->lengthKeys - 1] != '\n')
    return JFDB_compact(db, NULL);
  return db;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4e0, %rsp            # imm = 0x4E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x1, %edi
movl $0xd98, %esi            # imm = 0xD98
callq 0x1120
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10a0
movl %eax, -0x24(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1fc5
movq $0x0, -0x8(%rbp)
jmp 0x2308
cmpl $0x0, -0x24(%rbp)
je 0x1fd4
cmpl $0x400, -0x24(%rbp)     # imm = 0x400
jle 0x1ff8
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x3, %esi
leaq 0x70e5(%rip), %rdx      # 0x90cd
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x10(%rbp), %rdi
callq 0x11c0
movq -0x20(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x202e
movq -0x20(%rbp), %rdi
movl $0x2, %esi
leaq 0x70c6(%rip), %rdx      # 0x90e4
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2308
movb $0x0, %al
callq 0x72c0
movq -0x20(%rbp), %rcx
movq %rax, 0x988(%rcx)
cmpq $0x0, %rax
jne 0x2066
movq -0x20(%rbp), %rdi
movl $0x2, %esi
leaq 0x708e(%rip), %rdx      # 0x90e4
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2308
movb $0x0, %al
callq 0x72c0
movq -0x20(%rbp), %rcx
movq %rax, 0x990(%rcx)
cmpq $0x0, %rax
jne 0x209e
movq -0x20(%rbp), %rdi
movl $0x2, %esi
leaq 0x7056(%rip), %rdx      # 0x90e4
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2308
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8c(%rax)
leaq -0x430(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x406, %esi            # imm = 0x406
leaq 0x7003(%rip), %rdx      # 0x90c5
movb $0x0, %al
callq 0x10d0
movq -0x20(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x23d0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xa8(%rax)
movq -0x20(%rbp), %rdi
callq 0x2540
cmpl $0x0, %eax
je 0x2100
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2308
leaq -0x430(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x406, %esi            # imm = 0x406
leaq 0x6fa6(%rip), %rdx      # 0x90bd
movb $0x0, %al
callq 0x10d0
movq -0x20(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x23d0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x90(%rax)
movq -0x20(%rbp), %rdi
callq 0x2540
cmpl $0x0, %eax
je 0x2155
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x20(%rbp), %rax
movl 0x90(%rax), %edi
leaq -0x4c0(%rbp), %rsi
callq 0x11d0
cmpl $0x0, %eax
je 0x2190
movq -0x20(%rbp), %rdi
movl $0x4, %esi
leaq 0x6f72(%rip), %rdx      # 0x90f2
movb $0x0, %al
callq 0x2320
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x20(%rbp), %rax
addq $0xc0, %rax
addq $0x10, %rax
movq %rax, -0x4c8(%rbp)
movq -0x4c8(%rbp), %rax
cmpq $0x70, 0x48(%rax)
jbe 0x21c7
movq -0x4c8(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x4d0(%rbp)
jmp 0x21d5
movl $0x70, %eax
movq %rax, -0x4d0(%rbp)
jmp 0x21d5
movq -0x4d0(%rbp), %rcx
movq -0x4c8(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x20(%rbp), %rdi
movq -0x4c8(%rbp), %rax
movq 0x48(%rax), %rdx
movl $0x2, %esi
movl $0x1, %ecx
callq 0x2570
movq -0x20(%rbp), %rdi
callq 0x2540
cmpl $0x0, %eax
je 0x2220
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x4c8(%rbp), %rax
cmpq $0x70, 0x40(%rax)
jbe 0x2242
movq -0x4c8(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x4d8(%rbp)
jmp 0x2250
movl $0x70, %eax
movq %rax, -0x4d8(%rbp)
jmp 0x2250
movq -0x4d8(%rbp), %rcx
movq -0x4c8(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rdi
movq -0x4c8(%rbp), %rax
movq 0x40(%rax), %rdx
movl $0x1, %ecx
movl %ecx, %esi
callq 0x2570
movq -0x20(%rbp), %rdi
callq 0x2540
cmpl $0x0, %eax
je 0x2295
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x490(%rbp), %rax
movq -0x4c8(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jbe 0x22bc
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x26f0
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x4c8(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x2300
movq -0x20(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x4c8(%rbp), %rcx
movq 0x40(%rcx), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x2300
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x26f0
movq %rax, -0x8(%rbp)
jmp 0x2308
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x4e0, %rsp            # imm = 0x4E0
popq %rbp
retq
nopw %cs:(%rax,%rax)