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


x86 assembly (clang, -O0):
<smm_fstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
call   1070 <smm_errno_update@plt>
cmpq   $0x0,-0x10(%rbp)
jne    117a <smm_fstat+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <smm_fstat+0xb8>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    11a7 <smm_fstat+0x57>
mov    $0xc0000008,%edi
call   1070 <smm_errno_update@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <smm_fstat+0xb8>
mov    -0x18(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     11c5 <smm_fstat+0x75>
movl   $0x0,-0x4(%rbp)
jmp    1208 <smm_fstat+0xb8>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11e3 <smm_fstat+0x93>
movl   $0x1,-0x4(%rbp)
jmp    1208 <smm_fstat+0xb8>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1201 <smm_fstat+0xb1>
movl   $0x3,-0x4(%rbp)
jmp    1208 <smm_fstat+0xb8>
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,%rbp
xor    %edi,%edi
call   1070 <smm_errno_update@plt>
mov    $0xffffffff,%ebx
test   %rbp,%rbp
je     11b8 <smm_fstat+0x68>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1050 <lstat@plt>
test   %rax,%rax
js     11ae <smm_fstat+0x5e>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1060 <S_ISREG@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11b8 <smm_fstat+0x68>
mov    %ebp,%edi
call   1040 <S_ISDIR@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11b8 <smm_fstat+0x68>
mov    %ebp,%edi
call   1030 <S_ISLNK@plt>
cmp    $0x1,%rax
mov    $0x3,%ebx
sbb    $0x0,%ebx
jmp    11b8 <smm_fstat+0x68>
mov    $0xc0000008,%edi
call   1070 <smm_errno_update@plt>
mov    %ebx,%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 %34
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @lstat(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 %34
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 %34
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 %34
27: ; preds = %21
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISLNK(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 3, i32* %2
br label %34
33: ; preds = %27
store i32 2, i32* %2
br label %34
34: ; preds = %33, %32, %26, %20, %13, %8
%35 = load i32, i32* %2
ret i32 %35
}

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 %23, label %6
6: ; preds = %1
%7 = call i64 @lstat(i8* %0, %struct.stat* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @smm_errno_update(i32 -1073741816)
br label %23
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, 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
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = tail call i64 @S_ISLNK(i32 %13)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i32 2, i32 3
br label %23
23: ; preds = %19, %16, %11, %1, %9
%24 = phi i32 [ -1, %9 ], [ -1, %1 ], [ 0, %11 ], [ 1, %16 ], [ %22, %19 ]
ret i32 %24
}