#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int);
int main(int argc, char **argv) {
  int n;
  int child_pid;
  int pid;
  if (argc < 2) {
    printf("Usage: part2.exe [number of childen/2]\n\0");
    return 1;
  }
  n = atoi(argv[1]) * 2;
  for (n; n > 0; n--) {
    child_pid = fork();
    if (child_pid > 0) {
      wait(child_pid);
    } else if (child_pid == 0) {
      pid = getpid();
      printf("I am process %d and I am terminating\n\0", pid);
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1226 <main+0xc6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    121f <main+0xbf>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11d8 <main+0x78>
mov    -0x18(%rbp),%edi
call   1070 <wait@plt>
jmp    120c <main+0xac>
cmpl   $0x0,-0x18(%rbp)
jne    1207 <main+0xa7>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    120c <main+0xac>
jmp    1211 <main+0xb1>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11ad <main+0x4d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jg     1189 <main+0x19>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
jle    11dd <main+0x6d>
lea    (%rax,%rax,1),%ebx
add    $0x1,%ebx
jmp    11af <main+0x3f>
xchg   %ax,%ax
mov    %eax,%edi
call   1080 <wait@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jle    11dd <main+0x6d>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jg     11a0 <main+0x30>
jne    11a7 <main+0x37>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Usage: part2.exe [number of childen/2]\0A\00\00"
@.str.1 = [39 x i8] c"I am process %d and I am terminating\0A\00\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %44
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %6
%19 = load i32, i32* %6
br label %20
20: ; preds = %40, %13
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %20
%24 = call i32 (...) @fork()
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* %7
%29 = call i32 @wait(i32 %28)
br label %39
30: ; preds = %23
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = call i32 (...) @getpid()
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
br label %39
39: ; preds = %38, %27
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, -1
store i32 %42, i32* %6
br label %20
43: ; preds = %20
store i32 0, i32* %3
br label %44
44: ; preds = %43, %11
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str.1 = [39 x i8] c"I am process %d and I am terminating\0A\00\00"
@str = [39 x i8] c"Usage: part2.exe [number of childen/2]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %28
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %6
%12 = shl nuw nsw i32 %9, 1
br label %13
13: ; preds = %11, %25
%14 = phi i32 [ %26, %25 ], [ %12, %11 ]
%15 = tail call i32 (...) @fork()
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @wait(i32 %15)
br label %25
19: ; preds = %13
%20 = icmp eq i32 %15, 0
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17, %19
%26 = add nsw i32 %14, -1
%27 = icmp sgt i32 %14, 1
br i1 %27, label %13, label %28
28: ; preds = %25, %6, %4
%29 = phi i32 [ 1, %4 ], [ 0, %6 ], [ 0, %25 ]
ret i32 %29
}