// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %rdx
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rbx
movaps %xmm0, (%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
xorl %r12d, %r12d
movq %r12, 0x30(%rbx)
movq %rsi, (%rbx)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x39a0
leaq 0xa8(%rsp), %r15
movq $-0x5, (%r15)
movw $0x1, 0x8(%r15)
movq %r14, 0x8(%rsp)
leaq 0x998(%r14), %rax
movq %rax, 0x10(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3f07
leaq 0x50(%rsp), %rdi
movq %rbx, (%rdi)
movq %r15, 0x8(%rdi)
movzwl 0x8(%r15), %eax
movw %ax, 0x10(%rdi)
movl $0x1, 0x14(%rdi)
movq %r12, 0x18(%rdi)
callq 0x37f8
testq %rax, %rax
je 0x32c3
movabsq $0x1000000000000000, %r13 # imm = 0x1000000000000000
movl $0xff, %r12d
leaq 0x30(%rsp), %rbp
cmpq $0x0, 0x10(%rsp)
je 0x32b0
movq 0x58(%rsp), %r14
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %rax
addq 0x98(%rax), %rbx
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x39a0
movq %r15, %rdi
movq %r14, %rsi
callq 0x3f07
cmpq $-0x2, %rax
je 0x3189
movq 0x78(%rsp), %rax
movq (%rax), %rcx
movq %rcx, %rdx
shrq $0x3e, %rdx
cmpl $0x3, %edx
jb 0x31a0
testq %r13, %rcx
je 0x3270
movq (%rbx), %rbx
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %rbx
jne 0x313a
jmp 0x32b0
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movw $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq 0x988(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r14, %r12
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x3f07
movq %r15, 0x30(%rsp)
movq %r14, 0x38(%rsp)
movq %r12, %r14
movl $0xff, %r12d
movzwl 0x20(%rsp), %eax
movw %ax, 0x40(%rsp)
movl $0x1, 0x44(%rsp)
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rbp, %rdi
callq 0x37f8
testq %rax, %rax
je 0x3189
movq 0x78(%rsp), %rcx
movq (%rcx), %rdx
testq %r13, %rdx
jne 0x3207
movq %rdx, %rax
orq %r13, %rax
movq %rax, (%rcx)
movq %rdx, %rax
shrq $0x28, %rax
andl %r12d, %eax
shrq $0x30, %rdx
andl $0xfff, %edx            # imm = 0xFFF
addq %rdx, %rcx
addq $0x8, %rcx
movl %eax, %edx
cmpl %r12d, %eax
jne 0x3252
movl (%rcx), %edx
testl %edx, %edx
je 0x3207
xorl %edx, %edx
cmpl $0xff, %eax
sete %dl
movq (%rcx,%rdx,4), %rsi
movq 0x8(%rsp), %rdi
callq 0x3931
jmp 0x3207
movq %rcx, %rdx
orq %r13, %rdx
movq %rdx, (%rax)
movq %rcx, %rdx
shrq $0x28, %rdx
andl %r12d, %edx
shrq $0x30, %rcx
andl $0xfff, %ecx            # imm = 0xFFF
addq %rcx, %rax
addq $0x8, %rax
cmpl %r12d, %edx
jne 0x329f
movl (%rax), %edx
addq $0x4, %rax
testl %edx, %edx
je 0x32b0
movq (%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x3931
leaq 0x50(%rsp), %rdi
callq 0x37f8
testq %rax, %rax
jne 0x3124
movq 0x8(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq