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

/* Variables and functions */
int /*<<< orphan*/ MAXPATHLEN;
int /*<<< orphan*/ endmntent(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
struct mntent *getmntent(int /*<<< orphan*/ *);
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *realpath(char const *, char *);
int /*<<< orphan*/ *setmntent(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char const *, int);
scalar_t__ umount(char const *);
int umount_submounts(const char *root) {
  FILE *fp;
  struct mntent *mnt;
  int len;
  char path[MAXPATHLEN + 1];
  if (realpath(root, path) == NULL) {
    logger(-1, errno, "realpath(%s) failed", root);
    return -1;
  }
  if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
    logger(-1, errno, "Unable to open /proc/mounts");
    return -1;
  }
  strcat(path, "/");
  len = strlen(path);
  while ((mnt = getmntent(fp)) != NULL) {
    if (strncmp(path, mnt->mnt_dir, len) == 0) {
      if (umount(mnt->mnt_dir))
        logger(-1, errno, "Cannot umount %s", mnt->mnt_dir);
    }
  }
  endmntent(fp);
  return 0;
}


x86 assembly (clang, -O0):
<umount_submounts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <realpath@plt>
cmp    $0x0,%rax
jne    1211 <umount_submounts+0x81>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe09(%rip),%rdx
mov    $0x0,%al
call   1070 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    130b <umount_submounts+0x17b>
lea    0xe0d(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   10a0 <setmntent@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1261 <umount_submounts+0xd1>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edi
lea    0xdcf(%rip),%rdx
mov    $0x0,%al
call   1070 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    130b <umount_submounts+0x17b>
mov    -0x48(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x48(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <getmntent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12f4 <umount_submounts+0x164>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x24(%rbp),%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12ef <umount_submounts+0x15f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <umount@plt>
cmp    $0x0,%rax
je     12ea <umount_submounts+0x15a>
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    $0xffffffff,%edi
lea    0xd51(%rip),%rdx
mov    $0x0,%al
call   1070 <logger@plt>
jmp    12ef <umount_submounts+0x15f>
jmp    127d <umount_submounts+0xed>
mov    -0x18(%rbp),%rdi
call   1030 <endmntent@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<umount_submounts>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rsi
call   1080 <realpath@plt>
test   %rax,%rax
je     1289 <umount_submounts+0xf9>
lea    0xe4f(%rip),%rdi
lea    0xe37(%rip),%rsi
call   10a0 <setmntent@plt>
test   %rax,%rax
je     12af <umount_submounts+0x11f>
mov    %rax,%r14
lea    0xe3d(%rip),%rsi
mov    %r13,%rdi
call   10b0 <strcat@plt>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1090 <getmntent@plt>
test   %rax,%rax
je     127d <umount_submounts+0xed>
mov    %rax,%rbx
lea    0xe15(%rip),%r12
jmp    1240 <umount_submounts+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1090 <getmntent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     127d <umount_submounts+0xed>
mov    (%rbx),%rsi
mov    %r13,%rdi
mov    %r15d,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1230 <umount_submounts+0xa0>
mov    (%rbx),%rdi
call   1050 <umount@plt>
test   %rax,%rax
je     1230 <umount_submounts+0xa0>
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
mov    (%rbx),%rcx
mov    $0xffffffff,%edi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <logger@plt>
jmp    1230 <umount_submounts+0xa0>
mov    %r14,%rdi
call   1030 <endmntent@plt>
xor    %ebx,%ebx
jmp    12d0 <umount_submounts+0x140>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
lea    0xd67(%rip),%rdx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <logger@plt>
jmp    12d0 <umount_submounts+0x140>
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
lea    0xd57(%rip),%rdx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
xor    %eax,%eax
call   1070 <logger@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.mntent = type { i8* }
@MAXPATHLEN = global i32 0
@errno = global i32 0
@.str = [20 x i8] c"realpath(%s) failed\00"
@.str.1 = [13 x i8] c"/proc/mounts\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [28 x i8] c"Unable to open /proc/mounts\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [17 x i8] c"Cannot umount %s\00"
define i32 @umount_submounts(i8* %0) {
%2 = load i32, i32* @MAXPATHLEN
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32* @realpath(i8* %0, i8* %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i32 (i32, i32, i8*, ...) @logger(i32 -1, i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %41
11: ; preds = %1
%12 = call i32* @setmntent(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = call i32 (i32, i32, i8*, ...) @logger(i32 -1, i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %41
17: ; preds = %11
%18 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @strlen(i8* %5)
%20 = call %struct.mntent* @getmntent(i32* %12)
%21 = icmp eq %struct.mntent* %20, null
br i1 %21, label %39, label %22
22: ; preds = %17, %36
%23 = phi %struct.mntent* [ %37, %36 ], [ %20, %17 ]
%24 = getelementptr inbounds %struct.mntent, %struct.mntent* %23, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @strncmp(i8* %5, i8* %25, i32 %19)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %22
%29 = load i8*, i8** %24
%30 = call i64 @umount(i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i32, i32* @errno
%34 = load i8*, i8** %24
%35 = call i32 (i32, i32, i8*, ...) @logger(i32 -1, i32 %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %28, %32, %22
%37 = call %struct.mntent* @getmntent(i32* %12)
%38 = icmp eq %struct.mntent* %37, null
br i1 %38, label %39, label %22
39: ; preds = %36, %17
%40 = call i32 @endmntent(i32* %12)
br label %41
41: ; preds = %39, %14, %8
%42 = phi i32 [ -1, %8 ], [ -1, %14 ], [ 0, %39 ]
ret i32 %42
}