#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 stat {
  scalar_t__ st_dev;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ newmount;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ MNT_FORCE;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ chdir(char const *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errmsg(int /*<<< orphan*/, int /*<<< orphan*/, int, char *,
                          ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ mount(char const *, char *, int /*<<< orphan*/ *, int,
                 int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ recursiveRemove(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ umount(char const *);
int /*<<< orphan*/ umount2(char const *, int /*<<< orphan*/);
int switchroot(const char *newroot) {
  const char *umounts[] = {"/dev", "/proc", "/sys", "/run", NULL};
  int i;
  int cfd;
  pid_t pid;
  struct stat newroot_stat, sb;
  if (stat(newroot, &newroot_stat) != 0) {
    errmsg(0, 0, 1, "failed to stat directory %s", newroot);
    return -1;
  }
  for (i = 0; umounts[i] != NULL; i++) {
    char newmount[4096];
    snprintf(newmount, sizeof(newmount), "%s%s", newroot, umounts[i]);
    if ((stat(newmount, &sb) != 0) || (sb.st_dev != newroot_stat.st_dev)) {
      umount(umounts[i]);
      continue;
    }
    if (mount(umounts[i], newmount, NULL, 8192, NULL) < 0) {
      errmsg(0, 0, 1, "failed to mount moving %s to %s", umounts[i], newmount);
      errmsg(0, 0, 0, "forcing unmount of %s", umounts[i]);
      umount2(umounts[i], MNT_FORCE);
    }
  }
  if (chdir(newroot)) {
    errmsg(0, 0, 1, "failed to change directory to %s", newroot);
    return -1;
  }
  cfd = open("/", O_RDONLY);
  if (mount(newroot, "/", NULL, 8192, NULL) < 0) {
    errmsg(0, 0, 1, "failed to mount moving %s to /", newroot);
    return -1;
  }
  if (chroot(".")) {
    errmsg(0, 0, 1, "failed to change root");
    return -1;
  }
  if (cfd >= 0) {
    pid = fork();
    if (pid <= 0) {
      recursiveRemove(cfd);
      if (pid == 0)
        exit(EXIT_SUCCESS);
    }
    close(cfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<switchroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1090 <memset@plt>
lea    0xdfa(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xdf4(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xdef(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xde9(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
je     1269 <switchroot+0x89>
mov    -0x10(%rbp),%r8
xor    %esi,%esi
mov    $0x1,%edx
lea    0xdc1(%rip),%rcx
mov    %esi,%edi
mov    $0x0,%al
call   10c0 <errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a8 <switchroot+0x2c8>
movl   $0x0,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmpq   $0x0,-0x40(%rbp,%rax,8)
je     137a <switchroot+0x19a>
lea    -0x1060(%rbp),%rdi
mov    -0x10(%rbp),%rcx
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%r8
mov    $0x1000,%esi
lea    0xd91(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x1060(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jne    12cd <switchroot+0xed>
mov    -0x60(%rbp),%rax
cmp    -0x58(%rbp),%rax
je     12e0 <switchroot+0x100>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1040 <umount@plt>
jmp    136c <switchroot+0x18c>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
lea    -0x1060(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x2000,%ecx
mov    %r8,%rdx
call   1060 <mount@plt>
cmp    $0x0,%rax
jge    1367 <switchroot+0x187>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%r8
lea    -0x1060(%rbp),%r9
xor    %esi,%esi
mov    $0x1,%edx
lea    0xd0c(%rip),%rcx
mov    %esi,%edi
mov    $0x0,%al
call   10c0 <errmsg@plt>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%r8
xor    %edx,%edx
lea    0xd11(%rip),%rcx
mov    %edx,%edi
mov    %edx,%esi
mov    $0x0,%al
call   10c0 <errmsg@plt>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
mov    0x2c80(%rip),%rax
mov    (%rax),%esi
call   1050 <umount2@plt>
jmp    136c <switchroot+0x18c>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1270 <switchroot+0x90>
mov    -0x10(%rbp),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
je     13b4 <switchroot+0x1d4>
mov    -0x10(%rbp),%r8
xor    %esi,%esi
mov    $0x1,%edx
lea    0xccd(%rip),%rcx
mov    %esi,%edi
mov    $0x0,%al
call   10c0 <errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a8 <switchroot+0x2c8>
mov    0x2c2d(%rip),%rax
mov    (%rax),%esi
lea    0xce6(%rip),%rdi
call   10e0 <open@plt>
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xcd3(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x2000,%ecx
mov    %r8,%rdx
call   1060 <mount@plt>
cmp    $0x0,%rax
jge    141a <switchroot+0x23a>
mov    -0x10(%rbp),%r8
xor    %esi,%esi
mov    $0x1,%edx
lea    0xc88(%rip),%rcx
mov    %esi,%edi
mov    $0x0,%al
call   10c0 <errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a8 <switchroot+0x2c8>
lea    0xc8b(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     1453 <switchroot+0x273>
xor    %esi,%esi
mov    $0x1,%edx
lea    0xc70(%rip),%rcx
mov    %esi,%edi
mov    $0x0,%al
call   10c0 <errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a8 <switchroot+0x2c8>
cmpl   $0x0,-0x48(%rbp)
jl     14a1 <switchroot+0x2c1>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jg     1499 <switchroot+0x2b9>
mov    -0x48(%rbp),%edi
call   10d0 <recursiveRemove@plt>
cmpq   $0x0,-0x50(%rbp)
jne    1494 <switchroot+0x2b4>
mov    0x2b43(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    1499 <switchroot+0x2b9>
mov    -0x48(%rbp),%edi
call   10a0 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switchroot>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1048,%rsp
mov    %rdi,%r14
movq   $0x0,0x30(%rsp)
lea    0xe0c(%rip),%rbx
mov    %rbx,0x10(%rsp)
lea    0xe05(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xdff(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xdf8(%rip),%rax
mov    %rax,0x28(%rsp)
lea    0x8(%rsp),%rsi
call   10a0 <stat@plt>
test   %rax,%rax
je     1238 <switchroot+0x68>
lea    0xde2(%rip),%rcx
jmp    1312 <switchroot+0x142>
lea    0x18(%rsp),%rbp
lea    0xded(%rip),%r15
lea    0x40(%rsp),%r12
mov    %rsp,%r13
jmp    1269 <switchroot+0x99>
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <umount@plt>
mov    0x0(%rbp),%rbx
add    $0x8,%rbp
test   %rbx,%rbx
je     12fe <switchroot+0x12e>
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1080 <snprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   10a0 <stat@plt>
test   %rax,%rax
jne    1250 <switchroot+0x80>
mov    (%rsp),%rax
cmp    0x8(%rsp),%rax
jne    1250 <switchroot+0x80>
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %edx,%edx
mov    $0x2000,%ecx
xor    %r8d,%r8d
call   1060 <mount@plt>
test   %rax,%rax
jns    1258 <switchroot+0x88>
xor    %edi,%edi
xor    %esi,%esi
mov    $0x1,%edx
lea    0xd72(%rip),%rcx
mov    %rbx,%r8
mov    %r12,%r9
xor    %eax,%eax
call   10b0 <errmsg@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
lea    0xd78(%rip),%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   10b0 <errmsg@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <umount2@plt>
jmp    1258 <switchroot+0x88>
mov    %r14,%rdi
call   1070 <chdir@plt>
test   %rax,%rax
je     133e <switchroot+0x16e>
lea    0xd5a(%rip),%rcx
xor    %edi,%edi
xor    %esi,%esi
mov    $0x1,%edx
mov    %r14,%r8
xor    %eax,%eax
call   10b0 <errmsg@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x1048,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
lea    0xd5c(%rip),%rbp
mov    %rbp,%rdi
call   10d0 <open@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
mov    $0x2000,%ecx
xor    %r8d,%r8d
call   1060 <mount@plt>
test   %rax,%rax
js     139c <switchroot+0x1cc>
lea    0xd33(%rip),%rdi
call   1030 <chroot@plt>
test   %rax,%rax
je     13a8 <switchroot+0x1d8>
lea    0xd24(%rip),%rcx
xor    %edi,%edi
xor    %esi,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   10b0 <errmsg@plt>
jmp    1325 <switchroot+0x155>
lea    0xcea(%rip),%rcx
jmp    1312 <switchroot+0x142>
xor    %ebp,%ebp
test   %ebx,%ebx
js     132a <switchroot+0x15a>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jg     13cd <switchroot+0x1fd>
mov    %rax,%r14
mov    %ebx,%edi
call   10c0 <recursiveRemove@plt>
test   %r14,%r14
je     13d9 <switchroot+0x209>
mov    %ebx,%edi
call   1090 <close@plt>
jmp    132a <switchroot+0x15a>
mov    0x2bf0(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [5 x i8] c"/sys\00"
@.str.3 = [5 x i8] c"/run\00"
@.str.4 = [28 x i8] c"failed to stat directory %s\00"
@.str.5 = [5 x i8] c"%s%s\00"
@.str.6 = [32 x i8] c"failed to mount moving %s to %s\00"
@.str.7 = [22 x i8] c"forcing unmount of %s\00"
@MNT_FORCE = global i32 0
@.str.8 = [33 x i8] c"failed to change directory to %s\00"
@.str.9 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.10 = [31 x i8] c"failed to mount moving %s to /\00"
@.str.11 = [2 x i8] c".\00"
@.str.12 = [22 x i8] c"failed to change root\00"
@EXIT_SUCCESS = global i32 0
define i32 @switchroot(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [5 x i8*]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca %struct.stat
%10 = alloca [4096 x i8]
store i8* %0, i8** %3
%11 = bitcast [5 x i8*]* %4 to i8*
%12 = bitcast i8* %11 to [5 x i8*]*
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8** %16
%17 = load i8*, i8** %3
%18 = call i64 @stat(i8* %17, %struct.stat* %8)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %21)
store i32 -1, i32* %2
br label %124
23: ; preds = %1
store i32 0, i32* %5
br label %24
24: ; preds = %80, %23
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %26
%28 = load i8*, i8** %27
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %83
30: ; preds = %24
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%32 = load i8*, i8** %3
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @snprintf(i8* %31, i32 4096, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %32, i8* %36)
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%39 = call i64 @stat(i8* %38, %struct.stat* %9)
%40 = icmp ne i64 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %30
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%43 = load i64, i64* %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%45 = load i64, i64* %44
%46 = icmp ne i64 %43, %45
br i1 %46, label %47, label %53
47: ; preds = %41, %30
%48 = load i32, i32* %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %49
%51 = load i8*, i8** %50
%52 = call i32 @umount(i8* %51)
br label %80
53: ; preds = %41
%54 = load i32, i32* %5
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %55
%57 = load i8*, i8** %56
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%59 = call i64 @mount(i8* %57, i8* %58, i32* null, i32 8192, i32* null)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %79
61: ; preds = %53
%62 = load i32, i32* %5
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %63
%65 = load i8*, i8** %64
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%67 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %65, i8* %66)
%68 = load i32, i32* %5
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %69
%71 = load i8*, i8** %70
%72 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %71)
%73 = load i32, i32* %5
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %74
%76 = load i8*, i8** %75
%77 = load i32, i32* @MNT_FORCE
%78 = call i32 @umount2(i8* %76, i32 %77)
br label %79
79: ; preds = %61, %53
br label %80
80: ; preds = %79, %47
%81 = load i32, i32* %5
%82 = add nsw i32 %81, 1
store i32 %82, i32* %5
br label %24
83: ; preds = %24
%84 = load i8*, i8** %3
%85 = call i64 @chdir(i8* %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = load i8*, i8** %3
%89 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %88)
store i32 -1, i32* %2
br label %124
90: ; preds = %83
%91 = load i32, i32* @O_RDONLY
%92 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 %91)
store i32 %92, i32* %6
%93 = load i8*, i8** %3
%94 = call i64 @mount(i8* %93, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32* null, i32 8192, i32* null)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %90
%97 = load i8*, i8** %3
%98 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.10, i64 0, i64 0), i8* %97)
store i32 -1, i32* %2
br label %124
99: ; preds = %90
%100 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %104
102: ; preds = %99
%103 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0))
store i32 -1, i32* %2
br label %124
104: ; preds = %99
%105 = load i32, i32* %6
%106 = icmp sge i32 %105, 0
br i1 %106, label %107, label %123
107: ; preds = %104
%108 = call i64 (...) @fork()
store i64 %108, i64* %7
%109 = load i64, i64* %7
%110 = icmp sle i64 %109, 0
br i1 %110, label %111, label %120
111: ; preds = %107
%112 = load i32, i32* %6
%113 = call i32 @recursiveRemove(i32 %112)
%114 = load i64, i64* %7
%115 = icmp eq i64 %114, 0
br i1 %115, label %116, label %119
116: ; preds = %111
%117 = load i32, i32* @EXIT_SUCCESS
%118 = call i32 @exit(i32 %117)
unreachable
119: ; preds = %111
br label %120
120: ; preds = %119, %107
%121 = load i32, i32* %6
%122 = call i32 @close(i32 %121)
br label %123
123: ; preds = %120, %104
store i32 0, i32* %2
br label %124
124: ; preds = %123, %102, %96, %87, %20
%125 = load i32, i32* %2
ret i32 %125
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [5 x i8] c"/sys\00"
@.str.3 = [5 x i8] c"/run\00"
@.str.4 = [28 x i8] c"failed to stat directory %s\00"
@.str.5 = [5 x i8] c"%s%s\00"
@.str.6 = [32 x i8] c"failed to mount moving %s to %s\00"
@.str.7 = [22 x i8] c"forcing unmount of %s\00"
@MNT_FORCE = global i32 0
@.str.8 = [33 x i8] c"failed to change directory to %s\00"
@.str.9 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.10 = [31 x i8] c"failed to mount moving %s to /\00"
@.str.11 = [2 x i8] c".\00"
@.str.12 = [22 x i8] c"failed to change root\00"
@EXIT_SUCCESS = global i32 0
define i32 @switchroot(i8* %0) {
%2 = alloca [5 x i8*]
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = bitcast [5 x i8*]* %2 to i8*
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
%8 = bitcast i8** %7 to i64*
store i64 0, i64* %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %12
%13 = bitcast %struct.stat* %3 to i8*
%14 = bitcast %struct.stat* %4 to i8*
%15 = call i64 @stat(i8* %0, %struct.stat* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %23
21: ; preds = %1
%22 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %78
23: ; preds = %17, %43
%24 = phi i64 [ 0, %17 ], [ %44, %43 ]
%25 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %17 ], [ %46, %43 ]
%26 = call i32 @snprintf(i8* %18, i32 4096, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %25)
%27 = call i64 @stat(i8* %18, %struct.stat* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i64, i64* %19
%31 = load i64, i64* %20
%32 = icmp eq i64 %30, %31
br i1 %32, label %35, label %33
33: ; preds = %29, %23
%34 = call i32 @umount(i8* %25)
br label %43
35: ; preds = %29
%36 = call i64 @mount(i8* %25, i8* %18, i32* null, i32 8192, i32* null)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %25, i8* %18)
%40 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %25)
%41 = load i32, i32* @MNT_FORCE
%42 = call i32 @umount2(i8* %25, i32 %41)
br label %43
43: ; preds = %35, %38, %33
%44 = add nuw i64 %24, 1
%45 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %23
48: ; preds = %43
%49 = call i64 @chdir(i8* %0)
%50 = icmp eq i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %0)
br label %78
53: ; preds = %48
%54 = load i32, i32* @O_RDONLY
%55 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 %54)
%56 = call i64 @mount(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32* null, i32 8192, i32* null)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.10, i64 0, i64 0), i8* %0)
br label %78
60: ; preds = %53
%61 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
%62 = icmp eq i64 %61, 0
br i1 %62, label %65, label %63
63: ; preds = %60
%64 = call i32 (i32, i32, i32, i8*, ...) @errmsg(i32 0, i32 0, i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0))
br label %78
65: ; preds = %60
%66 = icmp sgt i32 %55, -1
br i1 %66, label %67, label %78
67: ; preds = %65
%68 = call i64 (...) @fork()
%69 = icmp slt i64 %68, 1
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = call i32 @recursiveRemove(i32 %55)
%72 = icmp eq i64 %68, 0
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32, i32* @EXIT_SUCCESS
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %70, %67
%77 = call i32 @close(i32 %55)
br label %78
78: ; preds = %65, %76, %63, %58, %51, %21
%79 = phi i32 [ -1, %21 ], [ -1, %51 ], [ -1, %58 ], [ -1, %63 ], [ 0, %76 ], [ 0, %65 ]
ret i32 %79
}