#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 tree_node {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ destroy_binary_tree(struct tree_node **);
int /*<<< orphan*/ insert_into_binary_tree(struct tree_node **, int const);
int /*<<< orphan*/ printf(char *, ...);
struct tree_node *generate_binary_tree(const int numbers[], const int n) {
  int i;
  struct tree_node *root = NULL;
  if (numbers == NULL || n < 1) {
    printf("invalid params(%p, %d).\n", numbers, n);
    return NULL;
  }
  for (i = 0; i < n; ++i) {
    if (!insert_into_binary_tree(&root, numbers[i]))
      break;
  }
  if (i < n) {
    printf("generate_binary_tree failed.\n");
    destroy_binary_tree(&root);
    root = NULL;
  }
  return root;
}


x86 assembly (clang, -O0):
<generate_binary_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     115c <generate_binary_tree+0x2c>
cmpl   $0x1,-0x14(%rbp)
jge    117e <generate_binary_tree+0x4e>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <generate_binary_tree+0xc9>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c6 <generate_binary_tree+0x96>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    -0x20(%rbp),%rdi
call   1040 <insert_into_binary_tree@plt>
cmp    $0x0,%eax
jne    11b3 <generate_binary_tree+0x83>
jmp    11c6 <generate_binary_tree+0x96>
jmp    11b8 <generate_binary_tree+0x88>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1185 <generate_binary_tree+0x55>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f1 <generate_binary_tree+0xc1>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1050 <destroy_binary_tree@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_binary_tree>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%edx
mov    %rdi,%r15
movq   $0x0,(%rsp)
test   %rdi,%rdi
je     118f <generate_binary_tree+0x4f>
test   %edx,%edx
jle    118f <generate_binary_tree+0x4f>
mov    %edx,%r12d
xor    %ebx,%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%esi
mov    %r14,%rdi
call   1050 <insert_into_binary_tree@plt>
test   %eax,%eax
je     11a5 <generate_binary_tree+0x65>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1170 <generate_binary_tree+0x30>
mov    (%rsp),%r14
jmp    11bc <generate_binary_tree+0x7c>
lea    0xe6a(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11bc <generate_binary_tree+0x7c>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1060 <destroy_binary_tree@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tree_node = type { i32 }
@.str = [25 x i8] c"invalid params(%p, %d).\0A\00"
@.str.1 = [30 x i8] c"generate_binary_tree failed.\0A\00"
define %struct.tree_node* @generate_binary_tree(i32* %0, i32 %1) {
%3 = alloca %struct.tree_node*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.tree_node*
store i32* %0, i32** %4
store i32 %1, i32* %5
store %struct.tree_node* null, %struct.tree_node** %7
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %10, %2
%14 = load i32*, i32** %4
%15 = load i32, i32* %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32* %14, i32 %15)
store %struct.tree_node* null, %struct.tree_node** %3
br label %44
17: ; preds = %10
store i32 0, i32* %6
br label %18
18: ; preds = %32, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %5
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32*, i32** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = call i32 @insert_into_binary_tree(%struct.tree_node** %7, i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %22
br label %35
31: ; preds = %22
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %18
35: ; preds = %30, %18
%36 = load i32, i32* %6
%37 = load i32, i32* %5
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @destroy_binary_tree(%struct.tree_node** %7)
store %struct.tree_node* null, %struct.tree_node** %7
br label %42
42: ; preds = %39, %35
%43 = load %struct.tree_node*, %struct.tree_node** %7
store %struct.tree_node* %43, %struct.tree_node** %3
br label %44
44: ; preds = %42, %13
%45 = load %struct.tree_node*, %struct.tree_node** %3
ret %struct.tree_node* %45
}

LLVM IR (-O2):
%struct.tree_node = type { i32 }
@.str = [25 x i8] c"invalid params(%p, %d).\0A\00"
@str = [29 x i8] c"generate_binary_tree failed.\00"
define %struct.tree_node* @generate_binary_tree(i32* %0, i32 %1) {
%3 = alloca %struct.tree_node*
%4 = bitcast %struct.tree_node** %3 to i8*
store %struct.tree_node* null, %struct.tree_node** %3
%5 = icmp eq i32* %0, null
%6 = icmp slt i32 %1, 1
%7 = or i1 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %14
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32* %0, i32 %1)
br label %26
12: ; preds = %14
%13 = icmp eq i64 %20, %9
br i1 %13, label %24, label %14
14: ; preds = %8, %12
%15 = phi i64 [ 0, %8 ], [ %20, %12 ]
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = load i32, i32* %16
%18 = call i32 @insert_into_binary_tree(%struct.tree_node** %3, i32 %17)
%19 = icmp eq i32 %18, 0
%20 = add nuw nsw i64 %15, 1
br i1 %19, label %21, label %12
21: ; preds = %14
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%23 = call i32 @destroy_binary_tree(%struct.tree_node** %3)
br label %26
24: ; preds = %12
%25 = load %struct.tree_node*, %struct.tree_node** %3
br label %26
26: ; preds = %21, %24, %10
%27 = phi %struct.tree_node* [ null, %10 ], [ %25, %24 ], [ null, %21 ]
ret %struct.tree_node* %27
}