#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strerror(int /*<<< orphan*/);
char *get_mount_path(char *path) {
  FILE *f;
  int ret;
  char *out_path = NULL;
  f = fopen("/proc/mounts", "r");
  if (!f) {
    fprintf(stderr, "Cannot open /proc/mounts: %s\n", strerror(errno));
    return NULL;
  }
  while (1) {
    char *line = NULL, *delim;
    size_t insize;
    ret = getline(&line, &insize, f);
    if (ret < 0)
      break;
    delim = strchr(line, ' ');
    if (!delim) {
      free(line);
      continue;
    }
    *delim = 0;
    if (strcmp(line, path)) {
      free(line);
      continue;
    }
    delim++;
    out_path = delim;
    delim = strchr(delim, ' ');
    if (!delim) {
      free(line);
      out_path = NULL;
      break;
    }
    *delim = 0;
    break;
  }
  fclose(f);
  if (out_path)
    return strdup(out_path);
  else
    return NULL;
}


x86 assembly (clang, -O0):
<get_mount_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe55(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ff <get_mount_path+0x6f>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe22(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1302 <get_mount_path+0x172>
jmp    1204 <get_mount_path+0x74>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122f <get_mount_path+0x9f>
jmp    12d4 <get_mount_path+0x144>
mov    -0x30(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    125a <get_mount_path+0xca>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1204 <get_mount_path+0x74>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1286 <get_mount_path+0xf6>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1204 <get_mount_path+0x74>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12cd <get_mount_path+0x13d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    12d4 <get_mount_path+0x144>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     12fa <get_mount_path+0x16a>
mov    -0x28(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1302 <get_mount_path+0x172>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mount_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xe58(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1276 <get_mount_path+0xe6>
mov    %rax,%r13
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1090 <getline@plt>
test   %eax,%eax
js     12aa <get_mount_path+0x11a>
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    121c <get_mount_path+0x8c>
nop
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <getline@plt>
test   %eax,%eax
js     12aa <get_mount_path+0x11a>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11f5 <get_mount_path+0x65>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11f0 <get_mount_path+0x60>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12a0 <get_mount_path+0x110>
movb   $0x0,(%rax)
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
jmp    12b4 <get_mount_path+0x124>
mov    0x2d63(%rip),%rax
mov    (%rax),%ebx
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd7b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    12b2 <get_mount_path+0x122>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Cannot open /proc/mounts: %s\0A\00"
@errno = global i32 0
define i8* @get_mount_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %6
%10 = 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* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %16)
store i8* null, i8** %2
br label %64
18: ; preds = %1
br label %19
19: ; preds = %18, %30, %39
store i8* null, i8** %7
%20 = load i32*, i32** %4
%21 = call i32 @getline(i8** %7, i64* %9, i32* %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %55
25: ; preds = %19
%26 = load i8*, i8** %7
%27 = call i8* @strchr(i8* %26, i8 signext 32)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = call i32 @free(i8* %31)
br label %19
33: ; preds = %25
%34 = load i8*, i8** %8
store i8 0, i8* %34
%35 = load i8*, i8** %7
%36 = load i8*, i8** %3
%37 = call i64 @strcmp(i8* %35, i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
br label %19
42: ; preds = %33
%43 = load i8*, i8** %8
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %8
%45 = load i8*, i8** %8
store i8* %45, i8** %6
%46 = load i8*, i8** %8
%47 = call i8* @strchr(i8* %46, i8 signext 32)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp ne i8* %48, null
br i1 %49, label %53, label %50
50: ; preds = %42
%51 = load i8*, i8** %7
%52 = call i32 @free(i8* %51)
store i8* null, i8** %6
br label %55
53: ; preds = %42
%54 = load i8*, i8** %8
store i8 0, i8* %54
br label %55
55: ; preds = %53, %50, %24
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %6
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i8*, i8** %6
%62 = call i8* @strdup(i8* %61)
store i8* %62, i8** %2
br label %64
63: ; preds = %55
store i8* null, i8** %2
br label %64
64: ; preds = %63, %60, %13
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"Cannot open /proc/mounts: %s\0A\00"
@errno = global i32 0
define noalias i8* @get_mount_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%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 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = bitcast i8** %2 to i8*
%8 = bitcast i64* %3 to i8*
store i8* null, i8** %2
%9 = call i32 @getline(i8** %2, i64* %3, i32* %4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %42, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %44
16: ; preds = %6, %37
%17 = load i8*, i8** %2
%18 = call i8* @strchr(i8* dereferenceable(1) %17, i8 signext 32)
%19 = icmp eq i8* %18, null
br i1 %19, label %37, label %20
20: ; preds = %16
store i8 0, i8* %18
%21 = load i8*, i8** %2
%22 = call i64 @strcmp(i8* %21, i8* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = load i8*, i8** %2
br label %37
26: ; preds = %20
%27 = getelementptr inbounds i8, i8* %18, i64 1
%28 = call i8* @strchr(i8* %27, i8 signext 32)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8*, i8** %2
%32 = call i32 @free(i8* %31)
%33 = call i32 @fclose(i32* %4)
br label %44
34: ; preds = %26
store i8 0, i8* %28
%35 = call i32 @fclose(i32* %4)
%36 = call i8* @strdup(i8* %27)
br label %44
37: ; preds = %16, %24
%38 = phi i8* [ %25, %24 ], [ %17, %16 ]
%39 = call i32 @free(i8* %38)
store i8* null, i8** %2
%40 = call i32 @getline(i8** %2, i64* %3, i32* %4)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %16
42: ; preds = %37, %6
%43 = call i32 @fclose(i32* %4)
br label %44
44: ; preds = %42, %30, %34, %11
%45 = phi i8* [ %36, %34 ], [ null, %11 ], [ null, %42 ], [ null, %30 ]
ret i8* %45
}