#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 foo(int);
int /*<<< orphan*/ printf(char *, int);
int bar(int n) {
  printf("bar %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 bar %d\n", n);
  return 0;
}


x86 assembly (clang, -O0):
<bar>:
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 <bar+0x30>
movl   $0x1,-0x4(%rbp)
jmp    11de <bar+0xae>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1194 <bar+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 <bar+0xae>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11c8 <bar+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 <bar+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):
<bar>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xedb(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0x1,%r14d
xor    %ebp,%ebp
test   %ebx,%ebx
je     119c <bar+0x7c>
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
cmp    $0x1,%eax
jne    1177 <bar+0x57>
add    %ebx,%ebp
lea    -0x1(%rbx),%esi
mov    %r15,%rdi
call   1030 <printf@plt>
add    $0xffffffff,%ebx
jne    1150 <bar+0x30>
jmp    119c <bar+0x7c>
test   %eax,%eax
jne    118b <bar+0x6b>
lea    -0x1(%rbx),%edi
call   1040 <foo@plt>
mov    %eax,%r14d
add    %ebx,%r14d
jmp    119c <bar+0x7c>
lea    0xe6e(%rip),%rdi
mov    %ebx,%esi
call   1030 <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"bar %d\0A\00"
@.str.1 = [19 x i8] c"\09done with bar %d\0A\00"
define i32 @bar(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"bar %d\0A\00"
@.str.1 = [19 x i8] c"\09done with bar %d\0A\00"
define i32 @bar(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, %12
%5 = phi i32 [ %13, %12 ], [ %0, %1 ]
%6 = phi i32 [ %14, %12 ], [ 0, %1 ]
%7 = srem i32 %5, 2
switch i32 %7, label %17 [
i32 0, label %8
i32 1, label %12
]
8: ; preds = %4
%9 = add nsw i32 %5, -1
%10 = tail call i32 @foo(i32 %9)
%11 = add nsw i32 %10, %5
br label %19
12: ; preds = %4
%13 = add nsw i32 %5, -1
%14 = add nsw i32 %5, %6
%15 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13)
%16 = icmp eq i32 %13, 0
br i1 %16, label %19, label %4
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 = %12, %1, %17, %8
%20 = phi i32 [ %6, %8 ], [ %6, %17 ], [ 0, %1 ], [ %14, %12 ]
%21 = phi i32 [ %11, %8 ], [ 0, %17 ], [ 1, %1 ], [ 1, %12 ]
%22 = add nsw i32 %21, %20
ret i32 %22
}