// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/tests/tree.c
// Function: main
int main(void)
{
	char *key[NUM];
	int v[NUM];

	struct tt_bintree bt = {
		.knum	= {
			.size	= 0,	/* Not used */
			.cmp	= keycmp,
			.swap	= NULL,	/* Not used */
		},
		.ops	= NULL,
	};

	tt_bintree_init(&bt);
	printf("\n");

	printf("Insert... ");
	for (int i = 0; i < NUM; i++) {
		key[i] = v2key(i);
		v[i] = i;
		tt_bintree_insert(&bt, key[i], &v[i]);
	}
	printf("Done\n");
	printf("Nodes = %d, height = %d\n", bt._count, tree_height(bt._root));
	printf("\n");

	printf("Walk... ");
	tt_bintree_walk(&bt, NULL, walk);
	printf("Done\n");
	printf("\n");

	struct tt_bintree_node *node = tt_bintree_min(&bt);
	printf("Min: %s -> %d\n", (const char *)node->key, *(int *)node->data);
	node = tt_bintree_max(&bt);
	printf("Max: %s -> %d\n", (const char *)node->key, *(int *)node->data);
	printf("\n");

	printf("Search... ");
	for (int i = 0; i < NUM; i++) {
		node = tt_bintree_search(&bt, key[i]);
		if (!node) {
			printf("Search error\n");
			break;
		}
	}
	printf("Done\n");
	printf("\n");

	printf("Delete... ");
	/* Delete half nodes */
	for (int i = 0; i < NUM; i += 2) {
		node = tt_bintree_search(&bt, key[i]);
		tt_bintree_delete(&bt, node);
		/* Verify deleted */
		if (tt_bintree_search(&bt, key[i])) {
			printf("error\n");
			break;
		}
	}
	/* Verify undeleted */
	for (int i = 1; i < NUM; i += 2) {
		node = tt_bintree_search(&bt, key[i]);
		if (!node) {
			printf("error\n");
			break;
		}
	}
	/* Delete all */
	for (int i = 1; i < NUM; i += 2)
		tt_bintree_delete(&bt, tt_bintree_max(&bt));
	/* Insert & delete again */
	for (int i = 0; i < NUM; i++)
		tt_bintree_insert(&bt, key[i], &v[i]);
	for (int i = 0; i < NUM; i++)
		tt_bintree_delete(&bt, tt_bintree_min(&bt));
	printf("Done\n");
	printf("\n");

	tt_bintree_free(&bt);

	for (int i = 0; i < NUM; i++)
		free(key[i]);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x3058, %rsp           # imm = 0x3058
movq 0x5af0(%rip), %rax      # 0x6d70
movq %rsp, %rbx
movq %rax, 0x30(%rbx)
movups 0x5ad2(%rip), %xmm0     # 0x6d60
movaps %xmm0, 0x20(%rbx)
movups 0x5ab7(%rip), %xmm0     # 0x6d50
movaps %xmm0, 0x10(%rbx)
movups 0x5a9c(%rip), %xmm0     # 0x6d40
movaps %xmm0, (%rbx)
movq %rbx, %rdi
callq 0x2bf8
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x2d46(%rip), %rdi      # 0x4004
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x10b0
pushq $0x1a
popq %rbp
cmpq $0x400, %r14            # imm = 0x400
je 0x1350
xorl %r15d, %r15d
movl %r14d, %eax
cmpl $0x1a, %eax
jb 0x12f0
xorl %edx, %edx
divl %ebp
addb $0x61, %dl
movb %dl, 0x46(%rsp,%r15)
incq %r15
jmp 0x12da
addb $0x61, %al
movl %r15d, %ecx
movb %al, 0x46(%rsp,%rcx)
leal 0x2(%r15), %edi
callq 0x1130
incl %r15d
movslq %r15d, %rsi
movq %rax, %rcx
movq %rsi, %rdx
subq $0x1, %rdx
jb 0x1324
movb 0x45(%rsp,%rsi), %sil
movb %sil, (%rcx)
incq %rcx
movq %rdx, %rsi
jmp 0x130e
movb $0x0, (%rcx)
movq %rax, 0x50(%rsp,%r14,8)
leaq (%rsp,%r14,4), %rdx
addq $0x2050, %rdx           # imm = 0x2050
movl %r14d, (%rdx)
movq 0x20(%rsp), %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq *0x18(%rcx)
incq %r14
jmp 0x12cb
leaq 0x2d1b(%rip), %r14      # 0x4072
movq %r14, %rdi
callq 0x1060
movq %rsp, %rbx
movl 0x30(%rbx), %ebp
movq 0x28(%rbx), %rdi
callq 0x15f6
leaq 0x2c9a(%rip), %rdi      # 0x400f
xorl %r15d, %r15d
movl %ebp, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10b0
pushq $0xa
popq %rbp
movl %ebp, %edi
callq 0x1040
leaq 0x2c94(%rip), %rdi      # 0x4028
xorl %eax, %eax
callq 0x10b0
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rdi
leaq 0x27c(%rip), %rdx       # 0x1626
xorl %esi, %esi
callq *0x28(%rax)
movq %r14, %rdi
callq 0x1060
movl %ebp, %edi
callq 0x1040
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rdi
callq *(%rax)
movq (%rax), %rsi
movq 0x8(%rax), %rax
movl (%rax), %edx
leaq 0x2c59(%rip), %rdi      # 0x4031
xorl %eax, %eax
callq 0x10b0
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rdi
callq *0x8(%rax)
movq (%rax), %rsi
movq 0x8(%rax), %rax
movl (%rax), %edx
leaq 0x2c46(%rip), %rdi      # 0x4040
xorl %eax, %eax
callq 0x10b0
movl %ebp, %edi
callq 0x1040
leaq 0x2c40(%rip), %rdi      # 0x404f
xorl %eax, %eax
callq 0x10b0
cmpq $0x400, %r15            # imm = 0x400
je 0x1443
movq 0x50(%rsp,%r15,8), %rsi
movq 0x20(%rsp), %rax
movq %rbx, %rdi
callq *0x10(%rax)
incq %r15
testq %rax, %rax
jne 0x1416
leaq 0x2c27(%rip), %rdi      # 0x4065
callq 0x1060
leaq 0x2c28(%rip), %rdi      # 0x4072
callq 0x1060
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x2bfc(%rip), %rdi      # 0x405a
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x10b0
movq %rsp, %rbx
cmpq $0x3ff, %r15            # imm = 0x3FF
ja 0x14b8
movq 0x50(%rsp,%r15,8), %r14
movq 0x20(%rsp), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x10(%rax)
movq 0x20(%rsp), %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq *0x20(%rcx)
movq 0x20(%rsp), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x10(%rax)
addq $0x2, %r15
testq %rax, %rax
je 0x146b
leaq 0x2bb9(%rip), %rdi      # 0x406c
callq 0x1060
pushq $0x1
popq %r14
movq %rsp, %rbx
cmpq $0x3ff, %r14            # imm = 0x3FF
ja 0x14ed
movq 0x50(%rsp,%r14,8), %rsi
movq 0x20(%rsp), %rax
movq %rbx, %rdi
callq *0x10(%rax)
addq $0x2, %r14
testq %rax, %rax
jne 0x14bf
leaq 0x2b84(%rip), %rdi      # 0x406c
callq 0x1060
pushq $0x1
popq %rbp
movq %rsp, %rbx
cmpl $0x400, %ebp            # imm = 0x400
jae 0x151b
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdi
callq *0x8(%rax)
movq 0x20(%rsp), %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq *0x20(%rcx)
addl $0x2, %ebp
jmp 0x14f3
xorl %r14d, %r14d
movq %rsp, %rbx
cmpq $0x1000, %r14           # imm = 0x1000
je 0x154b
movq 0x50(%rsp,%r14,2), %rsi
leaq (%rsp,%r14), %rdx
addq $0x2050, %rdx           # imm = 0x2050
movq 0x20(%rsp), %rax
movq %rbx, %rdi
callq *0x18(%rax)
addq $0x4, %r14
jmp 0x1521
movl $0x400, %ebp            # imm = 0x400
movq %rsp, %rbx
subl $0x1, %ebp
jb 0x1574
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdi
callq *(%rax)
movq 0x20(%rsp), %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq *0x20(%rcx)
jmp 0x1553
leaq 0x2af7(%rip), %rdi      # 0x4072
callq 0x1060
pushq $0xa
popq %rdi
callq 0x1040
movq %rsp, %rdi
callq 0x2c7d
xorl %ebx, %ebx
cmpq $0x400, %rbx            # imm = 0x400
je 0x15aa
movq 0x50(%rsp,%rbx,8), %rdi
callq 0x1030
incq %rbx
jmp 0x1592
xorl %eax, %eax
addq $0x3058, %rsp           # imm = 0x3058
popq %rbx
popq %r14
popq %r15
popq %rbp
retq