#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ treeSize(struct TreeNode *, int *);
int /*<<< orphan*/ walk(struct TreeNode *, int *, int *, int);
int *preorderTraversal(struct TreeNode *root, int *returnSize) {
  int *intarray, ix = 0;
  if (!root) {
    printf("Received NULL root\n");
    return NULL;
  }
  treeSize(root, returnSize);
  printf("\nReceived tree with %d elements\n", *returnSize);
  intarray = (int *)malloc(1 + sizeof(int) * (*returnSize));
  if (intarray == NULL)
    return NULL;
  walk(root, intarray, &ix, 0x2);
  return intarray;
}


x86 assembly (clang, -O0):
<preorderTraversal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117d <preorderTraversal+0x3d>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ee <preorderTraversal+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <treeSize@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d0 <preorderTraversal+0x90>
movq   $0x0,-0x8(%rbp)
jmp    11ee <preorderTraversal+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
mov    $0x2,%ecx
call   1060 <walk@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<preorderTraversal>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
test   %rdi,%rdi
je     11b4 <preorderTraversal+0x64>
mov    %rsi,%rbx
mov    %rdi,%r15
call   1050 <treeSize@plt>
mov    (%rbx),%esi
lea    0xe86(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%eax
lea    0x1(,%rax,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11c3 <preorderTraversal+0x73>
mov    %rax,%rbx
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x2,%ecx
call   1070 <walk@plt>
mov    %rbx,%r14
jmp    11c3 <preorderTraversal+0x73>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TreeNode = type { i32 }
@.str = [20 x i8] c"Received NULL root\0A\00"
@.str.1 = [33 x i8] c"\0AReceived tree with %d elements\0A\00"
define i32* @preorderTraversal(%struct.TreeNode* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca %struct.TreeNode*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store %struct.TreeNode* %0, %struct.TreeNode** %4
store i32* %1, i32** %5
store i32 0, i32* %7
%8 = load %struct.TreeNode*, %struct.TreeNode** %4
%9 = icmp ne %struct.TreeNode* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %3
br label %35
12: ; preds = %2
%13 = load %struct.TreeNode*, %struct.TreeNode** %4
%14 = load i32*, i32** %5
%15 = call i32 @treeSize(%struct.TreeNode* %13, i32* %14)
%16 = load i32*, i32** %5
%17 = load i32, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = mul i64 4, %21
%23 = add i64 1, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %12
store i32* null, i32** %3
br label %35
30: ; preds = %12
%31 = load %struct.TreeNode*, %struct.TreeNode** %4
%32 = load i32*, i32** %6
%33 = call i32 @walk(%struct.TreeNode* %31, i32* %32, i32* %7, i32 2)
%34 = load i32*, i32** %6
store i32* %34, i32** %3
br label %35
35: ; preds = %30, %29, %10
%36 = load i32*, i32** %3
ret i32* %36
}

LLVM IR (-O2):
%struct.TreeNode = type { i32 }
@.str.1 = [33 x i8] c"\0AReceived tree with %d elements\0A\00"
@str = [19 x i8] c"Received NULL root\00"
define i32* @preorderTraversal(%struct.TreeNode* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = icmp eq %struct.TreeNode* %0, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %20
8: ; preds = %2
%9 = tail call i32 @treeSize(%struct.TreeNode* %0, i32* %1)
%10 = load i32, i32* %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %1
%13 = shl i32 %12, 2
%14 = or i32 %13, 1
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i32*
%17 = icmp eq i64 %15, 0
br i1 %17, label %20, label %18
18: ; preds = %8
%19 = call i32 @walk(%struct.TreeNode* %0, i32* %16, i32* %3, i32 2)
br label %20
20: ; preds = %8, %18, %6
%21 = phi i32* [ %16, %18 ], [ null, %6 ], [ null, %8 ]
ret i32* %21
}