#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*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int main() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork error:");
    exit(1);
  } else if (pid == 0) {
    printf("I am child process.I am exiting.\n");
    exit(0);
  }
  printf("I am father process.I will sleep two seconds\n");
  sleep(2);
  system("ps -o pid,ppid,state,tty,command");
  printf("father process is exiting.\n");
  getchar();
  return 0;
}


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)
cmpq   $0x0,-0x10(%rbp)
jge    11ab <main+0x3b>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <main+0x59>
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11ce <main+0x5e>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0xe71(%rip),%rdi
call   1030 <system@plt>
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11b8 <main+0x48>
je     11ce <main+0x5e>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
lea    0xe70(%rip),%rdi
call   1040 <system@plt>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork error:\00"
@.str.1 = [34 x i8] c"I am child process.I am exiting.\0A\00"
@.str.2 = [46 x i8] c"I am father process.I will sleep two seconds\0A\00"
@.str.3 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@.str.4 = [28 x i8] c"father process is exiting.\0A\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 = load i64, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %9
br label %16
16: ; preds = %15
%17 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @sleep(i32 2)
%19 = call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 (...) @getchar()
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork error:\00"
@.str.3 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@str = [45 x i8] c"I am father process.I will sleep two seconds\00"
@str.5 = [27 x i8] c"father process is exiting.\00"
@str.6 = [33 x i8] c"I am child process.I am exiting.\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 ([12 x i8], [12 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 %11
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 2)
%14 = tail call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%16 = tail call i32 (...) @getchar()
ret i32 0
}