#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_removable(char *path) {
  char newpath[MAXPATHLEN];
  struct stat sb;
  int i = -1;
  FILE *f;
  snprintf(newpath, sizeof(newpath), "%s/removable", path);
  if (!lstat(newpath, &sb)) {
    f = fopen(newpath, "r");
    if (f) {
      int err;
      err = fscanf(f, "%d\n", &i);
      fclose(f);
      if ((err == EOF) || (err != 1))
        i = -1;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<sysfs_is_removable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
lea    0xe64(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    1223 <sysfs_is_removable+0xd3>
mov    -0x40(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     121e <sysfs_is_removable+0xce>
mov    -0x30(%rbp),%rdi
lea    0xe2e(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%eax
je     1212 <sysfs_is_removable+0xc2>
cmpl   $0x1,-0x34(%rbp)
je     1219 <sysfs_is_removable+0xc9>
movl   $0xffffffff,-0x24(%rbp)
jmp    121e <sysfs_is_removable+0xce>
jmp    1223 <sysfs_is_removable+0xd3>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_is_removable>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rcx
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0xffffffff,-0x1c(%rbp)
lea    0xe78(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jne    11fe <sysfs_is_removable+0xae>
lea    0xe60(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fe <sysfs_is_removable+0xae>
mov    %rax,%rbx
lea    0xe4b(%rip),%rsi
lea    -0x1c(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2e06(%rip),%rax
cmpl   $0x1,(%rax)
mov    $0xffffffff,%eax
mov    $0xffffffff,%r14d
je     11f6 <sysfs_is_removable+0xa6>
mov    -0x1c(%rbp),%r14d
cmp    $0x1,%r15d
cmovne %eax,%r14d
mov    %r14d,%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 = [13 x i8] c"%s/removable\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
define i32 @sysfs_is_removable(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @MAXPATHLEN
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
store i32 -1, i32* %6
%13 = trunc i64 %10 to i32
%14 = load i8*, i8** %2
%15 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @lstat(i8* %12, %struct.stat* %5)
%17 = icmp ne i32 %16, 0
br i1 %17, label %36, label %18
18: ; preds = %1
%19 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32*, i32** %7
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %6)
store i32 %24, i32* %8
%25 = load i32*, i32** %7
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %8
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %33, label %30
30: ; preds = %22
%31 = load i32, i32* %8
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %30, %22
store i32 -1, i32* %6
br label %34
34: ; preds = %33, %30
br label %35
35: ; preds = %34, %18
br label %36
36: ; preds = %35, %1
%37 = load i32, i32* %6
%38 = load i8*, i8** %3
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@.str = [13 x i8] c"%s/removable\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
define i32 @sysfs_is_removable(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 ([13 x i8], [13 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 %12, label %24
12: ; preds = %1
%13 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%17 = call i32 @fclose(i32* %13)
%18 = load i32, i32* @EOF
%19 = icmp ne i32 %16, 1
%20 = icmp eq i32 %18, 1
%21 = or i1 %19, %20
%22 = load i32, i32* %3
%23 = select i1 %21, i32 -1, i32 %22
br label %24
24: ; preds = %15, %12, %1
%25 = phi i32 [ -1, %12 ], [ -1, %1 ], [ %23, %15 ]
ret i32 %25
}