#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_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISFIFO(int /*<<< orphan*/);
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ S_ISSOCK(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
const char *file_type(const char *path) {
  struct stat s;
  if (lstat(path, &s) < 0) {
    fprintf(stderr, "Unable to get info for %s: ", path);
    perror(NULL);
    return NULL;
  }
  if (S_ISREG(s.st_mode)) {
    return "regular file";
  } else if (S_ISDIR(s.st_mode)) {
    return "directory";
  } else if (S_ISCHR(s.st_mode)) {
    return "character device";
  } else if (S_ISBLK(s.st_mode)) {
    return "block device";
  } else if (S_ISSOCK(s.st_mode)) {
    return "socket";
  } else if (S_ISFIFO(s.st_mode)) {
    return "fifo (named pipe)";
  } else if (S_ISLNK(s.st_mode)) {
    return "symbolic link";
  } else {
    return "unknown file type";
  }
}


x86 assembly (clang, -O0):
<file_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
jge    11f2 <file_type+0x52>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe29(%rip),%rsi
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     1214 <file_type+0x74>
lea    0xe11(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1236 <file_type+0x96>
lea    0xdfc(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   10a0 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1258 <file_type+0xb8>
lea    0xde4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   10b0 <S_ISBLK@plt>
cmp    $0x0,%rax
je     127a <file_type+0xda>
lea    0xdd3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   1050 <S_ISSOCK@plt>
cmp    $0x0,%rax
je     129c <file_type+0xfc>
lea    0xdbe(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   1040 <S_ISFIFO@plt>
cmp    $0x0,%rax
je     12be <file_type+0x11e>
lea    0xda3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     12e0 <file_type+0x140>
lea    0xd93(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <file_type+0x14b>
lea    0xd91(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_type>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <lstat@plt>
test   %rax,%rax
js     1200 <file_type+0x60>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     11d3 <file_type+0x33>
lea    0xe4e(%rip),%rbx
jmp    127e <file_type+0xde>
mov    %ebx,%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     11eb <file_type+0x4b>
lea    0xe43(%rip),%rbx
jmp    127e <file_type+0xde>
mov    %ebx,%edi
call   10a0 <S_ISCHR@plt>
test   %rax,%rax
je     1223 <file_type+0x83>
lea    0xe35(%rip),%rbx
jmp    127e <file_type+0xde>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   10c0 <perror@plt>
jmp    127e <file_type+0xde>
mov    %ebx,%edi
call   10b0 <S_ISBLK@plt>
test   %rax,%rax
je     1238 <file_type+0x98>
lea    0xe0e(%rip),%rbx
jmp    127e <file_type+0xde>
mov    %ebx,%edi
call   1050 <S_ISSOCK@plt>
test   %rax,%rax
je     124d <file_type+0xad>
lea    0xe06(%rip),%rbx
jmp    127e <file_type+0xde>
mov    %ebx,%edi
call   1040 <S_ISFIFO@plt>
test   %rax,%rax
je     1262 <file_type+0xc2>
lea    0xdf8(%rip),%rbx
jmp    127e <file_type+0xde>
mov    %ebx,%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
lea    0xe05(%rip),%rax
lea    0xdf0(%rip),%rbx
cmove  %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"Unable to get info for %s: \00"
@.str.1 = [13 x i8] c"regular file\00"
@.str.2 = [10 x i8] c"directory\00"
@.str.3 = [17 x i8] c"character device\00"
@.str.4 = [13 x i8] c"block device\00"
@.str.5 = [7 x i8] c"socket\00"
@.str.6 = [18 x i8] c"fifo (named pipe)\00"
@.str.7 = [14 x i8] c"symbolic link\00"
@.str.8 = [18 x i8] c"unknown file type\00"
define i8* @file_type(i8* %0) {
%2 = alloca i8*
%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 %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %3
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @perror(i32* null)
store i8* null, i8** %2
br label %56
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISREG(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %56
19: ; preds = %13
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISDIR(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %56
25: ; preds = %19
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISCHR(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %56
31: ; preds = %25
%32 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i64 @S_ISBLK(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %56
37: ; preds = %31
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @S_ISSOCK(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %2
br label %56
43: ; preds = %37
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @S_ISFIFO(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8** %2
br label %56
49: ; preds = %43
%50 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @S_ISLNK(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8** %2
br label %56
55: ; preds = %49
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8** %2
br label %56
56: ; preds = %55, %54, %48, %42, %36, %30, %24, %18, %8
%57 = load i8*, i8** %2
ret i8* %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"Unable to get info for %s: \00"
@.str.1 = [13 x i8] c"regular file\00"
@.str.2 = [10 x i8] c"directory\00"
@.str.3 = [17 x i8] c"character device\00"
@.str.4 = [13 x i8] c"block device\00"
@.str.5 = [7 x i8] c"socket\00"
@.str.6 = [18 x i8] c"fifo (named pipe)\00"
@.str.7 = [14 x i8] c"symbolic link\00"
@.str.8 = [18 x i8] c"unknown file type\00"
define i8* @file_type(i8* %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 %10
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @perror(i32* null)
br label %34
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @S_ISREG(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %10
%16 = tail call i64 @S_ISDIR(i32 %12)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = tail call i64 @S_ISCHR(i32 %12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = tail call i64 @S_ISBLK(i32 %12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = tail call i64 @S_ISSOCK(i32 %12)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = tail call i64 @S_ISFIFO(i32 %12)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = tail call i64 @S_ISLNK(i32 %12)
%32 = icmp eq i64 %31, 0
%33 = select i1 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0)
br label %34
34: ; preds = %30, %27, %24, %21, %18, %15, %10, %6
%35 = phi i8* [ null, %6 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %10 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %15 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %18 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %21 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %24 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), %27 ], [ %33, %30 ]
ret i8* %35
}