#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 */
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int pjf_isfile(const char *path) {
  struct stat stats;
  if (stat(path, &stats))
    return -1;
  if (S_ISREG(stats.st_mode))
    return 1;
  if (S_ISLNK(stats.st_mode))
    return 2;
  return -2;
}


x86 assembly (clang, -O0):
<pjf_isfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     115f <pjf_isfile+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <pjf_isfile+0x72>
mov    -0x18(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     117d <pjf_isfile+0x4d>
movl   $0x1,-0x4(%rbp)
jmp    11a2 <pjf_isfile+0x72>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     119b <pjf_isfile+0x6b>
movl   $0x2,-0x4(%rbp)
jmp    11a2 <pjf_isfile+0x72>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pjf_isfile>:
push   %rax
mov    %rsp,%rsi
call   1040 <stat@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1148 <pjf_isfile+0x18>
pop    %rcx
ret
mov    (%rsp),%edi
call   1050 <S_ISREG@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    1146 <pjf_isfile+0x16>
mov    (%rsp),%edi
call   1030 <S_ISLNK@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    -0x2(,%rcx,4),%eax
pop    %rcx
ret

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

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