#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 {
  scalar_t__ st_rdev;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ENODEV;
int ENOSYS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
scalar_t__ makedev(int, int);
scalar_t__ stat(char const *, struct stat *);
int is_mounted(const char *device) {
  struct stat statbuf;
  FILE *fp;
  int maj, min;
  int mounted = 0;
  if (stat(device, &statbuf) < 0)
    return -ENODEV;
  fp = fopen("/proc/self/mountinfo", "r");
  if (fp == NULL)
    return -ENOSYS;
  while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
    if (makedev(maj, min) == statbuf.st_rdev) {
      mounted = 1;
      break;
    }
  }
  fclose(fp);
  return mounted;
}


x86 assembly (clang, -O0):
<is_mounted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    118d <is_mounted+0x3d>
xor    %eax,%eax
mov    0x2e55(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <is_mounted+0xcd>
lea    0xe6c(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c2 <is_mounted+0x72>
xor    %eax,%eax
mov    0x2e30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <is_mounted+0xcd>
jmp    11c7 <is_mounted+0x77>
mov    -0x20(%rbp),%rdi
lea    0xe45(%rip),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    120e <is_mounted+0xbe>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1070 <makedev@plt>
cmp    -0x18(%rbp),%rax
jne    1209 <is_mounted+0xb9>
movl   $0x1,-0x2c(%rbp)
jmp    120e <is_mounted+0xbe>
jmp    11c7 <is_mounted+0x77>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_mounted>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
js     11d0 <is_mounted+0x80>
lea    0xe8e(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d9 <is_mounted+0x89>
mov    %rax,%rbx
lea    0xe8a(%rip),%r14
lea    0x4(%rsp),%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    11e6 <is_mounted+0x96>
mov    0x4(%rsp),%edi
mov    (%rsp),%esi
call   1070 <makedev@plt>
cmp    0x8(%rsp),%rax
jne    11a0 <is_mounted+0x50>
mov    $0x1,%ebp
jmp    11e8 <is_mounted+0x98>
mov    0x2e01(%rip),%rax
jmp    11e0 <is_mounted+0x90>
mov    0x2e08(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    11f0 <is_mounted+0xa0>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@ENODEV = global i32 0
@.str = [21 x i8] c"/proc/self/mountinfo\00"
@.str.1 = [2 x i8] c"r\00"
@ENOSYS = global i32 0
@.str.2 = [21 x i8] c"%*s %*s %i:%i %*[^\0A]\00"
define i32 @is_mounted(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %8
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @ENODEV
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %40
15: ; preds = %1
%16 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %5
%17 = load i32*, i32** %5
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @ENOSYS
%21 = sub nsw i32 0, %20
store i32 %21, i32* %2
br label %40
22: ; preds = %15
br label %23
23: ; preds = %35, %22
%24 = load i32*, i32** %5
%25 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32* %6, i32* %7)
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i64 @makedev(i32 %28, i32 %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp eq i64 %30, %32
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 1, i32* %8
br label %36
35: ; preds = %27
br label %23
36: ; preds = %34, %23
%37 = load i32*, i32** %5
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %8
store i32 %39, i32* %2
br label %40
40: ; preds = %36, %19, %12
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@ENODEV = global i32 0
@.str = [21 x i8] c"/proc/self/mountinfo\00"
@.str.1 = [2 x i8] c"r\00"
@ENOSYS = global i32 0
@.str.2 = [21 x i8] c"%*s %*s %i:%i %*[^\0A]\00"
define i32 @is_mounted(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.stat* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @ENODEV
%12 = sub nsw i32 0, %11
br label %33
13: ; preds = %1
%14 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %21
18: ; preds = %13
%19 = load i32, i32* @ENOSYS
%20 = sub nsw i32 0, %19
br label %33
21: ; preds = %16, %24
%22 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* %3
%26 = load i32, i32* %4
%27 = call i64 @makedev(i32 %25, i32 %26)
%28 = load i64, i64* %17
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %21
30: ; preds = %24, %21
%31 = phi i32 [ 0, %21 ], [ 1, %24 ]
%32 = call i32 @fclose(i32* %14)
br label %33
33: ; preds = %30, %18, %10
%34 = phi i32 [ %12, %10 ], [ %20, %18 ], [ %31, %30 ]
ret i32 %34
}