#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 */
scalar_t__ EACCES;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int _ioperm(int /*<<< orphan*/, int, int);
scalar_t__ errno;
int _iopl(unsigned int level) {
  if (level > 3) {
    __set_errno(EINVAL);
    return -1;
  }
  if (level) {
    int retval = _ioperm(0, 0x10000, 1);
    if (retval < 0 && errno == EACCES)
      __set_errno(EPERM);
    return retval;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_iopl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jbe    114f <_iopl+0x2f>
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <_iopl+0x91>
cmpl   $0x0,-0x8(%rbp)
je     11aa <_iopl+0x8a>
xor    %edi,%edi
mov    $0x10000,%esi
mov    $0x1,%edx
call   1040 <_ioperm@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119f <_iopl+0x7f>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <_iopl+0x7f>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b1 <_iopl+0x91>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_iopl>:
push   %rbx
cmp    $0x3,%edi
jbe    113b <_iopl+0x1b>
mov    $0xffffffff,%ebx
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    1177 <_iopl+0x57>
test   %edi,%edi
je     1175 <_iopl+0x55>
xor    %edi,%edi
mov    $0x10000,%esi
mov    $0x1,%edx
call   1040 <_ioperm@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1177 <_iopl+0x57>
mov    0x2e7b(%rip),%rax
mov    (%rax),%rax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%rax
jne    1177 <_iopl+0x57>
mov    0x2e4d(%rip),%rax
jmp    1132 <_iopl+0x12>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EPERM = global i32 0
define i32 @_iopl(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp ugt i32 %5, 3
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @EINVAL
%9 = call i32 @__set_errno(i32 %8)
store i32 -1, i32* %2
br label %27
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = call i32 @_ioperm(i32 0, i32 65536, i32 1)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EACCES
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @EPERM
%23 = call i32 @__set_errno(i32 %22)
br label %24
24: ; preds = %21, %17, %13
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %27
26: ; preds = %10
store i32 0, i32* %2
br label %27
27: ; preds = %26, %24, %7
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EPERM = global i32 0
define i32 @_iopl(i32 %0) {
%2 = icmp ugt i32 %0, 3
br i1 %2, label %12, label %3
3: ; preds = %1
%4 = icmp eq i32 %0, 0
br i1 %4, label %17, label %5
5: ; preds = %3
%6 = tail call i32 @_ioperm(i32 0, i32 65536, i32 1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EACCES
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %17
12: ; preds = %8, %1
%13 = phi i32* [ @EINVAL, %1 ], [ @EPERM, %8 ]
%14 = phi i32 [ -1, %1 ], [ %6, %8 ]
%15 = load i32, i32* %13
%16 = tail call i32 @__set_errno(i32 %15)
br label %17
17: ; preds = %12, %3, %5, %8
%18 = phi i32 [ %6, %8 ], [ %6, %5 ], [ 0, %3 ], [ %14, %12 ]
ret i32 %18
}