#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 fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
main() {
  int i;
  int x = 10;
  int pid1, pid2, status;
  printf("Before forking, the value of x is %d\n", x);
  if ((pid1 = fork()) == 0) {
    for (i = 0; i < 5; i++) {
      printf("\t\t\t At first child: x= %d\n", x);
      x = x + 10;
      sleep(1);
    }
  } else {
    if ((pid2 = fork()) == 0) {
      for (i = 0; i < 5; i++) {
        printf("\t\t\t\t\t\t At second child: x= %d\n", x);
        x = x + 20;
        sleep(1);
      }
    } else {
      for (i = 0; i < 5; i++) {
        printf("At parent: x= %d\n", x);
        x = x + 5;
        sleep(1);
      }
      waitpid(pid1, &status, 0);
      waitpid(pid2, &status, 0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xea0(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jne    11be <main+0x7e>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    11b9 <main+0x79>
mov    -0xc(%rbp),%esi
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0xc(%rbp)
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117f <main+0x3f>
jmp    1279 <main+0x139>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1217 <main+0xd7>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    1212 <main+0xd2>
mov    -0xc(%rbp),%esi
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0xc(%rbp)
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11d8 <main+0x98>
jmp    1274 <main+0x134>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    1258 <main+0x118>
mov    -0xc(%rbp),%esi
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0xc(%rbp)
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    121e <main+0xde>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
jmp    1279 <main+0x139>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
mov    $0xa,%esi
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1217 <main+0xd7>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1284 <main+0x144>
mov    %eax,%ebp
lea    0xede(%rip),%r14
mov    %r14,%rdi
mov    $0xa,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
mov    $0xf,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
mov    $0x14,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
mov    $0x19,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %r14,%rdi
mov    $0x1e,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
lea    0xc(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
jmp    12fe <main+0x1be>
lea    0xe08(%rip),%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1e,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x28,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
jmp    12ef <main+0x1af>
lea    0xdb6(%rip),%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1e,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x46,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5a,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Before forking, the value of x is %d\0A\00"
@.str.1 = [27 x i8] c"\09\09\09 At first child: x= %d\0A\00"
@.str.2 = [31 x i8] c"\09\09\09\09\09\09 At second child: x= %d\0A\00"
@.str.3 = [18 x i8] c"At parent: x= %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
store i32 10, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %0
store i32 0, i32* %2
br label %12
12: ; preds = %21, %11
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 5
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i32, i32* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 10
store i32 %19, i32* %3
%20 = call i32 @sleep(i32 1)
br label %21
21: ; preds = %15
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %12
24: ; preds = %12
br label %61
25: ; preds = %0
%26 = call i32 (...) @fork()
store i32 %26, i32* %5
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %25
store i32 0, i32* %2
br label %29
29: ; preds = %38, %28
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 5
br i1 %31, label %32, label %41
32: ; preds = %29
%33 = load i32, i32* %3
%34 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 20
store i32 %36, i32* %3
%37 = call i32 @sleep(i32 1)
br label %38
38: ; preds = %32
%39 = load i32, i32* %2
%40 = add nsw i32 %39, 1
store i32 %40, i32* %2
br label %29
41: ; preds = %29
br label %60
42: ; preds = %25
store i32 0, i32* %2
br label %43
43: ; preds = %52, %42
%44 = load i32, i32* %2
%45 = icmp slt i32 %44, 5
br i1 %45, label %46, label %55
46: ; preds = %43
%47 = load i32, i32* %3
%48 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 5
store i32 %50, i32* %3
%51 = call i32 @sleep(i32 1)
br label %52
52: ; preds = %46
%53 = load i32, i32* %2
%54 = add nsw i32 %53, 1
store i32 %54, i32* %2
br label %43
55: ; preds = %43
%56 = load i32, i32* %4
%57 = call i32 @waitpid(i32 %56, i32* %6, i32 0)
%58 = load i32, i32* %5
%59 = call i32 @waitpid(i32 %58, i32* %6, i32 0)
br label %60
60: ; preds = %55, %41
br label %61
61: ; preds = %60, %24
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [38 x i8] c"Before forking, the value of x is %d\0A\00"
@.str.1 = [27 x i8] c"\09\09\09 At first child: x= %d\0A\00"
@.str.2 = [31 x i8] c"\09\09\09\09\09\09 At second child: x= %d\0A\00"
@.str.3 = [18 x i8] c"At parent: x= %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 10)
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 10)
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 20)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 30)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 40)
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 50)
%16 = tail call i32 @sleep(i32 1)
br label %44
17: ; preds = %0
%18 = tail call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %17
%21 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 10)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 15)
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 20)
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 25)
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 30)
%30 = tail call i32 @sleep(i32 1)
%31 = call i32 @waitpid(i32 %4, i32* %1, i32 0)
%32 = call i32 @waitpid(i32 %18, i32* %1, i32 0)
br label %44
33: ; preds = %17
%34 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 10)
%35 = tail call i32 @sleep(i32 1)
%36 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 30)
%37 = tail call i32 @sleep(i32 1)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 50)
%39 = tail call i32 @sleep(i32 1)
%40 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 70)
%41 = tail call i32 @sleep(i32 1)
%42 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 90)
%43 = tail call i32 @sleep(i32 1)
br label %44
44: ; preds = %33, %6, %20
ret i32 0
}