#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 int pid_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int i;
  pid_t childpid;
  int n, temp;
  if (argc != 2) {
    fprintf(stderr, "Usage : %s processes\n", argv[0]);
    exit(1);
  }
  n = atoi(argv[1]);
  childpid = 0;
  for (i = 1; i < n; ++i)
    if (childpid = fork())
      break;
  if (childpid == -1) {
    perror("The fork Failed");
    exit(1);
  }
  fprintf(stderr, "\ni:%d  process ID:%ld   parent ID:%ld   child ID:%ld\n", i,
          (long)getpid(), (long)getppid(), (long)childpid);
  sleep(5);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c8 <main+0x48>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    121d <main+0x9d>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     120a <main+0x8a>
jmp    121d <main+0x9d>
jmp    120f <main+0x8f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e6 <main+0x66>
cmpl   $0xffffffff,-0x18(%rbp)
jne    123d <main+0xbd>
lea    0xde8(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    %rax,%r8
movslq -0x18(%rbp),%r9
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%edi
jne    11c7 <main+0x47>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
cmp    $0x2,%eax
jl     11eb <main+0x6b>
mov    %eax,%r12d
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11f5 <main+0x75>
add    $0x1,%ebx
cmp    %ebx,%r12d
jne    11b0 <main+0x30>
xor    %ebp,%ebp
jmp    1215 <main+0x95>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %ebp,%ebp
mov    $0x1,%r12d
jmp    1215 <main+0x95>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1212 <main+0x92>
lea    0xe13(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %ebx,%r12d
mov    0x2dc4(%rip),%rax
mov    (%rax),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1070 <getppid@plt>
movslq %ebp,%r9
lea    0xdec(%rip),%rsi
mov    %r14d,%edi
mov    %r12d,%edx
mov    %r15,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage : %s processes\0A\00"
@.str.1 = [16 x i8] c"The fork Failed\00"
@.str.2 = [54 x i8] c"\0Ai:%d process ID:%ld parent ID:%ld child ID:%ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %8
store i32 0, i32* %7
store i32 1, i32* %6
br label %24
24: ; preds = %33, %19
%25 = load i32, i32* %6
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = call i32 (...) @fork()
store i32 %29, i32* %7
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %36
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
br label %24
36: ; preds = %31, %24
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = load i32, i32* %6
%45 = call i64 (...) @getpid()
%46 = call i64 (...) @getppid()
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %44, i64 %45, i64 %46, i64 %48)
%50 = call i32 @sleep(i32 5)
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage : %s processes\0A\00"
@.str.1 = [16 x i8] c"The fork Failed\00"
@.str.2 = [54 x i8] c"\0Ai:%d process ID:%ld parent ID:%ld child ID:%ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %23
14: ; preds = %9, %17
%15 = phi i32 [ %18, %17 ], [ 1, %9 ]
%16 = tail call i32 (...) @fork()
switch i32 %16, label %23 [
i32 0, label %17
i32 -1, label %20
]
17: ; preds = %14
%18 = add nuw nsw i32 %15, 1
%19 = icmp eq i32 %18, %12
br i1 %19, label %23, label %14
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17, %14, %9
%24 = phi i32 [ 1, %9 ], [ %15, %14 ], [ %12, %17 ]
%25 = phi i32 [ 0, %9 ], [ %16, %14 ], [ 0, %17 ]
%26 = load i32, i32* @stderr
%27 = tail call i64 (...) @getpid()
%28 = tail call i64 (...) @getppid()
%29 = sext i32 %25 to i64
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %24, i64 %27, i64 %28, i64 %29)
%31 = tail call i32 @sleep(i32 5)
%32 = tail call i32 @exit(i32 0)
unreachable
}