#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 errno;
scalar_t__ fstat(int, struct stat *);
int getpid();
int /*<<< orphan*/ handle_error(char *, char const *);
int fd_warn_permissions(const char *path, int fd) {
  struct stat st;
  if (fstat(fd, &st) < 0)
    return -errno;
  if (st.st_mode & 0111)
    handle_error("Configuration file %s is marked executable. Please remove "
                 "executable permission bits. Proceeding anyway.",
                 path);
  if (st.st_mode & 0002)
    handle_error("Configuration file %s is marked world-writable. Please "
                 "remove world writability permission bits. Proceeding anyway.",
                 path);
  if (getpid() == 1 && (st.st_mode & 0044) != 0044)
    handle_error("Configuration file %s is marked world-inaccessible. This has "
                 "no effect as configuration data is accessible via APIs "
                 "without restrictions. Proceeding anyway.",
                 path);
  return 0;
}


x86 assembly (clang, -O0):
<fd_warn_permissions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <fstat@plt>
cmp    $0x0,%rax
jge    1168 <fd_warn_permissions+0x38>
xor    %eax,%eax
mov    0x2e82(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <fd_warn_permissions+0xac>
mov    -0x18(%rbp),%eax
and    $0x49,%eax
cmp    $0x0,%eax
je     1187 <fd_warn_permissions+0x57>
mov    -0x10(%rbp),%rsi
lea    0xe7e(%rip),%rdi
call   1050 <handle_error@plt>
mov    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11a6 <fd_warn_permissions+0x76>
mov    -0x10(%rbp),%rsi
lea    0xec8(%rip),%rdi
call   1050 <handle_error@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
cmp    $0x1,%eax
jne    11d5 <fd_warn_permissions+0xa5>
mov    -0x18(%rbp),%eax
and    $0x24,%eax
cmp    $0x24,%eax
je     11d5 <fd_warn_permissions+0xa5>
mov    -0x10(%rbp),%rsi
lea    0xf0d(%rip),%rdi
call   1050 <handle_error@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_warn_permissions>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%eax
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1040 <fstat@plt>
test   %rax,%rax
js     11a1 <fd_warn_permissions+0x71>
mov    0x8(%rsp),%ebx
test   $0x49,%bl
je     1166 <fd_warn_permissions+0x36>
lea    0xea2(%rip),%rdi
mov    %r14,%rsi
call   1050 <handle_error@plt>
test   $0x2,%bl
je     117a <fd_warn_permissions+0x4a>
lea    0xef7(%rip),%rdi
mov    %r14,%rsi
call   1050 <handle_error@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0x1,%eax
jne    11ac <fd_warn_permissions+0x7c>
and    $0x24,%ebx
cmp    $0x24,%ebx
je     11ac <fd_warn_permissions+0x7c>
lea    0xf46(%rip),%rdi
mov    %r14,%rsi
call   1050 <handle_error@plt>
jmp    11ac <fd_warn_permissions+0x7c>
mov    0x2e38(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [105 x i8] c"Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.\00"
@.str.1 = [116 x i8] c"Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.\00"
@.str.2 = [157 x i8] c"Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.\00"
define i32 @fd_warn_permissions(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = call i64 @fstat(i32 %7, %struct.stat* %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
store i32 %12, i32* %3
br label %41
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, 73
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 @handle_error(i8* getelementptr inbounds ([105 x i8], [105 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %13
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, 2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = call i32 @handle_error(i8* getelementptr inbounds ([116 x i8], [116 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %21
%30 = call i32 (...) @getpid()
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%34 = load i32, i32* %33
%35 = and i32 %34, 36
%36 = icmp ne i32 %35, 36
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i8*, i8** %4
%39 = call i32 @handle_error(i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %37, %32, %29
store i32 0, i32* %3
br label %41
41: ; preds = %40, %10
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [105 x i8] c"Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.\00"
@.str.1 = [116 x i8] c"Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.\00"
@.str.2 = [157 x i8] c"Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.\00"
define i32 @fd_warn_permissions(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @fstat(i32 %1, %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %30
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = and i32 %12, 73
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = tail call i32 @handle_error(i8* getelementptr inbounds ([105 x i8], [105 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %15, %10
%18 = and i32 %12, 2
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @handle_error(i8* getelementptr inbounds ([116 x i8], [116 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %20, %17
%23 = tail call i32 (...) @getpid()
%24 = icmp ne i32 %23, 1
%25 = and i32 %12, 36
%26 = icmp eq i32 %25, 36
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = tail call i32 @handle_error(i8* getelementptr inbounds ([157 x i8], [157 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %22, %28, %7
%31 = phi i32 [ %9, %7 ], [ 0, %28 ], [ 0, %22 ]
ret i32 %31
}