#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 /*<<< orphan*/ printf(char *, ...);
void unroll_fp(int *fp) {
  int limit;
  limit = 16;
  while (limit > 0 && fp) {
    if (fp[0] < 0x40000000) {
      printf("Called from -- %08x\n", fp[0]);
      fp = (int *)fp[-1];
    } else {
      printf("Leaf routine\n");
      fp = (int *)fp[0];
    }
    limit--;
  }
}


x86 assembly (clang, -O0):
<unroll_fp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x10,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jle    113d <unroll_fp+0x2d>
cmpq   $0x0,-0x8(%rbp)
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    114d <unroll_fp+0x3d>
jmp    11a9 <unroll_fp+0x99>
mov    -0x8(%rbp),%rax
cmpl   $0x40000000,(%rax)
jge    1182 <unroll_fp+0x72>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x4(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    119b <unroll_fp+0x8b>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1123 <unroll_fp+0x13>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unroll_fp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1189 <unroll_fp+0x69>
mov    %rdi,%rbx
mov    $0x10,%ebp
lea    0xec5(%rip),%r14
lea    0xed3(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rbx),%esi
cmp    $0x3fffffff,%esi
jg     1170 <unroll_fp+0x50>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xfffffffffffffffc,%rbx
cmp    $0x2,%ebp
jae    117d <unroll_fp+0x5d>
jmp    1189 <unroll_fp+0x69>
nop
mov    %r15,%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jb     1189 <unroll_fp+0x69>
mov    (%rbx),%eax
movslq %eax,%rbx
add    $0xffffffff,%ebp
test   %ebx,%ebx
jne    1150 <unroll_fp+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Called from -- %08x\0A\00"
@.str.1 = [14 x i8] c"Leaf routine\0A\00"
define void @unroll_fp(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
store i32* %0, i32** %2
store i32 16, i32* %3
br label %4
4: ; preds = %34, %1
%5 = load i32, i32* %3
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br label %10
10: ; preds = %7, %4
%11 = phi i1 [ false, %4 ], [ %9, %7 ]
br i1 %11, label %12, label %37
12: ; preds = %10
%13 = load i32*, i32** %2
%14 = getelementptr inbounds i32, i32* %13, i64 0
%15 = load i32, i32* %14
%16 = icmp slt i32 %15, 1073741824
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = load i32*, i32** %2
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32*, i32** %2
%23 = getelementptr inbounds i32, i32* %22, i64 -1
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %2
br label %34
27: ; preds = %12
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32*, i32** %2
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i32*
store i32* %33, i32** %2
br label %34
34: ; preds = %27, %17
%35 = load i32, i32* %3
%36 = add nsw i32 %35, -1
store i32 %36, i32* %3
br label %4
37: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Called from -- %08x\0A\00"
@str = [13 x i8] c"Leaf routine\00"
define void @unroll_fp(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %22, label %3
3: ; preds = %1, %13
%4 = phi i32 [ %18, %13 ], [ 16, %1 ]
%5 = phi i32* [ %17, %13 ], [ %0, %1 ]
%6 = load i32, i32* %5
%7 = icmp slt i32 %6, 1073741824
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %6)
%10 = getelementptr inbounds i32, i32* %5, i64 -1
br label %13
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = phi i32* [ %10, %8 ], [ %5, %11 ]
%15 = load i32, i32* %14
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
%18 = add nsw i32 %4, -1
%19 = icmp ugt i32 %4, 1
%20 = icmp ne i32 %15, 0
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %3, label %22
22: ; preds = %13, %1
ret void
}