#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork() ");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    printf("This is child with pid %u and parent pid %u\n", getpid(),
           getppid());
  } else {
    printf("This is parent with pid %u\n", getpid());
    sleep(3);
  }
  exit(EXIT_SUCCESS);
}


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)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11b6 <main+0x46>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11ea <main+0x7a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120b <main+0x9b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
jmp    1210 <main+0xa0>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    1199 <main+0x29>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
jne    11c5 <main+0x55>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe50(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11df <main+0x6f>
lea    0xe69(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"fork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"This is child with pid %u and parent pid %u\0A\00"
@.str.2 = [28 x i8] c"This is parent with pid %u\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i64, i64* %6
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (...) @getppid()
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
br label %25
21: ; preds = %14
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 3)
br label %25
25: ; preds = %21, %17
br label %26
26: ; preds = %25
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"fork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"This is child with pid %u and parent pid %u\0A\00"
@.str.2 = [28 x i8] c"This is parent with pid %u\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %3, 0
%11 = tail call i32 (...) @getpid()
br i1 %10, label %12, label %15
12: ; preds = %9
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13)
br label %18
15: ; preds = %9
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%17 = tail call i32 @sleep(i32 3)
br label %18
18: ; preds = %12, %15
%19 = load i32, i32* @EXIT_SUCCESS
%20 = tail call i32 @exit(i32 %19)
unreachable
}