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

/* Variables and functions */
struct treeNode *addLeaf(struct treeNode *, int);
struct treeNode *addLeafToLeftBranch(struct treeNode *, struct treeNode *, int);
struct treeNode *addTwoLeaves(struct treeNode *, int);
struct treeNode *addTwoLeavesAndCreateNewRoot(struct treeNode *,
                                              struct treeNode *, int);
int findHistogramSize(struct treeNode *);
struct treeNode *generateTree(struct treeNode *root,
                              struct treeNode *histogram) {
  int index = findHistogramSize(histogram);
  while (index >= 0) {
    if (NULL != root) {
      if (index == 0) {
        root = addLeafToLeftBranch(histogram, root, index);
      } else {
        root = addTwoLeavesAndCreateNewRoot(histogram, root, index);
      }
    } else {
      if (index == 0) {
        root = addLeaf(histogram, index);
      } else {
        root = addTwoLeaves(histogram, index);
      }
    }
    index -= 2;
  }
  return root;
}


x86 assembly (clang, -O0):
<generateTree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <findHistogramSize@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1200 <generateTree+0xb0>
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11be <generateTree+0x6e>
cmpl   $0x0,-0x14(%rbp)
jne    11a5 <generateTree+0x55>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <addLeafToLeftBranch@plt>
mov    %rax,-0x8(%rbp)
jmp    11b9 <generateTree+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <addTwoLeavesAndCreateNewRoot@plt>
mov    %rax,-0x8(%rbp)
jmp    11f2 <generateTree+0xa2>
cmpl   $0x0,-0x14(%rbp)
jne    11dd <generateTree+0x8d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <addLeaf@plt>
mov    %rax,-0x8(%rbp)
jmp    11ed <generateTree+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <addTwoLeaves@plt>
mov    %rax,-0x8(%rbp)
jmp    11f2 <generateTree+0xa2>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <generateTree+0x1c>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generateTree>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1070 <findHistogramSize@plt>
test   %eax,%eax
js     11a3 <generateTree+0x53>
mov    %eax,%ebx
jmp    118e <generateTree+0x3e>
nopl   0x0(%rax,%rax,1)
test   %ebx,%ebx
je     11ac <generateTree+0x5c>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <addTwoLeavesAndCreateNewRoot@plt>
mov    %rax,%r15
lea    -0x2(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
jle    11a3 <generateTree+0x53>
test   %r15,%r15
jne    1170 <generateTree+0x20>
test   %ebx,%ebx
je     11be <generateTree+0x6e>
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <addTwoLeaves@plt>
jmp    1181 <generateTree+0x31>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <addLeafToLeftBranch@plt>
mov    %r14,%rdi
xor    %esi,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <addLeaf@plt>

LLVM IR (-O0):
%struct.treeNode = type { i32 }
define %struct.treeNode* @generateTree(%struct.treeNode* %0, %struct.treeNode* %1) {
%3 = alloca %struct.treeNode*
%4 = alloca %struct.treeNode*
%5 = alloca i32
store %struct.treeNode* %0, %struct.treeNode** %3
store %struct.treeNode* %1, %struct.treeNode** %4
%6 = load %struct.treeNode*, %struct.treeNode** %4
%7 = call i32 @findHistogramSize(%struct.treeNode* %6)
store i32 %7, i32* %5
br label %8
8: ; preds = %40, %2
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %43
11: ; preds = %8
%12 = load %struct.treeNode*, %struct.treeNode** %3
%13 = icmp ne %struct.treeNode* null, %12
br i1 %13, label %14, label %28
14: ; preds = %11
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load %struct.treeNode*, %struct.treeNode** %4
%19 = load %struct.treeNode*, %struct.treeNode** %3
%20 = load i32, i32* %5
%21 = call %struct.treeNode* @addLeafToLeftBranch(%struct.treeNode* %18, %struct.treeNode* %19, i32 %20)
store %struct.treeNode* %21, %struct.treeNode** %3
br label %27
22: ; preds = %14
%23 = load %struct.treeNode*, %struct.treeNode** %4
%24 = load %struct.treeNode*, %struct.treeNode** %3
%25 = load i32, i32* %5
%26 = call %struct.treeNode* @addTwoLeavesAndCreateNewRoot(%struct.treeNode* %23, %struct.treeNode* %24, i32 %25)
store %struct.treeNode* %26, %struct.treeNode** %3
br label %27
27: ; preds = %22, %17
br label %40
28: ; preds = %11
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load %struct.treeNode*, %struct.treeNode** %4
%33 = load i32, i32* %5
%34 = call %struct.treeNode* @addLeaf(%struct.treeNode* %32, i32 %33)
store %struct.treeNode* %34, %struct.treeNode** %3
br label %39
35: ; preds = %28
%36 = load %struct.treeNode*, %struct.treeNode** %4
%37 = load i32, i32* %5
%38 = call %struct.treeNode* @addTwoLeaves(%struct.treeNode* %36, i32 %37)
store %struct.treeNode* %38, %struct.treeNode** %3
br label %39
39: ; preds = %35, %31
br label %40
40: ; preds = %39, %27
%41 = load i32, i32* %5
%42 = sub nsw i32 %41, 2
store i32 %42, i32* %5
br label %8
43: ; preds = %8
%44 = load %struct.treeNode*, %struct.treeNode** %3
ret %struct.treeNode* %44
}

LLVM IR (-O2):
%struct.treeNode = type { i32 }
define %struct.treeNode* @generateTree(%struct.treeNode* %0, %struct.treeNode* %1) {
%3 = tail call i32 @findHistogramSize(%struct.treeNode* %1)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %5, label %24
5: ; preds = %2, %20
%6 = phi i32 [ %22, %20 ], [ %3, %2 ]
%7 = phi %struct.treeNode* [ %21, %20 ], [ %0, %2 ]
%8 = icmp eq %struct.treeNode* %7, null
%9 = icmp eq i32 %6, 0
br i1 %8, label %15, label %10
10: ; preds = %5
br i1 %9, label %11, label %13
11: ; preds = %10
%12 = tail call %struct.treeNode* @addLeafToLeftBranch(%struct.treeNode* %1, %struct.treeNode* %7, i32 0)
br label %24
13: ; preds = %10
%14 = tail call %struct.treeNode* @addTwoLeavesAndCreateNewRoot(%struct.treeNode* %1, %struct.treeNode* %7, i32 %6)
br label %20
15: ; preds = %5
br i1 %9, label %16, label %18
16: ; preds = %15
%17 = tail call %struct.treeNode* @addLeaf(%struct.treeNode* %1, i32 0)
br label %24
18: ; preds = %15
%19 = tail call %struct.treeNode* @addTwoLeaves(%struct.treeNode* %1, i32 %6)
br label %20
20: ; preds = %18, %13
%21 = phi %struct.treeNode* [ %14, %13 ], [ %19, %18 ]
%22 = add nsw i32 %6, -2
%23 = icmp sgt i32 %6, 1
br i1 %23, label %5, label %24
24: ; preds = %20, %11, %16, %2
%25 = phi %struct.treeNode* [ %0, %2 ], [ %17, %16 ], [ %12, %11 ], [ %21, %20 ]
ret %struct.treeNode* %25
}