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

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ endmntent(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
struct mntent *getmntent(int /*<<< orphan*/ *);
scalar_t__ path_begins_with(char *, char *);
int /*<<< orphan*/ *setmntent(char *, char *);
int strlen(char *);
char *xstrdup(char *);
char *find_device(char *path) {
  FILE *f = setmntent("/proc/mounts", "r");
  if (!f)
    die("Cannot open /proc/mounts: %m");
  struct mntent *me;
  int best_len = 0;
  char *best_dev = NULL;
  while (me = getmntent(f)) {
    if (!path_begins_with(me->mnt_fsname, "/dev"))
      continue;
    if (path_begins_with(path, me->mnt_dir)) {
      int len = strlen(me->mnt_dir);
      if (len > best_len) {
        best_len = len;
        free(best_dev);
        best_dev = xstrdup(me->mnt_fsname);
      }
    }
  }
  endmntent(f);
  return best_dev;
}


x86 assembly (clang, -O0):
<find_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1090 <setmntent@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <find_device+0x3a>
lea    0xe5a(%rip),%rdi
call   10a0 <die@plt>
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getmntent@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1262 <find_device+0xe2>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe3e(%rip),%rsi
call   1050 <path_begins_with@plt>
cmp    $0x0,%rax
jne    1202 <find_device+0x82>
jmp    11c9 <find_device+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <path_begins_with@plt>
cmp    $0x0,%rax
je     125d <find_device+0xdd>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1258 <find_device+0xd8>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1070 <xstrdup@plt>
mov    %rax,-0x28(%rbp)
jmp    125d <find_device+0xdd>
jmp    11c9 <find_device+0x49>
mov    -0x10(%rbp),%rdi
call   1040 <endmntent@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
lea    0xe6a(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1090 <setmntent@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11b6 <find_device+0x36>
lea    0xe5e(%rip),%rdi
call   10a0 <die@plt>
xor    %r12d,%r12d
lea    0xe6c(%rip),%r15
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1080 <getmntent@plt>
test   %rax,%rax
je     122a <find_device+0xaa>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %r15,%rsi
call   1050 <path_begins_with@plt>
test   %rax,%rax
je     11d0 <find_device+0x50>
mov    0x8(%rbx),%rsi
mov    (%rsp),%rdi
call   1050 <path_begins_with@plt>
test   %rax,%rax
je     11d0 <find_device+0x50>
mov    0x8(%rbx),%rdi
call   1060 <strlen@plt>
cmp    %r14d,%eax
jle    11d0 <find_device+0x50>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    (%rbx),%rdi
call   1070 <xstrdup@plt>
mov    %rax,%r12
mov    %ebp,%r14d
jmp    11d0 <find_device+0x50>
mov    %r13,%rdi
call   1040 <endmntent@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mntent = type { i8*, i8* }
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"Cannot open /proc/mounts: %m\00"
@.str.3 = [5 x i8] c"/dev\00"
define i8* @find_device(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.mntent*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
%8 = call i32* @setmntent(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* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i32 0, i32* %5
store i8* null, i8** %6
br label %14
14: ; preds = %49, %24, %13
%15 = load i32*, i32** %3
%16 = call %struct.mntent* @getmntent(i32* %15)
store %struct.mntent* %16, %struct.mntent** %4
%17 = icmp ne %struct.mntent* %16, null
br i1 %17, label %18, label %50
18: ; preds = %14
%19 = load %struct.mntent*, %struct.mntent** %4
%20 = getelementptr inbounds %struct.mntent, %struct.mntent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @path_begins_with(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %18
br label %14
25: ; preds = %18
%26 = load i8*, i8** %2
%27 = load %struct.mntent*, %struct.mntent** %4
%28 = getelementptr inbounds %struct.mntent, %struct.mntent* %27, i32 0, i32 1
%29 = load i8*, i8** %28
%30 = call i64 @path_begins_with(i8* %26, i8* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %49
32: ; preds = %25
%33 = load %struct.mntent*, %struct.mntent** %4
%34 = getelementptr inbounds %struct.mntent, %struct.mntent* %33, i32 0, i32 1
%35 = load i8*, i8** %34
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = load i32, i32* %5
%39 = icmp sgt i32 %37, %38
br i1 %39, label %40, label %48
40: ; preds = %32
%41 = load i32, i32* %7
store i32 %41, i32* %5
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
%44 = load %struct.mntent*, %struct.mntent** %4
%45 = getelementptr inbounds %struct.mntent, %struct.mntent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i8* @xstrdup(i8* %46)
store i8* %47, i8** %6
br label %48
48: ; preds = %40, %32
br label %49
49: ; preds = %48, %25
br label %14
50: ; preds = %14
%51 = load i32*, i32** %3
%52 = call i32 @endmntent(i32* %51)
%53 = load i8*, i8** %6
ret i8* %53
}

LLVM IR (-O2):
%struct.mntent = type { i8*, i8* }
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"Cannot open /proc/mounts: %m\00"
@.str.3 = [5 x i8] c"/dev\00"
define i8* @find_device(i8* %0) {
%2 = tail call i32* @setmntent(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))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
br label %7
7: ; preds = %6, %29
%8 = phi i8* [ null, %6 ], [ %33, %29 ]
%9 = phi i32 [ 0, %6 ], [ %26, %29 ]
br label %10
10: ; preds = %28, %7
br label %11
11: ; preds = %10, %14
%12 = tail call %struct.mntent* @getmntent(i32* %2)
%13 = icmp eq %struct.mntent* %12, null
br i1 %13, label %34, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.mntent, %struct.mntent* %12, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i64 @path_begins_with(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %11, label %19
19: ; preds = %14
%20 = getelementptr inbounds %struct.mntent, %struct.mntent* %12, i64 0, i32 1
%21 = load i8*, i8** %20
%22 = tail call i64 @path_begins_with(i8* %0, i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %19
%25 = load i8*, i8** %20
%26 = tail call i32 @strlen(i8* %25)
%27 = icmp sgt i32 %26, %9
br i1 %27, label %29, label %28
28: ; preds = %24, %19
br label %10
29: ; preds = %24
%30 = getelementptr inbounds %struct.mntent, %struct.mntent* %12, i64 0, i32 0
%31 = tail call i32 @free(i8* %8)
%32 = load i8*, i8** %30
%33 = tail call i8* @xstrdup(i8* %32)
br label %7
34: ; preds = %11
%35 = tail call i32 @endmntent(i32* %2)
ret i8* %8
}