#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 st_mode;
};

/* Variables and functions */
int EINVAL;
int PATH_MAX;
#define S_IFBLK 129
int S_IFMT;
#define S_IFREG 128
int __tapdisk_blk_storage_type(char *);
int __tapdisk_fs_storage_type(char *);
int errno;
char *realpath(char const *, char *);
int stat(char *, struct stat *);
int tapdisk_storage_type(const char *path) {
  char rpath[PATH_MAX], *p;
  struct stat st;
  int err, rv;
  p = realpath(path, rpath);
  if (!p)
    return -errno;
  err = stat(rpath, &st);
  if (err)
    return -errno;
  switch (st.st_mode & S_IFMT) {
  case S_IFBLK:
    rv = __tapdisk_blk_storage_type(rpath);
    break;
  case S_IFREG:
    rv = __tapdisk_fs_storage_type(rpath);
    break;
  default:
    rv = -EINVAL;
    break;
  }
  return rv;
}


x86 assembly (clang, -O0):
<tapdisk_storage_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <realpath@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11aa <tapdisk_storage_type+0x6a>
xor    %eax,%eax
mov    0x2e37(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    124f <tapdisk_storage_type+0x10f>
mov    -0x48(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11de <tapdisk_storage_type+0x9e>
xor    %eax,%eax
mov    0x2e03(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    124f <tapdisk_storage_type+0x10f>
mov    -0x30(%rbp),%eax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x4c(%rbp)
sub    $0x80,%eax
je     1223 <tapdisk_storage_type+0xe3>
jmp    11ff <tapdisk_storage_type+0xbf>
mov    -0x4c(%rbp),%eax
sub    $0x81,%eax
jne    1234 <tapdisk_storage_type+0xf4>
jmp    1212 <tapdisk_storage_type+0xd2>
mov    -0x48(%rbp),%rdi
call   1030 <__tapdisk_blk_storage_type@plt>
mov    %eax,-0x38(%rbp)
jmp    1242 <tapdisk_storage_type+0x102>
mov    -0x48(%rbp),%rdi
call   1040 <__tapdisk_fs_storage_type@plt>
mov    %eax,-0x38(%rbp)
jmp    1242 <tapdisk_storage_type+0x102>
xor    %eax,%eax
mov    0x2d8b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tapdisk_storage_type>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1060 <realpath@plt>
test   %rax,%rax
je     117d <tapdisk_storage_type+0x3d>
lea    -0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %eax,%eax
je     118f <tapdisk_storage_type+0x4f>
mov    0x2e4c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
and    -0x10(%rbp),%eax
cmp    $0x80,%eax
je     11b3 <tapdisk_storage_type+0x73>
cmp    $0x81,%eax
jne    11bd <tapdisk_storage_type+0x7d>
mov    %rbx,%rdi
call   1030 <__tapdisk_blk_storage_type@plt>
jmp    1188 <tapdisk_storage_type+0x48>
mov    %rbx,%rdi
call   1040 <__tapdisk_fs_storage_type@plt>
jmp    1188 <tapdisk_storage_type+0x48>
mov    0x2e04(%rip),%rcx
jmp    1184 <tapdisk_storage_type+0x44>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i32 0
@S_IFMT = global i32 0
@EINVAL = global i32 0
define i32 @tapdisk_storage_type(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
%15 = load i8*, i8** %3
%16 = call i8* @realpath(i8* %15, i8* %14)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %1
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %2
store i32 1, i32* %10
br label %43
22: ; preds = %1
%23 = call i32 @stat(i8* %14, %struct.stat* %7)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @errno
%28 = sub nsw i32 0, %27
store i32 %28, i32* %2
store i32 1, i32* %10
br label %43
29: ; preds = %22
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @S_IFMT
%33 = and i32 %31, %32
switch i32 %33, label %38 [
i32 129, label %34
i32 128, label %36
]
34: ; preds = %29
%35 = call i32 @__tapdisk_blk_storage_type(i8* %14)
store i32 %35, i32* %9
br label %41
36: ; preds = %29
%37 = call i32 @__tapdisk_fs_storage_type(i8* %14)
store i32 %37, i32* %9
br label %41
38: ; preds = %29
%39 = load i32, i32* @EINVAL
%40 = sub nsw i32 0, %39
store i32 %40, i32* %9
br label %41
41: ; preds = %38, %36, %34
%42 = load i32, i32* %9
store i32 %42, i32* %2
store i32 1, i32* %10
br label %43
43: ; preds = %41, %26, %19
%44 = load i8*, i8** %4
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i32 0
@S_IFMT = global i32 0
@EINVAL = global i32 0
define i32 @tapdisk_storage_type(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast %struct.stat* %2 to i8*
%7 = call i8* @realpath(i8* %0, i8* %5)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %30
12: ; preds = %1
%13 = call i32 @stat(i8* %5, %struct.stat* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
br label %30
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @S_IFMT
%22 = and i32 %21, %20
switch i32 %22, label %27 [
i32 129, label %23
i32 128, label %25
]
23: ; preds = %18
%24 = call i32 @__tapdisk_blk_storage_type(i8* %5)
br label %30
25: ; preds = %18
%26 = call i32 @__tapdisk_fs_storage_type(i8* %5)
br label %30
27: ; preds = %18
%28 = load i32, i32* @EINVAL
%29 = sub nsw i32 0, %28
br label %30
30: ; preds = %23, %25, %27, %15, %9
%31 = phi i32 [ %17, %15 ], [ %11, %9 ], [ %29, %27 ], [ %26, %25 ], [ %24, %23 ]
ret i32 %31
}