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

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int);
int S_IWOTH;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t testSafe(char *ifaceConfig, int fd) {
  struct stat sb;
  if (fstat(fd, &sb)) {
    fprintf(stderr, "failed to stat %s: %s\n", ifaceConfig, strerror(errno));
    exit(1);
  }
  if (!S_ISREG(sb.st_mode)) {
    fprintf(stderr, "%s is not a normal file\n", ifaceConfig);
    exit(1);
  }
  if (sb.st_uid) {
    fprintf(stderr, "%s should be owned by root\n", ifaceConfig);
    exit(1);
  }
  if (sb.st_mode & S_IWOTH) {
    fprintf(stderr, "%s should not be world writeable\n", ifaceConfig);
    exit(1);
  }
  return sb.st_size;
}


x86 assembly (clang, -O0):
<testSafe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     11b9 <testSafe+0x69>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11ef <testSafe+0x9f>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     121f <testSafe+0xcf>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%eax
mov    0x2d9f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1259 <testSafe+0x109>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testSafe>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%eax
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
jne    1197 <testSafe+0x47>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1040 <S_ISREG@plt>
test   %eax,%eax
je     11ce <testSafe+0x7e>
cmpq   $0x0,0x10(%rsp)
jne    11e0 <testSafe+0x90>
mov    0x2e41(%rip),%rax
test   %ebp,(%rax)
jne    11f2 <testSafe+0xa2>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e42(%rip),%rax
mov    (%rax),%ebp
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe4b(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
jmp    1202 <testSafe+0xb2>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
jmp    1202 <testSafe+0xb2>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i64, i64 }
@stderr = global i32 0
@.str = [23 x i8] c"failed to stat %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"%s is not a normal file\0A\00"
@.str.2 = [28 x i8] c"%s should be owned by root\0A\00"
@S_IWOTH = global i32 0
@.str.3 = [34 x i8] c"%s should not be world writeable\0A\00"
define i64 @testSafe(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = call i64 @fstat(i32 %6, %struct.stat* %5)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %3
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @S_ISREG(i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %3
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 2
%28 = load i64, i64* %27
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %3
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
%36 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @S_IWOTH
%39 = and i32 %37, %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %3
%44 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %35
%47 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%48 = load i64, i64* %47
ret i64 %48
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i64 }
@stderr = global i32 0
@.str = [23 x i8] c"failed to stat %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"%s is not a normal file\0A\00"
@.str.2 = [28 x i8] c"%s should be owned by root\0A\00"
@S_IWOTH = global i32 0
@.str.3 = [34 x i8] c"%s should not be world writeable\0A\00"
define i64 @testSafe(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 eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @S_ISREG(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load i32, i32* @S_IWOTH
%32 = and i32 %31, %15
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%40 = load i64, i64* %39
ret i64 %40
}