#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 fork();
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
int backup() {
  int restarts = 0, ret;
  for (;; restarts++) {
    ret = fork();
    if (ret == 0) {
      printf("Restart %d\n", restarts);
      printf("New echo server with PID: %d\n", (int)getpid());
      return restarts;
    }
    printf("Waiting %d for %d\n", (int)getpid(), ret);
    while (ret != waitpid(ret, 0, 0))
      ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<backup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1194 <backup+0x54>
mov    -0x4(%rbp),%esi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <waitpid@plt>
mov    %eax,%ecx
mov    -0xc(%rbp),%eax
cmp    %ecx,%eax
je     11d2 <backup+0x92>
jmp    11ae <backup+0x6e>
jmp    11d7 <backup+0x97>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114f <backup+0xf>

x86 assembly (clang, -O2):
<backup>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     119f <backup+0x5f>
mov    %eax,%ebp
lea    0xed0(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %eax,%ebp
jne    1180 <backup+0x40>
add    $0x1,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1160 <backup+0x20>
lea    0xe5a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe4f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Restart %d\0A\00"
@.str.1 = [30 x i8] c"New echo server with PID: %d\0A\00"
@.str.2 = [19 x i8] c"Waiting %d for %d\0A\00"
define i32 @backup() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
br label %3
3: ; preds = %26, %0
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %3
%8 = load i32, i32* %1
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i64 (...) @getpid()
%11 = trunc i64 %10 to i32
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %1
ret i32 %13
14: ; preds = %3
%15 = call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = load i32, i32* %2
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 %17)
br label %19
19: ; preds = %24, %14
%20 = load i32, i32* %2
%21 = load i32, i32* %2
%22 = call i32 @waitpid(i32 %21, i32 0, i32 0)
%23 = icmp ne i32 %20, %22
br i1 %23, label %24, label %25
24: ; preds = %19
br label %19
25: ; preds = %19
br label %26
26: ; preds = %25
%27 = load i32, i32* %1
%28 = add nsw i32 %27, 1
store i32 %28, i32* %1
br label %3
}

LLVM IR (-O2):
@.str = [12 x i8] c"Restart %d\0A\00"
@.str.1 = [30 x i8] c"New echo server with PID: %d\0A\00"
@.str.2 = [19 x i8] c"Waiting %d for %d\0A\00"
define i32 @backup() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %9
3: ; preds = %18, %0
%4 = phi i32 [ 0, %0 ], [ %19, %18 ]
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i64 (...) @getpid()
%7 = trunc i64 %6 to i32
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %7)
ret i32 %4
9: ; preds = %0, %18
%10 = phi i32 [ %20, %18 ], [ %1, %0 ]
%11 = phi i32 [ %19, %18 ], [ 0, %0 ]
%12 = tail call i64 (...) @getpid()
%13 = trunc i64 %12 to i32
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %10)
br label %15
15: ; preds = %15, %9
%16 = tail call i32 @waitpid(i32 %10, i32 0, i32 0)
%17 = icmp eq i32 %10, %16
br i1 %17, label %18, label %15
18: ; preds = %15
%19 = add nuw nsw i32 %11, 1
%20 = tail call i32 (...) @fork()
%21 = icmp eq i32 %20, 0
br i1 %21, label %3, label %9
}