#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 /*<<< orphan*/ err_sys(char *);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t pid;
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid == 0) {
    puts("child process here.");
    puts("child process will sleep for 10 seconds...");
    sleep(10);
  } else {
    puts("parent process here.");
    printf("parent get the child pid: %d\n", pid);
  }
  sleep(3);
  if (pid == 0) {
    puts("child process exit.");
  } else {
    puts("parent process exit.");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1185 <main+0x35>
lea    0xe85(%rip),%rdi
call   1040 <err_sys@plt>
jmp    11d8 <main+0x88>
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <main+0x67>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
jmp    11d3 <main+0x83>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdi
call   1050 <printf@plt>
jmp    11d8 <main+0x88>
mov    $0x3,%edi
call   1060 <sleep@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11fe <main+0xae>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
jmp    120a <main+0xba>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1186 <main+0x36>
je     119b <main+0x4b>
mov    %rax,%rbx
lea    0xee1(%rip),%rdi
call   1030 <puts@plt>
lea    0xeea(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0xf0d(%rip),%rbx
jmp    11c4 <main+0x74>
lea    0xe73(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xef8(%rip),%rbx
jmp    11c4 <main+0x74>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
lea    0xeb9(%rip),%rbx
mov    $0x3,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [20 x i8] c"child process here.\00"
@.str.2 = [43 x i8] c"child process will sleep for 10 seconds...\00"
@.str.3 = [21 x i8] c"parent process here.\00"
@.str.4 = [30 x i8] c"parent get the child pid: %d\0A\00"
@.str.5 = [20 x i8] c"child process exit.\00"
@.str.6 = [21 x i8] c"parent process exit.\00"
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 %7
5: ; preds = %0
%6 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %19
7: ; preds = %0
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @sleep(i32 10)
br label %18
14: ; preds = %7
%15 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%16 = load i64, i64* %2
%17 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %16)
br label %18
18: ; preds = %14, %10
br label %19
19: ; preds = %18, %5
%20 = call i32 @sleep(i32 3)
%21 = load i64, i64* %2
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %27
25: ; preds = %19
%26 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %25, %23
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [20 x i8] c"child process here.\00"
@.str.2 = [43 x i8] c"child process will sleep for 10 seconds...\00"
@.str.3 = [21 x i8] c"parent process here.\00"
@.str.4 = [30 x i8] c"parent get the child pid: %d\0A\00"
@.str.5 = [20 x i8] c"child process exit.\00"
@.str.6 = [21 x i8] c"parent process exit.\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %14
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %10, label %7
7: ; preds = %5
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %1)
br label %14
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 10)
br label %14
14: ; preds = %3, %7, %10
%15 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), %10 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), %7 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), %3 ]
%16 = tail call i32 @sleep(i32 3)
%17 = tail call i32 @puts(i8* dereferenceable(1) %15)
ret i32 0
}