#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 fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int main(int args, char *argc[]) {
  int i = 0;
  int childNum = 2;
  int childPID = 0;
  int printchildPID[childNum];
  for (i = 0; i < childNum; i++) {
    childPID = fork();
    if (childPID == 0) {
      printf("I am child %d and here's my PID: %d \n", i, getpid());
      break;
    } else if (childPID == -1) {
      printf("FORK NOT SUCCEED\n");
      break;
    } else if (childPID > 0) {
      printchildPID[i] = childPID;
    }
  }
  if (childPID > 0) {
    printf("I am the father and heres my sons PIDs: ");
    for (i = 0; i < childNum; i++) {
      printf("%d ", printchildPID[i]);
    }
    printf("\n");
  }
  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)
movl   $0x0,-0x14(%rbp)
movl   $0x2,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1220 <main+0xf0>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11ce <main+0x9e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <main+0xf0>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11eb <main+0xbb>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <main+0xf0>
cmpl   $0x0,-0x1c(%rbp)
jle    1203 <main+0xd3>
mov    -0x38(%rbp),%rax
mov    -0x1c(%rbp),%edx
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1208 <main+0xd8>
jmp    120d <main+0xdd>
jmp    1212 <main+0xe2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1189 <main+0x59>
cmpl   $0x0,-0x1c(%rbp)
jle    1280 <main+0x150>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1272 <main+0x142>
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123f <main+0x10f>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     119d <main+0x4d>
mov    %eax,%ebx
test   %eax,%eax
jne    116a <main+0x1a>
mov    %ebx,%ebp
jmp    1182 <main+0x32>
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     119d <main+0x4d>
mov    %eax,%r14d
mov    $0x1,%ebp
test   %eax,%eax
jne    11b0 <main+0x60>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe70(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11a9 <main+0x59>
lea    0xeaf(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
jle    11a9 <main+0x59>
lea    0xe6d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe88(%rip),%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11a9 <main+0x59>

LLVM IR (-O0):
@.str = [38 x i8] c"I am child %d and here's my PID: %d \0A\00"
@.str.1 = [18 x i8] c"FORK NOT SUCCEED\0A\00"
@.str.2 = [41 x i8] c"I am the father and heres my sons PIDs: \00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [2 x i8] c"\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 i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 2, i32* %7
store i32 0, i32* %8
%11 = load i32, i32* %7
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %9
%14 = alloca i32, i64 %12
store i64 %12, i64* %10
store i32 0, i32* %6
br label %15
15: ; preds = %43, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %46
19: ; preds = %15
%20 = call i32 (...) @fork()
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* %6
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
br label %46
27: ; preds = %19
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %46
32: ; preds = %27
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* %8
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %14, i64 %38
store i32 %36, i32* %39
br label %40
40: ; preds = %35, %32
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %15
46: ; preds = %30, %23, %15
%47 = load i32, i32* %8
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %66
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %51
51: ; preds = %61, %49
%52 = load i32, i32* %6
%53 = load i32, i32* %7
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %64
55: ; preds = %51
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %14, i64 %57
%59 = load i32, i32* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %55
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
store i32 %63, i32* %6
br label %51
64: ; preds = %51
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %46
store i32 0, i32* %3
%67 = load i8*, i8** %9
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [38 x i8] c"I am child %d and here's my PID: %d \0A\00"
@.str.2 = [41 x i8] c"I am the father and heres my sons PIDs: \00"
@.str.3 = [4 x i8] c"%d \00"
@str = [17 x i8] c"FORK NOT SUCCEED\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %10 [
i32 0, label %4
i32 -1, label %8
]
4: ; preds = %10, %2
%5 = phi i32 [ %3, %2 ], [ 1, %10 ]
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
br label %19
8: ; preds = %10, %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %19
10: ; preds = %2
%11 = tail call i32 (...) @fork()
switch i32 %11, label %12 [
i32 0, label %4
i32 -1, label %8
]
12: ; preds = %10
%13 = icmp sgt i32 %11, 0
br i1 %13, label %14, label %19
14: ; preds = %12
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %3)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%18 = tail call i32 @putchar(i32 10)
br label %19
19: ; preds = %8, %4, %14, %12
ret i32 0
}