#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 waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
int backup() {
  int restarts = 0, ret;
  for (;; restarts++) {
    if (restarts >= 3)
      ret = -1;
    else
      ret = fork();
    if (ret < 0) {
      printf("Graceful degradation .. running without backup\n");
      return ret;
    }
    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,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     1165 <backup+0x25>
movl   $0xffffffff,-0xc(%rbp)
jmp    116f <backup+0x2f>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1192 <backup+0x52>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <backup+0xe0>
cmpl   $0x0,-0xc(%rbp)
jne    11cf <backup+0x8f>
mov    -0x8(%rbp),%esi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <backup+0xe0>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <waitpid@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
cmp    %ecx,%eax
je     120d <backup+0xcd>
jmp    11e9 <backup+0xa9>
jmp    1212 <backup+0xd2>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    114f <backup+0xf>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backup>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1224 <backup+0xd4>
je     123e <backup+0xee>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeb0(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %eax,%ebx
jne    1190 <backup+0x40>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1224 <backup+0xd4>
je     1232 <backup+0xe2>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %eax,%ebx
jne    11d0 <backup+0x80>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1224 <backup+0xd4>
je     1239 <backup+0xe9>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %eax,%ebx
jne    1210 <backup+0xc0>
mov    $0xffffffff,%ebx
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
jmp    1267 <backup+0x117>
mov    $0x1,%ebp
jmp    123e <backup+0xee>
mov    $0x2,%ebp
lea    0xdbb(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdb0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [12 x i8] c"Restart %d\0A\00"
@.str.2 = [30 x i8] c"New echo server with PID: %d\0A\00"
@.str.3 = [19 x i8] c"Waiting %d for %d\0A\00"
@str = [47 x i8] c"Graceful degradation .. running without backup\00"
define i32 @backup() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %42, %33, %21, %0
%4 = phi i32 [ %1, %0 ], [ %22, %21 ], [ %34, %33 ], [ -1, %42 ]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
br label %45
6: ; preds = %0
%7 = icmp eq i32 %1, 0
br i1 %7, label %8, label %14
8: ; preds = %36, %24, %6
%9 = phi i32 [ 0, %6 ], [ 1, %24 ], [ 2, %36 ]
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i64 (...) @getpid()
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %45
14: ; preds = %6
%15 = tail call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %1)
br label %18
18: ; preds = %18, %14
%19 = tail call i32 @waitpid(i32 %1, i32 0, i32 0)
%20 = icmp eq i32 %1, %19
br i1 %20, label %21, label %18
21: ; preds = %18
%22 = tail call i32 (...) @fork()
%23 = icmp slt i32 %22, 0
br i1 %23, label %3, label %24
24: ; preds = %21
%25 = icmp eq i32 %22, 0
br i1 %25, label %8, label %26
26: ; preds = %24
%27 = tail call i64 (...) @getpid()
%28 = trunc i64 %27 to i32
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %22)
br label %30
30: ; preds = %30, %26
%31 = tail call i32 @waitpid(i32 %22, i32 0, i32 0)
%32 = icmp eq i32 %22, %31
br i1 %32, label %33, label %30
33: ; preds = %30
%34 = tail call i32 (...) @fork()
%35 = icmp slt i32 %34, 0
br i1 %35, label %3, label %36
36: ; preds = %33
%37 = icmp eq i32 %34, 0
br i1 %37, label %8, label %38
38: ; preds = %36
%39 = tail call i64 (...) @getpid()
%40 = trunc i64 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %34)
br label %42
42: ; preds = %42, %38
%43 = tail call i32 @waitpid(i32 %34, i32 0, i32 0)
%44 = icmp eq i32 %34, %43
br i1 %44, label %3, label %42
45: ; preds = %8, %3
%46 = phi i32 [ %4, %3 ], [ %9, %8 ]
ret i32 %46
}