#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*/ child();
int fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid;
  pid = fork();
  if (pid == -1)
    puts("fork failed!");
  else if (pid == 0)
    child();
  else {
    printf("My child pid is %d\n", pid);
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1184 <main+0x34>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
jmp    11b7 <main+0x67>
cmpl   $0x0,-0x8(%rbp)
jne    119a <main+0x4a>
mov    $0x0,%al
call   1030 <child@plt>
jmp    11b2 <main+0x62>
mov    -0x8(%rbp),%esi
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
jmp    11b7 <main+0x67>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     116f <main+0x1f>
cmp    $0xffffffff,%eax
jne    1178 <main+0x28>
lea    0xe98(%rip),%rdi
call   1040 <puts@plt>
jmp    118d <main+0x3d>
xor    %eax,%eax
call   1030 <child@plt>
jmp    118d <main+0x3d>
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"fork failed!\00"
@.str.1 = [20 x i8] c"My child pid is %d\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 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (...) @child()
br label %17
13: ; preds = %8
%14 = load i32, i32* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @wait(i32* null)
br label %17
17: ; preds = %13, %11
br label %18
18: ; preds = %17, %6
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"fork failed!\00"
@.str.1 = [20 x i8] c"My child pid is %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %6 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %9
4: ; preds = %0
%5 = tail call i32 (...) @child()
br label %9
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%8 = tail call i32 @wait(i32* null)
br label %9
9: ; preds = %4, %6, %2
ret i32 0
}