#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int O_NOCTTY;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
int open(char const *, int);
int /*<<< orphan*/ warn(char *, char const *);
int /*<<< orphan*/ warnx(char *, char const *);
int open_and_stat(const char *name) {
  struct stat st;
  int flags = (O_RDONLY | O_NONBLOCK | O_NOCTTY);
  int fd = open(name, flags);
  if (fd < 0) {
    warn("Failed to open file %s", name);
    return -1;
  }
  if (fstat(fd, &st) != 0) {
    warnx("%s is too small - ignored\n", name);
    close(fd);
    return -1;
  }
  if (!S_ISREG(st.st_mode)) {
    warnx("%s is not a directory or a regular file - ignored\n", name);
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_and_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    0x2e6c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11bf <open_and_stat+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <open_and_stat+0xd4>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11f9 <open_and_stat+0x99>
mov    -0x10(%rbp),%rsi
lea    0xe37(%rip),%rdi
call   1040 <warnx@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <open_and_stat+0xd4>
mov    -0x18(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    122e <open_and_stat+0xce>
mov    -0x10(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1040 <warnx@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <open_and_stat+0xd4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_and_stat>:
push   %r14
push   %rbx
push   %rax
mov    0x2e75(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e50(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11b4 <open_and_stat+0x54>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11d2 <open_and_stat+0x72>
lea    0xe74(%rip),%rdi
mov    %r14,%rsi
call   1040 <warnx@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11c3 <open_and_stat+0x63>
lea    0xe45(%rip),%rdi
mov    %r14,%rsi
call   1030 <warn@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
jne    11c8 <open_and_stat+0x68>
lea    0xe4d(%rip),%rdi
jmp    11a3 <open_and_stat+0x43>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@.str = [23 x i8] c"Failed to open file %s\00"
@.str.1 = [27 x i8] c"%s is too small - ignored\0A\00"
@.str.2 = [51 x i8] c"%s is not a directory or a regular file - ignored\0A\00"
define i32 @open_and_stat(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
store i32 %11, i32* %5
%12 = load i8*, i8** %3
%13 = load i32, i32* %5
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32 @warn(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %2
br label %41
20: ; preds = %1
%21 = load i32, i32* %6
%22 = call i64 @fstat(i32 %21, %struct.stat* %4)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = call i32 @warnx(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %2
br label %41
29: ; preds = %20
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISREG(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %29
%35 = load i8*, i8** %3
%36 = call i32 @warnx(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %2
br label %41
39: ; preds = %29
%40 = load i32, i32* %6
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %34, %24, %17
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
@.str = [23 x i8] c"Failed to open file %s\00"
@.str.1 = [27 x i8] c"%s is too small - ignored\0A\00"
@.str.2 = [51 x i8] c"%s is not a directory or a regular file - ignored\0A\00"
define i32 @open_and_stat(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @warn(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %27
13: ; preds = %1
%14 = call i64 @fstat(i32 %9, %struct.stat* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @warnx(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = tail call i32 @close(i32 %9)
br label %27
19: ; preds = %13
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @S_ISREG(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = tail call i32 @warnx(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%26 = tail call i32 @close(i32 %9)
br label %27
27: ; preds = %19, %24, %16, %11
%28 = phi i32 [ -1, %11 ], [ -1, %16 ], [ -1, %24 ], [ %9, %19 ]
ret i32 %28
}