#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;
  int st_size;
};

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int S_IFMT;
int S_IFREG;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
int openr(const char *filename) {
  int fd, oerrno;
  struct stat s;
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return (fd);
  if (fstat(fd, &s) < 0) {
    oerrno = errno;
    close(fd);
    errno = oerrno;
    return (-1);
  }
  if ((s.st_mode & S_IFMT) != S_IFREG) {
    close(fd);
    errno = EINVAL;
    return (-1);
  }
  if (s.st_size > 65536) {
    close(fd);
    errno = EINVAL;
    return (-1);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<openr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1166 <openr+0x36>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <openr+0xf2>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
jge    11a8 <openr+0x78>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2e2e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <openr+0xf2>
mov    -0x20(%rbp),%eax
mov    0x2e1e(%rip),%rcx
and    (%rcx),%eax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%eax
je     11e9 <openr+0xb9>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
mov    0x2ded(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <openr+0xf2>
cmpl   $0x10000,-0x1c(%rbp)
jle    121c <openr+0xec>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dba(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <openr+0xf2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openr>:
push   %rbp
push   %rbx
push   %rax
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a7 <openr+0x77>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <fstat@plt>
test   %rax,%rax
js     1189 <openr+0x59>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
and    (%rsp),%eax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%eax
jne    1177 <openr+0x47>
cmpl   $0x10001,0x4(%rsp)
jl     11a7 <openr+0x77>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ebp
jmp    1199 <openr+0x69>
mov    0x2e38(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e28(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
@S_IFMT = global i32 0
@S_IFREG = global i32 0
@EINVAL = global i32 0
define i32 @openr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %44
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i64 @fstat(i32 %15, %struct.stat* %6)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @errno
store i32 %19, i32* %5
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %5
store i32 %22, i32* @errno
store i32 -1, i32* %2
br label %44
23: ; preds = %14
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @S_IFMT
%27 = and i32 %25, %26
%28 = load i32, i32* @S_IFREG
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* @EINVAL
store i32 %33, i32* @errno
store i32 -1, i32* %2
br label %44
34: ; preds = %23
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%36 = load i32, i32* %35
%37 = icmp sgt i32 %36, 65536
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @EINVAL
store i32 %41, i32* @errno
store i32 -1, i32* %2
br label %44
42: ; preds = %34
%43 = load i32, i32* %4
store i32 %43, i32* %2
br label %44
44: ; preds = %42, %38, %30, %18, %12
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
@S_IFMT = global i32 0
@S_IFREG = global i32 0
@EINVAL = global i32 0
define i32 @openr(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %32, label %7
7: ; preds = %1
%8 = call i64 @fstat(i32 %5, %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = tail call i32 @close(i32 %5)
br label %30
13: ; preds = %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @S_IFMT
%17 = and i32 %16, %15
%18 = load i32, i32* @S_IFREG
%19 = icmp eq i32 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = tail call i32 @close(i32 %5)
%22 = load i32, i32* @EINVAL
br label %30
23: ; preds = %13
%24 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%25 = load i32, i32* %24
%26 = icmp sgt i32 %25, 65536
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = tail call i32 @close(i32 %5)
%29 = load i32, i32* @EINVAL
br label %30
30: ; preds = %10, %20, %27
%31 = phi i32 [ %29, %27 ], [ %22, %20 ], [ %11, %10 ]
store i32 %31, i32* @errno
br label %32
32: ; preds = %30, %23, %1
%33 = phi i32 [ %5, %1 ], [ %5, %23 ], [ -1, %30 ]
ret i32 %33
}