#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 MAXPATHLEN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ 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))
    return -1;
found:
  f = fopen(newpath, "r");
  if (f) {
    fscanf(f, "%d\n", &i);
    fclose(f);
    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    0x2e85(%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,%rax
jne    11c1 <sysfs_is_disk+0x71>
jmp    1276 <sysfs_is_disk+0x126>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe39(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jne    11f7 <sysfs_is_disk+0xa7>
jmp    1276 <sysfs_is_disk+0x126>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe15(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jne    122d <sysfs_is_disk+0xdd>
jmp    1276 <sysfs_is_disk+0x126>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xdef(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
je     1271 <sysfs_is_disk+0x121>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1302 <sysfs_is_disk+0x1b2>
jmp    1276 <sysfs_is_disk+0x126>
mov    -0x48(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12ee <sysfs_is_disk+0x19e>
mov    -0x38(%rbp),%rdi
lea    0xda6(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x54(%rbp)
test   %eax,%eax
je     12d6 <sysfs_is_disk+0x186>
jmp    12c5 <sysfs_is_disk+0x175>
mov    -0x54(%rbp),%eax
sub    $0xe,%eax
jne    12e2 <sysfs_is_disk+0x192>
jmp    12d6 <sysfs_is_disk+0x186>
movl   $0x1,-0x2c(%rbp)
jmp    12e9 <sysfs_is_disk+0x199>
movl   $0x0,-0x2c(%rbp)
jmp    12f5 <sysfs_is_disk+0x1a5>
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    0x2e81(%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   %rax,%rax
je     1225 <sysfs_is_disk+0xd5>
lea    0xe5d(%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   %rax,%rax
je     1225 <sysfs_is_disk+0xd5>
lea    0xe49(%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   %rax,%rax
je     1225 <sysfs_is_disk+0xd5>
lea    0xe33(%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>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1271 <sysfs_is_disk+0x121>
lea    0xe18(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     126c <sysfs_is_disk+0x11c>
mov    %rax,%rbx
lea    0xe03(%rip),%rsi
lea    -0x24(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
test   %eax,%eax
sete   %cl
cmp    $0xe,%eax
sete   %al
or     %cl,%al
movzbl %al,%eax
jmp    1271 <sysfs_is_disk+0x121>
mov    $0xfffffffe,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [2 x i8] c"r\00"
@.str.5 = [4 x i8] c"%d\0A\00"
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
store i8* %0, i8** %3
%10 = load i32, i32* @MAXPATHLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
store i32 -1, i32* %7
%14 = trunc i64 %11 to i32
%15 = load i8*, i8** %3
%16 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i64 @lstat(i8* %13, %struct.stat* %6)
%18 = icmp ne i64 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %1
br label %42
20: ; preds = %1
%21 = trunc i64 %11 to i32
%22 = load i8*, i8** %3
%23 = call i32 @snprintf(i8* %13, i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i64 @lstat(i8* %13, %struct.stat* %6)
%25 = icmp ne i64 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %20
br label %42
27: ; preds = %20
%28 = trunc i64 %11 to i32
%29 = load i8*, i8** %3
%30 = call i32 @snprintf(i8* %13, i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i64 @lstat(i8* %13, %struct.stat* %6)
%32 = icmp ne i64 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %27
br label %42
34: ; preds = %27
%35 = trunc i64 %11 to i32
%36 = load i8*, i8** %3
%37 = call i32 @snprintf(i8* %13, i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = call i64 @lstat(i8* %13, %struct.stat* %6)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 -1, i32* %2
store i32 1, i32* %9
br label %58
41: ; preds = %34
br label %42
42: ; preds = %41, %33, %26, %19
%43 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %43, i32** %8
%44 = load i32*, i32** %8
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %55
46: ; preds = %42
%47 = load i32*, i32** %8
%48 = call i32 @fscanf(i32* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %7)
%49 = load i32*, i32** %8
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %7
switch i32 %51, label %53 [
i32 0, label %52
i32 14, label %52
]
52: ; preds = %46, %46
store i32 1, i32* %7
br label %54
53: ; preds = %46
store i32 0, i32* %7
br label %54
54: ; preds = %53, %52
br label %56
55: ; preds = %42
store i32 -2, i32* %7
br label %56
56: ; preds = %55, %54
%57 = load i32, i32* %7
store i32 %57, i32* %2
store i32 1, i32* %9
br label %58
58: ; preds = %56, %40
%59 = load i8*, i8** %4
%60 = load i32, i32* %2
ret i32 %60
}

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 = [2 x i8] c"r\00"
@.str.5 = [4 x i8] c"%d\0A\00"
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 i64 @lstat(i8* %6, %struct.stat* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %24, 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 i64 @lstat(i8* %6, %struct.stat* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, 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 i64 @lstat(i8* %6, %struct.stat* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, 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 i64 @lstat(i8* %6, %struct.stat* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %20, %16, %12, %1
%25 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %35, label %27
27: ; preds = %24
%28 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%29 = call i32 @fclose(i32* %25)
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
%32 = icmp eq i32 %30, 14
%33 = or i1 %31, %32
%34 = zext i1 %33 to i32
br label %35
35: ; preds = %24, %27, %20
%36 = phi i32 [ -1, %20 ], [ %34, %27 ], [ -2, %24 ]
ret i32 %36
}