// File: /workspace/github_c_makefile_star2_O0/Tarsnap[P]kivaloo/kvlds/serialize.c
// Function: serialize
int
serialize(struct btree * T, struct node * N, size_t buflen)
{
	size_t pagelen;
	uint8_t * p;
	size_t i;

	/* Sanity check: This node should be dirty and have no page buffer. */
	assert(N->state == NODE_STATE_DIRTY);
	assert(N->pagebuf == NULL);

	/* Sanity check: The node must have a height (incl. 0). */
	assert(N->height != -1);

	/* Sanity check: We can only store 2 bytes of nkeys. */
	assert(N->nkeys <= UINT16_MAX);

	/* Get the page length.  This also sets N->pagelen. */
	pagelen = serialize_size(N);

	/* Sanity check: The page should fit into the buffer. */
	assert(pagelen <= buflen);

	/* Allocate a page buffer. */
	if ((N->pagebuf = malloc(buflen)) == NULL)
		goto err0;
	p = N->pagebuf;

	/* Copy magic. */
	memcpy(p, "KVLDS\0", 6);
	p += 6;

	/* Write out the number of keys. */
	be16enc(p, (uint16_t)N->nkeys);
	p += 2;

	/* Write height and rootedness. */
	if (N->root)
		*p = (uint8_t)(0x80 + N->height);
	else
		*p = (uint8_t)N->height;
	p += 1;

	/* Write the matching prefix length. */
	*p = N->mlen_t;
	p += 1;

	/* If this is a root, write the size of the tree. */
	if (N->root) {
		be64enc(p, T->nnodes);
		p += 8;
	}

	/* Write out node data. */
	if (N->type == NODE_TYPE_LEAF) {
		/* Write out the keys. */
		for (i = 0; i < N->nkeys; i++) {
			kvldskey_serialize(N->u.pairs[i].k, p);
			N->u.pairs[i].k = (struct kvldskey *)p;
			p += kvldskey_serial_size(N->u.pairs[i].k);
		}

		/* Write out the values. */
		for (i = 0; i < N->nkeys; i++) {
			kvldskey_serialize(N->u.pairs[i].v, p);
			N->u.pairs[i].v = (struct kvldskey *)p;
			p += kvldskey_serial_size(N->u.pairs[i].v);
		}
	} else {
		/* Write out the keys. */
		for (i = 0; i < N->nkeys; i++) {
			kvldskey_serialize(N->u.keys[i], p);
			N->u.keys[i] = (struct kvldskey *)p;
			p += kvldskey_serial_size(N->u.keys[i]);
		}

		/* Write out the child structures. */
		for (i = 0; i <= N->nkeys; i++) {
			/* Sanity check: Merging should be complete. */
			assert(N->v.children[i]->merging == 0);

			/* Page number of child. */
			be64enc(p, N->v.children[i]->pagenum);
			p += 8;

			/* Page number of child's oldest leaf. */
			be64enc(p, N->v.children[i]->oldestleaf);
			p += 8;

			/* Page size of leaf. */
			be32enc(p, N->v.children[i]->pagesize);
			p += 4;
		}
	}

	/* Sanity-check: Make sure we computed the size correctly. */
	assert(p == N->pagebuf + pagelen);

	/* Zero the remaining space. */
	memset(p, 0, buflen - pagelen);

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
shrb $0x2, %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0xe78a
jmp 0xe7a9
leaq 0x10c0a(%rip), %rdi     # 0x1f39b
leaq 0x112d4(%rip), %rsi     # 0x1fa6c
movl $0x4f, %edx
leaq 0x112d4(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0xe7b6
jmp 0xe7d5
leaq 0x112f0(%rip), %rdi     # 0x1faad
leaq 0x112a8(%rip), %rsi     # 0x1fa6c
movl $0x50, %edx
leaq 0x112a8(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x18(%rbp), %rax
movsbl 0x1d(%rax), %eax
cmpl $-0x1, %eax
je 0xe7e4
jmp 0xe803
leaq 0x112d5(%rip), %rdi     # 0x1fac0
leaq 0x1127a(%rip), %rsi     # 0x1fa6c
movl $0x53, %edx
leaq 0x1127a(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x18(%rbp), %rax
cmpq $0xffff, 0x38(%rax)     # imm = 0xFFFF
ja 0xe813
jmp 0xe832
leaq 0x112b6(%rip), %rdi     # 0x1fad0
leaq 0x1124b(%rip), %rsi     # 0x1fa6c
movl $0x56, %edx
leaq 0x1124b(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0xecb0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
ja 0xe84b
jmp 0xe86a
leaq 0x11295(%rip), %rdi     # 0x1fae7
leaq 0x11213(%rip), %rsi     # 0x1fa6c
movl $0x5c, %edx
leaq 0x11213(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x20(%rbp), %rdi
callq 0x22a0
movq -0x18(%rbp), %rcx
movq %rax, 0x50(%rcx)
cmpq $0x0, %rax
jne 0xe886
jmp 0xec96
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x1138c(%rip), %ecx     # 0x1fc28
movl %ecx, (%rax)
movw 0x11387(%rip), %cx      # 0x1fc2c
movw %cx, 0x4(%rax)
movq -0x30(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movzwl %ax, %esi
callq 0xeec0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
shrb $0x4, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xe900
movq -0x18(%rbp), %rax
movsbl 0x1d(%rax), %eax
addl $0x80, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
jmp 0xe90d
movq -0x18(%rbp), %rax
movb 0x1d(%rax), %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movb 0x1e(%rax), %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
shrb $0x4, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xe963
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0xef00
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movb 0x1c(%rax), %al
andb $0x3, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xead1
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xea20
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movzbl (%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x2280
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movzbl (%rax), %eax
addq $0x1, %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xe980
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xeacc
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x2280
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
addq $0x1, %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xea28
jmp 0xec44
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xeb5d
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzbl (%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x2280
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzbl (%rax), %eax
addq $0x1, %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xead9
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
ja 0xec42
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movb 0x1c(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xeb99
jmp 0xebb8
leaq 0x10f59(%rip), %rdi     # 0x1faf9
leaq 0x10ec5(%rip), %rsi     # 0x1fa6c
movl $0x96, %edx
leaq 0x10ec5(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rsi
callq 0xef00
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rsi
callq 0xef00
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x18(%rax), %esi
callq 0xefd0
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xeb65
jmp 0xec44
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jne 0xec5b
jmp 0xec7a
leaq 0x10eb6(%rip), %rdi     # 0x1fb18
leaq 0x10e03(%rip), %rsi     # 0x1fa6c
movl $0xa7, %edx
leaq 0x10e03(%rip), %rcx     # 0x1fa78
callq 0x21d0
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rdx
subq -0x28(%rbp), %rdx
xorl %esi, %esi
callq 0x21e0
movl $0x0, -0x4(%rbp)
jmp 0xec9d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)