#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 /*<<< orphan*/ _exit(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int wait(int *);
int main(void) {
  pid_t pid, wpid;
  int child_status;
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "can't fork, error %d\n", errno);
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    int j;
    for (j = 0; j < 10; j++) {
      printf("child: %d\n", j);
      sleep(2);
    }
    _exit(0);
  } else {
    int i;
    for (i = 0; i < 10; i++) {
      printf("parent: %d\n", i);
      sleep(1);
    }
    do {
      wpid = wait(&child_status);
    } while (wpid != pid);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11bf <main+0x4f>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1208 <main+0x98>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    1201 <main+0x91>
mov    -0x14(%rbp),%esi
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d0 <main+0x60>
xor    %edi,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    1240 <main+0xd0>
mov    -0x18(%rbp),%esi
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120f <main+0x9f>
jmp    1245 <main+0xd5>
lea    -0x10(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1245 <main+0xd5>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0x113>
mov    %eax,%ebp
test   %eax,%eax
jne    12af <main+0x13f>
lea    0xe7d(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
lea    0xd64(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xd6b(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %r14,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xc(%rsp),%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1080 <wait@plt>
cmp    %ebp,%eax
jne    13a0 <main+0x230>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"can't fork, error %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"child: %d\0A\00"
@.str.2 = [12 x i8] c"parent: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %16
store i32 0, i32* %5
br label %20
20: ; preds = %27, %19
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 10
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* %5
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i32 @sleep(i32 2)
br label %27
27: ; preds = %23
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %20
30: ; preds = %20
%31 = call i32 @_exit(i32 0)
unreachable
32: ; preds = %16
store i32 0, i32* %6
br label %33
33: ; preds = %40, %32
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 10
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* %6
%38 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = call i32 @sleep(i32 1)
br label %40
40: ; preds = %36
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %33
43: ; preds = %33
br label %44
44: ; preds = %46, %43
%45 = call i32 @wait(i32* %4)
store i32 %45, i32* %3
br label %46
46: ; preds = %44
%47 = load i32, i32* %3
%48 = load i32, i32* %2
%49 = icmp ne i32 %47, %48
br i1 %49, label %44, label %50
50: ; preds = %46
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"can't fork, error %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"child: %d\0A\00"
@.str.2 = [12 x i8] c"parent: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %26 [
i32 -1, label %47
i32 0, label %4
]
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 0)
%6 = tail call i32 @sleep(i32 2)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 1)
%8 = tail call i32 @sleep(i32 2)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 2)
%10 = tail call i32 @sleep(i32 2)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 3)
%12 = tail call i32 @sleep(i32 2)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 4)
%14 = tail call i32 @sleep(i32 2)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 5)
%16 = tail call i32 @sleep(i32 2)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 6)
%18 = tail call i32 @sleep(i32 2)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 7)
%20 = tail call i32 @sleep(i32 2)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 8)
%22 = tail call i32 @sleep(i32 2)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 9)
%24 = tail call i32 @sleep(i32 2)
%25 = tail call i32 @_exit(i32 0)
unreachable
26: ; preds = %0
%27 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 0)
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 1)
%30 = tail call i32 @sleep(i32 1)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 2)
%32 = tail call i32 @sleep(i32 1)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 3)
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 4)
%36 = tail call i32 @sleep(i32 1)
%37 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 5)
%38 = tail call i32 @sleep(i32 1)
%39 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 6)
%40 = tail call i32 @sleep(i32 1)
%41 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 7)
%42 = tail call i32 @sleep(i32 1)
%43 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 8)
%44 = tail call i32 @sleep(i32 1)
%45 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 9)
%46 = tail call i32 @sleep(i32 1)
br label %53
47: ; preds = %0
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = tail call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = tail call i32 @exit(i32 %51)
unreachable
53: ; preds = %26, %53
%54 = call i32 @wait(i32* %1)
%55 = icmp eq i32 %54, %3
br i1 %55, label %56, label %53
56: ; preds = %53
%57 = call i32 @exit(i32 0)
unreachable
}