#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 */
struct test {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ expect(char *, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void priv_io(int asroot, int injail, struct test *test) {
  int error, fd;
  fd = open("/dev/io", O_RDONLY);
  if (fd < 0)
    error = -1;
  else
    error = 0;
  if (asroot && injail)
    expect("priv_io(asroot, injail)", error, -1, EPERM);
  if (asroot && !injail)
    expect("priv_io(asroot, !injail)", error, 0, 0);
  if (!asroot && injail)
    expect("priv_io(!asroot, injail)", error, -1, EPERM);
  if (!asroot && !injail)
    expect("priv_io(!asroot, !injail)", error, -1, EPERM);
  if (fd != -1)
    close(fd);
}


x86 assembly (clang, -O0):
<priv_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
lea    0xeae(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1170 <priv_io+0x40>
movl   $0xffffffff,-0x14(%rbp)
jmp    1177 <priv_io+0x47>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11a8 <priv_io+0x78>
cmpl   $0x0,-0x8(%rbp)
je     11a8 <priv_io+0x78>
mov    -0x14(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
lea    0xe6a(%rip),%rdi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
cmpl   $0x0,-0x4(%rbp)
je     11cf <priv_io+0x9f>
cmpl   $0x0,-0x8(%rbp)
jne    11cf <priv_io+0x9f>
mov    -0x14(%rbp),%esi
lea    0xe5a(%rip),%rdi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1030 <expect@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1200 <priv_io+0xd0>
cmpl   $0x0,-0x8(%rbp)
je     1200 <priv_io+0xd0>
mov    -0x14(%rbp),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
lea    0xe43(%rip),%rdi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1231 <priv_io+0x101>
cmpl   $0x0,-0x8(%rbp)
jne    1231 <priv_io+0x101>
mov    -0x14(%rbp),%esi
mov    0x2db2(%rip),%rax
mov    (%rax),%ecx
lea    0xe2b(%rip),%rdi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
cmpl   $0xffffffff,-0x18(%rbp)
je     1243 <priv_io+0x113>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<priv_io>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
lea    0xeb5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r14d
mov    %eax,%r15d
sar    $0x1f,%r15d
test   %ebx,%ebx
je     117f <priv_io+0x4f>
test   %ebp,%ebp
je     117f <priv_io+0x4f>
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
lea    0xe96(%rip),%rdi
mov    %r15d,%esi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
test   %ebx,%ebx
je     119a <priv_io+0x6a>
test   %ebp,%ebp
jne    119a <priv_io+0x6a>
lea    0xe92(%rip),%rdi
mov    %r15d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <expect@plt>
test   %ebx,%ebx
jne    11bf <priv_io+0x8f>
test   %ebp,%ebp
je     11bf <priv_io+0x8f>
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
lea    0xe87(%rip),%rdi
mov    %r15d,%esi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
or     %ebx,%ebp
je     11db <priv_io+0xab>
cmp    $0xffffffff,%r14d
je     11fe <priv_io+0xce>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
lea    0xe67(%rip),%rdi
mov    %r15d,%esi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
cmp    $0xffffffff,%r14d
jne    11c9 <priv_io+0x99>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.test = type { i32 }
@.str = [8 x i8] c"/dev/io\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"priv_io(asroot, injail)\00"
@EPERM = global i32 0
@.str.2 = [25 x i8] c"priv_io(asroot, !injail)\00"
@.str.3 = [25 x i8] c"priv_io(!asroot, injail)\00"
@.str.4 = [26 x i8] c"priv_io(!asroot, !injail)\00"
define void @priv_io(i32 %0, i32 %1, %struct.test* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.test*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.test* %2, %struct.test** %6
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %7
br label %15
14: ; preds = %3
store i32 0, i32* %7
br label %15
15: ; preds = %14, %13
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* %7
%23 = load i32, i32* @EPERM
%24 = call i32 @expect(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 -1, i32 %23)
br label %25
25: ; preds = %21, %18, %15
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %7
%33 = call i32 @expect(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 0, i32 0)
br label %34
34: ; preds = %31, %28, %25
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %34
%38 = load i32, i32* %5
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* %7
%42 = load i32, i32* @EPERM
%43 = call i32 @expect(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 -1, i32 %42)
br label %44
44: ; preds = %40, %37, %34
%45 = load i32, i32* %4
%46 = icmp ne i32 %45, 0
br i1 %46, label %54, label %47
47: ; preds = %44
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
br i1 %49, label %54, label %50
50: ; preds = %47
%51 = load i32, i32* %7
%52 = load i32, i32* @EPERM
%53 = call i32 @expect(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %51, i32 -1, i32 %52)
br label %54
54: ; preds = %50, %47, %44
%55 = load i32, i32* %8
%56 = icmp ne i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %54
ret void
}

LLVM IR (-O2):
%struct.test = type { i32 }
@.str = [8 x i8] c"/dev/io\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"priv_io(asroot, injail)\00"
@EPERM = global i32 0
@.str.2 = [25 x i8] c"priv_io(asroot, !injail)\00"
@.str.3 = [25 x i8] c"priv_io(!asroot, injail)\00"
@.str.4 = [26 x i8] c"priv_io(!asroot, !injail)\00"
define void @priv_io(i32 %0, i32 %1, %struct.test* nocapture readnone %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = ashr i32 %5, 31
%7 = icmp ne i32 %0, 0
%8 = icmp ne i32 %1, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @EPERM
%12 = tail call i32 @expect(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 -1, i32 %11)
br label %13
13: ; preds = %10, %3
%14 = icmp eq i32 %0, 0
%15 = or i1 %14, %8
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @expect(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %6, i32 0, i32 0)
br label %18
18: ; preds = %16, %13
%19 = and i1 %14, %8
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = load i32, i32* @EPERM
%22 = tail call i32 @expect(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 -1, i32 %21)
br label %23
23: ; preds = %20, %18
%24 = or i32 %1, %0
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @EPERM
%28 = tail call i32 @expect(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %6, i32 -1, i32 %27)
br label %29
29: ; preds = %26, %23
%30 = icmp eq i32 %5, -1
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = tail call i32 @close(i32 %5)
br label %33
33: ; preds = %31, %29
ret void
}