// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/kvlds/btree_node.c
// Function: btree_node_dirty
struct node *
btree_node_dirty(struct btree * T, struct node * N)
{
	struct node * N_dirty;
	size_t i;

	/* Sanity check the node. */
	assert(node_present(N));
	assert(N->state == NODE_STATE_CLEAN);
	assert(pool_rec_lockcount(T->P, N) > 0);

	/* Notify the cleaner. */
	btree_cleaning_notify_dirtying(T->cstate, N);

	/* If we are not a root and p_dirty is clean, recurse up the tree. */
	if ((N->root == 0) &&
	    (N->p_dirty->state == NODE_STATE_CLEAN) &&
	    (btree_node_dirty(T, N->p_dirty) == NULL))
		goto err0;

	/* Allocate a new dirty node. */
	if ((N_dirty = btree_node_mknode(T, N->type, N->height, N->nkeys,
	    NULL, NULL, NULL)) == NULL)
		goto err0;

	/* Copy more node data. */
	N_dirty->root = N->root;
	N_dirty->mlen_t = N->mlen_t;
	N_dirty->mlen_n = N->mlen_n;
	N_dirty->p_dirty = N->p_dirty;

	/* The old node is now SHADOW. */
	N->state = NODE_STATE_SHADOW;
	N->p_dirty = NULL;
	btree_node_lock(T, N);

	/* The new node is dirty. */
	N_dirty->oldestncleaf = (uint64_t)(-1);
	N_dirty->p_shadow = NULL;

	/* Leaf or parent? */
	if (N->type == NODE_TYPE_LEAF) {
		/* Duplicate key-value pairs. */
		if (IMALLOC(N_dirty->u.pairs, N->nkeys, struct kvpair_const))
			goto err1;
		memcpy(N_dirty->u.pairs, N->u.pairs,
		    N->nkeys * sizeof(struct kvpair_const));
	} else {
		/* Duplicate keys. */
		if (IMALLOC(N_dirty->u.keys, N->nkeys,
		    const struct kvldskey *))
			goto err1;
		memcpy(N_dirty->u.keys, N->u.keys,
		    N->nkeys * sizeof(const struct kvldskey *));

		/* Copy child vector. */
		if (IMALLOC(N_dirty->v.children, N->nkeys + 1, struct node *))
			goto err2;
		memcpy(N_dirty->v.children, N->v.children,
		    (N->nkeys + 1) * sizeof(struct node *));

		/* Tell children (if any) about their new dirty parent. */
		for (i = 0; i <= N_dirty->nkeys; i++) {
			if (node_hasplock(N_dirty->v.children[i]))
				btree_node_unlock(T, N);
			N_dirty->v.children[i]->p_dirty = N_dirty;
			if (node_hasplock(N_dirty->v.children[i]))
				btree_node_lock(T, N_dirty);
		}
	}

	/* Update the dirty tree structure. */
	if (N_dirty->root == 0) {
		for (i = 0; i <= N_dirty->p_dirty->nkeys; i++)
			if (N_dirty->p_dirty->v.children[i] == N)
				N_dirty->p_dirty->v.children[i] = N_dirty;
	} else {
		btree_node_unlock(T, T->root_dirty);
		T->root_dirty = N_dirty;
		btree_node_lock(T, T->root_dirty);
	}

	/* Success! */
	return (N_dirty);

err2:
	free(N_dirty->u.keys);
err1:
	pool_rec_free(T->P, N_dirty);
	node_free(N_dirty);
err0:
	/* Failure! */
	return (NULL);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movb 0x1c(%rsi), %al
testb $0x2, %al
jne 0x7d39
testb $0xc, %al
jne 0x7d58
movq %rsi, %rbx
movq %rdi, %r14
movq 0x30(%rdi), %rdi
callq 0xe7b0
testq %rax, %rax
je 0x7d77
movq 0x40(%r14), %rdi
movq %rbx, %rsi
callq 0x60e9
movb 0x1c(%rbx), %al
testb $0x10, %al
jne 0x7b22
movq 0x28(%rbx), %rsi
testb $0xc, 0x1c(%rsi)
jne 0x7b22
movq %r14, %rdi
callq 0x7abe
testq %rax, %rax
je 0x7d27
movb 0x1c(%rbx), %al
andb $0x3, %al
movzbl %al, %esi
movsbl 0x1d(%rbx), %edx
movq 0x38(%rbx), %rcx
andq $0x0, (%rsp)
xorl %r12d, %r12d
movq %r14, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x767c
testq %rax, %rax
je 0x7d2a
movq %rax, %r15
movb 0x1c(%rbx), %al
andb $0x10, %al
movb 0x1c(%r15), %cl
andb $-0x11, %cl
orb %al, %cl
movb %cl, 0x1c(%r15)
movb 0x1e(%rbx), %al
movb %al, 0x1e(%r15)
movb 0x1f(%rbx), %al
movb %al, 0x1f(%r15)
movq 0x28(%rbx), %rax
movq %rax, 0x28(%r15)
movb 0x1c(%rbx), %al
andb $-0xd, %al
orb $0x4, %al
movb %al, 0x1c(%rbx)
andq $0x0, 0x28(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7a99
orq $-0x1, 0x10(%r15)
andq $0x0, 0x20(%r15)
movb 0x1c(%rbx), %al
andb $0x3, %al
movq 0x38(%rbx), %rdi
cmpb $0x1, %al
jne 0x7c12
pushq $0x10
popq %rsi
callq 0x7d96
movq %rax, 0x40(%r15)
testq %rax, %rax
setne %cl
movq 0x38(%rbx), %rdx
testq %rdx, %rdx
sete %sil
orb %cl, %sil
cmpb $0x1, %sil
jne 0x7d13
movq 0x40(%rbx), %rsi
shlq $0x4, %rdx
movq %rax, %rdi
callq 0x2280
testb $0x10, 0x1c(%r15)
jne 0x7cea
xorl %eax, %eax
movq 0x28(%r15), %rcx
cmpq 0x38(%rcx), %rax
ja 0x7d05
movq 0x48(%rcx), %rcx
cmpq %rbx, (%rcx,%rax,8)
jne 0x7c0d
movq %r15, (%rcx,%rax,8)
incq %rax
jmp 0x7bf1
pushq $0x8
popq %rsi
callq 0x7d96
movq %rax, 0x40(%r15)
testq %rax, %rax
setne %cl
movq 0x38(%rbx), %r12
testq %r12, %r12
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x7d13
movq 0x40(%rbx), %rsi
leaq (,%r12,8), %rdx
movq %rax, %rdi
callq 0x2280
incq %r12
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x7d96
movq %rax, 0x48(%r15)
testq %rax, %rax
setne %dl
movq 0x38(%rbx), %rcx
cmpq $-0x1, %rcx
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x7d0a
movq 0x48(%rbx), %rsi
leaq 0x8(,%rcx,8), %rdx
movq %rax, %rdi
callq 0x2280
xorl %r12d, %r12d
cmpq 0x38(%r15), %r12
ja 0x7be4
movq 0x48(%r15), %rax
movq (%rax,%r12,8), %rax
movb 0x1c(%rax), %cl
andb $0x3, %cl
cmpb $0x2, %cl
je 0x7cc5
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7dc2
movq 0x48(%r15), %rax
movq (%rax,%r12,8), %rax
movq %r15, 0x28(%rax)
movq 0x48(%r15), %rax
movq (%rax,%r12,8), %rax
movb 0x1c(%rax), %al
andb $0x3, %al
cmpb $0x2, %al
je 0x7ce5
movq %r14, %rdi
movq %r15, %rsi
callq 0x7a99
incq %r12
jmp 0x7c95
movq 0x28(%r14), %rsi
movq %r14, %rdi
callq 0x7dc2
movq %r15, 0x28(%r14)
movq %r14, %rdi
movq %r15, %rsi
callq 0x7a99
movq %r15, %r12
jmp 0x7d2a
movq 0x40(%r15), %rdi
callq 0x2040
movq 0x30(%r14), %rdi
movq %r15, %rsi
callq 0xe776
movq %r15, %rdi
callq 0x9388
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xa3ea(%rip), %rdi      # 0x1212a
leaq 0xa2fa(%rip), %rsi      # 0x12041
leaq 0xa3ec(%rip), %rcx      # 0x1213a
movl $0x1c8, %edx            # imm = 0x1C8
callq 0x21c0
leaq 0xa418(%rip), %rdi      # 0x12177
leaq 0xa2db(%rip), %rsi      # 0x12041
leaq 0xa3cd(%rip), %rcx      # 0x1213a
movl $0x1c9, %edx            # imm = 0x1C9
callq 0x21c0
leaq 0xa416(%rip), %rdi      # 0x12194
leaq 0xa2bc(%rip), %rsi      # 0x12041
leaq 0xa3ae(%rip), %rcx      # 0x1213a
movl $0x1ca, %edx            # imm = 0x1CA
callq 0x21c0