#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 dummy;
};
typedef int /*<<< orphan*/ newpath;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int MAXPATHLEN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int sysfs_is_disk(char *path) {
  char newpath[MAXPATHLEN];
  struct stat sb;
  int i = -1;
  FILE *f;
  snprintf(newpath, sizeof(newpath), "%s/device/type", path);
  if (!lstat(newpath, &sb))
    goto found;
  snprintf(newpath, sizeof(newpath), "%s/../device/type", path);
  if (!lstat(newpath, &sb))
    goto found;
  snprintf(newpath, sizeof(newpath), "%s/device/media", path);
  if (!lstat(newpath, &sb))
    goto found;
  snprintf(newpath, sizeof(newpath), "%s/../device/media", path);
  if (!lstat(newpath, &sb))
    goto found;
  snprintf(newpath, sizeof(newpath), "%s/device/devtype", path);
  if (!lstat(newpath, &sb))
    return 1;
  snprintf(newpath, sizeof(newpath), "%s/../device/devtype", path);
  if (!lstat(newpath, &sb))
    return 1;
  return -1;
found:
  f = fopen(newpath, "r");
  if (f) {
    int err;
    err = fscanf(f, "%d\n", &i);
    fclose(f);
    if ((err == EOF) || (err != 1))
      return 0;
    switch (i) {
    case 0x0:
    case 0xe:
      i = 1;
      break;
    default:
      i = 0;
      break;
    }
  } else
    i = -2;
  return i;
}


x86 assembly (clang, -O0):
<sysfs_is_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe60(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    11c0 <sysfs_is_disk+0x70>
jmp    12f8 <sysfs_is_disk+0x1a8>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe3a(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    11f5 <sysfs_is_disk+0xa5>
jmp    12f8 <sysfs_is_disk+0x1a8>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe17(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    122a <sysfs_is_disk+0xda>
jmp    12f8 <sysfs_is_disk+0x1a8>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xdf2(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    125f <sysfs_is_disk+0x10f>
jmp    12f8 <sysfs_is_disk+0x1a8>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xdd0(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    12a2 <sysfs_is_disk+0x152>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13b6 <sysfs_is_disk+0x266>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xd9f(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    12e5 <sysfs_is_disk+0x195>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13b6 <sysfs_is_disk+0x266>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13b6 <sysfs_is_disk+0x266>
mov    -0x48(%rbp),%rdi
lea    0xd68(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     13a2 <sysfs_is_disk+0x252>
mov    -0x38(%rbp),%rdi
lea    0xd4b(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%eax
mov    0x2ca7(%rip),%rcx
cmp    (%rcx),%eax
je     1353 <sysfs_is_disk+0x203>
cmpl   $0x1,-0x40(%rbp)
je     1366 <sysfs_is_disk+0x216>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13b6 <sysfs_is_disk+0x266>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x54(%rbp)
test   %eax,%eax
je     138a <sysfs_is_disk+0x23a>
jmp    1379 <sysfs_is_disk+0x229>
mov    -0x54(%rbp),%eax
sub    $0xe,%eax
jne    1396 <sysfs_is_disk+0x246>
jmp    138a <sysfs_is_disk+0x23a>
movl   $0x1,-0x2c(%rbp)
jmp    139d <sysfs_is_disk+0x24d>
movl   $0x0,-0x2c(%rbp)
jmp    13a9 <sysfs_is_disk+0x259>
movl   $0xfffffffe,-0x2c(%rbp)
mov    -0x2c(%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):
<sysfs_is_disk>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e79(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0xffffffff,-0x24(%rbp)
lea    0xe74(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
je     1274 <sysfs_is_disk+0x124>
lea    0xe5a(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
je     1274 <sysfs_is_disk+0x124>
lea    0xe43(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
je     1274 <sysfs_is_disk+0x124>
lea    0xe2e(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
je     1274 <sysfs_is_disk+0x124>
lea    0xe1c(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
je     12e2 <sysfs_is_disk+0x192>
lea    0xe05(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
neg    %eax
sbb    %eax,%eax
or     $0x1,%eax
jmp    12d7 <sysfs_is_disk+0x187>
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     12d2 <sysfs_is_disk+0x182>
mov    %rax,%rbx
lea    0xddb(%rip),%rsi
lea    -0x24(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    $0x1,%r14d
jne    12d7 <sysfs_is_disk+0x187>
mov    0x2d30(%rip),%rcx
cmpl   $0x1,(%rcx)
je     12d7 <sysfs_is_disk+0x187>
mov    -0x24(%rbp),%eax
test   %eax,%eax
sete   %cl
cmp    $0xe,%eax
sete   %al
or     %cl,%al
movzbl %al,%eax
jmp    12d7 <sysfs_is_disk+0x187>
mov    $0xfffffffe,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    12d7 <sysfs_is_disk+0x187>

LLVM IR (-O0):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [15 x i8] c"%s/device/type\00"
@.str.1 = [18 x i8] c"%s/../device/type\00"
@.str.2 = [16 x i8] c"%s/device/media\00"
@.str.3 = [19 x i8] c"%s/../device/media\00"
@.str.4 = [18 x i8] c"%s/device/devtype\00"
@.str.5 = [21 x i8] c"%s/../device/devtype\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
define i32 @sysfs_is_disk(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @MAXPATHLEN
%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
store i32 -1, i32* %7
%15 = trunc i64 %12 to i32
%16 = load i8*, i8** %3
%17 = call i32 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @lstat(i8* %14, %struct.stat* %6)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %1
br label %57
21: ; preds = %1
%22 = trunc i64 %12 to i32
%23 = load i8*, i8** %3
%24 = call i32 @snprintf(i8* %14, i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = call i32 @lstat(i8* %14, %struct.stat* %6)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %21
br label %57
28: ; preds = %21
%29 = trunc i64 %12 to i32
%30 = load i8*, i8** %3
%31 = call i32 @snprintf(i8* %14, i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = call i32 @lstat(i8* %14, %struct.stat* %6)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %28
br label %57
35: ; preds = %28
%36 = trunc i64 %12 to i32
%37 = load i8*, i8** %3
%38 = call i32 @snprintf(i8* %14, i32 %36, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = call i32 @lstat(i8* %14, %struct.stat* %6)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %35
br label %57
42: ; preds = %35
%43 = trunc i64 %12 to i32
%44 = load i8*, i8** %3
%45 = call i32 @snprintf(i8* %14, i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %44)
%46 = call i32 @lstat(i8* %14, %struct.stat* %6)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42
store i32 1, i32* %2
store i32 1, i32* %9
br label %81
49: ; preds = %42
%50 = trunc i64 %12 to i32
%51 = load i8*, i8** %3
%52 = call i32 @snprintf(i8* %14, i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %51)
%53 = call i32 @lstat(i8* %14, %struct.stat* %6)
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %49
store i32 1, i32* %2
store i32 1, i32* %9
br label %81
56: ; preds = %49
store i32 -1, i32* %2
store i32 1, i32* %9
br label %81
57: ; preds = %41, %34, %27, %20
%58 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %58, i32** %8
%59 = load i32*, i32** %8
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %78
61: ; preds = %57
%62 = load i32*, i32** %8
%63 = call i32 @fscanf(i32* %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32* %7)
store i32 %63, i32* %10
%64 = load i32*, i32** %8
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %10
%67 = load i32, i32* @EOF
%68 = icmp eq i32 %66, %67
br i1 %68, label %72, label %69
69: ; preds = %61
%70 = load i32, i32* %10
%71 = icmp ne i32 %70, 1
br i1 %71, label %72, label %73
72: ; preds = %69, %61
store i32 0, i32* %2
store i32 1, i32* %9
br label %81
73: ; preds = %69
%74 = load i32, i32* %7
switch i32 %74, label %76 [
i32 0, label %75
i32 14, label %75
]
75: ; preds = %73, %73
store i32 1, i32* %7
br label %77
76: ; preds = %73
store i32 0, i32* %7
br label %77
77: ; preds = %76, %75
br label %79
78: ; preds = %57
store i32 -2, i32* %7
br label %79
79: ; preds = %78, %77
%80 = load i32, i32* %7
store i32 %80, i32* %2
store i32 1, i32* %9
br label %81
81: ; preds = %79, %72, %56, %55, %48
%82 = load i8*, i8** %4
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [15 x i8] c"%s/device/type\00"
@.str.1 = [18 x i8] c"%s/../device/type\00"
@.str.2 = [16 x i8] c"%s/device/media\00"
@.str.3 = [19 x i8] c"%s/../device/media\00"
@.str.4 = [18 x i8] c"%s/device/devtype\00"
@.str.5 = [21 x i8] c"%s/../device/devtype\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
define i32 @sysfs_is_disk(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca i32
%4 = load i32, i32* @MAXPATHLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%9 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @lstat(i8* %6, %struct.stat* %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %1
%13 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = call i32 @lstat(i8* %6, %struct.stat* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %33, label %16
16: ; preds = %12
%17 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = call i32 @lstat(i8* %6, %struct.stat* %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %16
%21 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%22 = call i32 @lstat(i8* %6, %struct.stat* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %20
%25 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%26 = call i32 @lstat(i8* %6, %struct.stat* %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %49, label %28
28: ; preds = %24
%29 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%30 = call i32 @lstat(i8* %6, %struct.stat* %2)
%31 = icmp eq i32 %30, 0
%32 = select i1 %31, i32 1, i32 -1
br label %49
33: ; preds = %20, %16, %12, %1
%34 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %49, label %36
36: ; preds = %33
%37 = call i32 @fscanf(i32* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32* %3)
%38 = call i32 @fclose(i32* %34)
%39 = load i32, i32* @EOF
%40 = icmp ne i32 %37, 1
%41 = icmp eq i32 %39, 1
%42 = or i1 %40, %41
br i1 %42, label %49, label %43
43: ; preds = %36
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, 0
%46 = icmp eq i32 %44, 14
%47 = or i1 %45, %46
%48 = zext i1 %47 to i32
br label %49
49: ; preds = %43, %33, %36, %28, %24
%50 = phi i32 [ 1, %24 ], [ %32, %28 ], [ 0, %36 ], [ %48, %43 ], [ -2, %33 ]
ret i32 %50
}