#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 O_EXCL;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int test_priv() {
  int fd;
  fd = open("/proc/kcore", O_RDONLY | O_EXCL, 0644);
  if (fd == -1) {
    perror("open");
    if (errno == EACCES)
      printf("non-root now...\n");
    close(fd);
    return -1;
  }
  printf("root now...\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<test_priv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
lea    0xe9f(%rip),%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11be <test_priv+0x7e>
lea    0xe8d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <test_priv+0x6a>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <test_priv+0x99>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_priv>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea4(%rip),%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     118b <test_priv+0x4b>
mov    %eax,%ebx
xor    %ebp,%ebp
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    11c0 <test_priv+0x80>
lea    0xe58(%rip),%rdi
mov    $0xffffffff,%ebp
jmp    1176 <test_priv+0x36>
mov    $0xffffffff,%ebp
jmp    117b <test_priv+0x3b>

LLVM IR (-O0):
@.str = [12 x i8] c"/proc/kcore\00"
@O_RDONLY = global i32 0
@O_EXCL = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.2 = [17 x i8] c"non-root now...\0A\00"
@.str.3 = [13 x i8] c"root now...\0A\00"
define i32 @test_priv() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_EXCL
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5, i32 420)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %19
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = load i64, i64* @errno
%12 = load i64, i64* @EACCES
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
store i32 -1, i32* %1
br label %23
19: ; preds = %0
%20 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
store i32 0, i32* %1
br label %23
23: ; preds = %19, %16
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/kcore\00"
@O_RDONLY = global i32 0
@O_EXCL = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i64 0
@EACCES = global i64 0
@str = [12 x i8] c"root now...\00"
@str.4 = [16 x i8] c"non-root now...\00"
define i32 @test_priv() {
%1 = load i32, i32* @O_RDONLY
%2 = load i32, i32* @O_EXCL
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3, i32 420)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = load i64, i64* @errno
%9 = load i64, i64* @EACCES
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %16
11: ; preds = %0, %6
%12 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0), %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %0 ]
%13 = phi i32 [ -1, %6 ], [ %4, %0 ]
%14 = phi i32 [ -1, %6 ], [ 0, %0 ]
%15 = tail call i32 @puts(i8* dereferenceable(1) %12)
br label %16
16: ; preds = %11, %6
%17 = phi i32 [ -1, %6 ], [ %13, %11 ]
%18 = phi i32 [ -1, %6 ], [ %14, %11 ]
%19 = tail call i32 @close(i32 %17)
ret i32 %18
}