#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);
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);
  } else {
    printf("I am parent, pid = %d, parent = %d, fork() returned %d\n", getpid(),
           getppid(), pid);
    while (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   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    119f <main+0x3f>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11d3 <main+0x73>
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    0xe3d(%rip),%rdi
call   1040 <printf@plt>
jmp    11fd <main+0x9d>
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
mov    -0x14(%rbp),%ecx
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
jmp    11f8 <main+0x98>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11a2 <main+0x42>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11cd <main+0x6d>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xeb1(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    %ebx,%ecx
call   1040 <printf@plt>
nopw   0x0(%rax,%rax,1)
jmp    11a0 <main+0x40>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe4d(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe2c(%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 = [56 x i8] c"I am parent, pid = %d, parent = %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 %20
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 (...) @getppid()
%18 = load i32, i32* %6
%19 = call 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 %26
20: ; preds = %12
%21 = call i32 (...) @getpid()
%22 = call i32 (...) @getppid()
%23 = load i32, i32* %6
%24 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22, i32 %23)
br label %25
25: ; preds = %20, %25
br label %25
26: ; preds = %15
ret i32 0
}

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 = [56 x i8] c"I am parent, pid = %d, parent = %d, fork() returned %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %11 [
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 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9, i32 0)
ret i32 0
11: ; preds = %2
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13, i32 %3)
br label %15
15: ; preds = %15, %11
br label %15
}