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

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ errno;
int fstat(int, struct stat *);
scalar_t__ getuid();
int /*<<< orphan*/ log_error(char *, char *);
int /*<<< orphan*/ log_print(char *, char *);
int check_file_secrecy_fd(int fd, char *name, size_t *file_size) {
  struct stat st;
  if (fstat(fd, &st) == -1) {
    log_error("check_file_secrecy: stat (\"%s\") failed", name);
    return -1;
  }
  if (st.st_uid != 0 && st.st_uid != getuid()) {
    log_print("check_file_secrecy_fd: "
              "not loading %s - file owner is not process user",
              name);
    errno = EPERM;
    return -1;
  }
  if (file_size)
    *file_size = (size_t)st.st_size;
  return 0;
}


x86 assembly (clang, -O0):
<check_file_secrecy_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1184 <check_file_secrecy_fd+0x44>
mov    -0x10(%rbp),%rsi
lea    0xe8d(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <check_file_secrecy_fd+0xb9>
cmpq   $0x0,-0x28(%rbp)
je     11dc <check_file_secrecy_fd+0x9c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     11dc <check_file_secrecy_fd+0x9c>
mov    -0x10(%rbp),%rsi
lea    0xe6e(%rip),%rdi
call   1050 <log_print@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <check_file_secrecy_fd+0xb9>
cmpq   $0x0,-0x18(%rbp)
je     11f2 <check_file_secrecy_fd+0xb2>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_file_secrecy_fd>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rsp,%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     1182 <check_file_secrecy_fd+0x42>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1171 <check_file_secrecy_fd+0x31>
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    %rax,%rbx
jne    1193 <check_file_secrecy_fd+0x53>
xor    %eax,%eax
test   %r15,%r15
je     11b9 <check_file_secrecy_fd+0x79>
mov    0x8(%rsp),%rcx
mov    %rcx,(%r15)
jmp    11b9 <check_file_secrecy_fd+0x79>
lea    0xe77(%rip),%rdi
mov    %r14,%rsi
call   1030 <log_error@plt>
jmp    11b4 <check_file_secrecy_fd+0x74>
lea    0xe8d(%rip),%rdi
mov    %r14,%rsi
call   1050 <log_print@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    0x2e2e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@.str = [39 x i8] c"check_file_secrecy: stat (\22%s\22) failed\00"
@.str.1 = [71 x i8] c"check_file_secrecy_fd: not loading %s - file owner is not process user\00"
@EPERM = global i32 0
@errno = global i32 0
define i32 @check_file_secrecy_fd(i32 %0, i8* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca %struct.stat
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64* %2, i64** %7
%9 = load i32, i32* %5
%10 = call i32 @fstat(i32 %9, %struct.stat* %8)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = call i32 @log_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %4
br label %36
15: ; preds = %3
%16 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%21 = load i64, i64* %20
%22 = call i64 (...) @getuid()
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = call i32 @log_print(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EPERM
store i32 %27, i32* @errno
store i32 -1, i32* %4
br label %36
28: ; preds = %19, %15
%29 = load i64*, i64** %7
%30 = icmp ne i64* %29, null
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%33 = load i64, i64* %32
%34 = load i64*, i64** %7
store i64 %33, i64* %34
br label %35
35: ; preds = %31, %28
store i32 0, i32* %4
br label %36
36: ; preds = %35, %24, %12
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@.str = [39 x i8] c"check_file_secrecy: stat (\22%s\22) failed\00"
@.str.1 = [71 x i8] c"check_file_secrecy_fd: not loading %s - file owner is not process user\00"
@EPERM = global i32 0
@errno = global i32 0
define i32 @check_file_secrecy_fd(i32 %0, i8* %1, i64* writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @fstat(i32 %0, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @log_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %25
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i64, i64* %11
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %10
%15 = tail call i64 (...) @getuid()
%16 = icmp eq i64 %12, %15
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32 @log_print(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%19 = load i32, i32* @EPERM
store i32 %19, i32* @errno
br label %25
20: ; preds = %14, %10
%21 = icmp eq i64* %2, null
br i1 %21, label %25, label %22
22: ; preds = %20
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%24 = load i64, i64* %23
store i64 %24, i64* %2
br label %25
25: ; preds = %20, %22, %17, %8
%26 = phi i32 [ -1, %8 ], [ -1, %17 ], [ 0, %22 ], [ 0, %20 ]
ret i32 %26
}