#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 scalar_t__ uid_t;
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
int EROFS;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int /*<<< orphan*/ W_OK;
char *_PATH_MOUNTED;
int access(char *, int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ getuid();
int lstat(char *, struct stat *);
int /*<<< orphan*/ setreuid(scalar_t__, int);
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char *, size_t);
int mtab_needs_update(const char *mnt) {
  int res;
  struct stat stbuf;
  if (strncmp(mnt, _PATH_MOUNTED, strlen(mnt)) == 0 &&
      _PATH_MOUNTED[strlen(mnt)] == '/')
    return 0;
  res = lstat(_PATH_MOUNTED, &stbuf);
  if (res == -1) {
    if (errno == ENOENT)
      return 0;
  } else {
    uid_t ruid;
    int err;
    if (S_ISLNK(stbuf.st_mode))
      return 0;
    ruid = getuid();
    if (ruid != 0)
      setreuid(0, -1);
    res = access(_PATH_MOUNTED, W_OK);
    err = (res == -1) ? errno : 0;
    if (ruid != 0)
      setreuid(ruid, -1);
    if (err == EROFS)
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<mtab_needs_update>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11ec <mtab_needs_update+0x7c>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11ec <mtab_needs_update+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    1306 <mtab_needs_update+0x196>
mov    0x2ded(%rip),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1237 <mtab_needs_update+0xc7>
mov    0x2db5(%rip),%rax
mov    (%rax),%rax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <mtab_needs_update+0xc2>
movl   $0x0,-0x4(%rbp)
jmp    1306 <mtab_needs_update+0x196>
jmp    12ff <mtab_needs_update+0x18f>
mov    -0x18(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1255 <mtab_needs_update+0xe5>
movl   $0x0,-0x4(%rbp)
jmp    1306 <mtab_needs_update+0x196>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1279 <mtab_needs_update+0x109>
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%esi
call   1080 <setreuid@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%rdi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12b1 <mtab_needs_update+0x141>
mov    0x2d23(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    12bc <mtab_needs_update+0x14c>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    12bc <mtab_needs_update+0x14c>
mov    -0x48(%rbp),%rax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12dc <mtab_needs_update+0x16c>
mov    -0x20(%rbp),%rdi
mov    $0xffffffff,%esi
call   1080 <setreuid@plt>
mov    -0x24(%rbp),%eax
mov    0x2d02(%rip),%rcx
cmp    (%rcx),%eax
jne    12fa <mtab_needs_update+0x18a>
movl   $0x0,-0x4(%rbp)
jmp    1306 <mtab_needs_update+0x196>
jmp    12ff <mtab_needs_update+0x18f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtab_needs_update>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e5f(%rip),%r15
mov    (%r15),%r14
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
mov    (%r15),%r14
test   %rax,%rax
jne    11b7 <mtab_needs_update+0x47>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmpb   $0x2f,(%r14,%rcx,1)
je     1253 <mtab_needs_update+0xe3>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <lstat@plt>
cmp    $0xffffffff,%eax
je     1236 <mtab_needs_update+0xc6>
mov    (%rsp),%edi
call   1040 <S_ISLNK@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1253 <mtab_needs_update+0xe3>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f6 <mtab_needs_update+0x86>
xor    %edi,%edi
mov    $0xffffffff,%esi
call   1080 <setreuid@plt>
mov    (%r15),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
jne    1215 <mtab_needs_update+0xa5>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebp
test   %rbx,%rbx
je     1227 <mtab_needs_update+0xb7>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1080 <setreuid@plt>
mov    0x2dba(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%ebp
jne    124e <mtab_needs_update+0xde>
jmp    1253 <mtab_needs_update+0xe3>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rcx
mov    0x2d71(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     1253 <mtab_needs_update+0xe3>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@_PATH_MOUNTED = global i8* null
@errno = global i64 0
@ENOENT = global i64 0
@W_OK = global i32 0
@EROFS = global i32 0
define i32 @mtab_needs_update(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i8*, i8** @_PATH_MOUNTED
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = call i64 @strncmp(i8* %8, i8* %9, i64 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %1
%15 = load i8*, i8** @_PATH_MOUNTED
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 47
br i1 %21, label %22, label %23
22: ; preds = %14
store i32 0, i32* %2
br label %70
23: ; preds = %14, %1
%24 = load i8*, i8** @_PATH_MOUNTED
%25 = call i32 @lstat(i8* %24, %struct.stat* %5)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 0, i32* %2
br label %70
33: ; preds = %28
br label %69
34: ; preds = %23
%35 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @S_ISLNK(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 0, i32* %2
br label %70
40: ; preds = %34
%41 = call i64 (...) @getuid()
store i64 %41, i64* %6
%42 = load i64, i64* %6
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @setreuid(i64 0, i32 -1)
br label %46
46: ; preds = %44, %40
%47 = load i8*, i8** @_PATH_MOUNTED
%48 = load i32, i32* @W_OK
%49 = call i32 @access(i8* %47, i32 %48)
store i32 %49, i32* %4
%50 = load i32, i32* %4
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = load i64, i64* @errno
br label %55
54: ; preds = %46
br label %55
55: ; preds = %54, %52
%56 = phi i64 [ %53, %52 ], [ 0, %54 ]
%57 = trunc i64 %56 to i32
store i32 %57, i32* %7
%58 = load i64, i64* %6
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i64, i64* %6
%62 = call i32 @setreuid(i64 %61, i32 -1)
br label %63
63: ; preds = %60, %55
%64 = load i32, i32* %7
%65 = load i32, i32* @EROFS
%66 = icmp eq i32 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63
store i32 0, i32* %2
br label %70
68: ; preds = %63
br label %69
69: ; preds = %68, %33
store i32 1, i32* %2
br label %70
70: ; preds = %69, %67, %39, %32, %22
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@_PATH_MOUNTED = global i8* null
@errno = global i64 0
@ENOENT = global i64 0
@W_OK = global i32 0
@EROFS = global i32 0
define i32 @mtab_needs_update(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i8*, i8** @_PATH_MOUNTED
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = tail call i64 @strncmp(i8* %0, i8* %4, i64 %5)
%7 = icmp eq i64 %6, 0
%8 = load i8*, i8** @_PATH_MOUNTED
br i1 %7, label %9, label %14
9: ; preds = %1
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 47
br i1 %13, label %45, label %14
14: ; preds = %1, %9
%15 = call i32 @lstat(i8* %8, %struct.stat* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %45, label %44
21: ; preds = %14
%22 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i64 @S_ISLNK(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %45
26: ; preds = %21
%27 = tail call i64 (...) @getuid()
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @setreuid(i64 0, i32 -1)
br label %31
31: ; preds = %29, %26
%32 = load i8*, i8** @_PATH_MOUNTED
%33 = load i32, i32* @W_OK
%34 = tail call i32 @access(i8* %32, i32 %33)
%35 = icmp eq i32 %34, -1
%36 = load i64, i64* @errno
%37 = trunc i64 %36 to i32
%38 = select i1 %35, i32 %37, i32 0
br i1 %28, label %41, label %39
39: ; preds = %31
%40 = tail call i32 @setreuid(i64 %27, i32 -1)
br label %41
41: ; preds = %39, %31
%42 = load i32, i32* @EROFS
%43 = icmp eq i32 %38, %42
br i1 %43, label %45, label %44
44: ; preds = %41, %17
br label %45
45: ; preds = %21, %17, %9, %41, %44
%46 = phi i32 [ 1, %44 ], [ 0, %41 ], [ 0, %9 ], [ 0, %17 ], [ 0, %21 ]
ret i32 %46
}