#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 */
typedef int /*<<< orphan*/ mi;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int scan_mounts(char *mpath, char *device) {
  FILE *f;
  char mi[1024];
  f = fopen("/proc/self/mountstats", "r");
  if (!f)
    return -1;
  while (fgets(mi, sizeof(mi), f)) {
    char *dev, *path, *e;
    dev = mi + sizeof("device");
    e = strchr(dev, ' ');
    *e = '\0';
    path = e + 1 + sizeof("mounted on");
    e = strchr(path, ' ');
    *e = '\0';
    if (strcmp(path, mpath))
      continue;
    strcpy(device, dev);
    fclose(f);
    return 0;
  }
  fclose(f);
  return -1;
}


x86 assembly (clang, -O0):
<scan_mounts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <scan_mounts+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <scan_mounts+0x125>
jmp    11a6 <scan_mounts+0x46>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1275 <scan_mounts+0x115>
lea    -0x420(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x438(%rbp),%rax
add    $0x1,%rax
add    $0xb,%rax
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1250 <scan_mounts+0xf0>
jmp    11a6 <scan_mounts+0x46>
mov    -0x18(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1285 <scan_mounts+0x125>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_mounts>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe82(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f8 <scan_mounts+0x98>
mov    %rax,%rbx
lea    0x7(%rsp),%r12
mov    %rsp,%r13
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11ff <scan_mounts+0x9f>
mov    %r12,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
movb   $0x0,(%rax)
lea    0xc(%rax),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
movb   $0x0,(%rax)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11a0 <scan_mounts+0x40>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
jmp    1204 <scan_mounts+0xa4>
mov    $0xffffffff,%ebp
jmp    120c <scan_mounts+0xac>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/proc/self/mountstats\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @scan_mounts(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
store i32 -1, i32* %3
br label %47
15: ; preds = %2
br label %16
16: ; preds = %37, %15
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%18 = load i32*, i32** %6
%19 = call i64 @fgets(i8* %17, i32 1024, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %16
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%23 = getelementptr inbounds i8, i8* %22, i64 7
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = call i8* @strchr(i8* %24, i8 signext 32)
store i8* %25, i8** %10
%26 = load i8*, i8** %10
store i8 0, i8* %26
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = getelementptr inbounds i8, i8* %28, i64 11
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = call i8* @strchr(i8* %30, i8 signext 32)
store i8* %31, i8** %10
%32 = load i8*, i8** %10
store i8 0, i8* %32
%33 = load i8*, i8** %9
%34 = load i8*, i8** %4
%35 = call i64 @strcmp(i8* %33, i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %21
br label %16
38: ; preds = %21
%39 = load i8*, i8** %5
%40 = load i8*, i8** %8
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %3
br label %47
44: ; preds = %16
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
store i32 -1, i32* %3
br label %47
47: ; preds = %44, %38, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [22 x i8] c"/proc/self/mountstats\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @scan_mounts(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %23, label %7
7: ; preds = %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 7
br label %9
9: ; preds = %7, %12
%10 = call i64 @fgets(i8* %4, i32 1024, i32* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = call i8* @strchr(i8* %8, i8 signext 32)
store i8 0, i8* %13
%14 = getelementptr inbounds i8, i8* %13, i64 12
%15 = call i8* @strchr(i8* %14, i8 signext 32)
store i8 0, i8* %15
%16 = call i64 @strcmp(i8* %14, i8* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %9
18: ; preds = %12
%19 = call i32 @strcpy(i8* %1, i8* %8)
br label %20
20: ; preds = %9, %18
%21 = phi i32 [ 0, %18 ], [ -1, %9 ]
%22 = call i32 @fclose(i32* %5)
br label %23
23: ; preds = %20, %2
%24 = phi i32 [ -1, %2 ], [ %21, %20 ]
ret i32 %24
}