#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 /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  int pid;
  if ((pid = fork()) == -1) {
    perror("Fork:");
    exit(-1);
  }
  if (pid == 0) {
    printf("I am child, pid = %d, parent = %d, fork() returned %d\n", getpid(),
           getppid(), pid);
    while (1)
      sleep(1);
  } else {
    printf("I am parent, pid = %d, fork() returned %d\n", getpid(), pid);
    while (1)
      sleep(1);
  }
  return 0;
}


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    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11af <main+0x3f>
lea    0xe60(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11ef <main+0x7f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11e0 <main+0x70>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1209 <main+0x99>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11ac <main+0x3c>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11dc <main+0x6c>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeac(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
nopl   0x0(%rax)
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11a0 <main+0x30>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe43(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11d0 <main+0x60>
lea    0xe1d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [6 x i8] c"Fork:\00"
@.str.1 = [55 x i8] c"I am child, pid = %d, parent = %d, fork() returned %d\0A\00"
@.str.2 = [43 x i8] c"I am parent, pid = %d, fork() returned %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %13 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9, i32 0)
br label %11
11: ; preds = %11, %7
%12 = tail call i32 @sleep(i32 1)
br label %11
13: ; preds = %2
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %3)
br label %16
16: ; preds = %16, %13
%17 = tail call i32 @sleep(i32 1)
br label %16
}