#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  pid_t pid, ret;
  pid = getpid();
  printf("Before: my pid is %d\n", pid);
  ret = fork();
  if (ret == -1) {
    perror("fork() failed");
    exit(1);
  } else if (ret == 0) {
    printf("I'm child proces, my pid = %d\n", getpid());
  } else {
    printf("I'm parent, my child is %d\n", ret);
  }
  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   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a9 <main+0x59>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11cd <main+0x7d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
jmp    11dc <main+0x8c>
mov    -0x18(%rbp),%esi
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
jmp    11e1 <main+0x91>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     117f <main+0x2f>
cmp    $0xffffffff,%eax
je     1198 <main+0x48>
lea    0xec6(%rip),%rdi
jmp    118d <main+0x3d>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe97(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [22 x i8] c"Before: my pid is %d\0A\00"
@.str.1 = [14 x i8] c"fork() failed\00"
@.str.2 = [31 x i8] c"I'm child proces, my pid = %d\0A\00"
@.str.3 = [28 x i8] c"I'm parent, my child is %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @fork()
switch i32 %5, label %12 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %14
12: ; preds = %2
%13 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %5)
br label %14
14: ; preds = %9, %12
ret i32 0
}