#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 */

/* Variables and functions */
int bar(int);
int /*<<< orphan*/ printf(char *, int);
int foo(int n) {
  printf("foo %d\n", n);
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return n + foo(n - 1);
  if (n % 2 == 1)
    return n + bar(n - 1);
  printf("\tdone with foo %d\n", n);
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xec6(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1160 <foo+0x30>
movl   $0x1,-0x4(%rbp)
jmp    11de <foo+0xae>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1194 <foo+0x64>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
call   1050 <foo@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11de <foo+0xae>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11c8 <foo+0x98>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
call   1030 <bar@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11de <foo+0xae>
mov    -0x8(%rbp),%esi
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xedb(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0x1,%r14d
xor    %ebp,%ebp
test   %ebx,%ebx
je     119a <foo+0x7a>
lea    0xec1(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%ecx
shr    $0x1f,%ecx
add    %ebx,%ecx
and    $0xfffffffe,%ecx
mov    %ebx,%eax
sub    %ecx,%eax
jne    1174 <foo+0x54>
add    %ebx,%ebp
lea    -0x1(%rbx),%esi
mov    %r15,%rdi
call   1040 <printf@plt>
add    $0xffffffff,%ebx
jne    1150 <foo+0x30>
jmp    119a <foo+0x7a>
cmp    $0x1,%eax
jne    1189 <foo+0x69>
lea    -0x1(%rbx),%edi
call   1030 <bar@plt>
mov    %eax,%r14d
add    %ebx,%r14d
jmp    119a <foo+0x7a>
lea    0xe70(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %r14d,%r14d
add    %r14d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"foo %d\0A\00"
@.str.1 = [19 x i8] c"\09done with foo %d\0A\00"
define i32 @foo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %2
br label %32
9: ; preds = %1
%10 = load i32, i32* %3
%11 = srem i32 %10, 2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* %3
%15 = load i32, i32* %3
%16 = sub nsw i32 %15, 1
%17 = call i32 @foo(i32 %16)
%18 = add nsw i32 %14, %17
store i32 %18, i32* %2
br label %32
19: ; preds = %9
%20 = load i32, i32* %3
%21 = srem i32 %20, 2
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* %3
%25 = load i32, i32* %3
%26 = sub nsw i32 %25, 1
%27 = call i32 @bar(i32 %26)
%28 = add nsw i32 %24, %27
store i32 %28, i32* %2
br label %32
29: ; preds = %19
%30 = load i32, i32* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 0, i32* %2
br label %32
32: ; preds = %29, %23, %13, %8
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [8 x i8] c"foo %d\0A\00"
@.str.1 = [19 x i8] c"\09done with foo %d\0A\00"
define i32 @foo(i32 %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = icmp eq i32 %0, 0
br i1 %3, label %19, label %4
4: ; preds = %1, %8
%5 = phi i32 [ %9, %8 ], [ %0, %1 ]
%6 = phi i32 [ %10, %8 ], [ 0, %1 ]
%7 = srem i32 %5, 2
switch i32 %7, label %17 [
i32 0, label %8
i32 1, label %13
]
8: ; preds = %4
%9 = add nsw i32 %5, -1
%10 = add nsw i32 %5, %6
%11 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
%12 = icmp eq i32 %9, 0
br i1 %12, label %19, label %4
13: ; preds = %4
%14 = add nsw i32 %5, -1
%15 = tail call i32 @bar(i32 %14)
%16 = add nsw i32 %15, %5
br label %19
17: ; preds = %4
%18 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %19
19: ; preds = %8, %1, %17, %13
%20 = phi i32 [ %6, %13 ], [ %6, %17 ], [ 0, %1 ], [ %10, %8 ]
%21 = phi i32 [ %16, %13 ], [ 0, %17 ], [ 1, %1 ], [ 1, %8 ]
%22 = add nsw i32 %21, %20
ret i32 %22
}