#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;
};
struct mntent {
  char const *mnt_fsname;
  int /*<<< orphan*/ mnt_dir;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
struct mntent *getmntent(int /*<<< orphan*/ *);
int /*<<< orphan*/ logmsg(int, char *, char const *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
scalar_t__ strcmp(char const *, char const *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
bool isBlockDeviceMounted(const char *aBlkDevice) {
  FILE *f = fopen("/proc/mounts", "r");
  struct mntent *entry;
  bool isMounted = false;
  struct stat blkDevStat;
  if (stat(aBlkDevice, &blkDevStat) != 0) {
    logmsg(1,
           "Unable to stat %s to determine if it's mounted. Assuming it is "
           "mounted for safety. Stat reported: %s\n",
           aBlkDevice, strerror(errno));
    return true;
  }
  while ((entry = getmntent(f)) != NULL) {
    if (strcmp(entry->mnt_fsname, aBlkDevice) == 0) {
      logmsg(4, "%s mounted at %s\n", aBlkDevice, entry->mnt_dir);
      isMounted = true;
      break;
    }
    if (strcmp(entry->mnt_fsname, "none")) {
      struct stat newDevStat;
      if (stat(entry->mnt_fsname, &newDevStat) == 0) {
        if (newDevStat.st_rdev == blkDevStat.st_rdev) {
          logmsg(
              4,
              "%s has identical struct stat.st_rdev with %s, mounted at %s\n",
              aBlkDevice, entry->mnt_fsname, entry->mnt_dir);
          isMounted = true;
          break;
        }
      }
    }
  }
  fclose(f);
  return isMounted;
}


x86 assembly (clang, -O0):
<isBlockDeviceMounted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11ed <isBlockDeviceMounted+0x7d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rdx
movslq %eax,%rcx
mov    $0x1,%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1070 <logmsg@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e3 <isBlockDeviceMounted+0x173>
jmp    11f2 <isBlockDeviceMounted+0x82>
mov    -0x18(%rbp),%rdi
call   1060 <getmntent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12d4 <isBlockDeviceMounted+0x164>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    124e <isBlockDeviceMounted+0xde>
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq 0x8(%rax),%rcx
mov    $0x4,%edi
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1070 <logmsg@plt>
movl   $0x1,-0x24(%rbp)
jmp    12d4 <isBlockDeviceMounted+0x164>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2b(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     12cf <isBlockDeviceMounted+0x15f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    12ca <isBlockDeviceMounted+0x15a>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    12c5 <isBlockDeviceMounted+0x155>
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%r8d
mov    $0x4,%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1070 <logmsg@plt>
movl   $0x1,-0x24(%rbp)
jmp    12d4 <isBlockDeviceMounted+0x164>
jmp    12ca <isBlockDeviceMounted+0x15a>
jmp    12cf <isBlockDeviceMounted+0x15f>
jmp    11f2 <isBlockDeviceMounted+0x82>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isBlockDeviceMounted>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xe78(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     11db <isBlockDeviceMounted+0x6b>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rcx
lea    0xe4e(%rip),%rsi
mov    $0x1,%r15d
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <logmsg@plt>
jmp    1297 <isBlockDeviceMounted+0x127>
mov    %r14,%rdi
call   1060 <getmntent@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     128f <isBlockDeviceMounted+0x11f>
mov    %rax,%rbp
lea    0xe8e(%rip),%r12
lea    0x8(%rsp),%r13
jmp    1210 <isBlockDeviceMounted+0xa0>
mov    %r14,%rdi
call   1060 <getmntent@plt>
mov    %rax,%rbp
test   %rax,%rax
je     128f <isBlockDeviceMounted+0x11f>
mov    0x0(%rbp),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     126f <isBlockDeviceMounted+0xff>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1200 <isBlockDeviceMounted+0x90>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    1200 <isBlockDeviceMounted+0x90>
mov    0x8(%rsp),%rax
cmp    0x10(%rsp),%rax
jne    1200 <isBlockDeviceMounted+0x90>
mov    0x0(%rbp),%rcx
mov    0x8(%rbp),%r8d
lea    0xe2e(%rip),%rsi
mov    $0x4,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <logmsg@plt>
jmp    1289 <isBlockDeviceMounted+0x119>
movslq 0x8(%rbp),%rcx
lea    0xdfb(%rip),%rsi
mov    $0x4,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <logmsg@plt>
mov    $0x1,%r15d
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mntent = type { i8*, i32 }
%struct.stat = type { i64 }
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [102 x i8] c"Unable to stat %s to determine if it's mounted. Assuming it is mounted for safety. Stat reported: %s\0A\00"
@errno = global i32 0
@.str.3 = [18 x i8] c"%s mounted at %s\0A\00"
@.str.4 = [5 x i8] c"none\00"
@.str.5 = [61 x i8] c"%s has identical struct stat.st_rdev with %s, mounted at %s\0A\00"
define i32 @isBlockDeviceMounted(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.mntent*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca %struct.stat
store i8* %0, i8** %3
%9 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
store i32 0, i32* %6
%10 = load i8*, i8** %3
%11 = call i64 @stat(i8* %10, %struct.stat* %7)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i8*
%19 = call i32 (i32, i8*, i8*, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.2, i64 0, i64 0), i8* %14, i8* %18)
store i32 1, i32* %2
br label %74
20: ; preds = %1
br label %21
21: ; preds = %69, %20
%22 = load i32*, i32** %4
%23 = call %struct.mntent* @getmntent(i32* %22)
store %struct.mntent* %23, %struct.mntent** %5
%24 = icmp ne %struct.mntent* %23, null
br i1 %24, label %25, label %70
25: ; preds = %21
%26 = load %struct.mntent*, %struct.mntent** %5
%27 = getelementptr inbounds %struct.mntent, %struct.mntent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = load i8*, i8** %3
%30 = call i64 @strcmp(i8* %28, i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %25
%33 = load i8*, i8** %3
%34 = load %struct.mntent*, %struct.mntent** %5
%35 = getelementptr inbounds %struct.mntent, %struct.mntent* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = sext i32 %36 to i64
%38 = inttoptr i64 %37 to i8*
%39 = call i32 (i32, i8*, i8*, i8*, ...) @logmsg(i32 4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %33, i8* %38)
store i32 1, i32* %6
br label %70
40: ; preds = %25
%41 = load %struct.mntent*, %struct.mntent** %5
%42 = getelementptr inbounds %struct.mntent, %struct.mntent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %69
46: ; preds = %40
%47 = load %struct.mntent*, %struct.mntent** %5
%48 = getelementptr inbounds %struct.mntent, %struct.mntent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i64 @stat(i8* %49, %struct.stat* %8)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %68
52: ; preds = %46
%53 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%54 = load i64, i64* %53
%55 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%56 = load i64, i64* %55
%57 = icmp eq i64 %54, %56
br i1 %57, label %58, label %67
58: ; preds = %52
%59 = load i8*, i8** %3
%60 = load %struct.mntent*, %struct.mntent** %5
%61 = getelementptr inbounds %struct.mntent, %struct.mntent* %60, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = load %struct.mntent*, %struct.mntent** %5
%64 = getelementptr inbounds %struct.mntent, %struct.mntent* %63, i32 0, i32 1
%65 = load i32, i32* %64
%66 = call i32 (i32, i8*, i8*, i8*, ...) @logmsg(i32 4, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0), i8* %59, i8* %62, i32 %65)
store i32 1, i32* %6
br label %70
67: ; preds = %52
br label %68
68: ; preds = %67, %46
br label %69
69: ; preds = %68, %40
br label %21
70: ; preds = %58, %32, %21
%71 = load i32*, i32** %4
%72 = call i32 @fclose(i32* %71)
%73 = load i32, i32* %6
store i32 %73, i32* %2
br label %74
74: ; preds = %70, %13
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.mntent = type { i8*, i32 }
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [102 x i8] c"Unable to stat %s to determine if it's mounted. Assuming it is mounted for safety. Stat reported: %s\0A\00"
@errno = global i32 0
@.str.3 = [18 x i8] c"%s mounted at %s\0A\00"
@.str.4 = [5 x i8] c"none\00"
@.str.5 = [61 x i8] c"%s has identical struct stat.st_rdev with %s, mounted at %s\0A\00"
define i32 @isBlockDeviceMounted(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = bitcast %struct.stat* %2 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = call %struct.mntent* @getmntent(i32* %4)
%10 = icmp eq %struct.mntent* %9, null
br i1 %10, label %54, label %11
11: ; preds = %8
%12 = bitcast %struct.stat* %3 to i8*
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %21
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
%20 = call i32 (i32, i8*, i8*, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %19)
br label %57
21: ; preds = %11, %51
%22 = phi %struct.mntent* [ %9, %11 ], [ %52, %51 ]
%23 = getelementptr inbounds %struct.mntent, %struct.mntent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = getelementptr inbounds %struct.mntent, %struct.mntent* %22, i64 0, i32 1
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = call i32 (i32, i8*, i8*, i8*, ...) @logmsg(i32 4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %31)
br label %54
33: ; preds = %21
%34 = load i8*, i8** %23
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %51, label %37
37: ; preds = %33
%38 = load i8*, i8** %23
%39 = call i64 @stat(i8* %38, %struct.stat* %3)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i64, i64* %13
%43 = load i64, i64* %14
%44 = icmp eq i64 %42, %43
br i1 %44, label %46, label %45
45: ; preds = %41, %37
br label %51
46: ; preds = %41
%47 = load i8*, i8** %23
%48 = getelementptr inbounds %struct.mntent, %struct.mntent* %22, i64 0, i32 1
%49 = load i32, i32* %48
%50 = call i32 (i32, i8*, i8*, i8*, ...) @logmsg(i32 4, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %47, i32 %49)
br label %54
51: ; preds = %45, %33
%52 = call %struct.mntent* @getmntent(i32* %4)
%53 = icmp eq %struct.mntent* %52, null
br i1 %53, label %54, label %21
54: ; preds = %51, %8, %46, %27
%55 = phi i32 [ 1, %27 ], [ 1, %46 ], [ 0, %8 ], [ 0, %51 ]
%56 = call i32 @fclose(i32* %4)
br label %57
57: ; preds = %54, %15
%58 = phi i32 [ 1, %15 ], [ %55, %54 ]
ret i32 %58
}