#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 S_IFREG;
int access(char const *, int);
int stat(char const *, struct stat *);
int fileAccess(const char *file, int mode) {
  struct {
    struct stat s;
    unsigned short x;
  } fileStat;
  int retVal;
  if ((retVal = stat(file, &fileStat.s)) < 0) {
    return (retVal);
  }
  if ((fileStat.s.st_mode & S_IFREG) == 0) {
    return (1);
  }
  return (access(file, mode));
}


x86 assembly (clang, -O0):
<fileAccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <stat@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1153 <fileAccess+0x33>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1183 <fileAccess+0x63>
mov    -0x20(%rbp),%eax
mov    0x2e8b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1174 <fileAccess+0x54>
movl   $0x1,-0x4(%rbp)
jmp    1183 <fileAccess+0x63>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <access@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileAccess>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1030 <stat@plt>
test   %eax,%eax
js     1157 <fileAccess+0x37>
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
je     1152 <fileAccess+0x32>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <access@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { %struct.stat, i16 }
%struct.stat = type { i32 }
@S_IFREG = global i32 0
define i32 @fileAccess(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.anon
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = getelementptr inbounds %struct.anon, %struct.anon* %6, i32 0, i32 0
%10 = call i32 @stat(i8* %8, %struct.stat* %9)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %7
store i32 %13, i32* %3
br label %26
14: ; preds = %2
%15 = getelementptr inbounds %struct.anon, %struct.anon* %6, i32 0, i32 0
%16 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IFREG
%19 = and i32 %17, %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 1, i32* %3
br label %26
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = call i32 @access(i8* %23, i32 %24)
store i32 %25, i32* %3
br label %26
26: ; preds = %22, %21, %12
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.anon = type { %struct.stat, i16 }
%struct.stat = type { i32 }
@S_IFREG = global i32 0
define i32 @fileAccess(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.anon
%4 = bitcast %struct.anon* %3 to i8*
%5 = getelementptr inbounds %struct.anon, %struct.anon* %3, i64 0, i32 0
%6 = call i32 @stat(i8* %0, %struct.stat* %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = getelementptr inbounds %struct.anon, %struct.anon* %3, i64 0, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @S_IFREG
%12 = and i32 %11, %10
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @access(i8* %0, i32 %1)
br label %16
16: ; preds = %8, %2, %14
%17 = phi i32 [ %15, %14 ], [ %6, %2 ], [ 1, %8 ]
ret i32 %17
}