#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 */
scalar_t__ EOF;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ gettoken();
scalar_t__ is_balanced(struct tree_node *);
struct tree_node *parse_node();
int /*<<< orphan*/ print_tree(struct tree_node *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int main(void) {
  printf("Enter the tree in the form node(left_child)(right_child)\n");
  printf("Example: 0(1(2()())(3()()))(4()())\n");
  while (1) {
    printf("> ");
    fflush(stdout);
    if (gettoken() == EOF)
      break;
    struct tree_node *root = parse_node();
    printf("  \x1b[32mTree:\x1b[0m ");
    print_tree(root);
    printf("\n");
    printf("  \x1b[32mIs balanced:\x1b[0m %s\n",
           is_balanced(root) ? "yes" : "no");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xecc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   1030 <gettoken@plt>
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jne    11c3 <main+0x63>
jmp    1228 <main+0xc8>
mov    $0x0,%al
call   1040 <parse_node@plt>
mov    %rax,-0x10(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <print_tree@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <is_balanced@plt>
mov    %rax,%rcx
lea    0xe8d(%rip),%rsi
lea    0xe82(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    118b <main+0x2b>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
call   1050 <puts@plt>
lea    0xed3(%rip),%rdi
call   1050 <puts@plt>
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e20(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
call   1040 <gettoken@plt>
mov    0x2e1a(%rip),%r13
cmp    0x0(%r13),%rax
je     125c <main+0xdc>
lea    0xe52(%rip),%r14
lea    0xe2f(%rip),%r15
lea    0xe13(%rip),%r12
nopl   (%rax)
xor    %eax,%eax
call   1060 <parse_node@plt>
mov    %rax,%rbx
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   10a0 <print_tree@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1090 <is_balanced@plt>
test   %rax,%rax
mov    %r14,%rsi
lea    0xe06(%rip),%rax
cmove  %rax,%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x0(%rbp),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
call   1040 <gettoken@plt>
cmp    0x0(%r13),%rax
jne    11f0 <main+0x70>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tree_node = type { i32 }
@.str = [58 x i8] c"Enter the tree in the form node(left_child)(right_child)\0A\00"
@.str.1 = [36 x i8] c"Example: 0(1(2()())(3()()))(4()())\0A\00"
@.str.2 = [3 x i8] c"> \00"
@stdout = global i32 0
@EOF = global i64 0
@.str.3 = [18 x i8] c" \1B[32mTree:\1B[0m \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [28 x i8] c" \1B[32mIs balanced:\1B[0m %s\0A\00"
@.str.6 = [4 x i8] c"yes\00"
@.str.7 = [3 x i8] c"no\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.tree_node*
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %5
5: ; preds = %0, %13
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @stdout
%8 = call i32 @fflush(i32 %7)
%9 = call i64 (...) @gettoken()
%10 = load i64, i64* @EOF
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %5
br label %25
13: ; preds = %5
%14 = call %struct.tree_node* (...) @parse_node()
store %struct.tree_node* %14, %struct.tree_node** %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%16 = load %struct.tree_node*, %struct.tree_node** %2
%17 = call i32 @print_tree(%struct.tree_node* %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = load %struct.tree_node*, %struct.tree_node** %2
%20 = call i64 @is_balanced(%struct.tree_node* %19)
%21 = icmp ne i64 %20, 0
%22 = zext i1 %21 to i64
%23 = select i1 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %23)
br label %5
25: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
%struct.tree_node = type { i32 }
@.str.2 = [3 x i8] c"> \00"
@stdout = global i32 0
@EOF = global i64 0
@.str.3 = [18 x i8] c" \1B[32mTree:\1B[0m \00"
@.str.5 = [28 x i8] c" \1B[32mIs balanced:\1B[0m %s\0A\00"
@.str.6 = [4 x i8] c"yes\00"
@.str.7 = [3 x i8] c"no\00"
@str = [57 x i8] c"Enter the tree in the form node(left_child)(right_child)\00"
@str.8 = [35 x i8] c"Example: 0(1(2()())(3()()))(4()())\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.8, i64 0, i64 0))
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%4 = load i32, i32* @stdout
%5 = tail call i32 @fflush(i32 %4)
%6 = tail call i64 (...) @gettoken()
%7 = load i64, i64* @EOF
%8 = icmp eq i64 %6, %7
br i1 %8, label %24, label %9
9: ; preds = %0, %9
%10 = tail call %struct.tree_node* (...) @parse_node()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @print_tree(%struct.tree_node* %10)
%13 = tail call i32 @putchar(i32 10)
%14 = tail call i64 @is_balanced(%struct.tree_node* %10)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @stdout
%20 = tail call i32 @fflush(i32 %19)
%21 = tail call i64 (...) @gettoken()
%22 = load i64, i64* @EOF
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %9
24: ; preds = %9, %0
ret i32 0
}