#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int fork();
scalar_t__ getpid();
int /*<<< orphan*/ throwing_fork_errors(int);
int fork_n_times(int n) {
  int i, times = 0;
  pid_t fork_key = getpid();
  for (i = 0; i < n - 1; i++) {
    if (getpid() == fork_key) {
      times++;
      int fk = fork();
      throwing_fork_errors(fk);
    }
  }
  if (getpid() == fork_key)
    times++;
  return times;
}


x86 assembly (clang, -O0):
<fork_n_times>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a4 <fork_n_times+0x74>
mov    $0x0,%al
call   1030 <getpid@plt>
cmp    -0x18(%rbp),%rax
jne    1191 <fork_n_times+0x61>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <throwing_fork_errors@plt>
jmp    1196 <fork_n_times+0x66>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1154 <fork_n_times+0x24>
mov    $0x0,%al
call   1030 <getpid@plt>
cmp    -0x18(%rbp),%rax
jne    11be <fork_n_times+0x8e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_n_times>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
cmp    $0x2,%ebp
jl     1175 <fork_n_times+0x45>
add    $0xffffffff,%ebp
xor    %r14d,%r14d
jmp    1155 <fork_n_times+0x25>
add    $0xffffffff,%ebp
je     1175 <fork_n_times+0x45>
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %rbx,%rax
jne    1150 <fork_n_times+0x20>
add    $0x1,%r14d
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%edi
call   1040 <throwing_fork_errors@plt>
jmp    1150 <fork_n_times+0x20>
xor    %eax,%eax
call   1030 <getpid@plt>
xor    %ecx,%ecx
cmp    %rbx,%rax
sete   %cl
add    %r14d,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fork_n_times(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%7 = call i64 (...) @getpid()
store i64 %7, i64* %5
store i32 0, i32* %3
br label %8
8: ; preds = %24, %1
%9 = load i32, i32* %3
%10 = load i32, i32* %2
%11 = sub nsw i32 %10, 1
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = call i64 (...) @getpid()
%15 = load i64, i64* %5
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = call i32 (...) @fork()
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = call i32 @throwing_fork_errors(i32 %21)
br label %23
23: ; preds = %17, %13
br label %24
24: ; preds = %23
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %8
27: ; preds = %8
%28 = call i64 (...) @getpid()
%29 = load i64, i64* %5
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %34
34: ; preds = %31, %27
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
define i32 @fork_n_times(i32 %0) {
%2 = tail call i64 (...) @getpid()
%3 = add i32 %0, -1
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %18
5: ; preds = %1, %14
%6 = phi i32 [ %16, %14 ], [ 0, %1 ]
%7 = phi i32 [ %15, %14 ], [ 0, %1 ]
%8 = tail call i64 (...) @getpid()
%9 = icmp eq i64 %8, %2
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = add nsw i32 %7, 1
%12 = tail call i32 (...) @fork()
%13 = tail call i32 @throwing_fork_errors(i32 %12)
br label %14
14: ; preds = %5, %10
%15 = phi i32 [ %11, %10 ], [ %7, %5 ]
%16 = add nuw nsw i32 %6, 1
%17 = icmp eq i32 %16, %3
br i1 %17, label %18, label %5
18: ; preds = %14, %1
%19 = phi i32 [ 0, %1 ], [ %15, %14 ]
%20 = tail call i64 (...) @getpid()
%21 = icmp eq i64 %20, %2
%22 = zext i1 %21 to i32
%23 = add nsw i32 %19, %22
ret i32 %23
}