#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 atoi(char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int getppid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  pid_t childpid = 0;
  int i, n;
  if (argc != 2) {
    fprintf(stderr, "usage: %s processes\n", argv[0]);
    return 1;
  }
  n = atoi(argv[1]);
  for (i = 1; i < n; i++) {
    if (childpid == fork()) {
      break;
    }
  }
  fprintf(stderr, "i: %d\nprocess ID %d\nparent ID: %d\nchild ID:%ld\n", i,
          getpid(), getppid(), childpid);
  wait(NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b2 <main+0x52>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125d <main+0xfd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    120c <main+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jne    11f9 <main+0x99>
jmp    120c <main+0xac>
jmp    11fe <main+0x9e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c9 <main+0x69>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    %eax,%r8d
mov    -0x18(%rbp),%r9
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11e6 <main+0x86>
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
mov    $0x1,%ebx
cmp    $0x2,%eax
jl     11a5 <main+0x45>
mov    %eax,%ebp
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11a5 <main+0x45>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1190 <main+0x30>
mov    %ebp,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe4c(%rip),%rsi
mov    %r15d,%edi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1070 <wait@plt>
jmp    1206 <main+0xa6>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"usage: %s processes\0A\00"
@.str.1 = [48 x i8] c"i: %d\0Aprocess ID %d\0Aparent ID: %d\0Achild ID:%ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %43
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %8
store i32 1, i32* %7
br label %22
22: ; preds = %32, %17
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i64, i64* %6
%28 = call i64 (...) @fork()
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %35
31: ; preds = %26
br label %32
32: ; preds = %31
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %22
35: ; preds = %30, %22
%36 = load i32, i32* @stderr
%37 = load i32, i32* %7
%38 = call i32 (...) @getpid()
%39 = call i32 (...) @getppid()
%40 = load i64, i64* %6
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %38, i32 %39, i64 %40)
%42 = call i32 @wait(i32* null)
store i32 0, i32* %3
br label %43
43: ; preds = %35, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"usage: %s processes\0A\00"
@.str.1 = [48 x i8] c"i: %d\0Aprocess ID %d\0Aparent ID: %d\0Achild ID:%ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, 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 ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %27
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %8, %17
%14 = phi i32 [ %18, %17 ], [ 1, %8 ]
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = add nuw nsw i32 %14, 1
%19 = icmp eq i32 %18, %11
br i1 %19, label %20, label %13
20: ; preds = %17, %13, %8
%21 = phi i32 [ 1, %8 ], [ %14, %13 ], [ %11, %17 ]
%22 = load i32, i32* @stderr
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (...) @getppid()
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23, i32 %24, i64 0)
%26 = tail call i32 @wait(i32* null)
br label %27
27: ; preds = %20, %4
%28 = phi i32 [ 1, %4 ], [ 0, %20 ]
ret i32 %28
}