#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_ISCHR(int /*<<< orphan*/);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ perror(char *);
int ftype(const char *fname) {
  struct stat statres;
  if (lstat(fname, &statres) < 0) {
    perror("stat()");
    exit(1);
  }
  if (S_ISREG(statres.st_mode))
    return '-';
  if (S_ISDIR(statres.st_mode))
    return 'd';
  if (S_ISCHR(statres.st_mode))
    return 'c';
  else
    return '?';
}


x86 assembly (clang, -O0):
<ftype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
jge    1199 <ftype+0x39>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11b7 <ftype+0x57>
movl   $0x2d,-0x4(%rbp)
jmp    11fa <ftype+0x9a>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11d5 <ftype+0x75>
movl   $0x64,-0x4(%rbp)
jmp    11fa <ftype+0x9a>
mov    -0x18(%rbp),%edi
call   1060 <S_ISCHR@plt>
cmp    $0x0,%rax
je     11f3 <ftype+0x93>
movl   $0x63,-0x4(%rbp)
jmp    11fa <ftype+0x9a>
movl   $0x3f,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftype>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1040 <lstat@plt>
test   %rax,%rax
js     11bd <ftype+0x5d>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1050 <S_ISREG@plt>
mov    %rax,%rcx
mov    $0x2d,%eax
test   %rcx,%rcx
jne    11b7 <ftype+0x57>
mov    %ebx,%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
mov    $0x64,%eax
test   %rcx,%rcx
jne    11b7 <ftype+0x57>
mov    %ebx,%edi
call   1060 <S_ISCHR@plt>
test   %rax,%rax
mov    $0x3f,%ecx
mov    $0x63,%eax
cmove  %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"stat()\00"
define i32 @ftype(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 @lstat(i8* %5, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @S_ISREG(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 45, i32* %2
br label %30
17: ; preds = %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @S_ISDIR(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 100, i32* %2
br label %30
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISCHR(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 99, i32* %2
br label %30
29: ; preds = %23
store i32 63, i32* %2
br label %30
30: ; preds = %29, %28, %22, %16
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"stat()\00"
define i32 @ftype(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i64 @S_ISREG(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = tail call i64 @S_ISDIR(i32 %11)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i64 @S_ISCHR(i32 %11)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i32 63, i32 99
br label %21
21: ; preds = %17, %14, %9
%22 = phi i32 [ 45, %9 ], [ 100, %14 ], [ %20, %17 ]
ret i32 %22
}