#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_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ smm_errno_update(int);
scalar_t__ stat(char *, struct stat *);
int smm_fstat(char *fname) {
  struct stat fs;
  smm_errno_update(0);
  if (fname == NULL) {
    return -1;
  }
  if (stat(fname, &fs) < 0) {
    smm_errno_update((0xC0000000 | (8)));
    return -1;
  }
  if (S_ISREG(fs.st_mode)) {
    return 0;
  }
  if (S_ISDIR(fs.st_mode)) {
    return 1;
  }
  return 2;
}


x86 assembly (clang, -O0):
<smm_fstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
call   1060 <smm_errno_update@plt>
cmpq   $0x0,-0x10(%rbp)
jne    116a <smm_fstat+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <smm_fstat+0x9a>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    1197 <smm_fstat+0x57>
mov    $0xc0000008,%edi
call   1060 <smm_errno_update@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <smm_fstat+0x9a>
mov    -0x18(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11b5 <smm_fstat+0x75>
movl   $0x0,-0x4(%rbp)
jmp    11da <smm_fstat+0x9a>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11d3 <smm_fstat+0x93>
movl   $0x1,-0x4(%rbp)
jmp    11da <smm_fstat+0x9a>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smm_fstat>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %edi,%edi
call   1060 <smm_errno_update@plt>
mov    $0xffffffff,%ebp
test   %rbx,%rbx
je     1196 <smm_fstat+0x56>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     118c <smm_fstat+0x4c>
mov    (%rsp),%edi
call   1050 <S_ISREG@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1196 <smm_fstat+0x56>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x1,%rax
mov    $0x1,%ebp
adc    $0x0,%ebp
jmp    1196 <smm_fstat+0x56>
mov    $0xc0000008,%edi
call   1060 <smm_errno_update@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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