#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 */
typedef scalar_t__ uid_t;
struct stat {
  int st_mode;
  scalar_t__ st_uid;
  int st_nlink;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int);
int S_IWGRP;
int S_IWOTH;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ lstat(char const *, struct stat *);
FILE *iruserfopen(const char *file, uid_t okuser) {
  struct stat st;
  char *cp = NULL;
  FILE *res = NULL;
  if (lstat(file, &st))
    cp = "lstat failed";
  else if (!S_ISREG(st.st_mode))
    cp = "not regular file";
  else {
    res = fopen(file, "r");
    if (!res)
      cp = "cannot open";
    else if (fstat(fileno(res), &st) < 0)
      cp = "fstat failed";
    else if (st.st_uid && st.st_uid != okuser)
      cp = "bad owner";
    else if (st.st_mode & (S_IWGRP | S_IWOTH))
      cp = "writeable by other than owner";
    else if (st.st_nlink > 1)
      cp = "hard linked somewhere";
  }
  if (cp != NULL) {
    if (res)
      fclose(res);
    return NULL;
  }
  return res;
}


x86 assembly (clang, -O0):
<iruserfopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     11a7 <iruserfopen+0x47>
lea    0xe62(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    12b1 <iruserfopen+0x151>
mov    -0x30(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11c8 <iruserfopen+0x68>
lea    0xe4e(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    12ac <iruserfopen+0x14c>
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11f7 <iruserfopen+0x97>
lea    0xe30(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    12a7 <iruserfopen+0x147>
mov    -0x40(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    1225 <iruserfopen+0xc5>
lea    0xe0e(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    12a2 <iruserfopen+0x142>
cmpq   $0x0,-0x28(%rbp)
je     124e <iruserfopen+0xee>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     124e <iruserfopen+0xee>
lea    0xdf2(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    129d <iruserfopen+0x13d>
mov    -0x30(%rbp),%eax
mov    0x2d90(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d6f(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     127e <iruserfopen+0x11e>
lea    0xdcc(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1298 <iruserfopen+0x138>
cmpl   $0x1,-0x20(%rbp)
jle    1293 <iruserfopen+0x133>
lea    0xdd0(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1298 <iruserfopen+0x138>
jmp    129d <iruserfopen+0x13d>
jmp    12a2 <iruserfopen+0x142>
jmp    12a7 <iruserfopen+0x147>
jmp    12ac <iruserfopen+0x14c>
jmp    12b1 <iruserfopen+0x151>
cmpq   $0x0,-0x38(%rbp)
je     12dd <iruserfopen+0x17d>
cmpq   $0x0,-0x40(%rbp)
je     12d0 <iruserfopen+0x170>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e5 <iruserfopen+0x185>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iruserfopen>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %rax,%rax
je     1187 <iruserfopen+0x27>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     117a <iruserfopen+0x1a>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     117a <iruserfopen+0x1a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     11ee <iruserfopen+0x8e>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11d0 <iruserfopen+0x70>
cmp    %r14,%rax
jne    11ee <iruserfopen+0x8e>
mov    0x2e11(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %ecx,(%rsp)
jne    11ee <iruserfopen+0x8e>
cmpl   $0x2,0x10(%rsp)
jl     117c <iruserfopen+0x1c>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    117a <iruserfopen+0x1a>

LLVM IR (-O0):
%struct.stat = type { i32, i64, i32 }
@.str = [13 x i8] c"lstat failed\00"
@.str.1 = [17 x i8] c"not regular file\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [12 x i8] c"cannot open\00"
@.str.4 = [13 x i8] c"fstat failed\00"
@.str.5 = [10 x i8] c"bad owner\00"
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
@.str.6 = [30 x i8] c"writeable by other than owner\00"
@.str.7 = [22 x i8] c"hard linked somewhere\00"
define i32* @iruserfopen(i8* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* null, i8** %7
store i32* null, i32** %8
%9 = load i8*, i8** %4
%10 = call i64 @lstat(i8* %9, %struct.stat* %6)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %7
br label %61
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @S_ISREG(i32 %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %13
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %60
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %59
25: ; preds = %19
%26 = load i32*, i32** %8
%27 = call i32 @fileno(i32* %26)
%28 = call i64 @fstat(i32 %27, %struct.stat* %6)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %7
br label %58
31: ; preds = %25
%32 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%33 = load i64, i64* %32
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%37 = load i64, i64* %36
%38 = load i64, i64* %5
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %35
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8** %7
br label %57
41: ; preds = %35, %31
%42 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%43 = load i32, i32* %42
%44 = load i32, i32* @S_IWGRP
%45 = load i32, i32* @S_IWOTH
%46 = or i32 %44, %45
%47 = and i32 %43, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %41
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8** %7
br label %56
50: ; preds = %41
%51 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%52 = load i32, i32* %51
%53 = icmp sgt i32 %52, 1
br i1 %53, label %54, label %55
54: ; preds = %50
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8** %7
br label %55
55: ; preds = %54, %50
br label %56
56: ; preds = %55, %49
br label %57
57: ; preds = %56, %40
br label %58
58: ; preds = %57, %30
br label %59
59: ; preds = %58, %24
br label %60
60: ; preds = %59, %18
br label %61
61: ; preds = %60, %12
%62 = load i8*, i8** %7
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %71
64: ; preds = %61
%65 = load i32*, i32** %8
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32*, i32** %8
%69 = call i32 @fclose(i32* %68)
br label %70
70: ; preds = %67, %64
store i32* null, i32** %3
br label %73
71: ; preds = %61
%72 = load i32*, i32** %8
store i32* %72, i32** %3
br label %73
73: ; preds = %71, %70
%74 = load i32*, i32** %3
ret i32* %74
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i32 }
@.str.2 = [2 x i8] c"r\00"
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32* @iruserfopen(i8* nocapture %0, i64 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %38
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @S_ISREG(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %38, label %12
12: ; preds = %7
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %38, label %15
15: ; preds = %12
%16 = tail call i32 @fileno(i32* %13)
%17 = call i64 @fstat(i32 %16, %struct.stat* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 0
%23 = icmp eq i64 %21, %1
%24 = or i1 %22, %23
br i1 %24, label %25, label %36
25: ; preds = %19
%26 = load i32, i32* %8
%27 = load i32, i32* @S_IWGRP
%28 = load i32, i32* @S_IWOTH
%29 = or i32 %28, %27
%30 = and i32 %29, %26
%31 = icmp eq i32 %30, 0
%32 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%33 = load i32, i32* %32
%34 = icmp slt i32 %33, 2
%35 = select i1 %31, i1 %34, i1 false
br i1 %35, label %38, label %36
36: ; preds = %25, %19, %15
%37 = tail call i32 @fclose(i32* %13)
br label %38
38: ; preds = %25, %2, %7, %12, %36
%39 = phi i32* [ null, %36 ], [ null, %12 ], [ null, %7 ], [ null, %2 ], [ %13, %25 ]
ret i32* %39
}