#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, char const *, char const *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fstat(int /*<<< orphan*/, struct stat *);
char *strerror(int /*<<< orphan*/);
bool is_plain_file(FILE *file, const char *filename) {
  struct stat stat;
  int rc = fstat(fileno(file), &stat);
  if (rc != 0) {
    error("%s: %s\n", filename, strerror(errno));
    return false;
  }
  if (S_ISREG(stat.st_mode))
    return true;
  const char *reason =
      S_ISDIR(stat.st_mode) ? "is a directory" : "is not a plain file";
  error("%s: %s\n", filename, reason);
  return false;
}


x86 assembly (clang, -O0):
<is_plain_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c6 <is_plain_file+0x66>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe4b(%rip),%rdi
call   1060 <error@plt>
movl   $0x0,-0x4(%rbp)
jmp    1224 <is_plain_file+0xc4>
mov    -0x20(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11e4 <is_plain_file+0x84>
movl   $0x1,-0x4(%rbp)
jmp    1224 <is_plain_file+0xc4>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rdx
lea    0xe21(%rip),%rax
lea    0xe0b(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xde8(%rip),%rdi
call   1060 <error@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_plain_file>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1040 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %eax,%eax
je     119d <is_plain_file+0x3d>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdx
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
call   1060 <error@plt>
xor    %eax,%eax
jmp    11b4 <is_plain_file+0x54>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1050 <S_ISREG@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
je     11bb <is_plain_file+0x5b>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %ebp,%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
lea    0xe4b(%rip),%rax
lea    0xe35(%rip),%rdx
cmove  %rax,%rdx
jmp    118a <is_plain_file+0x2a>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [8 x i8] c"%s: %s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"is a directory\00"
@.str.2 = [20 x i8] c"is not a plain file\00"
define i32 @is_plain_file(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32*, i32** %4
%10 = call i32 @fileno(i32* %9)
%11 = call i32 @fstat(i32 %10, %struct.stat* %6)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
store i32 0, i32* %3
br label %35
19: ; preds = %2
%20 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISREG(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 1, i32* %3
br label %35
25: ; preds = %19
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISDIR(i32 %27)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0)
store i8* %31, i8** %8
%32 = load i8*, i8** %5
%33 = load i8*, i8** %8
%34 = call i32 @error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %32, i8* %33)
store i32 0, i32* %3
br label %35
35: ; preds = %25, %24, %14
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [8 x i8] c"%s: %s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"is a directory\00"
@.str.2 = [20 x i8] c"is not a plain file\00"
define i32 @is_plain_file(i32* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @fileno(i32* %0)
%6 = call i32 @fstat(i32 %5, %struct.stat* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
br label %20
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @S_ISREG(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = tail call i64 @S_ISDIR(i32 %13)
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0)
br label %20
20: ; preds = %8, %16
%21 = phi i8* [ %19, %16 ], [ %10, %8 ]
%22 = tail call i32 @error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %21)
br label %23
23: ; preds = %20, %11
%24 = phi i32 [ 1, %11 ], [ 0, %20 ]
ret i32 %24
}