#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 fork();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int main(void) {
  pid_t childpid;
  childpid = fork();
  if (childpid == -1) {
    perror("Failed to fork");
    return 1;
  }
  if (childpid == 0)
    printf("I am child %ld\n", (long)getpid());
  else
    printf("I am parent %ld\n", (long)getpid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    117b <main+0x3b>
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11bd <main+0x7d>
cmpl   $0x0,-0x8(%rbp)
jne    11a0 <main+0x60>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
jmp    11b6 <main+0x76>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1165 <main+0x25>
cmp    $0xffffffff,%eax
je     117f <main+0x3f>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xebc(%rip),%rdi
jmp    1173 <main+0x33>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9c(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%ebx
jmp    117b <main+0x3b>

LLVM IR (-O0):
@.str = [15 x i8] c"Failed to fork\00"
@.str.1 = [16 x i8] c"I am child %ld\0A\00"
@.str.2 = [17 x i8] c"I am parent %ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @fork()
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %18
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i64 (...) @getpid()
%13 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %12)
br label %17
14: ; preds = %8
%15 = call i64 (...) @getpid()
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %15)
br label %17
17: ; preds = %14, %11
store i32 0, i32* %1
br label %18
18: ; preds = %17, %6
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@.str = [15 x i8] c"Failed to fork\00"
@.str.1 = [16 x i8] c"I am child %ld\0A\00"
@.str.2 = [17 x i8] c"I am parent %ld\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %7 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %10
4: ; preds = %0
%5 = tail call i64 (...) @getpid()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %5)
br label %10
7: ; preds = %0
%8 = tail call i64 (...) @getpid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %8)
br label %10
10: ; preds = %4, %7, %2
%11 = phi i32 [ 1, %2 ], [ 0, %7 ], [ 0, %4 ]
ret i32 %11
}