#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 child_pid;
  int pid;
  int n;
  if (argc < 2) {
    printf("Usage: part3.exe [number of children/2]\n\0");
    return 1;
  }
  n = atoi(argv[1]) * 2;
  for (n; n > 0; n--) {
    child_pid = fork();
    pid = getpid();
    if (child_pid > 0) {
      wait(child_pid);
    } else if ((child_pid == 0) && !(n & 1)) {
      printf("I am the first process with process ID %d\n\0", pid);
      printf("%d\nI am process %d and I am terminating\n\0",
             (10 * (10 + 1)) / 2, pid);
      exit(1);
    } else if ((child_pid == 0) && (n & 1)) {
      printf("I am the second process with process ID %d\n\0", pid);
      printf("%d\nI am process %d and I am terminating\n\0",
             (100 * (100 + 1)) / 2, pid);
      exit(1);
    } else {
      printf("Too many processes already running");
      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    12a8 <main+0x148>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    12a1 <main+0x141>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11e2 <main+0x82>
mov    -0x14(%rbp),%edi
call   1070 <wait@plt>
jmp    128e <main+0x12e>
cmpl   $0x0,-0x14(%rbp)
jne    122c <main+0xcc>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    122c <main+0xcc>
mov    -0x18(%rbp),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edx
lea    0xe40(%rip),%rdi
mov    $0x37,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1276 <main+0x116>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1276 <main+0x116>
mov    -0x18(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edx
lea    0xdf6(%rip),%rdi
mov    $0x13ba,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1293 <main+0x133>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%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   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     118c <main+0x1c>
lea    0xf26(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11c7 <main+0x57>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
jle    11c5 <main+0x55>
mov    %eax,%ebx
add    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
test   %ebp,%ebp
jle    11cc <main+0x5c>
mov    %ebp,%edi
call   1080 <wait@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
jg     11a0 <main+0x30>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%r14d
and    $0x1,%ebx
mov    %ebp,%eax
or     %ebx,%eax
jne    11f7 <main+0x87>
lea    0xe21(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3c(%rip),%rdi
mov    $0x37,%esi
jmp    121c <main+0xac>
test   %ebp,%ebp
jne    1230 <main+0xc0>
test   %ebx,%ebx
je     1230 <main+0xc0>
lea    0xe50(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe15(%rip),%rdi
mov    $0x13ba,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"Usage: part3.exe [number of children/2]\0A\00\00"
@.str.1 = [44 x i8] c"I am the first process with process ID %d\0A\00\00"
@.str.2 = [42 x i8] c"%d\0AI am process %d and I am terminating\0A\00\00"
@.str.3 = [45 x i8] c"I am the second process with process ID %d\0A\00\00"
@.str.4 = [35 x i8] c"Too many processes already running\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 ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %65
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* %8
%19 = load i32, i32* %8
br label %20
20: ; preds = %61, %13
%21 = load i32, i32* %8
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %64
23: ; preds = %20
%24 = call i32 (...) @fork()
store i32 %24, i32* %6
%25 = call i32 (...) @getpid()
store i32 %25, i32* %7
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %6
%30 = call i32 @wait(i32 %29)
br label %60
31: ; preds = %23
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %44
34: ; preds = %31
%35 = load i32, i32* %8
%36 = and i32 %35, 1
%37 = icmp ne i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %34
%39 = load i32, i32* %7
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %7
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 55, i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34, %31
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = load i32, i32* %8
%49 = and i32 %48, 1
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* %7
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %7
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 5050, i32 %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %47, %44
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %28
br label %61
61: ; preds = %60
%62 = load i32, i32* %8
%63 = add nsw i32 %62, -1
store i32 %63, i32* %8
br label %20
64: ; preds = %20
store i32 0, i32* %3
br label %65
65: ; preds = %64, %11
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str.1 = [44 x i8] c"I am the first process with process ID %d\0A\00\00"
@.str.2 = [42 x i8] c"%d\0AI am process %d and I am terminating\0A\00\00"
@.str.3 = [45 x i8] c"I am the second process with process ID %d\0A\00\00"
@.str.4 = [35 x i8] c"Too many processes already running\00"
@str = [40 x i8] c"Usage: part3.exe [number of children/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 ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %41
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 %41
11: ; preds = %6
%12 = shl nuw nsw i32 %9, 1
br label %13
13: ; preds = %11, %18
%14 = phi i32 [ %20, %18 ], [ %12, %11 ]
%15 = tail call i32 (...) @fork()
%16 = tail call i32 (...) @getpid()
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = tail call i32 @wait(i32 %15)
%20 = add nsw i32 %14, -1
%21 = icmp sgt i32 %14, 1
br i1 %21, label %13, label %41
22: ; preds = %13
%23 = icmp eq i32 %15, 0
%24 = and i32 %14, 1
%25 = icmp eq i32 %24, 0
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 55, i32 %16)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = xor i1 %23, true
%33 = select i1 %32, i1 true, i1 %25
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 5050, i32 %16)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %18, %6, %4
%42 = phi i32 [ 1, %4 ], [ 0, %6 ], [ 0, %18 ]
ret i32 %42
}