#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 */

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ check_dune();
scalar_t__ dune_enter();
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int);
int fork();
int waitpid(int, int *, int /*<<< orphan*/);
int test_fork(void) {
  int pid;
  int rc;
  if (dune_enter())
    return 1;
  if (check_dune())
    return 2;
  pid = fork();
  if (pid == -1)
    return 3;
  if (pid == 0) {
    if (dune_enter())
      return 1;
    if (check_dune())
      exit(1);
    exit(69);
  } else {
    if (check_dune())
      return 4;
    if (waitpid(pid, &rc, 0) == -1)
      err(5, "waitpid()");
    if (WEXITSTATUS(rc) != 69)
      return 6;
  }
  return 255;
}


x86 assembly (clang, -O0):
<test_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <dune_enter@plt>
cmp    $0x0,%rax
je     1195 <test_fork+0x25>
movl   $0x1,-0x4(%rbp)
jmp    128c <test_fork+0x11c>
mov    $0x0,%al
call   1050 <check_dune@plt>
cmp    $0x0,%rax
je     11b2 <test_fork+0x42>
movl   $0x2,-0x4(%rbp)
jmp    128c <test_fork+0x11c>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d2 <test_fork+0x62>
movl   $0x3,-0x4(%rbp)
jmp    128c <test_fork+0x11c>
cmpl   $0x0,-0x8(%rbp)
jne    121e <test_fork+0xae>
mov    $0x0,%al
call   1030 <dune_enter@plt>
cmp    $0x0,%rax
je     11f9 <test_fork+0x89>
movl   $0x1,-0x4(%rbp)
jmp    128c <test_fork+0x11c>
mov    $0x0,%al
call   1050 <check_dune@plt>
cmp    $0x0,%rax
je     1214 <test_fork+0xa4>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x45,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <check_dune@plt>
cmp    $0x0,%rax
je     123b <test_fork+0xcb>
movl   $0x4,-0x4(%rbp)
jmp    128c <test_fork+0x11c>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1263 <test_fork+0xf3>
mov    $0x5,%edi
lea    0xda2(%rip),%rsi
call   1040 <err@plt>
mov    -0xc(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x45,%eax
je     1280 <test_fork+0x110>
movl   $0x6,-0x4(%rbp)
jmp    128c <test_fork+0x11c>
jmp    1285 <test_fork+0x115>
movl   $0xff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fork>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1030 <dune_enter@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    119d <test_fork+0x2d>
xor    %eax,%eax
call   1050 <check_dune@plt>
mov    %rax,%rcx
mov    $0x2,%eax
test   %rcx,%rcx
je     11a3 <test_fork+0x33>
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11df <test_fork+0x6f>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
jne    11e6 <test_fork+0x76>
call   1030 <dune_enter@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    119d <test_fork+0x2d>
xor    %eax,%eax
call   1050 <check_dune@plt>
test   %rax,%rax
jne    123a <test_fork+0xca>
mov    $0x45,%edi
call   1080 <exit@plt>
mov    $0x3,%eax
jmp    119d <test_fork+0x2d>
call   1050 <check_dune@plt>
mov    %rax,%rcx
mov    $0x4,%eax
test   %rcx,%rcx
jne    119d <test_fork+0x2d>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    121c <test_fork+0xac>
lea    0xdee(%rip),%rsi
mov    $0x5,%edi
call   1040 <err@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x45,%eax
mov    $0xff,%ecx
mov    $0x6,%eax
cmove  %ecx,%eax
jmp    119d <test_fork+0x2d>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"waitpid()\00"
define i32 @test_fork() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @dune_enter()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %33
5: ; preds = %0
%6 = tail call i64 (...) @check_dune()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %33
8: ; preds = %5
%9 = tail call i32 (...) @fork()
switch i32 %9, label %20 [
i32 -1, label %33
i32 0, label %10
]
10: ; preds = %8
%11 = tail call i64 (...) @dune_enter()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = tail call i64 (...) @check_dune()
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = tail call i32 @exit(i32 69)
unreachable
20: ; preds = %8
%21 = tail call i64 (...) @check_dune()
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = call i32 @waitpid(i32 %9, i32* %1, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @err(i32 5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = load i32, i32* %1
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = icmp eq i32 %30, 69
%32 = select i1 %31, i32 255, i32 6
br label %33
33: ; preds = %28, %20, %10, %8, %5, %0
%34 = phi i32 [ 1, %0 ], [ 2, %5 ], [ 3, %8 ], [ 1, %10 ], [ 4, %20 ], [ %32, %28 ]
ret i32 %34
}