// File: /workspace/github_c_makefile_star2_O0/jflatow[P]jfdb/src/db.c
// Function: JFDB_mark_dirty
static JFDB *JFDB_mark_dirty(JFDB *db, const JFT *tx, JFT_Offset prior) {
  // look for collisions in the old keys
  // if we find any, dirty them, and deallocate the regions
  JFT_Cursor txCursor = JFT_cursor((JFT *)tx), otherCursor;
  JFT_Stem stem = (JFT_Stem) {
    .pre = JFT_SYMBOL_PRIMARY,
    .size = 1,
    .data = db->keyData
  };
  JFT_Keys keys = JFT_keys(&txCursor, &stem, Forward);
  while (JFT_keys_next(&keys)) {
    // walk the tries from prior, all the way to the base
    // it is crucial to start from the prior offset
    // so that we free regions even in tries that have become obsolete
    JFT *trie;
    for (JFT_Offset pos = prior; pos; pos = JFT_parent_offset(trie)) {
      trie = JFDB_get_trie(db, pos);
      JFT_cursor_init(&otherCursor, In, trie);
      // if we fall off the trie, we should continue the search backwards
      if (JFT_cursor_find(&otherCursor, keys.stem) == JFT_SYMBOL_NIL)
        continue;
      // otherwise, this trie overlaps the key: we need to clobber
      if (JFT_node_type(otherCursor.node) == Leaf) {
        // we can stop after we hit a leaf:
        //  a leaf must have already dirtied and freed its ancestors
        // if its already dirty, do nothing (so we don't double free)
        // NB: this can happen e.g. if two longer keys clobber a prefix
        if (!JFT_is_dirty(otherCursor.node)) {
          JFT_set_dirty((JFT *)otherCursor.node, True);
          JFT_Leaf leaf = JFT_leaf(otherCursor.node);
          if (leaf.size)
            JFDB_vfree(db, *(JFDB_Region *)leaf.data);
          break;
        }
      } else {
        // we have to dirty and free all leaves in any branches we clobber
        // and then we still need to continue searching ancestors
        // NB: scratch[0] is guaranteed to be be big enough for keys of tx
        //     we know because we already merged (at least) tx into it
        JFT_Stem subStem = (JFT_Stem) {.data = db->scratch[0]->data};
        JFT_Keys subKeys = JFT_keys(&otherCursor, &subStem, Forward);
        while (JFT_keys_next(&subKeys)) {
          if (!JFT_is_dirty(otherCursor.node)) {
            JFT_set_dirty((JFT *)otherCursor.node, True);
            JFT_Leaf leaf = JFT_leaf(otherCursor.node);
            if (leaf.size)
              JFDB_vfree(db, *(JFDB_Region *)JFT_leaf(otherCursor.node).data);
          }
        }
      }
    }
  }
  return db;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x34a0
movq $-0x5, -0xa0(%rbp)
movw $0x1, -0x98(%rbp)
movq -0x8(%rbp), %rax
addq $0x998, %rax            # imm = 0x998
movq %rax, -0x90(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
movl $0x1, %ecx
callq 0x4610
leaq -0xc0(%rbp), %rdi
callq 0x4670
cmpq $0x0, %rax
je 0x378c
movq -0x18(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x3787
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x3480
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdx
leaq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0x4bd0
movq -0xb8(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x50a0
cmpq $-0x2, %rax
jne 0x3631
jmp 0x376f
movq -0x80(%rbp), %rdi
callq 0x4690
cmpl $0x3, %eax
jne 0x3697
movq -0x80(%rbp), %rdi
callq 0x46b0
cmpl $0x0, %eax
jne 0x3692
movq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0x46d0
movq -0x80(%rbp), %rdi
callq 0x4220
movl %eax, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
cmpl $0x0, -0xe0(%rbp)
je 0x368d
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rax
movq (%rax), %rsi
callq 0x4710
jmp 0x3787
jmp 0x376d
movq $0x0, -0xf8(%rbp)
movw $0x0, -0xf0(%rbp)
movq -0x8(%rbp), %rax
movq 0x988(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0xe8(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xf8(%rbp), %rdx
movl $0x1, %ecx
callq 0x4610
leaq -0x118(%rbp), %rdi
callq 0x4670
cmpq $0x0, %rax
je 0x376b
movq -0x80(%rbp), %rdi
callq 0x46b0
cmpl $0x0, %eax
jne 0x3766
movq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0x46d0
movq -0x80(%rbp), %rdi
callq 0x4220
movl %eax, -0x128(%rbp)
movq %rdx, -0x120(%rbp)
cmpl $0x0, -0x128(%rbp)
je 0x3764
movq -0x8(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x80(%rbp), %rdi
callq 0x4220
movq -0x140(%rbp), %rdi
movl %eax, -0x138(%rbp)
movq %rdx, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq (%rax), %rsi
callq 0x4710
jmp 0x3766
jmp 0x36e0
jmp 0x376d
jmp 0x376f
movq -0xc8(%rbp), %rdi
callq 0x34f0
movq %rax, -0xd0(%rbp)
jmp 0x35d6
jmp 0x35b5
movq -0x8(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax)