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

/* Variables and functions */
int MS_RDONLY;
int MS_REMOUNT;
int accept_dad_hack();
int /*<<< orphan*/ endmntent(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *, int /*<<< orphan*/);
struct mntent *find_mnt_entry(int /*<<< orphan*/ *, char *);
scalar_t__ mount(int /*<<< orphan*/ *, int /*<<< orphan*/, char *, int,
                 int /*<<< orphan*/ *);
int read_flags(int /*<<< orphan*/);
int /*<<< orphan*/ *setmntent(char *, char *);
int /*<<< orphan*/ warnx(char *);
int main(void) {
  FILE *mtab = setmntent("/etc/mtab", "r");
  struct mntent *proc = find_mnt_entry(mtab, "/proc/sys");
  endmntent(mtab);
  int flags = 0, rc = 0;
  if (!proc)
    warnx("Failed to find /proc/sys mount");
  else {
    flags = read_flags(proc->mnt_opts) & ~MS_RDONLY;
    if (mount(NULL, proc->mnt_dir, "none", MS_REMOUNT | flags, NULL) < 0)
      err(1, "failed to remount %s rw", proc->mnt_dir);
  }
  rc = accept_dad_hack();
  if (proc) {
    if (mount(NULL, proc->mnt_dir, "none", MS_REMOUNT | MS_RDONLY | flags,
              NULL) < 0)
      err(1, "failed to remount %s ro", proc->mnt_dir);
  }
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1090 <setmntent@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1040 <find_mnt_entry@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <endmntent@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ed <main+0x6d>
lea    0xe33(%rip),%rdi
call   1060 <warnx@plt>
jmp    1256 <main+0xd6>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   10a0 <read_flags@plt>
mov    0x2de8(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
or     -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%r8d
lea    0xe0d(%rip),%rdx
mov    %r8,%rdi
call   1050 <mount@plt>
cmp    $0x0,%rax
jge    1251 <main+0xd1>
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    $0x1,%edi
lea    0xdee(%rip),%rsi
call   1080 <err@plt>
jmp    1256 <main+0xd6>
mov    $0x0,%al
call   1070 <accept_dad_hack@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12c0 <main+0x140>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%ecx
mov    0x2d67(%rip),%rax
or     (%rax),%ecx
or     -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%r8d
lea    0xda3(%rip),%rdx
mov    %r8,%rdi
call   1050 <mount@plt>
cmp    $0x0,%rax
jge    12bb <main+0x13b>
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    $0x1,%edi
lea    0xd9c(%rip),%rsi
call   1080 <err@plt>
jmp    12c0 <main+0x140>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xe71(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1090 <setmntent@plt>
mov    %rax,%rbp
lea    0xe67(%rip),%rsi
mov    %rax,%rdi
call   1040 <find_mnt_entry@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <endmntent@plt>
test   %rbx,%rbx
je     1259 <main+0xd9>
mov    0x4(%rbx),%edi
call   10a0 <read_flags@plt>
mov    0x2e18(%rip),%r15
mov    (%r15),%ebp
not    %ebp
and    %eax,%ebp
mov    (%rbx),%esi
mov    0x2df8(%rip),%r12
mov    (%r12),%ecx
or     %ebp,%ecx
lea    0xe48(%rip),%rdx
xor    %edi,%edi
xor    %r8d,%r8d
call   1050 <mount@plt>
test   %rax,%rax
jns    120f <main+0x8f>
mov    (%rbx),%edx
lea    0xe35(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
xor    %eax,%eax
call   1070 <accept_dad_hack@plt>
mov    %eax,%r14d
mov    (%rbx),%esi
or     (%r12),%ebp
or     (%r15),%ebp
lea    0xe0c(%rip),%rdx
xor    %edi,%edi
mov    %ebp,%ecx
xor    %r8d,%r8d
call   1050 <mount@plt>
test   %rax,%rax
jns    124d <main+0xcd>
mov    (%rbx),%edx
lea    0xe0f(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb6(%rip),%rdi
call   1060 <warnx@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <accept_dad_hack@plt>

LLVM IR (-O0):
%struct.mntent = type { i32, i32 }
@.str = [10 x i8] c"/etc/mtab\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"/proc/sys\00"
@.str.3 = [31 x i8] c"Failed to find /proc/sys mount\00"
@MS_RDONLY = global i32 0
@.str.4 = [5 x i8] c"none\00"
@MS_REMOUNT = global i32 0
@.str.5 = [24 x i8] c"failed to remount %s rw\00"
@.str.6 = [24 x i8] c"failed to remount %s ro\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.mntent*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32* @setmntent(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = call %struct.mntent* @find_mnt_entry(i32* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store %struct.mntent* %8, %struct.mntent** %3
%9 = load i32*, i32** %2
%10 = call i32 @endmntent(i32* %9)
store i32 0, i32* %4
store i32 0, i32* %5
%11 = load %struct.mntent*, %struct.mntent** %3
%12 = icmp ne %struct.mntent* %11, null
br i1 %12, label %15, label %13
13: ; preds = %0
%14 = call i32 @warnx(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %37
15: ; preds = %0
%16 = load %struct.mntent*, %struct.mntent** %3
%17 = getelementptr inbounds %struct.mntent, %struct.mntent* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @read_flags(i32 %18)
%20 = load i32, i32* @MS_RDONLY
%21 = xor i32 %20, -1
%22 = and i32 %19, %21
store i32 %22, i32* %4
%23 = load %struct.mntent*, %struct.mntent** %3
%24 = getelementptr inbounds %struct.mntent, %struct.mntent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @MS_REMOUNT
%27 = load i32, i32* %4
%28 = or i32 %26, %27
%29 = call i64 @mount(i32* null, i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32* null)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %15
%32 = load %struct.mntent*, %struct.mntent** %3
%33 = getelementptr inbounds %struct.mntent, %struct.mntent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %31, %15
br label %37
37: ; preds = %36, %13
%38 = call i32 (...) @accept_dad_hack()
store i32 %38, i32* %5
%39 = load %struct.mntent*, %struct.mntent** %3
%40 = icmp ne %struct.mntent* %39, null
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = load %struct.mntent*, %struct.mntent** %3
%43 = getelementptr inbounds %struct.mntent, %struct.mntent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @MS_REMOUNT
%46 = load i32, i32* @MS_RDONLY
%47 = or i32 %45, %46
%48 = load i32, i32* %4
%49 = or i32 %47, %48
%50 = call i64 @mount(i32* null, i32 %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %49, i32* null)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %41
%53 = load %struct.mntent*, %struct.mntent** %3
%54 = getelementptr inbounds %struct.mntent, %struct.mntent* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %52, %41
br label %58
58: ; preds = %57, %37
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
%struct.mntent = type { i32, i32 }
@.str = [10 x i8] c"/etc/mtab\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"/proc/sys\00"
@.str.3 = [31 x i8] c"Failed to find /proc/sys mount\00"
@MS_RDONLY = global i32 0
@.str.4 = [5 x i8] c"none\00"
@MS_REMOUNT = global i32 0
@.str.5 = [24 x i8] c"failed to remount %s rw\00"
@.str.6 = [24 x i8] c"failed to remount %s ro\00"
define i32 @main() {
%1 = tail call i32* @setmntent(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call %struct.mntent* @find_mnt_entry(i32* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%3 = tail call i32 @endmntent(i32* %1)
%4 = icmp eq %struct.mntent* %2, null
br i1 %4, label %21, label %5
5: ; preds = %0
%6 = getelementptr inbounds %struct.mntent, %struct.mntent* %2, i64 0, i32 1
%7 = load i32, i32* %6
%8 = tail call i32 @read_flags(i32 %7)
%9 = load i32, i32* @MS_RDONLY
%10 = xor i32 %9, -1
%11 = and i32 %8, %10
%12 = getelementptr inbounds %struct.mntent, %struct.mntent* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @MS_REMOUNT
%15 = or i32 %14, %11
%16 = tail call i64 @mount(i32* null, i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32* null)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %5
%19 = load i32, i32* %12
%20 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %19)
br label %24
21: ; preds = %0
%22 = tail call i32 @warnx(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 (...) @accept_dad_hack()
br label %36
24: ; preds = %5, %18
%25 = tail call i32 (...) @accept_dad_hack()
%26 = load i32, i32* %12
%27 = load i32, i32* @MS_REMOUNT
%28 = load i32, i32* @MS_RDONLY
%29 = or i32 %27, %11
%30 = or i32 %29, %28
%31 = tail call i64 @mount(i32* null, i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = load i32, i32* %12
%35 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %21, %24, %33
%37 = phi i32 [ %25, %24 ], [ %25, %33 ], [ %23, %21 ]
ret i32 %37
}