#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 stat {
  scalar_t__ st_uid;
  int st_mode;
};

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IWGRP;
int S_IWOTH;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ geteuid();
scalar_t__ stat(char const *, struct stat *);
int check_secure(const char *pathname) {
  struct stat statbuf;
  if (stat(pathname, &statbuf) < 0) {
    return 3;
  }
  if (statbuf.st_uid != geteuid()) {
    return 1;
  }
  if (statbuf.st_mode &
      (S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) {
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_secure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    114f <check_secure+0x2f>
movl   $0x3,-0x4(%rbp)
jmp    11da <check_secure+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     117a <check_secure+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    11da <check_secure+0xba>
mov    -0x18(%rbp),%eax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e33(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e42(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e31(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e10(%rip),%rdx
or     (%rdx),%ecx
mov    0x2df7(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e2e(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11d3 <check_secure+0xb3>
movl   $0x2,-0x4(%rbp)
jmp    11da <check_secure+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_secure>:
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     1196 <check_secure+0x76>
mov    (%rsp),%rbx
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    $0x1,%eax
cmp    %rcx,%rbx
jne    119b <check_secure+0x7b>
mov    0x2e8f(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e75(%rip),%rax
or     (%rax),%ecx
mov    0x2e64(%rip),%rax
or     (%rax),%ecx
mov    0x2e43(%rip),%rax
or     (%rax),%ecx
mov    0x2e2a(%rip),%rax
or     (%rax),%ecx
mov    0x2e61(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
test   %ecx,0x8(%rsp)
setne  %al
add    %eax,%eax
jmp    119b <check_secure+0x7b>
mov    $0x3,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@S_IXUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IXOTH = global i32 0
define i32 @check_secure(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 3, i32* %2
br label %35
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%11 = load i64, i64* %10
%12 = call i64 (...) @geteuid()
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 1, i32* %2
br label %35
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IXUSR
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IWGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IXGRP
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IROTH
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IWOTH
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IXOTH
%30 = or i32 %28, %29
%31 = and i32 %17, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %15
store i32 2, i32* %2
br label %35
34: ; preds = %15
store i32 0, i32* %2
br label %35
35: ; preds = %34, %33, %14, %8
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@S_IXUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IXOTH = global i32 0
define i32 @check_secure(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %30, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = call i64 (...) @geteuid()
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %30
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IXUSR
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %15, %14
%17 = load i32, i32* @S_IWGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IXGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IWOTH
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IXOTH
%26 = or i32 %24, %25
%27 = and i32 %26, %13
%28 = icmp eq i32 %27, 0
%29 = select i1 %28, i32 0, i32 2
br label %30
30: ; preds = %11, %6, %1
%31 = phi i32 [ 3, %1 ], [ 1, %6 ], [ %29, %11 ]
ret i32 %31
}