#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);
int /*<<< orphan*/ sleeprand();
int main(int argc, char *argv[]) {
  int pid;
  int x = 999;
  if ((pid = fork()) == -1) {
    perror("Fork:");
    exit(-1);
  }
  if (pid == 0) {
    x = 222;
    printf("1. I am child: x = %d\n", x);
    sleeprand();
    printf("2. I am child: x = %d\n", x);
    sleeprand();
    printf("3. I am child: x = %d\n", x);
    sleeprand();
    printf("4. I am child: x = %d\n", x);
    sleeprand();
    printf("5. I am child: x = %d\n", x);
  } else {
    printf("1. I am parent: x = %d\n", x);
    sleeprand();
    printf("2. I am parent: x = %d\n", x);
    sleeprand();
    printf("3. I am parent: x = %d\n", x);
    sleeprand();
    printf("4. I am parent: x = %d\n", x);
    sleeprand();
    printf("5. I am parent: x = %d\n", x);
  }
  return 0;
}


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)
movl   $0x3e7,-0x18(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1196 <main+0x46>
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1213 <main+0xc3>
movl   $0xde,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
jmp    127a <main+0x12a>
mov    -0x18(%rbp),%esi
lea    0xe5c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <sleeprand@plt>
mov    -0x18(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11d3 <main+0x83>
cmp    $0xffffffff,%eax
je     1248 <main+0xf8>
lea    0xf0d(%rip),%rdi
mov    $0x3e7,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xf0d(%rip),%rdi
mov    $0x3e7,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xf0d(%rip),%rdi
mov    $0x3e7,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xf0d(%rip),%rdi
mov    $0x3e7,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xf0d(%rip),%rdi
mov    $0x3e7,%esi
jmp    123f <main+0xef>
lea    0xe2c(%rip),%rdi
mov    $0xde,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xe2b(%rip),%rdi
mov    $0xde,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xe2a(%rip),%rdi
mov    $0xde,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xe29(%rip),%rdi
mov    $0xde,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <sleeprand@plt>
lea    0xe28(%rip),%rdi
mov    $0xde,%esi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xdb1(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"Fork:\00"
@.str.1 = [23 x i8] c"1. I am child: x = %d\0A\00"
@.str.2 = [23 x i8] c"2. I am child: x = %d\0A\00"
@.str.3 = [23 x i8] c"3. I am child: x = %d\0A\00"
@.str.4 = [23 x i8] c"4. I am child: x = %d\0A\00"
@.str.5 = [23 x i8] c"5. I am child: x = %d\0A\00"
@.str.6 = [24 x i8] c"1. I am parent: x = %d\0A\00"
@.str.7 = [24 x i8] c"2. I am parent: x = %d\0A\00"
@.str.8 = [24 x i8] c"3. I am parent: x = %d\0A\00"
@.str.9 = [24 x i8] c"4. I am parent: x = %d\0A\00"
@.str.10 = [24 x i8] c"5. I am parent: x = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 999, i32* %7
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %13
store i32 222, i32* %7
%17 = load i32, i32* %7
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 (...) @sleeprand()
%20 = load i32, i32* %7
%21 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = call i32 (...) @sleeprand()
%23 = load i32, i32* %7
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 (...) @sleeprand()
%26 = load i32, i32* %7
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = call i32 (...) @sleeprand()
%29 = load i32, i32* %7
%30 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %29)
br label %46
31: ; preds = %13
%32 = load i32, i32* %7
%33 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %32)
%34 = call i32 (...) @sleeprand()
%35 = load i32, i32* %7
%36 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %35)
%37 = call i32 (...) @sleeprand()
%38 = load i32, i32* %7
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i32 %38)
%40 = call i32 (...) @sleeprand()
%41 = load i32, i32* %7
%42 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i32 %41)
%43 = call i32 (...) @sleeprand()
%44 = load i32, i32* %7
%45 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %31, %16
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"Fork:\00"
@.str.1 = [23 x i8] c"1. I am child: x = %d\0A\00"
@.str.2 = [23 x i8] c"2. I am child: x = %d\0A\00"
@.str.3 = [23 x i8] c"3. I am child: x = %d\0A\00"
@.str.4 = [23 x i8] c"4. I am child: x = %d\0A\00"
@.str.5 = [23 x i8] c"5. I am child: x = %d\0A\00"
@.str.6 = [24 x i8] c"1. I am parent: x = %d\0A\00"
@.str.7 = [24 x i8] c"2. I am parent: x = %d\0A\00"
@.str.8 = [24 x i8] c"3. I am parent: x = %d\0A\00"
@.str.9 = [24 x i8] c"4. I am parent: x = %d\0A\00"
@.str.10 = [24 x i8] c"5. I am parent: x = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %17 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 222)
%9 = tail call i32 (...) @sleeprand()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 222)
%11 = tail call i32 (...) @sleeprand()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 222)
%13 = tail call i32 (...) @sleeprand()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 222)
%15 = tail call i32 (...) @sleeprand()
%16 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 222)
br label %27
17: ; preds = %2
%18 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 999)
%19 = tail call i32 (...) @sleeprand()
%20 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 999)
%21 = tail call i32 (...) @sleeprand()
%22 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i32 999)
%23 = tail call i32 (...) @sleeprand()
%24 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i32 999)
%25 = tail call i32 (...) @sleeprand()
%26 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i32 999)
br label %27
27: ; preds = %17, %7
ret i32 0
}