#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 /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int main(int argc, char *argv[]) {
  pid_t parent, child;
  int i;
  parent = getpid();
  if ((child = fork()) == -1) {
    do {
      perror("fork");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if (child) {
    for (i = 0; i < 10000; i++) {
      printf("In Parent %d, waiting for child %d\n", parent, child);
    }
  } else {
    child = getpid();
    for (i = 0; i < 10000; i++) {
      printf("In Child %d, forked from parent %d\n", child, parent);
    }
  }
  return EXIT_SUCCESS;
}


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)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a2 <main+0x52>
jmp    1188 <main+0x38>
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     11e5 <main+0x95>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2710,-0x1c(%rbp)
jge    11e0 <main+0x90>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b3 <main+0x63>
jmp    1228 <main+0xd8>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2710,-0x1c(%rbp)
jge    1223 <main+0xd3>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f6 <main+0xa6>
jmp    1228 <main+0xd8>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1194 <main+0x44>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     11d6 <main+0x86>
mov    $0x2710,%ebp
lea    0xe86(%rip),%r14
nop
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1040 <printf@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x30>
jmp    11c2 <main+0x72>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
mov    $0x2710,%ebp
lea    0xe7f(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
add    $0xffffffff,%ebp
jne    11b0 <main+0x60>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe23(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"In Parent %d, waiting for child %d\0A\00"
@.str.2 = [36 x i8] c"In Child %d, forked from parent %d\0A\00"
@EXIT_SUCCESS = global i32 0
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 = call i32 (...) @getpid()
store i32 %9, i32* %6
%10 = call i32 (...) @fork()
store i32 %10, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %2
br label %13
13: ; preds = %12
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; No predecessors!
br label %18
18: ; preds = %17, %2
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
store i32 0, i32* %8
br label %22
22: ; preds = %29, %21
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 10000
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %25
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %22
32: ; preds = %22
br label %46
33: ; preds = %18
%34 = call i32 (...) @getpid()
store i32 %34, i32* %7
store i32 0, i32* %8
br label %35
35: ; preds = %42, %33
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 10000
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i32, i32* %7
%40 = load i32, i32* %6
%41 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40)
br label %42
42: ; preds = %38
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %35
45: ; preds = %35
br label %46
46: ; preds = %45, %32
%47 = load i32, i32* @EXIT_SUCCESS
ret i32 %47
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"In Parent %d, waiting for child %d\0A\00"
@.str.2 = [36 x i8] c"In Child %d, forked from parent %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @fork()
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %14
]
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2, %9
%10 = phi i32 [ %12, %9 ], [ 0, %2 ]
%11 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %4)
%12 = add nuw nsw i32 %10, 1
%13 = icmp eq i32 %12, 10000
br i1 %13, label %21, label %9
14: ; preds = %2
%15 = tail call i32 (...) @getpid()
br label %16
16: ; preds = %14, %16
%17 = phi i32 [ 0, %14 ], [ %19, %16 ]
%18 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %3)
%19 = add nuw nsw i32 %17, 1
%20 = icmp eq i32 %19, 10000
br i1 %20, label %21, label %16
21: ; preds = %16, %9
%22 = load i32, i32* @EXIT_SUCCESS
ret i32 %22
}