#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;
};
struct Tree {
  int dummy;
};
struct Stack {
  int dummy;
};

/* Variables and functions */
scalar_t__ handleFormulaPart(char const *, int, struct Stack *);
struct Stack *stack_create();
int /*<<< orphan*/ stack_delete(struct Stack *);
scalar_t__ stack_isempty(struct Stack *);
struct TreeNode *stack_pop(struct Stack *, int *);
struct Tree *tree_create(struct TreeNode *);
struct Tree *tree_from_expression(const char *formula) {
  struct Stack *stack = stack_create();
  for (int i = 0, c = formula[0]; c; i++, c = formula[i]) {
    if (formula[i] == ' ' && handleFormulaPart(formula, i, stack)) {
      stack_delete(stack);
      return NULL;
    } else if (!formula[i + 1] && handleFormulaPart(formula, i + 1, stack)) {
      stack_delete(stack);
      return NULL;
    }
  }
  if (stack_isempty(stack)) {
    stack_delete(stack);
    return NULL;
  }
  int error = 0;
  struct TreeNode *node = stack_pop(stack, &error);
  if (error || !stack_isempty(stack)) {
    stack_delete(stack);
    return NULL;
  }
  stack_delete(stack);
  return tree_create(node);
}


x86 assembly (clang, -O0):
<tree_from_expression>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <stack_create@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1248 <tree_from_expression+0xe8>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    11d7 <tree_from_expression+0x77>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1070 <handleFormulaPart@plt>
cmp    $0x0,%rax
je     11d7 <tree_from_expression+0x77>
mov    -0x18(%rbp),%rdi
call   1040 <stack_delete@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <tree_from_expression+0x172>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
cmpb   $0x0,(%rax,%rcx,1)
jne    1221 <tree_from_expression+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
call   1070 <handleFormulaPart@plt>
cmp    $0x0,%rax
je     1221 <tree_from_expression+0xc1>
mov    -0x18(%rbp),%rdi
call   1040 <stack_delete@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <tree_from_expression+0x172>
jmp    1226 <tree_from_expression+0xc6>
jmp    122b <tree_from_expression+0xcb>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x20(%rbp)
jmp    1188 <tree_from_expression+0x28>
mov    -0x18(%rbp),%rdi
call   1060 <stack_isempty@plt>
cmp    $0x0,%rax
je     1271 <tree_from_expression+0x111>
mov    -0x18(%rbp),%rdi
call   1040 <stack_delete@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <tree_from_expression+0x172>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1080 <stack_pop@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12a6 <tree_from_expression+0x146>
mov    -0x18(%rbp),%rdi
call   1060 <stack_isempty@plt>
cmp    $0x0,%rax
jne    12bc <tree_from_expression+0x15c>
mov    -0x18(%rbp),%rdi
call   1040 <stack_delete@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <tree_from_expression+0x172>
mov    -0x18(%rbp),%rdi
call   1040 <stack_delete@plt>
mov    -0x30(%rbp),%rdi
call   1030 <tree_create@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tree_from_expression>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
xor    %eax,%eax
call   1050 <stack_create@plt>
mov    %rax,%r14
mov    (%r15),%al
test   %al,%al
je     11db <tree_from_expression+0x7b>
xor    %ebx,%ebx
jmp    11a2 <tree_from_expression+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%r15,%rbx,1),%eax
test   %al,%al
je     11c0 <tree_from_expression+0x60>
add    $0x1,%rbx
test   %al,%al
je     11db <tree_from_expression+0x7b>
cmp    $0x20,%al
jne    1190 <tree_from_expression+0x30>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1070 <handleFormulaPart@plt>
test   %rax,%rax
je     1190 <tree_from_expression+0x30>
jmp    1204 <tree_from_expression+0xa4>
nopw   0x0(%rax,%rax,1)
lea    0x1(%rbx),%esi
mov    %r15,%rdi
mov    %r14,%rdx
call   1070 <handleFormulaPart@plt>
test   %rax,%rax
jne    1204 <tree_from_expression+0xa4>
movzbl 0x1(%r15,%rbx,1),%eax
jmp    119a <tree_from_expression+0x3a>
mov    %r14,%rdi
call   1060 <stack_isempty@plt>
test   %rax,%rax
jne    1204 <tree_from_expression+0xa4>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1080 <stack_pop@plt>
cmpl   $0x0,0xc(%rsp)
je     1218 <tree_from_expression+0xb8>
mov    %r14,%rdi
call   1040 <stack_delete@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <stack_isempty@plt>
test   %rax,%rax
je     1204 <tree_from_expression+0xa4>
mov    %r14,%rdi
call   1040 <stack_delete@plt>
mov    %rbx,%rdi
call   1030 <tree_create@plt>
jmp    120e <tree_from_expression+0xae>

LLVM IR (-O0):
%struct.Tree = type { i32 }
%struct.Stack = type { i32 }
%struct.TreeNode = type { i32 }
define %struct.Tree* @tree_from_expression(i8* %0) {
%2 = alloca %struct.Tree*
%3 = alloca i8*
%4 = alloca %struct.Stack*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.TreeNode*
store i8* %0, i8** %3
%9 = call %struct.Stack* (...) @stack_create()
store %struct.Stack* %9, %struct.Stack** %4
store i32 0, i32* %5
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
store i32 %13, i32* %6
br label %14
14: ; preds = %54, %1
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %63
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 32
br i1 %24, label %25, label %34
25: ; preds = %17
%26 = load i8*, i8** %3
%27 = load i32, i32* %5
%28 = load %struct.Stack*, %struct.Stack** %4
%29 = call i64 @handleFormulaPart(i8* %26, i32 %27, %struct.Stack* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load %struct.Stack*, %struct.Stack** %4
%33 = call i32 @stack_delete(%struct.Stack* %32)
store %struct.Tree* null, %struct.Tree** %2
br label %87
34: ; preds = %25, %17
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %52, label %42
42: ; preds = %34
%43 = load i8*, i8** %3
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
%46 = load %struct.Stack*, %struct.Stack** %4
%47 = call i64 @handleFormulaPart(i8* %43, i32 %45, %struct.Stack* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load %struct.Stack*, %struct.Stack** %4
%51 = call i32 @stack_delete(%struct.Stack* %50)
store %struct.Tree* null, %struct.Tree** %2
br label %87
52: ; preds = %42, %34
br label %53
53: ; preds = %52
br label %54
54: ; preds = %53
%55 = load i32, i32* %5
%56 = add nsw i32 %55, 1
store i32 %56, i32* %5
%57 = load i8*, i8** %3
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
store i32 %62, i32* %6
br label %14
63: ; preds = %14
%64 = load %struct.Stack*, %struct.Stack** %4
%65 = call i64 @stack_isempty(%struct.Stack* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load %struct.Stack*, %struct.Stack** %4
%69 = call i32 @stack_delete(%struct.Stack* %68)
store %struct.Tree* null, %struct.Tree** %2
br label %87
70: ; preds = %63
store i32 0, i32* %7
%71 = load %struct.Stack*, %struct.Stack** %4
%72 = call %struct.TreeNode* @stack_pop(%struct.Stack* %71, i32* %7)
store %struct.TreeNode* %72, %struct.TreeNode** %8
%73 = load i32, i32* %7
%74 = icmp ne i32 %73, 0
br i1 %74, label %79, label %75
75: ; preds = %70
%76 = load %struct.Stack*, %struct.Stack** %4
%77 = call i64 @stack_isempty(%struct.Stack* %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %82, label %79
79: ; preds = %75, %70
%80 = load %struct.Stack*, %struct.Stack** %4
%81 = call i32 @stack_delete(%struct.Stack* %80)
store %struct.Tree* null, %struct.Tree** %2
br label %87
82: ; preds = %75
%83 = load %struct.Stack*, %struct.Stack** %4
%84 = call i32 @stack_delete(%struct.Stack* %83)
%85 = load %struct.TreeNode*, %struct.TreeNode** %8
%86 = call %struct.Tree* @tree_create(%struct.TreeNode* %85)
store %struct.Tree* %86, %struct.Tree** %2
br label %87
87: ; preds = %82, %79, %67, %49, %31
%88 = load %struct.Tree*, %struct.Tree** %2
ret %struct.Tree* %88
}

LLVM IR (-O2):
%struct.Tree = type { i32 }
%struct.Stack = type { i32 }
%struct.TreeNode = type { i32 }
define %struct.Tree* @tree_from_expression(i8* %0) {
%2 = alloca i32
%3 = tail call %struct.Stack* (...) @stack_create()
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %32, label %6
6: ; preds = %1, %29
%7 = phi i8 [ %30, %29 ], [ %4, %1 ]
%8 = phi i64 [ %17, %29 ], [ 0, %1 ]
%9 = icmp eq i8 %7, 32
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = trunc i64 %8 to i32
%12 = tail call i64 @handleFormulaPart(i8* %0, i32 %11, %struct.Stack* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @stack_delete(%struct.Stack* %3)
br label %52
16: ; preds = %10, %6
%17 = add nuw i64 %8, 1
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = trunc i64 %17 to i32
%23 = tail call i64 @handleFormulaPart(i8* %0, i32 %22, %struct.Stack* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i8, i8* %18
br label %29
27: ; preds = %21
%28 = tail call i32 @stack_delete(%struct.Stack* %3)
br label %52
29: ; preds = %25, %16
%30 = phi i8 [ %26, %25 ], [ %19, %16 ]
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %6
32: ; preds = %29, %1
%33 = tail call i64 @stack_isempty(%struct.Stack* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = tail call i32 @stack_delete(%struct.Stack* %3)
br label %52
37: ; preds = %32
%38 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%39 = call %struct.TreeNode* @stack_pop(%struct.Stack* %3, i32* %2)
%40 = load i32, i32* %2
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = call i64 @stack_isempty(%struct.Stack* %3)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42, %37
%46 = call i32 @stack_delete(%struct.Stack* %3)
br label %50
47: ; preds = %42
%48 = call i32 @stack_delete(%struct.Stack* %3)
%49 = call %struct.Tree* @tree_create(%struct.TreeNode* %39)
br label %50
50: ; preds = %47, %45
%51 = phi %struct.Tree* [ null, %45 ], [ %49, %47 ]
br label %52
52: ; preds = %14, %27, %50, %35
%53 = phi %struct.Tree* [ null, %35 ], [ %51, %50 ], [ null, %27 ], [ null, %14 ]
ret %struct.Tree* %53
}