#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct node {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ btree_node_add(struct node *, int);
struct node *btree_node_create(int);
struct node *btree_rebalance(struct node *);
struct node *btree_build_from_array(int input[], unsigned int n_items) {
  struct node *root;
  if (n_items > 0) {
    root = btree_node_create(input[0]);
  }
  for (unsigned int i = 1; i < n_items; i++) {
    btree_node_add(root, input[i]);
  }
  root = btree_rebalance(root);
  return root;
}


x86 assembly (clang, -O0):
<btree_build_from_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jbe    1158 <btree_build_from_array+0x28>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1050 <btree_node_create@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    118c <btree_build_from_array+0x5c>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,4),%esi
call   1030 <btree_node_add@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115f <btree_build_from_array+0x2f>
mov    -0x18(%rbp),%rdi
call   1040 <btree_rebalance@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<btree_build_from_array>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    (%rdi),%edi
call   1050 <btree_node_create@plt>
mov    %rax,%r15
cmp    $0x2,%ebp
jb     1175 <btree_build_from_array+0x45>
mov    %ebp,%ebp
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14,%rbx,4),%esi
mov    %r15,%rdi
call   1030 <btree_node_add@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <btree_build_from_array+0x30>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <btree_rebalance@plt>

LLVM IR (-O0):
%struct.node = type { i32 }
define %struct.node* @btree_build_from_array(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.node*
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = icmp ugt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32*, i32** %3
%11 = getelementptr inbounds i32, i32* %10, i64 0
%12 = load i32, i32* %11
%13 = call %struct.node* @btree_node_create(i32 %12)
store %struct.node* %13, %struct.node** %5
br label %14
14: ; preds = %9, %2
store i32 1, i32* %6
br label %15
15: ; preds = %27, %14
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = icmp ult i32 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load %struct.node*, %struct.node** %5
%21 = load i32*, i32** %3
%22 = load i32, i32* %6
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = call i32 @btree_node_add(%struct.node* %20, i32 %25)
br label %27
27: ; preds = %19
%28 = load i32, i32* %6
%29 = add i32 %28, 1
store i32 %29, i32* %6
br label %15
30: ; preds = %15
%31 = load %struct.node*, %struct.node** %5
%32 = call %struct.node* @btree_rebalance(%struct.node* %31)
store %struct.node* %32, %struct.node** %5
%33 = load %struct.node*, %struct.node** %5
ret %struct.node* %33
}

LLVM IR (-O2):
%struct.node = type { i32 }
define %struct.node* @btree_build_from_array(i32* nocapture %0, i32 %1) {
%3 = icmp ne i32 %1, 0
%4 = load i32, i32* %0
%5 = tail call %struct.node* @btree_node_create(i32 %4)
%6 = icmp ugt i32 %1, 1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = zext i32 %1 to i64
br label %11
9: ; preds = %11, %2
%10 = tail call %struct.node* @btree_rebalance(%struct.node* %5)
ret %struct.node* %10
11: ; preds = %7, %11
%12 = phi i64 [ 1, %7 ], [ %16, %11 ]
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = tail call i32 @btree_node_add(%struct.node* %5, i32 %14)
%16 = add nuw nsw i64 %12, 1
%17 = icmp eq i64 %16, %8
br i1 %17, label %9, label %11
}