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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
struct rb3_head *rb3_get_child(struct rb3_head *, int);
scalar_t__ rb3_get_color_bit(struct rb3_head *, int);
int rb3_get_parent_dir(struct rb3_head *);
int rb3_is_red(struct rb3_head *, int);
int rb3_is_valid_tree_helper(struct rb3_head *head, int isred, int dir,
                             int *depth) {
  int i;
  int depths[2] = {1, 1};
  *depth = 1;
  if (!head) {
    if (isred) {
      printf("red leaf child!\n");
      return 0;
    }
    return 1;
  }
  if (rb3_get_parent_dir(head) != dir) {
    printf("Directions messed up!\n");
    return 0;
  }
  for (i = 0; i < 2; i++) {
    if (isred && rb3_get_color_bit(head, i)) {
      printf("two red in a row!\n");
      return 0;
    }
    if (!rb3_is_valid_tree_helper(rb3_get_child(head, i), rb3_is_red(head, i),
                                  i, &depths[i]))
      return 0;
  }
  if (depths[0] != depths[1]) {
    printf("Unbalanced tree! got %d and %d\n", depths[0], depths[1]);
    return 0;
  }
  *depth = depths[0] + !isred;
  return 1;
}


x86 assembly (clang, -O0):
<rb3_is_valid_tree_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11c6 <rb3_is_valid_tree_helper+0x66>
cmpl   $0x0,-0x14(%rbp)
je     11ba <rb3_is_valid_tree_helper+0x5a>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ea <rb3_is_valid_tree_helper+0x18a>
movl   $0x1,-0x4(%rbp)
jmp    12ea <rb3_is_valid_tree_helper+0x18a>
mov    -0x10(%rbp),%rdi
call   1060 <rb3_get_parent_dir@plt>
cmp    -0x18(%rbp),%eax
je     11f2 <rb3_is_valid_tree_helper+0x92>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ea <rb3_is_valid_tree_helper+0x18a>
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
jge    129e <rb3_is_valid_tree_helper+0x13e>
cmpl   $0x0,-0x14(%rbp)
je     123d <rb3_is_valid_tree_helper+0xdd>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <rb3_get_color_bit@plt>
cmp    $0x0,%rax
je     123d <rb3_is_valid_tree_helper+0xdd>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ea <rb3_is_valid_tree_helper+0x18a>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <rb3_get_child@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <rb3_is_red@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x24(%rbp),%edx
movslq -0x24(%rbp),%rax
lea    -0x2c(%rbp),%rcx
shl    $0x2,%rax
add    %rax,%rcx
call   1030 <rb3_is_valid_tree_helper@plt>
cmp    $0x0,%eax
jne    128b <rb3_is_valid_tree_helper+0x12b>
movl   $0x0,-0x4(%rbp)
jmp    12ea <rb3_is_valid_tree_helper+0x18a>
jmp    1290 <rb3_is_valid_tree_helper+0x130>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11f9 <rb3_is_valid_tree_helper+0x99>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     12ca <rb3_is_valid_tree_helper+0x16a>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ea <rb3_is_valid_tree_helper+0x18a>
mov    -0x2c(%rbp),%ecx
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
add    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rb3_is_valid_tree_helper>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
movabs $0x100000001,%rax
mov    %rax,0x8(%rsp)
movl   $0x1,(%rcx)
test   %rdi,%rdi
je     120c <rb3_is_valid_tree_helper+0x9c>
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rdi,%rbx
call   1070 <rb3_get_parent_dir@plt>
cmp    %ebp,%eax
jne    121a <rb3_is_valid_tree_helper+0xaa>
test   %r14d,%r14d
je     11be <rb3_is_valid_tree_helper+0x4e>
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <rb3_get_color_bit@plt>
test   %rax,%rax
jne    1203 <rb3_is_valid_tree_helper+0x93>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <rb3_get_child@plt>
mov    %rax,%r12
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <rb3_is_red@plt>
lea    0x8(%rsp),%rcx
mov    %r12,%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1030 <rb3_is_valid_tree_helper@plt>
test   %eax,%eax
je     1228 <rb3_is_valid_tree_helper+0xb8>
test   %r14d,%r14d
je     1237 <rb3_is_valid_tree_helper+0xc7>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1090 <rb3_get_color_bit@plt>
test   %rax,%rax
je     1237 <rb3_is_valid_tree_helper+0xc7>
lea    0xe26(%rip),%rdi
jmp    1221 <rb3_is_valid_tree_helper+0xb1>
test   %r14d,%r14d
je     1282 <rb3_is_valid_tree_helper+0x112>
lea    0xe08(%rip),%rdi
jmp    1221 <rb3_is_valid_tree_helper+0xb1>
lea    0xe21(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <rb3_get_child@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <rb3_is_red@plt>
lea    0xc(%rsp),%rcx
mov    %r12,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   1030 <rb3_is_valid_tree_helper@plt>
test   %eax,%eax
je     1228 <rb3_is_valid_tree_helper+0xb8>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
cmp    %edx,%esi
jne    1289 <rb3_is_valid_tree_helper+0x119>
cmp    $0x1,%r14d
adc    $0x0,%esi
mov    %esi,(%r15)
mov    $0x1,%ebp
jmp    1228 <rb3_is_valid_tree_helper+0xb8>
lea    0xd70(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1228 <rb3_is_valid_tree_helper+0xb8>

LLVM IR (-O0):
%struct.rb3_head = type { i32 }
@__const.rb3_is_valid_tree_helper.depths = [2 x i32] [i32 1, i32 1]
@.str = [17 x i8] c"red leaf child!\0A\00"
@.str.1 = [23 x i8] c"Directions messed up!\0A\00"
@.str.2 = [19 x i8] c"two red in a row!\0A\00"
@.str.3 = [32 x i8] c"Unbalanced tree! got %d and %d\0A\00"
define i32 @rb3_is_valid_tree_helper(%struct.rb3_head* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.rb3_head*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [2 x i32]
store %struct.rb3_head* %0, %struct.rb3_head** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%12 = bitcast [2 x i32]* %11 to i8*
%13 = load i32*, i32** %9
store i32 1, i32* %13
%14 = load %struct.rb3_head*, %struct.rb3_head** %6
%15 = icmp ne %struct.rb3_head* %14, null
br i1 %15, label %22, label %16
16: ; preds = %4
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %82
21: ; preds = %16
store i32 1, i32* %5
br label %82
22: ; preds = %4
%23 = load %struct.rb3_head*, %struct.rb3_head** %6
%24 = call i32 @rb3_get_parent_dir(%struct.rb3_head* %23)
%25 = load i32, i32* %8
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %82
29: ; preds = %22
store i32 0, i32* %10
br label %30
30: ; preds = %58, %29
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 2
br i1 %32, label %33, label %61
33: ; preds = %30
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load %struct.rb3_head*, %struct.rb3_head** %6
%38 = load i32, i32* %10
%39 = call i64 @rb3_get_color_bit(%struct.rb3_head* %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %82
43: ; preds = %36, %33
%44 = load %struct.rb3_head*, %struct.rb3_head** %6
%45 = load i32, i32* %10
%46 = call %struct.rb3_head* @rb3_get_child(%struct.rb3_head* %44, i32 %45)
%47 = load %struct.rb3_head*, %struct.rb3_head** %6
%48 = load i32, i32* %10
%49 = call i32 @rb3_is_red(%struct.rb3_head* %47, i32 %48)
%50 = load i32, i32* %10
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 %52
%54 = call i32 @rb3_is_valid_tree_helper(%struct.rb3_head* %46, i32 %49, i32 %50, i32* %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %43
store i32 0, i32* %5
br label %82
57: ; preds = %43
br label %58
58: ; preds = %57
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %30
61: ; preds = %30
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%63 = load i32, i32* %62
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%65 = load i32, i32* %64
%66 = icmp ne i32 %63, %65
br i1 %66, label %67, label %73
67: ; preds = %61
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%69 = load i32, i32* %68
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %69, i32 %71)
store i32 0, i32* %5
br label %82
73: ; preds = %61
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%75 = load i32, i32* %74
%76 = load i32, i32* %7
%77 = icmp ne i32 %76, 0
%78 = xor i1 %77, true
%79 = zext i1 %78 to i32
%80 = add nsw i32 %75, %79
%81 = load i32*, i32** %9
store i32 %80, i32* %81
store i32 1, i32* %5
br label %82
82: ; preds = %73, %67, %56, %41, %27, %21, %19
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
%struct.rb3_head = type { i32 }
@.str.3 = [32 x i8] c"Unbalanced tree! got %d and %d\0A\00"
@str = [16 x i8] c"red leaf child!\00"
@str.4 = [18 x i8] c"two red in a row!\00"
@str.5 = [22 x i8] c"Directions messed up!\00"
define i32 @rb3_is_valid_tree_helper(%struct.rb3_head* %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to [2 x i32]*
%7 = bitcast i64* %5 to i8*
store i64 4294967297, i64* %5
store i32 1, i32* %3
%8 = icmp eq %struct.rb3_head* %0, null
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = icmp eq i32 %1, 0
br i1 %10, label %51, label %11
11: ; preds = %9
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %51
13: ; preds = %4
%14 = tail call i32 @rb3_get_parent_dir(%struct.rb3_head* %0)
%15 = icmp eq i32 %14, %2
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = icmp eq i32 %1, 0
br i1 %17, label %40, label %35
18: ; preds = %13
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.5, i64 0, i64 0))
br label %51
20: ; preds = %40
br i1 %17, label %24, label %21
21: ; preds = %20
%22 = tail call i64 @rb3_get_color_bit(%struct.rb3_head* %0, i32 1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %38
24: ; preds = %21, %20
%25 = tail call %struct.rb3_head* @rb3_get_child(%struct.rb3_head* %0, i32 1)
%26 = tail call i32 @rb3_is_red(%struct.rb3_head* %0, i32 1)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%28 = call i32 @rb3_is_valid_tree_helper(%struct.rb3_head* %25, i32 %26, i32 1, i32* %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %51, label %30
30: ; preds = %24
%31 = bitcast i64* %5 to i32*
%32 = load i32, i32* %31
%33 = load i32, i32* %27
%34 = icmp eq i32 %32, %33
br i1 %34, label %48, label %46
35: ; preds = %16
%36 = tail call i64 @rb3_get_color_bit(%struct.rb3_head* %0, i32 0)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %21, %35
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %51
40: ; preds = %35, %16
%41 = tail call %struct.rb3_head* @rb3_get_child(%struct.rb3_head* %0, i32 0)
%42 = tail call i32 @rb3_is_red(%struct.rb3_head* %0, i32 0)
%43 = bitcast i64* %5 to i32*
%44 = call i32 @rb3_is_valid_tree_helper(%struct.rb3_head* %41, i32 %42, i32 0, i32* %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %51, label %20
46: ; preds = %30
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33)
br label %51
48: ; preds = %30
%49 = zext i1 %17 to i32
%50 = add nsw i32 %32, %49
store i32 %50, i32* %3
br label %51
51: ; preds = %40, %24, %9, %48, %46, %38, %18, %11
%52 = phi i32 [ 0, %18 ], [ 0, %38 ], [ 0, %46 ], [ 1, %48 ], [ 0, %11 ], [ 1, %9 ], [ 0, %24 ], [ 0, %40 ]
ret i32 %52
}