#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_IFMT;
int S_IFREG;
int stat(char const *, struct stat *);
int is_file(const char *filename) {
  struct stat sb;
  if (filename == NULL)
    return 0;
  if (stat(filename, &sb) == -1)
    return 0;
  if ((sb.st_mode & S_IFMT) == S_IFREG)
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<is_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1133 <is_file+0x23>
movl   $0x0,-0x4(%rbp)
jmp    1183 <is_file+0x73>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    1155 <is_file+0x45>
movl   $0x0,-0x4(%rbp)
jmp    1183 <is_file+0x73>
mov    -0x18(%rbp),%eax
mov    0x2e79(%rip),%rcx
and    (%rcx),%eax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%eax
jne    117c <is_file+0x6c>
movl   $0x1,-0x4(%rbp)
jmp    1183 <is_file+0x73>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_file>:
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
test   %rdi,%rdi
je     1146 <is_file+0x36>
lea    0x8(%rsp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     1146 <is_file+0x36>
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2ea9(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%eax
sete   %bl
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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