#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(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
void gen_fork(void) {
  pid_t pid;
  int status;
  pid = fork();
  if (pid == 0) {
    printf("fork parent: %d, child: %d\n", getppid(), getpid());
    exit(0);
  } else if (pid < 0) {
    fprintf(stderr, "fork() failed\n");
    exit(1);
  } else {
    wait(&status);
  }
}


x86 assembly (clang, -O0):
<gen_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b7 <gen_fork+0x47>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jge    11e1 <gen_fork+0x71>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1080 <wait@plt>
jmp    11ef <gen_fork+0x7f>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_fork>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1193 <gen_fork+0x23>
js     11ba <gen_fork+0x4a>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe56(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"fork parent: %d, child: %d\0A\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"fork() failed\0A\00"
define void @gen_fork() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = call i32 (...) @getppid()
%8 = call i32 (...) @getpid()
%9 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = load i64, i64* %1
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i32 @wait(i32* %2)
br label %20
20: ; preds = %18
br label %21
21: ; preds = %20
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"fork parent: %d, child: %d\0A\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"fork() failed\0A\00"
define void @gen_fork() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = tail call i32 (...) @getppid()
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = icmp slt i64 %3, 0
br i1 %11, label %12, label %16
12: ; preds = %10
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = call i32 @wait(i32* %1)
ret void
}