#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *const, int);
scalar_t__ valid_mountpoint(char const *const, long);
const char *find_mountpoint(const char *fstype, long magic, char *mountpoint,
                            int len, const char *const *known_mountpoints) {
  const char *const *ptr;
  char format[128];
  char type[100];
  FILE *fp;
  if (known_mountpoints) {
    ptr = known_mountpoints;
    while (*ptr) {
      if (valid_mountpoint(*ptr, magic) == 0) {
        strncpy(mountpoint, *ptr, len - 1);
        mountpoint[len - 1] = 0;
        return mountpoint;
      }
      ptr++;
    }
  }
  fp = fopen("/proc/mounts", "r");
  if (fp == NULL)
    return NULL;
  snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len);
  while (fscanf(fp, format, mountpoint, type) == 2) {
    if (strcmp(type, fstype) == 0)
      break;
  }
  fclose(fp);
  if (strcmp(type, fstype) != 0)
    return NULL;
  return mountpoint;
}


x86 assembly (clang, -O0):
<find_mountpoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1213 <find_mountpoint+0xa3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpq   $0x0,(%rax)
je     120e <find_mountpoint+0x9e>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <valid_mountpoint@plt>
cmp    $0x0,%rax
jne    11fd <find_mountpoint+0x8d>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e9 <find_mountpoint+0x179>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    11a1 <find_mountpoint+0x31>
jmp    1213 <find_mountpoint+0xa3>
lea    0xde6(%rip),%rdi
lea    0xdec(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    1248 <find_mountpoint+0xd8>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <find_mountpoint+0x179>
lea    -0xc0(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    $0x80,%esi
lea    0xdb1(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x138(%rbp),%rdi
lea    -0xc0(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x130(%rbp),%rcx
call   1060 <fscanf@plt>
cmp    $0x2,%eax
jne    12ae <find_mountpoint+0x13e>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12a9 <find_mountpoint+0x139>
jmp    12ae <find_mountpoint+0x13e>
jmp    1263 <find_mountpoint+0xf3>
mov    -0x138(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12e1 <find_mountpoint+0x171>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <find_mountpoint+0x179>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_mountpoint>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rdi,%r12
test   %r8,%r8
je     11ce <find_mountpoint+0x5e>
mov    %r8,%rbp
mov    (%r8),%rdi
test   %rdi,%rdi
je     11ce <find_mountpoint+0x5e>
mov    %rsi,%rbx
add    $0x8,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rsi
call   1090 <valid_mountpoint@plt>
test   %rax,%rax
je     1259 <find_mountpoint+0xe9>
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    11b0 <find_mountpoint+0x40>
lea    0xe2b(%rip),%rdi
lea    0xe31(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1254 <find_mountpoint+0xe4>
mov    %rax,%rbp
lea    0xe1f(%rip),%rdx
lea    0x70(%rsp),%r13
mov    %r13,%rdi
mov    $0x80,%esi
mov    %r15d,%ecx
call   1050 <snprintf@plt>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <fscanf@plt>
cmp    $0x2,%eax
jne    1236 <find_mountpoint+0xc6>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1210 <find_mountpoint+0xa0>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rsp,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
cmovne %rcx,%r14
jmp    1272 <find_mountpoint+0x102>
xor    %r14d,%r14d
jmp    1272 <find_mountpoint+0x102>
mov    -0x8(%rbp),%rsi
movslq %r15d,%rbx
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %r14,%rax
add    $0xf8,%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"
@.str.2 = [33 x i8] c"%%*s %%%ds %%99s %%*s %%*d %%*d\0A\00"
define i8* @find_mountpoint(i8* %0, i64 %1, i8* %2, i32 %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8**
%12 = alloca i8**
%13 = alloca [128 x i8]
%14 = alloca [100 x i8]
%15 = alloca i32*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8** %4, i8*** %11
%16 = load i8**, i8*** %11
%17 = icmp ne i8** %16, null
br i1 %17, label %18, label %47
18: ; preds = %5
%19 = load i8**, i8*** %11
store i8** %19, i8*** %12
br label %20
20: ; preds = %43, %18
%21 = load i8**, i8*** %12
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %46
24: ; preds = %20
%25 = load i8**, i8*** %12
%26 = load i8*, i8** %25
%27 = load i64, i64* %8
%28 = call i64 @valid_mountpoint(i8* %26, i64 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %24
%31 = load i8*, i8** %9
%32 = load i8**, i8*** %12
%33 = load i8*, i8** %32
%34 = load i32, i32* %10
%35 = sub nsw i32 %34, 1
%36 = call i32 @strncpy(i8* %31, i8* %33, i32 %35)
%37 = load i8*, i8** %9
%38 = load i32, i32* %10
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %9
store i8* %42, i8** %6
br label %80
43: ; preds = %24
%44 = load i8**, i8*** %12
%45 = getelementptr inbounds i8*, i8** %44, i32 1
store i8** %45, i8*** %12
br label %20
46: ; preds = %20
br label %47
47: ; preds = %46, %5
%48 = 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* %48, i32** %15
%49 = load i32*, i32** %15
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %52
51: ; preds = %47
store i8* null, i8** %6
br label %80
52: ; preds = %47
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%54 = load i32, i32* %10
%55 = call i32 @snprintf(i8* %53, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %69, %52
%57 = load i32*, i32** %15
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%59 = load i8*, i8** %9
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%61 = call i32 @fscanf(i32* %57, i8* %58, i8* %59, i8* %60)
%62 = icmp eq i32 %61, 2
br i1 %62, label %63, label %70
63: ; preds = %56
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%65 = load i8*, i8** %7
%66 = call i64 @strcmp(i8* %64, i8* %65)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %63
br label %70
69: ; preds = %63
br label %56
70: ; preds = %68, %56
%71 = load i32*, i32** %15
%72 = call i32 @fclose(i32* %71)
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%74 = load i8*, i8** %7
%75 = call i64 @strcmp(i8* %73, i8* %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %70
store i8* null, i8** %6
br label %80
78: ; preds = %70
%79 = load i8*, i8** %9
store i8* %79, i8** %6
br label %80
80: ; preds = %78, %77, %51, %30
%81 = load i8*, i8** %6
ret i8* %81
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/mounts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"%%*s %%%ds %%99s %%*s %%*d %%*d\0A\00"
define i8* @find_mountpoint(i8* %0, i64 %1, i8* %2, i32 %3, i8** %4) {
%6 = alloca [128 x i8]
%7 = alloca [100 x i8]
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%10 = icmp eq i8** %4, null
br i1 %10, label %29, label %11
11: ; preds = %5
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %29, label %14
14: ; preds = %11, %25
%15 = phi i8* [ %27, %25 ], [ %12, %11 ]
%16 = phi i8** [ %26, %25 ], [ %4, %11 ]
%17 = tail call i64 @valid_mountpoint(i8* %15, i64 %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i8*, i8** %16
%21 = add nsw i32 %3, -1
%22 = tail call i32 @strncpy(i8* %2, i8* %20, i32 %21)
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %2, i64 %23
store i8 0, i8* %24
br label %45
25: ; preds = %14
%26 = getelementptr inbounds i8*, i8** %16, i64 1
%27 = load i8*, i8** %26
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %14
29: ; preds = %25, %11, %5
%30 = 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))
%31 = icmp eq i32* %30, null
br i1 %31, label %45, label %32
32: ; preds = %29
%33 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %3)
br label %34
34: ; preds = %37, %32
%35 = call i32 @fscanf(i32* %30, i8* %8, i8* %2, i8* %9)
%36 = icmp eq i32 %35, 2
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i64 @strcmp(i8* %9, i8* %0)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %34
40: ; preds = %37, %34
%41 = call i32 @fclose(i32* %30)
%42 = call i64 @strcmp(i8* %9, i8* %0)
%43 = icmp eq i64 %42, 0
%44 = select i1 %43, i8* %2, i8* null
br label %45
45: ; preds = %40, %29, %19
%46 = phi i8* [ %2, %19 ], [ null, %29 ], [ %44, %40 ]
ret i8* %46
}