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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char const *, ...);
int /*<<< orphan*/ tree_delete(struct Tree *);
int tree_evaluate(struct Tree *, int *);
struct Tree *tree_from_expression(char const *);
int /*<<< orphan*/ tree_print(struct Tree *);
int /*<<< orphan*/ tree_simplify_onestep(struct Tree *);
int test_expression(const char *expression, int expected) {
  struct Tree *tree;
  int value, error;
  printf(">>> Testing expression: %s\n", expression);
  tree = tree_from_expression(expression);
  if (tree == NULL) {
    printf("Failed to construct expression tree for %s\n", expression);
    tree_delete(tree);
    return 0;
  }
  tree_print(tree);
  value = tree_evaluate(tree, &error);
  if (error != 0) {
    printf("Error argument set on evaluation of %s\n", expression);
    tree_delete(tree);
    return 0;
  }
  if (value != expected) {
    printf("Failed to evaluate %s to %d (got %d instead)\n", expression,
           expected, value);
    tree_delete(tree);
    return 0;
  }
  tree_simplify_onestep(tree);
  tree_print(tree);
  value = tree_evaluate(tree, &error);
  if (error != 0) {
    printf("Error argument set on evaluation of %s after simplifying tree\n",
           expression);
    tree_delete(tree);
    return 0;
  }
  if (value != expected) {
    printf(
        "Failed to evaluate %s to %d after simplifying tree (got %d instead)\n",
        expression, expected, value);
    tree_delete(tree);
    return 0;
  }
  tree_simplify_onestep(tree);
  tree_print(tree);
  value = tree_evaluate(tree, &error);
  if (error != 0) {
    printf("Error argument set on evaluation of %s after simplifying tree\n",
           expression);
    tree_delete(tree);
    return 0;
  }
  if (value != expected) {
    printf(
        "Failed to evaluate %s to %d after simplifying tree (got %d instead)\n",
        expression, expected, value);
    tree_delete(tree);
    return 0;
  }
  tree_delete(tree);
  return 1;
}


x86 assembly (clang, -O0):
<test_expression>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <tree_from_expression@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <test_expression+0x60>
mov    -0x10(%rbp),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x20(%rbp),%rdi
call   1070 <tree_print@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <tree_evaluate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     120a <test_expression+0xaa>
mov    -0x10(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1243 <test_expression+0xe3>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x20(%rbp),%rdi
call   1080 <tree_simplify_onestep@plt>
mov    -0x20(%rbp),%rdi
call   1070 <tree_print@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <tree_evaluate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1296 <test_expression+0x136>
mov    -0x10(%rbp),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12cf <test_expression+0x16f>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x20(%rbp),%rdi
call   1080 <tree_simplify_onestep@plt>
mov    -0x20(%rbp),%rdi
call   1070 <tree_print@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <tree_evaluate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1322 <test_expression+0x1c2>
mov    -0x10(%rbp),%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     135b <test_expression+0x1fb>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <test_expression+0x20b>
mov    -0x20(%rbp),%rdi
call   1030 <tree_delete@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_expression>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe8d(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <tree_from_expression@plt>
test   %rax,%rax
je     11c2 <test_expression+0x62>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <tree_print@plt>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1060 <tree_evaluate@plt>
cmpl   $0x0,0x4(%rsp)
je     11d7 <test_expression+0x77>
lea    0xe95(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <test_expression+0xc0>
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
jmp    1223 <test_expression+0xc3>
cmp    %ebp,%eax
jne    1208 <test_expression+0xa8>
mov    %r14,%rdi
call   1080 <tree_simplify_onestep@plt>
mov    %r14,%rdi
call   1070 <tree_print@plt>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1060 <tree_evaluate@plt>
cmpl   $0x0,0x4(%rsp)
je     1236 <test_expression+0xd6>
lea    0xe98(%rip),%rdi
jmp    11b3 <test_expression+0x53>
lea    0xe61(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <tree_delete@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    %ebp,%eax
jne    1272 <test_expression+0x112>
mov    %r14,%rdi
call   1080 <tree_simplify_onestep@plt>
mov    %r14,%rdi
call   1070 <tree_print@plt>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1060 <tree_evaluate@plt>
cmpl   $0x0,0x4(%rsp)
jne    11ff <test_expression+0x9f>
cmp    %ebp,%eax
jne    1272 <test_expression+0x112>
mov    %r14,%rdi
call   1030 <tree_delete@plt>
mov    $0x1,%r15d
jmp    1228 <test_expression+0xc8>
lea    0xe64(%rip),%rdi
jmp    120f <test_expression+0xaf>

LLVM IR (-O0):
%struct.Tree = type { i32 }
@.str = [28 x i8] c">>> Testing expression: %s\0A\00"
@.str.1 = [44 x i8] c"Failed to construct expression tree for %s\0A\00"
@.str.2 = [40 x i8] c"Error argument set on evaluation of %s\0A\00"
@.str.3 = [46 x i8] c"Failed to evaluate %s to %d (got %d instead)\0A\00"
@.str.4 = [63 x i8] c"Error argument set on evaluation of %s after simplifying tree\0A\00"
@.str.5 = [69 x i8] c"Failed to evaluate %s to %d after simplifying tree (got %d instead)\0A\00"
define i32 @test_expression(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.Tree*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %4
%12 = call %struct.Tree* @tree_from_expression(i8* %11)
store %struct.Tree* %12, %struct.Tree** %6
%13 = load %struct.Tree*, %struct.Tree** %6
%14 = icmp eq %struct.Tree* %13, null
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load %struct.Tree*, %struct.Tree** %6
%19 = call i32 @tree_delete(%struct.Tree* %18)
store i32 0, i32* %3
br label %96
20: ; preds = %2
%21 = load %struct.Tree*, %struct.Tree** %6
%22 = call i32 @tree_print(%struct.Tree* %21)
%23 = load %struct.Tree*, %struct.Tree** %6
%24 = call i32 @tree_evaluate(%struct.Tree* %23, i32* %8)
store i32 %24, i32* %7
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8*, i8** %4
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load %struct.Tree*, %struct.Tree** %6
%31 = call i32 @tree_delete(%struct.Tree* %30)
store i32 0, i32* %3
br label %96
32: ; preds = %20
%33 = load i32, i32* %7
%34 = load i32, i32* %5
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = load i32, i32* %7
%40 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %37, i32 %38, i32 %39)
%41 = load %struct.Tree*, %struct.Tree** %6
%42 = call i32 @tree_delete(%struct.Tree* %41)
store i32 0, i32* %3
br label %96
43: ; preds = %32
%44 = load %struct.Tree*, %struct.Tree** %6
%45 = call i32 @tree_simplify_onestep(%struct.Tree* %44)
%46 = load %struct.Tree*, %struct.Tree** %6
%47 = call i32 @tree_print(%struct.Tree* %46)
%48 = load %struct.Tree*, %struct.Tree** %6
%49 = call i32 @tree_evaluate(%struct.Tree* %48, i32* %8)
store i32 %49, i32* %7
%50 = load i32, i32* %8
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %43
%53 = load i8*, i8** %4
%54 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = load %struct.Tree*, %struct.Tree** %6
%56 = call i32 @tree_delete(%struct.Tree* %55)
store i32 0, i32* %3
br label %96
57: ; preds = %43
%58 = load i32, i32* %7
%59 = load i32, i32* %5
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i8*, i8** %4
%63 = load i32, i32* %5
%64 = load i32, i32* %7
%65 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0), i8* %62, i32 %63, i32 %64)
%66 = load %struct.Tree*, %struct.Tree** %6
%67 = call i32 @tree_delete(%struct.Tree* %66)
store i32 0, i32* %3
br label %96
68: ; preds = %57
%69 = load %struct.Tree*, %struct.Tree** %6
%70 = call i32 @tree_simplify_onestep(%struct.Tree* %69)
%71 = load %struct.Tree*, %struct.Tree** %6
%72 = call i32 @tree_print(%struct.Tree* %71)
%73 = load %struct.Tree*, %struct.Tree** %6
%74 = call i32 @tree_evaluate(%struct.Tree* %73, i32* %8)
store i32 %74, i32* %7
%75 = load i32, i32* %8
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %68
%78 = load i8*, i8** %4
%79 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load %struct.Tree*, %struct.Tree** %6
%81 = call i32 @tree_delete(%struct.Tree* %80)
store i32 0, i32* %3
br label %96
82: ; preds = %68
%83 = load i32, i32* %7
%84 = load i32, i32* %5
%85 = icmp ne i32 %83, %84
br i1 %85, label %86, label %93
86: ; preds = %82
%87 = load i8*, i8** %4
%88 = load i32, i32* %5
%89 = load i32, i32* %7
%90 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0), i8* %87, i32 %88, i32 %89)
%91 = load %struct.Tree*, %struct.Tree** %6
%92 = call i32 @tree_delete(%struct.Tree* %91)
store i32 0, i32* %3
br label %96
93: ; preds = %82
%94 = load %struct.Tree*, %struct.Tree** %6
%95 = call i32 @tree_delete(%struct.Tree* %94)
store i32 1, i32* %3
br label %96
96: ; preds = %93, %86, %77, %61, %52, %36, %27, %15
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
%struct.Tree = type { i32 }
@.str = [28 x i8] c">>> Testing expression: %s\0A\00"
@.str.1 = [44 x i8] c"Failed to construct expression tree for %s\0A\00"
@.str.2 = [40 x i8] c"Error argument set on evaluation of %s\0A\00"
@.str.3 = [46 x i8] c"Failed to evaluate %s to %d (got %d instead)\0A\00"
@.str.4 = [63 x i8] c"Error argument set on evaluation of %s after simplifying tree\0A\00"
@.str.5 = [69 x i8] c"Failed to evaluate %s to %d after simplifying tree (got %d instead)\0A\00"
define i32 @test_expression(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call %struct.Tree* @tree_from_expression(i8* %0)
%7 = icmp eq %struct.Tree* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%10 = tail call i32 @tree_delete(%struct.Tree* null)
br label %54
11: ; preds = %2
%12 = tail call i32 @tree_print(%struct.Tree* %6)
%13 = call i32 @tree_evaluate(%struct.Tree* %6, i32* %3)
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
19: ; preds = %11
%20 = icmp eq i32 %13, %1
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %1, i32 %13)
%23 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
24: ; preds = %19
%25 = call i32 @tree_simplify_onestep(%struct.Tree* %6)
%26 = call i32 @tree_print(%struct.Tree* %6)
%27 = call i32 @tree_evaluate(%struct.Tree* %6, i32* %3)
%28 = load i32, i32* %3
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%32 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
33: ; preds = %24
%34 = icmp eq i32 %27, %1
br i1 %34, label %38, label %35
35: ; preds = %33
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %1, i32 %27)
%37 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
38: ; preds = %33
%39 = call i32 @tree_simplify_onestep(%struct.Tree* %6)
%40 = call i32 @tree_print(%struct.Tree* %6)
%41 = call i32 @tree_evaluate(%struct.Tree* %6, i32* %3)
%42 = load i32, i32* %3
%43 = icmp eq i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %38
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%46 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
47: ; preds = %38
%48 = icmp eq i32 %41, %1
br i1 %48, label %52, label %49
49: ; preds = %47
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %1, i32 %41)
%51 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
52: ; preds = %47
%53 = call i32 @tree_delete(%struct.Tree* %6)
br label %54
54: ; preds = %52, %49, %44, %35, %30, %21, %16, %8
%55 = phi i32 [ 0, %8 ], [ 0, %16 ], [ 0, %21 ], [ 0, %30 ], [ 0, %35 ], [ 0, %44 ], [ 0, %49 ], [ 1, %52 ]
ret i32 %55
}