#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int _error(char const *, char *, int);
int /*<<< orphan*/ _exit(int);
int fork();
int /*<<< orphan*/ printf(char *, char const *);
int waitpid(int, int *, int /*<<< orphan*/);
int _fork(char const *progname) {
  pid_t pid;
  int res = 42;
  pid_t p;
  int status;
  printf("%s: Testing fork()\n", progname);
  if ((pid = fork()) == -1)
    return _error(progname, "fork", 4);
  if (pid == 0)
    _exit(res);
  for (;;) {
    if ((p = waitpid(pid, &status, 0)) == -1)
      break;
    if (p != pid)
      continue;
    if (WIFEXITED(status) && WEXITSTATUS(status) == res)
      return 0;
  }
  return _error(progname, "waitpid", 8);
}


x86 assembly (clang, -O0):
<_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x2a,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c3 <_fork+0x53>
mov    -0x10(%rbp),%rdi
lea    0xe63(%rip),%rsi
mov    $0x4,%edx
call   1050 <_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1256 <_fork+0xe6>
cmpl   $0x0,-0x14(%rbp)
jne    11d5 <_fork+0x65>
mov    -0x18(%rbp),%edi
call   1030 <_exit@plt>
jmp    11da <_fork+0x6a>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <_fork+0x89>
jmp    123e <_fork+0xce>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     120a <_fork+0x9a>
jmp    11da <_fork+0x6a>
mov    -0x20(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1239 <_fork+0xc9>
mov    -0x20(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    -0x18(%rbp),%eax
jne    1239 <_fork+0xc9>
movl   $0x0,-0x4(%rbp)
jmp    1256 <_fork+0xe6>
jmp    11da <_fork+0x6a>
mov    -0x10(%rbp),%rdi
lea    0xdd0(%rip),%rsi
mov    $0x8,%edx
call   1050 <_error@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fork>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe7e(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11f5 <_fork+0x85>
mov    %eax,%ebp
test   %eax,%eax
je     122e <_fork+0xbe>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1211 <_fork+0xa1>
lea    0xc(%rsp),%rbx
jmp    11d1 <_fork+0x61>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1211 <_fork+0xa1>
cmp    %ebp,%eax
jne    11c0 <_fork+0x50>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11c0 <_fork+0x50>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x2a,%eax
jne    11c0 <_fork+0x50>
xor    %eax,%eax
jmp    1225 <_fork+0xb5>
lea    0xe18(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <_error@plt>
lea    0xe01(%rip),%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1050 <_error@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x2a,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"%s: Testing fork()\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [8 x i8] c"waitpid\00"
define i32 @_fork(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 42, i32* %5
%8 = load i8*, i8** %3
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = call i32 (...) @fork()
store i32 %10, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @_error(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
store i32 %14, i32* %2
br label %46
15: ; preds = %1
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* %5
%20 = call i32 @_exit(i32 %19)
unreachable
21: ; preds = %15
br label %22
22: ; preds = %42, %31, %21
%23 = load i32, i32* %4
%24 = call i32 @waitpid(i32 %23, i32* %7, i32 0)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %22
br label %43
27: ; preds = %22
%28 = load i32, i32* %6
%29 = load i32, i32* %4
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %22
32: ; preds = %27
%33 = load i32, i32* %7
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* %7
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = load i32, i32* %5
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 0, i32* %2
br label %46
42: ; preds = %36, %32
br label %22
43: ; preds = %26
%44 = load i8*, i8** %3
%45 = call i32 @_error(i8* %44, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 8)
store i32 %45, i32* %2
br label %46
46: ; preds = %43, %41, %12
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [20 x i8] c"%s: Testing fork()\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [8 x i8] c"waitpid\00"
define i32 @_fork(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32 (...) @fork()
switch i32 %5, label %6 [
i32 -1, label %9
i32 0, label %11
]
6: ; preds = %1
%7 = call i32 @waitpid(i32 %5, i32* %2, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %27, label %13
9: ; preds = %1
%10 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
br label %29
11: ; preds = %1
%12 = tail call i32 @_exit(i32 42)
unreachable
13: ; preds = %6, %24
%14 = phi i32 [ %25, %24 ], [ %7, %6 ]
%15 = icmp eq i32 %14, %5
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i32, i32* %2
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* %2
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = icmp eq i32 %22, 42
br i1 %23, label %29, label %24
24: ; preds = %16, %20, %13
%25 = call i32 @waitpid(i32 %5, i32* %2, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %13
27: ; preds = %24, %6
%28 = call i32 @_error(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 8)
br label %29
29: ; preds = %20, %27, %9
%30 = phi i32 [ %10, %9 ], [ %28, %27 ], [ 0, %20 ]
ret i32 %30
}