#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main(void) {
  int pchild;
  if ((pchild = fork()) == 0) {
    int i;
    for (i = 0; i < 10; i++) {
      printf("Child #%d\n", i);
      sleep(1);
    }
    exit(500);
  } else if (pchild > 0) {
    int i, result;
    for (i = 0; i < 7; i++) {
      printf("Parent #%d\n", i);
      sleep(1);
    }
    printf("awaiting child's termination...\n");
    wait(&result);
    printf("Child's result=%d\n", result);
  } else
    perror("Fork failure");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    11c6 <main+0x66>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11bc <main+0x5c>
mov    -0xc(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1189 <main+0x29>
mov    $0x1f4,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    1237 <main+0xd7>
movl   $0x0,-0x10(%rbp)
cmpl   $0x7,-0x10(%rbp)
jge    120a <main+0xaa>
mov    -0x10(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d7 <main+0x77>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x14(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1243 <main+0xe3>
lea    0xe0d(%rip),%rdi
call   1040 <perror@plt>
jmp    1248 <main+0xe8>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    128c <main+0x11c>
lea    0xe75(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x1f4,%edi
call   1060 <exit@plt>
jg     12a1 <main+0x131>
lea    0xd95(%rip),%rdi
call   1050 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xd63(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xcdc(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    0xc(%rsp),%esi
lea    0xca2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Child #%d\0A\00"
@.str.1 = [12 x i8] c"Parent #%d\0A\00"
@.str.2 = [33 x i8] c"awaiting child's termination...\0A\00"
@.str.3 = [19 x i8] c"Child's result=%d\0A\00"
@.str.4 = [13 x i8] c"Fork failure\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %0
store i32 0, i32* %3
br label %9
9: ; preds = %16, %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i32, i32* %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @sleep(i32 1)
br label %16
16: ; preds = %12
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %9
19: ; preds = %9
%20 = call i32 @exit(i32 500)
unreachable
21: ; preds = %0
%22 = load i32, i32* %2
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %21
store i32 0, i32* %4
br label %25
25: ; preds = %32, %24
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 7
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i32, i32* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @sleep(i32 1)
br label %32
32: ; preds = %28
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
br label %25
35: ; preds = %25
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @wait(i32* %5)
%38 = load i32, i32* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %42
40: ; preds = %21
%41 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
br label %43
43: ; preds = %42
%44 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [11 x i8] c"Child #%d\0A\00"
@.str.1 = [12 x i8] c"Parent #%d\0A\00"
@.str.3 = [19 x i8] c"Child's result=%d\0A\00"
@.str.4 = [13 x i8] c"Fork failure\00"
@str = [32 x i8] c"awaiting child's termination...\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %26
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 0)
%6 = tail call i32 @sleep(i32 1)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 1)
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 2)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 3)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 4)
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 5)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 6)
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 7)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 8)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 9)
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 @exit(i32 500)
unreachable
26: ; preds = %0
%27 = icmp sgt i32 %2, 0
br i1 %27, label %28, label %48
28: ; preds = %26
%29 = bitcast i32* %1 to i8*
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 0)
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
%33 = tail call i32 @sleep(i32 1)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 2)
%35 = tail call i32 @sleep(i32 1)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 3)
%37 = tail call i32 @sleep(i32 1)
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 4)
%39 = tail call i32 @sleep(i32 1)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 5)
%41 = tail call i32 @sleep(i32 1)
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 6)
%43 = tail call i32 @sleep(i32 1)
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%45 = call i32 @wait(i32* %1)
%46 = load i32, i32* %1
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %46)
br label %50
48: ; preds = %26
%49 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %28, %48
%51 = call i32 @exit(i32 0)
unreachable
}