#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 */
int S_ISBLK(int /*<<< orphan*/);
int S_ISCHR(int /*<<< orphan*/);
int S_ISDIR(int /*<<< orphan*/);
int S_ISFIFO(int /*<<< orphan*/);
int S_ISREG(int /*<<< orphan*/);
int S_ISSOCK(int /*<<< orphan*/);
int /*<<< orphan*/ stat(char const *, struct stat *);
int filetype(const char *path) {
  struct stat path_stat;
  stat(path, &path_stat);
  if (S_ISREG(path_stat.st_mode) == 1)
    return 0;
  if (S_ISDIR(path_stat.st_mode) == 1)
    return 1;
  if (S_ISCHR(path_stat.st_mode) == 1)
    return 2;
  if (S_ISBLK(path_stat.st_mode) == 1)
    return 3;
  if (S_ISFIFO(path_stat.st_mode) == 1)
    return 4;
  if (S_ISSOCK(path_stat.st_mode) == 1)
    return 5;
  else
    return 7;
}


x86 assembly (clang, -O0):
<filetype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    -0x18(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x1,%eax
jne    11a6 <filetype+0x36>
movl   $0x0,-0x4(%rbp)
jmp    123e <filetype+0xce>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x1,%eax
jne    11c3 <filetype+0x53>
movl   $0x1,-0x4(%rbp)
jmp    123e <filetype+0xce>
mov    -0x18(%rbp),%edi
call   1080 <S_ISCHR@plt>
cmp    $0x1,%eax
jne    11e0 <filetype+0x70>
movl   $0x2,-0x4(%rbp)
jmp    123e <filetype+0xce>
mov    -0x18(%rbp),%edi
call   1090 <S_ISBLK@plt>
cmp    $0x1,%eax
jne    11fd <filetype+0x8d>
movl   $0x3,-0x4(%rbp)
jmp    123e <filetype+0xce>
mov    -0x18(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x1,%eax
jne    121a <filetype+0xaa>
movl   $0x4,-0x4(%rbp)
jmp    123e <filetype+0xce>
mov    -0x18(%rbp),%edi
call   1040 <S_ISSOCK@plt>
cmp    $0x1,%eax
jne    1237 <filetype+0xc7>
movl   $0x5,-0x4(%rbp)
jmp    123e <filetype+0xce>
movl   $0x7,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filetype>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1070 <S_ISREG@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     11f4 <filetype+0x84>
mov    %ebx,%edi
call   1050 <S_ISDIR@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0x1,%ecx
je     11f4 <filetype+0x84>
mov    %ebx,%edi
call   1080 <S_ISCHR@plt>
mov    %eax,%ecx
mov    $0x2,%eax
cmp    $0x1,%ecx
je     11f4 <filetype+0x84>
mov    %ebx,%edi
call   1090 <S_ISBLK@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    $0x1,%ecx
je     11f4 <filetype+0x84>
mov    %ebx,%edi
call   1030 <S_ISFIFO@plt>
mov    %eax,%ecx
mov    $0x4,%eax
cmp    $0x1,%ecx
je     11f4 <filetype+0x84>
mov    %ebx,%edi
call   1040 <S_ISSOCK@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
setne  %cl
lea    (%rcx,%rcx,1),%eax
add    $0x5,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @filetype(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @stat(i8* %5, %struct.stat* %4)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%8 = load i32, i32* %7
%9 = call i32 @S_ISREG(i32 %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %43
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @S_ISDIR(i32 %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 1, i32* %2
br label %43
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @S_ISCHR(i32 %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 2, i32* %2
br label %43
24: ; preds = %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @S_ISBLK(i32 %26)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 3, i32* %2
br label %43
30: ; preds = %24
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @S_ISFIFO(i32 %32)
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 4, i32* %2
br label %43
36: ; preds = %30
%37 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @S_ISSOCK(i32 %38)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 5, i32* %2
br label %43
42: ; preds = %36
store i32 7, i32* %2
br label %43
43: ; preds = %42, %41, %35, %29, %23, %17, %11
%44 = load i32, i32* %2
ret i32 %44
}

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