#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 */
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  pid_t child;
  child = fork();
  if (child < 0) {
    fprintf(stderr, "fork failed!\n");
    return -1;
  }
  if (child > 0)
    printf("Parent %d. Will exit\n", getpid());
  else {
    printf("In child, waiting\n");
    sleep(1);
    printf("Wait done, parent should be finished by now\n");
  }
  return 4;
}


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    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    119d <main+0x4d>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <main+0xa1>
cmpq   $0x0,-0x18(%rbp)
jle    11c4 <main+0x74>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ea <main+0x9a>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     1188 <main+0x28>
je     11a4 <main+0x44>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe91(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11c6 <main+0x66>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    $0x4,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [22 x i8] c"Parent %d. Will exit\0A\00"
@.str.2 = [19 x i8] c"In child, waiting\0A\00"
@.str.3 = [45 x i8] c"Wait done, parent should be finished by now\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %24
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %23
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @sleep(i32 1)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %19, %16
store i32 4, i32* %3
br label %24
24: ; preds = %23, %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [22 x i8] c"Parent %d. Will exit\0A\00"
@str = [18 x i8] c"In child, waiting\00"
@str.4 = [44 x i8] c"Wait done, parent should be finished by now\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %2
%9 = icmp eq i64 %3, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %17
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.4, i64 0, i64 0))
br label %17
17: ; preds = %10, %13, %5
%18 = phi i32 [ -1, %5 ], [ 4, %13 ], [ 4, %10 ]
ret i32 %18
}