#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;
};
typedef int /*<<< orphan*/ pathbuf;

/* Variables and functions */
int PATH_MAX;
int S_IRWXO;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, char *, char *);
scalar_t__ readlink(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
void sanity_check(void) {
  char pathbuf[PATH_MAX];
  struct stat s;
  if (readlink("/proc/self/exe", pathbuf, sizeof(pathbuf)) < 0)
    err(2, "readlink /proc/self/exe failed: ");
  if (stat(pathbuf, &s) < 0)
    err(3, "stat failed: ");
  if ((s.st_mode & S_IRWXO) != 0)
    errx(1,
         "bad permission: others have permissions on %s binary\n\t"
         "suggested fix: chmod o-rwx %s\n",
         "suns", pathbuf);
}


x86 assembly (clang, -O0):
<sanity_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x8(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x20(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x10(%rbp)
mov    %eax,%edx
lea    0xe83(%rip),%rdi
call   1030 <readlink@plt>
cmp    $0x0,%rax
jge    119d <sanity_check+0x5d>
mov    $0x2,%edi
lea    0xe77(%rip),%rsi
call   1060 <err@plt>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    11c5 <sanity_check+0x85>
mov    $0x3,%edi
lea    0xe70(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%eax
mov    0x2e19(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f6 <sanity_check+0xb6>
mov    -0x20(%rbp),%rcx
mov    $0x1,%edi
lea    0xe54(%rip),%rsi
lea    0xea2(%rip),%rdx
call   1040 <errx@plt>
mov    -0x8(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sanity_check>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2ea3(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe96(%rip),%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
test   %rax,%rax
jns    1188 <sanity_check+0x48>
lea    0xe91(%rip),%rsi
mov    $0x2,%edi
call   1060 <err@plt>
lea    -0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jns    11aa <sanity_check+0x6a>
lea    0xe90(%rip),%rsi
mov    $0x3,%edi
call   1060 <err@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x10(%rbp)
je     11d3 <sanity_check+0x93>
lea    0xe7f(%rip),%rsi
lea    0xecd(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
call   1040 <errx@plt>
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [33 x i8] c"readlink /proc/self/exe failed: \00"
@.str.2 = [14 x i8] c"stat failed: \00"
@S_IRWXO = global i32 0
@.str.3 = [85 x i8] c"bad permission: others have permissions on %s binary\0A\09suggested fix: chmod o-rwx %s\0A\00"
@.str.4 = [5 x i8] c"suns\00"
define void @sanity_check() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = call i8* @llvm.stacksave()
store i8* %6, i8** %1
%7 = alloca i8, i64 %5
store i64 %5, i64* %2
%8 = trunc i64 %5 to i32
%9 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err(i32 2, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = call i64 @stat(i8* %7, %struct.stat* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @err(i32 3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @S_IRWXO
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @errx(i32 1, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %7)
br label %26
26: ; preds = %24, %18
%27 = load i8*, i8** %1
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [33 x i8] c"readlink /proc/self/exe failed: \00"
@.str.2 = [14 x i8] c"stat failed: \00"
@S_IRWXO = global i32 0
@.str.3 = [85 x i8] c"bad permission: others have permissions on %s binary\0A\09suggested fix: chmod o-rwx %s\0A\00"
@.str.4 = [5 x i8] c"suns\00"
define void @sanity_check() {
%1 = alloca %struct.stat
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = bitcast %struct.stat* %1 to i8*
%6 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %4, i32 %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = call i64 @stat(i8* %4, %struct.stat* %1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @err(i32 3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IRWXO
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %23
23: ; preds = %21, %15
ret void
}