#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 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\n", getpid(), getppid());
  } else {
    printf("I am parent, pid = %d, parent = %d\n", getpid(), getppid());
  }
  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    11d0 <main+0x70>
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
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
jmp    11f2 <main+0x92>
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    0xe3c(%rip),%rdi
call   1040 <printf@plt>
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     117c <main+0x1c>
cmp    $0xffffffff,%eax
je     11a8 <main+0x48>
lea    0xeaf(%rip),%rbx
jmp    1183 <main+0x23>
lea    0xe83(%rip),%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe51(%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 = [35 x i8] c"I am child, pid = %d, parent = %d\0A\00"
@.str.2 = [36 x i8] c"I am parent, pid = %d, parent = %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 %19
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 (...) @getppid()
%18 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
br label %23
19: ; preds = %12
%20 = call i32 (...) @getpid()
%21 = call i32 (...) @getppid()
%22 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
br label %23
23: ; preds = %19, %15
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"Fork:\00"
@.str.1 = [35 x i8] c"I am child, pid = %d, parent = %d\0A\00"
@.str.2 = [36 x i8] c"I am parent, pid = %d, parent = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %7 [
i32 -1, label %4
i32 0, label %8
]
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
br label %8
8: ; preds = %2, %7
%9 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), %7 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %2 ]
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getppid()
%12 = tail call i32 @printf(i8* %9, i32 %10, i32 %11)
ret i32 0
}