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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11af <main+0x4f>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
lea    0xe64(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11f8 <main+0x98>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11f1 <main+0x91>
mov    -0xc(%rbp),%esi
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c0 <main+0x60>
xor    %edi,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    1230 <main+0xd0>
mov    -0x10(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ff <main+0x9f>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     126a <main+0x10a>
test   %eax,%eax
jne    1296 <main+0x136>
lea    0xe96(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
lea    0xd7d(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xd84(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
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
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %14
store i32 0, i32* %3
br label %18
18: ; preds = %25, %17
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 10
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i32, i32* %3
%23 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 1)
br label %25
25: ; preds = %21
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %18
28: ; preds = %18
%29 = call i32 @_exit(i32 0)
unreachable
30: ; preds = %14
store i32 0, i32* %4
br label %31
31: ; preds = %38, %30
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 10
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i32, i32* %4
%36 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = call i32 @sleep(i32 1)
br label %38
38: ; preds = %34
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %31
41: ; preds = %31
%42 = 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 = tail call i32 (...) @fork()
switch i32 %1, label %24 [
i32 -1, label %46
i32 0, label %2
]
2: ; preds = %0
%3 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 0)
%4 = tail call i32 @sleep(i32 1)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 1)
%6 = tail call i32 @sleep(i32 1)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 2)
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 3)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 4)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 5)
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 6)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 7)
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 8)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 9)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @_exit(i32 0)
unreachable
24: ; preds = %0
%25 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 0)
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 1)
%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 2)
%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 3)
%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 4)
%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 5)
%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 6)
%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 7)
%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 8)
%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 9)
%44 = tail call i32 @sleep(i32 1)
%45 = tail call i32 @exit(i32 0)
unreachable
46: ; preds = %0
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = tail call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = tail call i32 @exit(i32 %50)
unreachable
}