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

/* Variables and functions */
int /*<<< orphan*/ _PATH_MOUNTED;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ endmntent(int /*<<< orphan*/ *);
struct mntent *getmntent(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ *setmntent(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, size_t);
int lookup_device(const char *dir, char **dev_name) {
  FILE *mtab = NULL;
  struct mntent *mtab_entry = NULL;
  size_t fsname_len = 0;
  int retval = -1;
  assert(NULL != dir);
  assert(NULL != dev_name);
  if (NULL == (mtab = setmntent(_PATH_MOUNTED, "r"))) {
    return -2;
  }
  while (NULL != (mtab_entry = getmntent(mtab))) {
    if (!strcmp(mtab_entry->mnt_dir, dir)) {
      fsname_len = strlen(mtab_entry->mnt_fsname) + 1;
      *dev_name = malloc(fsname_len);
      assert(NULL != *dev_name);
      strncpy(*dev_name, mtab_entry->mnt_fsname, fsname_len);
      retval = 0;
      break;
    }
  }
  endmntent(mtab);
  return retval;
}


x86 assembly (clang, -O0):
<lookup_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   10a0 <setmntent@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1208 <lookup_device+0x88>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12b4 <lookup_device+0x134>
jmp    120d <lookup_device+0x8d>
mov    -0x20(%rbp),%rdi
call   1090 <getmntent@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     12a5 <lookup_device+0x125>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    12a0 <lookup_device+0x120>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1040 <strncpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    12a5 <lookup_device+0x125>
jmp    120d <lookup_device+0x8d>
mov    -0x20(%rbp),%rdi
call   1030 <endmntent@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_device>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1070 <assert@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   10a0 <setmntent@plt>
test   %rax,%rax
je     1226 <lookup_device+0xa6>
mov    %rax,%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1090 <getmntent@plt>
test   %rax,%rax
je     122d <lookup_device+0xad>
mov    %rax,%rbx
mov    0x4(%rax),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    11d0 <lookup_device+0x50>
mov    (%rbx),%edi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r12
mov    %r12,%rdi
call   1080 <malloc@plt>
mov    %rax,(%r14)
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    (%r14),%rdi
mov    (%rbx),%esi
mov    %r12,%rdx
call   1040 <strncpy@plt>
xor    %ebx,%ebx
jmp    1232 <lookup_device+0xb2>
mov    $0xfffffffe,%ebx
jmp    123a <lookup_device+0xba>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1030 <endmntent@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.mntent = type { i32, i32 }
@_PATH_MOUNTED = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @lookup_device(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.mntent*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store %struct.mntent* null, %struct.mntent** %7
store i64 0, i64* %8
store i32 -1, i32* %9
%10 = load i8*, i8** %4
%11 = icmp ne i8* null, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i8**, i8*** %5
%15 = icmp ne i8** null, %14
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* @_PATH_MOUNTED
%19 = call i32* @setmntent(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %6
%20 = icmp eq i32* null, %19
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 -2, i32* %3
br label %61
22: ; preds = %2
br label %23
23: ; preds = %56, %22
%24 = load i32*, i32** %6
%25 = call %struct.mntent* @getmntent(i32* %24)
store %struct.mntent* %25, %struct.mntent** %7
%26 = icmp ne %struct.mntent* null, %25
br i1 %26, label %27, label %57
27: ; preds = %23
%28 = load %struct.mntent*, %struct.mntent** %7
%29 = getelementptr inbounds %struct.mntent, %struct.mntent* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i8*, i8** %4
%32 = call i32 @strcmp(i32 %30, i8* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %56, label %34
34: ; preds = %27
%35 = load %struct.mntent*, %struct.mntent** %7
%36 = getelementptr inbounds %struct.mntent, %struct.mntent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @strlen(i32 %37)
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
store i64 %40, i64* %8
%41 = load i64, i64* %8
%42 = call i8* @malloc(i64 %41)
%43 = load i8**, i8*** %5
store i8* %42, i8** %43
%44 = load i8**, i8*** %5
%45 = load i8*, i8** %44
%46 = icmp ne i8* null, %45
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
%51 = load %struct.mntent*, %struct.mntent** %7
%52 = getelementptr inbounds %struct.mntent, %struct.mntent* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i64, i64* %8
%55 = call i32 @strncpy(i8* %50, i32 %53, i64 %54)
store i32 0, i32* %9
br label %57
56: ; preds = %27
br label %23
57: ; preds = %34, %23
%58 = load i32*, i32** %6
%59 = call i32 @endmntent(i32* %58)
%60 = load i32, i32* %9
store i32 %60, i32* %3
br label %61
61: ; preds = %57, %21
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.mntent = type { i32, i32 }
@_PATH_MOUNTED = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @lookup_device(i8* %0, i8** %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8** %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @_PATH_MOUNTED
%10 = tail call i32* @setmntent(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %36, label %12
12: ; preds = %2, %15
%13 = tail call %struct.mntent* @getmntent(i32* %10)
%14 = icmp eq %struct.mntent* %13, null
br i1 %14, label %33, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.mntent, %struct.mntent* %13, i64 0, i32 1
%17 = load i32, i32* %16
%18 = tail call i32 @strcmp(i32 %17, i8* %0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %12
20: ; preds = %15
%21 = getelementptr inbounds %struct.mntent, %struct.mntent* %13, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @strlen(i32 %22)
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = tail call i8* @malloc(i64 %25)
store i8* %26, i8** %1
%27 = icmp ne i8* %26, null
%28 = zext i1 %27 to i32
%29 = tail call i32 @assert(i32 %28)
%30 = load i8*, i8** %1
%31 = load i32, i32* %21
%32 = tail call i32 @strncpy(i8* %30, i32 %31, i64 %25)
br label %33
33: ; preds = %12, %20
%34 = phi i32 [ 0, %20 ], [ -1, %12 ]
%35 = tail call i32 @endmntent(i32* %10)
br label %36
36: ; preds = %2, %33
%37 = phi i32 [ %34, %33 ], [ -2, %2 ]
ret i32 %37
}