#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*/ toks;
typedef int /*<<< orphan*/ lines;

/* Variables and functions */
int /*<<< orphan*/ MNT_DETACH;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int readfile(char *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int strsplit(char *, char, char **, int);
int umount2(char *, int /*<<< orphan*/);
void detachbut(char *path) {
  char *buf;
  int i, nrd, bufsize;
  bufsize = 16384;
  buf = malloc(bufsize);
  if ((nrd = readfile("/proc/mounts", buf, bufsize - 1)) == -1) {
    fprintf(stderr, "read /proc/mounts: %s\n", strerror(errno));
    exit(1);
  }
  buf[nrd] = '\0';
  char *lines[128];
  int nlin;
  nlin = strsplit(buf, '\n', lines, (int)(sizeof(lines) / sizeof(lines[0])));
  for (i = nlin - 1; i >= 0; i--) {
    char *toks[16];
    int ntok;
    ntok = strsplit(lines[i], ' ', toks, (int)(sizeof(toks) / sizeof(toks[0])));
    if (ntok >= 2 && strcmp(toks[1], path) != 0) {
      if (umount2(toks[1], MNT_DETACH) == -1) {
        fprintf(stderr, "umount %s: %s\n", toks[1], strerror(errno));
        exit(1);
      }
    }
  }
  free(buf);
}


x86 assembly (clang, -O0):
<detachbut>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4d0,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x4000,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
lea    0xe3d(%rip),%rdi
call   10b0 <readfile@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1212 <detachbut+0x82>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4b8(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x4b8(%rbp),%edi
mov    %rax,%rdx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rdx
mov    $0xa,%esi
mov    $0x80,%ecx
call   1090 <strsplit@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     132c <detachbut+0x19c>
movslq -0x14(%rbp),%rax
mov    -0x420(%rbp,%rax,8),%rdi
ff
lea    -0x4b0(%rbp),%rdx
mov    $0x20,%esi
mov    $0x10,%ecx
call   1090 <strsplit@plt>
mov    %eax,-0x4b4(%rbp)
cmpl   $0x2,-0x4b4(%rbp)
jl     1319 <detachbut+0x189>
mov    -0x4a8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1319 <detachbut+0x189>
mov    -0x4a8(%rbp),%rdi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
call   1040 <umount2@plt>
cmp    $0xffffffff,%eax
jne    1314 <detachbut+0x184>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c4(%rbp)
mov    -0x4a8(%rbp),%rax
mov    %rax,-0x4c0(%rbp)
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x4c4(%rbp),%edi
mov    -0x4c0(%rbp),%rdx
mov    %rax,%rcx
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1319 <detachbut+0x189>
jmp    131e <detachbut+0x18e>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    124a <detachbut+0xba>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x4d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detachbut>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x480,%rsp
mov    %rdi,%r15
mov    $0x4000,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe49(%rip),%rdi
mov    %rax,%rsi
mov    $0x3fff,%edx
call   10b0 <readfile@plt>
cmp    $0xffffffff,%eax
je     12ba <detachbut+0x12a>
cltq
movb   $0x0,(%r14,%rax,1)
lea    0x80(%rsp),%rdx
mov    %r14,%rdi
mov    $0xa,%esi
mov    $0x80,%ecx
call   1090 <strsplit@plt>
test   %eax,%eax
jle    12a1 <detachbut+0x111>
mov    %eax,%ebx
add    $0x1,%rbx
mov    %rsp,%r12
mov    0x2dca(%rip),%r13
jmp    121e <detachbut+0x8e>
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
jbe    12a1 <detachbut+0x111>
lea    -0x2(%rbx),%eax
mov    0x80(%rsp,%rax,8),%rdi
mov    $0x20,%esi
mov    %r12,%rdx
mov    $0x10,%ecx
call   1090 <strsplit@plt>
cmp    $0x2,%eax
jl     1210 <detachbut+0x80>
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1210 <detachbut+0x80>
mov    0x8(%rsp),%rdi
mov    0x0(%r13),%esi
call   1040 <umount2@plt>
cmp    $0xffffffff,%eax
jne    1210 <detachbut+0x80>
mov    0x2d74(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rsp),%r14
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd9c(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x480,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d1f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd35(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/mounts\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"read /proc/mounts: %s\0A\00"
@errno = global i32 0
@MNT_DETACH = global i32 0
@.str.2 = [15 x i8] c"umount %s: %s\0A\00"
define void @detachbut(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8*]
%8 = alloca i32
%9 = alloca [16 x i8*]
%10 = alloca i32
store i8* %0, i8** %2
store i32 16384, i32* %6
%11 = load i32, i32* %6
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = load i32, i32* %6
%15 = sub nsw i32 %14, 1
%16 = call i32 @readfile(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %15)
store i32 %16, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %1
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %3
%30 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 0
%31 = call i32 @strsplit(i8* %29, i8 signext 10, i8** %30, i32 128)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 1
store i32 %33, i32* %4
br label %34
34: ; preds = %68, %24
%35 = load i32, i32* %4
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %71
37: ; preds = %34
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 %39
%41 = load i8*, i8** %40
%42 = getelementptr inbounds [16 x i8*], [16 x i8*]* %9, i64 0, i64 0
%43 = call i32 @strsplit(i8* %41, i8 signext 32, i8** %42, i32 16)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp sge i32 %44, 2
br i1 %45, label %46, label %67
46: ; preds = %37
%47 = getelementptr inbounds [16 x i8*], [16 x i8*]* %9, i64 0, i64 1
%48 = load i8*, i8** %47
%49 = load i8*, i8** %2
%50 = call i64 @strcmp(i8* %48, i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %67
52: ; preds = %46
%53 = getelementptr inbounds [16 x i8*], [16 x i8*]* %9, i64 0, i64 1
%54 = load i8*, i8** %53
%55 = load i32, i32* @MNT_DETACH
%56 = call i32 @umount2(i8* %54, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %66
58: ; preds = %52
%59 = load i32, i32* @stderr
%60 = getelementptr inbounds [16 x i8*], [16 x i8*]* %9, i64 0, i64 1
%61 = load i8*, i8** %60
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %61, i8* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %52
br label %67
67: ; preds = %66, %46, %37
br label %68
68: ; preds = %67
%69 = load i32, i32* %4
%70 = add nsw i32 %69, -1
store i32 %70, i32* %4
br label %34
71: ; preds = %34
%72 = load i8*, i8** %3
%73 = call i32 @free(i8* %72)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/mounts\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"read /proc/mounts: %s\0A\00"
@errno = global i32 0
@MNT_DETACH = global i32 0
@.str.2 = [15 x i8] c"umount %s: %s\0A\00"
define void @detachbut(i8* %0) {
%2 = alloca [128 x i8*]
%3 = alloca [16 x i8*]
%4 = tail call dereferenceable_or_null(16384) i8* @malloc(i32 16384)
%5 = tail call i32 @readfile(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %4, i32 16383)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = sext i32 %5 to i64
%15 = getelementptr inbounds i8, i8* %4, i64 %14
store i8 0, i8* %15
%16 = bitcast [128 x i8*]* %2 to i8*
%17 = getelementptr inbounds [128 x i8*], [128 x i8*]* %2, i64 0, i64 0
%18 = call i32 @strsplit(i8* %4, i8 signext 10, i8** %17, i32 128)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %51
20: ; preds = %13
%21 = bitcast [16 x i8*]* %3 to i8*
%22 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%23 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 1
%24 = zext i32 %18 to i64
br label %25
25: ; preds = %20, %49
%26 = phi i64 [ %24, %20 ], [ %27, %49 ]
%27 = add nsw i64 %26, -1
%28 = and i64 %27, 4294967295
%29 = getelementptr inbounds [128 x i8*], [128 x i8*]* %2, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = call i32 @strsplit(i8* %30, i8 signext 32, i8** %22, i32 16)
%32 = icmp sgt i32 %31, 1
br i1 %32, label %33, label %49
33: ; preds = %25
%34 = load i8*, i8** %23
%35 = call i64 @strcmp(i8* %34, i8* %0)
%36 = icmp eq i64 %35, 0
br i1 %36, label %49, label %37
37: ; preds = %33
%38 = load i8*, i8** %23
%39 = load i32, i32* @MNT_DETACH
%40 = call i32 @umount2(i8* %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %49
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %23
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %37, %33, %25
%50 = icmp ugt i64 %26, 1
br i1 %50, label %25, label %51
51: ; preds = %49, %13
%52 = call i32 @free(i8* %4)
ret void
}