#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 scalar_t__ pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("Fork error!\n");
    exit(1);
  } else if (pid == 0) {
    sleep(3);
    printf("Child running...\n");
    sleep(3);
    printf("Child %d,returned value:%d.\n", getpid(), pid);
    exit(0);
  } else {
    printf("Parent running!\n");
    sleep(3);
    printf("Parent %d, returned value:%d.\n", getpid(), pid);
    waitpid(pid, 0, 0);
    exit(0);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11aa <main+0x3a>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f9 <main+0x89>
mov    $0x3,%edi
call   1080 <sleep@plt>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <waitpid@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11a3 <main+0x23>
lea    0xe6c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jne    11e5 <main+0x65>
mov    $0x3,%edi
call   1090 <sleep@plt>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3a(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1e(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"Fork error!\0A\00"
@.str.1 = [18 x i8] c"Child running...\0A\00"
@.str.2 = [29 x i8] c"Child %d,returned value:%d.\0A\00"
@.str.3 = [17 x i8] c"Parent running!\0A\00"
@.str.4 = [31 x i8] c"Parent %d, returned value:%d.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i64, i64* %2
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %8
%12 = call i32 @sleep(i32 3)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @sleep(i32 3)
%15 = call i32 (...) @getpid()
%16 = load i64, i64* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %15, i64 %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %8
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @sleep(i32 3)
%22 = call i32 (...) @getpid()
%23 = load i64, i64* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %22, i64 %23)
%25 = load i64, i64* %2
%26 = call i32 @waitpid(i64 %25, i32 0, i32 0)
%27 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"Fork error!\0A\00"
@.str.2 = [29 x i8] c"Child %d,returned value:%d.\0A\00"
@.str.4 = [31 x i8] c"Parent %d, returned value:%d.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [16 x i8] c"Parent running!\00"
@str.5 = [17 x i8] c"Child running...\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %15
8: ; preds = %6
%9 = tail call i32 @sleep(i32 3)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 3)
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %12, i64 0)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %6
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @sleep(i32 3)
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %18, i64 %1)
%20 = tail call i32 @waitpid(i64 %1, i32 0, i32 0)
%21 = tail call i32 @exit(i32 0)
unreachable
}