#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 */
typedef scalar_t__ uid_t;
struct stat {
  scalar_t__ st_uid;
  scalar_t__ st_gid;
  int st_mode;
};
typedef int pid_t;
typedef scalar_t__ gid_t;

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ err_sys(char *);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char const *, struct stat *);
int vn_check_read_permission(const char *filepath) {
  struct stat sb;
  pid_t prog_pid;
  uid_t prog_uid;
  gid_t prog_gid;
  char proc_path[20];
  prog_pid = getpid();
  sprintf(proc_path, "/proc/%d", prog_pid);
  if (stat(proc_path, &sb) < 0) {
    err_sys("[vn_check_read_permission] stat proc_path error");
  }
  prog_uid = sb.st_uid;
  prog_gid = sb.st_gid;
  if (stat(filepath, &sb) < 0) {
    err_sys("[vn_check_read_permission] stat filepath error");
  }
  if (prog_uid == sb.st_uid && (sb.st_mode & S_IRUSR)) {
    return 0;
  }
  if (prog_gid == sb.st_gid && (sb.st_mode & S_IRGRP)) {
    return 0;
  }
  if (sb.st_mode & S_IROTH) {
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<vn_check_read_permission>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xe9c(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    118c <vn_check_read_permission+0x4c>
lea    0xe82(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    11bf <vn_check_read_permission+0x7f>
lea    0xe7f(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11ee <vn_check_read_permission+0xae>
mov    -0x18(%rbp),%eax
mov    0x2df9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ee <vn_check_read_permission+0xae>
movl   $0x0,-0x4(%rbp)
jmp    1245 <vn_check_read_permission+0x105>
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    121d <vn_check_read_permission+0xdd>
mov    -0x18(%rbp),%eax
mov    0x2dda(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     121d <vn_check_read_permission+0xdd>
movl   $0x0,-0x4(%rbp)
jmp    1245 <vn_check_read_permission+0x105>
mov    -0x18(%rbp),%eax
mov    0x2db1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     123e <vn_check_read_permission+0xfe>
movl   $0x0,-0x4(%rbp)
jmp    1245 <vn_check_read_permission+0x105>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vn_check_read_permission>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xea6(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jns    1187 <vn_check_read_permission+0x47>
lea    0xe87(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x8(%rsp),%rbx
mov    0x10(%rsp),%r15
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1050 <stat@plt>
test   %rax,%rax
js     11ac <vn_check_read_permission+0x6c>
cmp    0x8(%rsp),%rbx
je     11bf <vn_check_read_permission+0x7f>
jmp    11d0 <vn_check_read_permission+0x90>
lea    0xe86(%rip),%rdi
call   1030 <err_sys@plt>
cmp    0x8(%rsp),%rbx
jne    11d0 <vn_check_read_permission+0x90>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
test   %ecx,0x18(%rsp)
jne    11f6 <vn_check_read_permission+0xb6>
mov    0x18(%rsp),%ecx
cmp    0x10(%rsp),%r15
jne    11e8 <vn_check_read_permission+0xa8>
mov    0x2dfe(%rip),%rdx
xor    %eax,%eax
test   %ecx,(%rdx)
jne    11f6 <vn_check_read_permission+0xb6>
mov    0x2de9(%rip),%rax
and    (%rax),%ecx
cmp    $0x1,%ecx
sbb    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64, i32 }
@.str = [9 x i8] c"/proc/%d\00"
@.str.1 = [48 x i8] c"[vn_check_read_permission] stat proc_path error\00"
@.str.2 = [47 x i8] c"[vn_check_read_permission] stat filepath error\00"
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @vn_check_read_permission(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca [20 x i8]
store i8* %0, i8** %3
%9 = call i32 (...) @getpid()
store i32 %9, i32* %5
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%14 = call i64 @stat(i8* %13, %struct.stat* %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @err_sys(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %6
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%22 = load i64, i64* %21
store i64 %22, i64* %7
%23 = load i8*, i8** %3
%24 = call i64 @stat(i8* %23, %struct.stat* %4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @err_sys(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %18
%29 = load i64, i64* %6
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%31 = load i64, i64* %30
%32 = icmp eq i64 %29, %31
br i1 %32, label %33, label %40
33: ; preds = %28
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 2
%35 = load i32, i32* %34
%36 = load i32, i32* @S_IRUSR
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 0, i32* %2
br label %60
40: ; preds = %33, %28
%41 = load i64, i64* %7
%42 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%43 = load i64, i64* %42
%44 = icmp eq i64 %41, %43
br i1 %44, label %45, label %52
45: ; preds = %40
%46 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 2
%47 = load i32, i32* %46
%48 = load i32, i32* @S_IRGRP
%49 = and i32 %47, %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
store i32 0, i32* %2
br label %60
52: ; preds = %45, %40
%53 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 2
%54 = load i32, i32* %53
%55 = load i32, i32* @S_IROTH
%56 = and i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %52
store i32 0, i32* %2
br label %60
59: ; preds = %52
store i32 -1, i32* %2
br label %60
60: ; preds = %59, %58, %51, %39
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i64, i64, i32 }
@.str = [9 x i8] c"/proc/%d\00"
@.str.1 = [48 x i8] c"[vn_check_read_permission] stat proc_path error\00"
@.str.2 = [47 x i8] c"[vn_check_read_permission] stat filepath error\00"
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @vn_check_read_permission(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [20 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i64 @stat(i8* %5, %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @err_sys(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%16 = load i64, i64* %15
%17 = call i64 @stat(i8* %0, %struct.stat* %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @err_sys(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i64, i64* %13
%23 = icmp eq i64 %14, %22
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%26 = load i32, i32* %25
%27 = load i32, i32* @S_IRUSR
%28 = and i32 %27, %26
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %24, %21
%31 = load i64, i64* %15
%32 = icmp eq i64 %16, %31
%33 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%34 = load i32, i32* %33
br i1 %32, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @S_IRGRP
%37 = and i32 %36, %34
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %30, %35
%40 = load i32, i32* @S_IROTH
%41 = and i32 %40, %34
%42 = icmp eq i32 %41, 0
%43 = sext i1 %42 to i32
br label %44
44: ; preds = %39, %35, %24
%45 = phi i32 [ 0, %24 ], [ 0, %35 ], [ %43, %39 ]
ret i32 %45
}