#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 *);
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) {
      fscanf(f, "%d\n", &i);
      fclose(f);
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<sysfs_is_removable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%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,-0x38(%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    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%eax
jne    11f8 <sysfs_is_removable+0xa8>
mov    -0x38(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f3 <sysfs_is_removable+0xa3>
mov    -0x30(%rbp),%rdi
lea    0xe2e(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11f8 <sysfs_is_removable+0xa8>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rcx
mov    0x2e83(%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,-0x14(%rbp)
lea    0xe77(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
lea    -0x18(%rbp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jne    11dd <sysfs_is_removable+0x8d>
lea    0xe5f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11dd <sysfs_is_removable+0x8d>
mov    %rax,%rbx
lea    0xe4a(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%r14d
mov    %r14d,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
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"
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*
store i8* %0, i8** %2
%8 = load i32, i32* @MAXPATHLEN
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
store i32 -1, i32* %6
%12 = trunc i64 %9 to i32
%13 = load i8*, i8** %2
%14 = call i32 @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @lstat(i8* %11, %struct.stat* %5)
%16 = icmp ne i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %1
%18 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32*, i32** %7
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%24 = load i32*, i32** %7
%25 = call i32 @fclose(i32* %24)
br label %26
26: ; preds = %21, %17
br label %27
27: ; preds = %26, %1
%28 = load i32, i32* %6
%29 = load i8*, i8** %3
ret i32 %28
}

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"
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 %19
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 %19, 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* %3
br label %19
19: ; preds = %12, %15, %1
%20 = phi i32 [ -1, %12 ], [ %18, %15 ], [ -1, %1 ]
ret i32 %20
}