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

/* Variables and functions */
int S_ISUID;
int S_IWOTH;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *get_executable_name();
int /*<<< orphan*/ log_error(char *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int check_binary_permissions() {
  char *executable_file = get_executable_name();
  if (executable_file == NULL) {
    return -1;
  }
  struct stat filestat;
  if (stat(executable_file, &filestat) != 0) {
    log_error("Could not stat the executable %s : %s", executable_file,
              strerror(errno));
    free(executable_file);
    return -1;
  }
  if ((filestat.st_mode & S_IWOTH) == S_IWOTH) {
    log_error("The setuid binary should not be writable by others.");
    free(executable_file);
    return -1;
  }
  if ((filestat.st_mode & S_ISUID) == 0) {
    log_error("The setuid binary should be set setuid.");
    free(executable_file);
    return -1;
  }
  free(executable_file);
  return 0;
}


x86 assembly (clang, -O0):
<check_binary_permissions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1060 <get_executable_name@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <check_binary_permissions+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <check_binary_permissions+0x106>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11d0 <check_binary_permissions+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <check_binary_permissions+0x106>
mov    -0x18(%rbp),%eax
mov    0x2dee(%rip),%rcx
and    (%rcx),%eax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
jne    120e <check_binary_permissions+0xbe>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <check_binary_permissions+0x106>
mov    -0x18(%rbp),%eax
mov    0x2dd0(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1246 <check_binary_permissions+0xf6>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <check_binary_permissions+0x106>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_binary_permissions>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <get_executable_name@plt>
test   %rax,%rax
je     1195 <check_binary_permissions+0x45>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     119c <check_binary_permissions+0x4c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <log_error@plt>
jmp    11d2 <check_binary_permissions+0x82>
mov    $0xffffffff,%ebp
jmp    11df <check_binary_permissions+0x8f>
mov    (%rsp),%eax
mov    0x2e22(%rip),%rcx
mov    %eax,%edx
not    %edx
test   %edx,(%rcx)
jne    11b7 <check_binary_permissions+0x67>
lea    0xe71(%rip),%rdi
jmp    11cb <check_binary_permissions+0x7b>
mov    0x2e2a(%rip),%rcx
xor    %ebp,%ebp
test   %eax,(%rcx)
jne    11d7 <check_binary_permissions+0x87>
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1040 <log_error@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [38 x i8] c"Could not stat the executable %s : %s\00"
@errno = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [52 x i8] c"The setuid binary should not be writable by others.\00"
@S_ISUID = global i32 0
@.str.2 = [40 x i8] c"The setuid binary should be set setuid.\00"
define i32 @check_binary_permissions() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = call i8* (...) @get_executable_name()
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %43
8: ; preds = %0
%9 = load i8*, i8** %2
%10 = call i64 @stat(i8* %9, %struct.stat* %3)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %15)
%17 = load i8*, i8** %2
%18 = call i32 @free(i8* %17)
store i32 -1, i32* %1
br label %43
19: ; preds = %8
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IWOTH
%23 = and i32 %21, %22
%24 = load i32, i32* @S_IWOTH
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %2
%29 = call i32 @free(i8* %28)
store i32 -1, i32* %1
br label %43
30: ; preds = %19
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @S_ISUID
%34 = and i32 %32, %33
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%38 = load i8*, i8** %2
%39 = call i32 @free(i8* %38)
store i32 -1, i32* %1
br label %43
40: ; preds = %30
%41 = load i8*, i8** %2
%42 = call i32 @free(i8* %41)
store i32 0, i32* %1
br label %43
43: ; preds = %40, %36, %26, %12, %7
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [38 x i8] c"Could not stat the executable %s : %s\00"
@errno = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [52 x i8] c"The setuid binary should not be writable by others.\00"
@S_ISUID = global i32 0
@.str.2 = [40 x i8] c"The setuid binary should be set setuid.\00"
define i32 @check_binary_permissions() {
%1 = alloca %struct.stat
%2 = tail call i8* (...) @get_executable_name()
%3 = icmp eq i8* %2, null
br i1 %3, label %29, label %4
4: ; preds = %0
%5 = bitcast %struct.stat* %1 to i8*
%6 = call i64 @stat(i8* %2, %struct.stat* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %10)
br label %26
12: ; preds = %4
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @S_IWOTH
%16 = and i32 %15, %14
%17 = icmp eq i32 %16, %15
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %26
20: ; preds = %12
%21 = load i32, i32* @S_ISUID
%22 = and i32 %21, %14
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %20, %24, %18, %8
%27 = phi i32 [ -1, %8 ], [ -1, %18 ], [ -1, %24 ], [ 0, %20 ]
%28 = call i32 @free(i8* %2)
br label %29
29: ; preds = %0, %26
%30 = phi i32 [ %27, %26 ], [ -1, %0 ]
ret i32 %30
}