#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;
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ S_ISREG(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
scalar_t__ getuid();
scalar_t__ stat(char const *, struct stat *);
void config_check_password_file(const char *path) {
  struct stat sb;
  if (stat(path, &sb) != 0) {
    if (errno == ENOENT) {
      return;
    } else {
      err(EXIT_FAILURE, "can't access %s", path);
    }
  }
  if (!S_ISREG(sb.st_mode)) {
    errx(EXIT_FAILURE, "%s is not a regular file", path);
  }
  if (sb.st_uid != 0 && sb.st_uid != getuid()) {
    errx(EXIT_FAILURE, "bad owner on %s", path);
  }
  if ((sb.st_mode & 022) != 0) {
    errx(EXIT_FAILURE, "bad permissions on %s", path);
  }
}


x86 assembly (clang, -O0):
<config_check_password_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11b0 <config_check_password_file+0x60>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    1192 <config_check_password_file+0x42>
jmp    1245 <config_check_password_file+0xf5>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe5a(%rip),%rsi
call   1070 <err@plt>
jmp    11b0 <config_check_password_file+0x60>
mov    -0x18(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11da <config_check_password_file+0x8a>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3b(%rip),%rsi
call   1040 <errx@plt>
cmpq   $0x0,-0x10(%rbp)
je     121d <config_check_password_file+0xcd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     121d <config_check_password_file+0xcd>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   1040 <errx@plt>
mov    -0x18(%rbp),%eax
and    $0x12,%eax
cmp    $0x0,%eax
je     1245 <config_check_password_file+0xf5>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdf9(%rip),%rsi
call   1040 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_check_password_file>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     119b <config_check_password_file+0x4b>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
je     120d <config_check_password_file+0xbd>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %r14,%rdx
call   1070 <err@plt>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
jne    11c0 <config_check_password_file+0x70>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
mov    %r14,%rdx
call   1040 <errx@plt>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     11ee <config_check_password_file+0x9e>
xor    %eax,%eax
call   1030 <getuid@plt>
cmp    %rax,%rbx
je     11ee <config_check_password_file+0x9e>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    %r14,%rdx
call   1040 <errx@plt>
testb  $0x12,0x8(%rsp)
je     120d <config_check_password_file+0xbd>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    %r14,%rdx
call   1040 <errx@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@ENOENT = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"can't access %s\00"
@.str.1 = [25 x i8] c"%s is not a regular file\00"
@.str.2 = [16 x i8] c"bad owner on %s\00"
@.str.3 = [22 x i8] c"bad permissions on %s\00"
define void @config_check_password_file(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @stat(i8* %4, %struct.stat* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7
br label %48
12: ; preds = %7
%13 = load i32, i32* @EXIT_FAILURE
%14 = load i8*, i8** %2
%15 = call i32 @err(i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %12
br label %17
17: ; preds = %16, %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISREG(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i32, i32* @EXIT_FAILURE
%24 = load i8*, i8** %2
%25 = call i32 @errx(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %22, %17
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%28 = load i64, i64* %27
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%32 = load i64, i64* %31
%33 = call i64 (...) @getuid()
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* @EXIT_FAILURE
%37 = load i8*, i8** %2
%38 = call i32 @errx(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %30, %26
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, 18
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @EXIT_FAILURE
%46 = load i8*, i8** %2
%47 = call i32 @errx(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %11, %44, %39
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@errno = global i64 0
@ENOENT = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [16 x i8] c"can't access %s\00"
@.str.1 = [25 x i8] c"%s is not a regular file\00"
@.str.2 = [16 x i8] c"bad owner on %s\00"
@.str.3 = [22 x i8] c"bad permissions on %s\00"
define void @config_check_password_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %38, label %10
10: ; preds = %6
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @err(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %13
13: ; preds = %10, %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @S_ISREG(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @errx(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %18, %13
%22 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = call i64 (...) @getuid()
%27 = icmp eq i64 %23, %26
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @errx(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %28, %25, %21
%32 = load i32, i32* %14
%33 = and i32 %32, 18
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @errx(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %38
38: ; preds = %31, %35, %6
ret void
}