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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
void ex_fork(void) {
  pid_t pid = fork();
  if (pid == 0) {
    printf("I'm the child!\n");
    exit(EXIT_SUCCESS);
  } else if (pid < 0) {
    fprintf(stderr, "An error happened!\n");
  } else {
    printf("I'm the parent.  I created a child with pid=%d.\n", pid);
  }
}


x86 assembly (clang, -O0):
<ex_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117a <ex_fork+0x3a>
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    119f <ex_fork+0x5f>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11b1 <ex_fork+0x71>
mov    -0x8(%rbp),%rsi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11b6 <ex_fork+0x76>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ex_fork>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1187 <ex_fork+0x37>
js     1171 <ex_fork+0x21>
lea    0xeae(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
pop    %rcx
jmp    1040 <printf@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
pop    %rax
jmp    1050 <fprintf@plt>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"I'm the child!\0A\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"An error happened!\0A\00"
@.str.2 = [49 x i8] c"I'm the parent. I created a child with pid=%d.\0A\00"
define void @ex_fork() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_SUCCESS
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %18
15: ; preds = %9
%16 = load i64, i64* %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %16)
br label %18
18: ; preds = %15, %12
br label %19
19: ; preds = %18
ret void
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"An error happened!\0A\00"
@.str.2 = [49 x i8] c"I'm the parent. I created a child with pid=%d.\0A\00"
@str = [15 x i8] c"I'm the child!\00"
define void @ex_fork() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = icmp slt i64 %1, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %14
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %1)
br label %14
14: ; preds = %9, %12
ret void
}