#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__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ failmsg(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
void expect_crash_status(int status) {
  if (WIFEXITED(status)) {
    if (WEXITSTATUS(status)) {
      printf("Subprocess exited with %d status: "
             "\e[0;31m"
             "FAIL"
             "\e[m"
             "\n",
             WEXITSTATUS(status));
      failmsg("Expected sandbox violation, got exit() with error status: "
              "\e[0;31m"
              "FAIL"
              "\e[m");
    } else {
      failmsg("Program worked but sandbox should have prevented it: "
              "\e[0;31m"
              "FAIL"
              "\e[m");
    }
  } else if (WIFSIGNALED(status)) {
    puts("Sandbox violation correctly caught: "
         "\e[0;32m"
         "OK"
         "\e[m");
  } else {
    errx(1, "Subprocess neither exited nor stopped: "
            "\e[0;31m"
            "BROKEN"
            "\e[m");
  }
}


x86 assembly (clang, -O0):
<expect_crash_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11d6 <expect_crash_status+0x66>
mov    -0x4(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     11c5 <expect_crash_status+0x55>
mov    -0x4(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
lea    0xe77(%rip),%rdi
call   1090 <failmsg@plt>
jmp    11d1 <expect_crash_status+0x61>
lea    0xeaf(%rip),%rdi
call   1090 <failmsg@plt>
jmp    120f <expect_crash_status+0x9f>
mov    -0x4(%rbp),%edi
call   1060 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11f9 <expect_crash_status+0x89>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
jmp    120a <expect_crash_status+0x9a>
mov    $0x1,%edi
lea    0xeeb(%rip),%rsi
call   1040 <errx@plt>
jmp    120f <expect_crash_status+0x9f>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expect_crash_status>:
push   %rbx
mov    %edi,%ebx
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11aa <expect_crash_status+0x3a>
mov    %ebx,%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     11c3 <expect_crash_status+0x53>
mov    %ebx,%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe8e(%rip),%rdi
pop    %rbx
jmp    1090 <failmsg@plt>
mov    %ebx,%edi
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     11d0 <expect_crash_status+0x60>
lea    0xf02(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>
lea    0xeb1(%rip),%rdi
pop    %rbx
jmp    1090 <failmsg@plt>
lea    0xf19(%rip),%rsi
mov    $0x1,%edi
pop    %rbx
jmp    1040 <errx@plt>

LLVM IR (-O0):
@.str = [50 x i8] c"Subprocess exited with %d status: \1B[0;31mFAIL\1B[m\0A\00"
@.str.1 = [73 x i8] c"Expected sandbox violation, got exit() with error status: \1B[0;31mFAIL\1B[m\00"
@.str.2 = [68 x i8] c"Program worked but sandbox should have prevented it: \1B[0;31mFAIL\1B[m\00"
@.str.3 = [49 x i8] c"Sandbox violation correctly caught: \1B[0;32mOK\1B[m\00"
@.str.4 = [56 x i8] c"Subprocess neither exited nor stopped: \1B[0;31mBROKEN\1B[m\00"
define void @expect_crash_status(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = call i64 @WIFEXITED(i32 %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %1
%7 = load i32, i32* %2
%8 = call i32 @WEXITSTATUS(i32 %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i32, i32* %2
%12 = call i32 @WEXITSTATUS(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 @failmsg(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0))
br label %17
15: ; preds = %6
%16 = call i32 @failmsg(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
br label %27
18: ; preds = %1
%19 = load i32, i32* %2
%20 = call i64 @WIFSIGNALED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %26
24: ; preds = %18
%25 = call i32 @errx(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %22
br label %27
27: ; preds = %26, %17
ret void
}

LLVM IR (-O2):
@.str = [50 x i8] c"Subprocess exited with %d status: \1B[0;31mFAIL\1B[m\0A\00"
@.str.1 = [73 x i8] c"Expected sandbox violation, got exit() with error status: \1B[0;31mFAIL\1B[m\00"
@.str.2 = [68 x i8] c"Program worked but sandbox should have prevented it: \1B[0;31mFAIL\1B[m\00"
@.str.3 = [49 x i8] c"Sandbox violation correctly caught: \1B[0;32mOK\1B[m\00"
@.str.4 = [56 x i8] c"Subprocess neither exited nor stopped: \1B[0;31mBROKEN\1B[m\00"
define void @expect_crash_status(i32 %0) {
%2 = tail call i64 @WIFEXITED(i32 %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = tail call i32 @WEXITSTATUS(i32 %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = tail call i32 @WEXITSTATUS(i32 %0)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @failmsg(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.1, i64 0, i64 0))
br label %20
11: ; preds = %4
%12 = tail call i32 @failmsg(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
br label %20
13: ; preds = %1
%14 = tail call i64 @WIFSIGNALED(i32 %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %20
18: ; preds = %13
%19 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %16, %18, %7, %11
ret void
}