#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 */

/* Variables and functions */
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ getuid();
scalar_t__ seteuid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
void do_chroot(char *newroot) {
  if (chroot(newroot) != 0 || chdir("/") != 0)
    err(1, "%s", newroot);
  if (seteuid(geteuid()) != 0 || setuid(getuid()) != 0) {
    err(1, "set[e]uid()");
  }
}


x86 assembly (clang, -O0):
<do_chroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
jne    11a5 <do_chroot+0x35>
lea    0xe6a(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11bc <do_chroot+0x4c>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1090 <seteuid@plt>
cmp    $0x0,%rax
jne    11ec <do_chroot+0x7c>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     11ff <do_chroot+0x8f>
mov    $0x1,%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_chroot>:
push   %rbx
mov    %rdi,%rbx
call   1030 <chroot@plt>
test   %rax,%rax
jne    118f <do_chroot+0x1f>
lea    0xe7b(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     11a5 <do_chroot+0x35>
lea    0xe6c(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <err@plt>
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1090 <seteuid@plt>
test   %rax,%rax
jne    11cb <do_chroot+0x5b>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1080 <setuid@plt>
test   %rax,%rax
je     11df <do_chroot+0x6f>
lea    0xe33(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
pop    %rbx
jmp    1070 <err@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [12 x i8] c"set[e]uid()\00"
define void @do_chroot(i8* %0) {
%2 = alloca i8*
store i8* %0, i8** %2
%3 = load i8*, i8** %2
%4 = call i64 @chroot(i8* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6, %1
%10 = load i8*, i8** %2
%11 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %6
%13 = call i32 (...) @geteuid()
%14 = call i64 @seteuid(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = call i32 (...) @getuid()
%18 = call i64 @setuid(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16, %12
%21 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [12 x i8] c"set[e]uid()\00"
define void @do_chroot(i8* %0) {
%2 = tail call i64 @chroot(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4, %1
%8 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %4
%10 = tail call i32 (...) @geteuid()
%11 = tail call i64 @seteuid(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = tail call i32 (...) @getuid()
%15 = tail call i64 @setuid(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13, %9
%18 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
ret void
}