#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*/ errno;
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv, char **envp) {
  pid_t p;
  p = fork();
  if (p < 0) {
    printf("fork failed: %s\n", strerror(errno));
  } else if (p == 0) {
    printf("In child!\n");
  } else {
    printf("In parent, child pid=%d!\n", (int)p);
  }
  printf("In child or parent\n");
  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    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1184 <main+0x54>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    %rax,%rsi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11bb <main+0x8b>
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <main+0x72>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11b6 <main+0x86>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11bb <main+0x8b>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1161 <main+0x21>
je     1182 <main+0x42>
lea    0xebb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    118e <main+0x4e>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe8a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    118e <main+0x4e>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"fork failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [11 x i8] c"In child!\0A\00"
@.str.2 = [26 x i8] c"In parent, child pid=%d!\0A\00"
@.str.3 = [20 x i8] c"In child or parent\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i64
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%9 = call i64 (...) @fork()
store i64 %9, i64* %8
%10 = load i64, i64* %8
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %26
16: ; preds = %3
%17 = load i64, i64* %8
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %25
21: ; preds = %16
%22 = load i64, i64* %8
%23 = trunc i64 %22 to i32
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %21, %19
br label %26
26: ; preds = %25, %12
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"fork failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"In parent, child pid=%d!\0A\00"
@str = [19 x i8] c"In child or parent\00"
@str.4 = [10 x i8] c"In child!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %17
10: ; preds = %3
%11 = icmp eq i64 %4, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
br label %17
14: ; preds = %10
%15 = trunc i64 %4 to i32
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %12, %14, %6
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
ret i32 0
}