// File: /workspace/github_c_makefile_star2_O3/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
movups %xmm0, 0x28(%rbx)
movups %xmm0, 0x18(%rbx)
movups %xmm0, 0x8(%rbx)
movq %rsi, (%rbx)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x35c4
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 0x3af3
leaq 0x50(%rsp), %rdi
movq %rbx, (%rdi)
movq %r15, 0x8(%rdi)
movzwl 0x8(%r15), %eax
movw %ax, 0x10(%rdi)
movl $0x1, 0x14(%rdi)
movq $0x0, 0x18(%rdi)
callq 0x3421
testq %rax, %rax
je 0x2f1e
movabsq $0x1000000000000000, %r13 # imm = 0x1000000000000000
leaq 0x70(%rsp), %r12
leaq 0x30(%rsp), %rbp
cmpq $0x0, 0x10(%rsp)
je 0x2ec4
movq 0x58(%rsp), %r14
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %rax
addq 0x98(%rax), %rbx
movq %r12, %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x35c4
movq %r12, %rdi
movq %r14, %rsi
callq 0x3af3
cmpq $-0x2, %rax
je 0x2eae
movq 0x78(%rsp), %rax
movq (%rax), %rcx
movq %rcx, %rdx
shrq $0x3e, %rdx
cmpl $0x3, %edx
jb 0x2df1
testq %r13, %rcx
jne 0x2eae
jmp 0x2ed9
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)
movq %r12, %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x3af3
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movzwl 0x20(%rsp), %eax
movw %ax, 0x40(%rsp)
movl $0x1, 0x44(%rsp)
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rbp, %rdi
callq 0x3421
testq %rax, %rax
je 0x2ea9
movq 0x78(%rsp), %rax
movq (%rax), %rcx
testq %r13, %rcx
jne 0x2e47
movq %rcx, %rdx
orq %r13, %rdx
movq %rdx, (%rax)
movq %rcx, %rdx
shrq $0x28, %rdx
testb %dl, %dl
je 0x2e47
shrq $0x30, %rcx
andl $0xfff, %ecx            # imm = 0xFFF
addq %rcx, %rax
addq $0x8, %rax
movzbl %dl, %ecx
cmpl $0xff, %ecx
jne 0x2e9a
cmpl $0x0, (%rax)
je 0x2e47
addq $0x4, %rax
movq (%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x355a
jmp 0x2e47
leaq 0x70(%rsp), %r12
movq (%rbx), %rbx
movabsq $0xffffffffff, %rax     # imm = 0xFFFFFFFFFF
andq %rax, %rbx
jne 0x2d9e
leaq 0x50(%rsp), %rdi
callq 0x3421
testq %rax, %rax
jne 0x2d88
jmp 0x2f1e
movq %rcx, %rdx
orq %r13, %rdx
movq %rdx, (%rax)
movq %rcx, %rdx
shrq $0x28, %rdx
movl $0xff, %esi
andl %esi, %edx
shrq $0x30, %rcx
andl $0xfff, %ecx            # imm = 0xFFF
addq %rcx, %rax
addq $0x8, %rax
cmpl %esi, %edx
jne 0x2f0b
movl (%rax), %edx
addq $0x4, %rax
testl %edx, %edx
je 0x2ec4
movq (%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x355a
jmp 0x2ec4
movq 0x8(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq