#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 */
int /*<<< orphan*/ chdir(char const *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *malloc(scalar_t__);
scalar_t__ pivot_root(char const *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
scalar_t__ strlen(char const *);
int do_pivot(const char *newroot) {
  char *put_old;
  put_old = malloc(strlen(newroot) + strlen("/put_old") + 2);
  sprintf(put_old, "%s%s", newroot, "/put_old");
  chdir(newroot);
  if (pivot_root(newroot, put_old)) {
    fprintf(stderr, "errno=%d\n", errno);
    fprintf(stderr, "failed pivot_root(%s, %s): %s\n", newroot, put_old,
            strerror(errno));
    return errno;
  }
  chdir("/");
  return 0;
}


x86 assembly (clang, -O0):
<do_pivot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
lea    0xe70(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe51(%rip),%rsi
lea    0xe41(%rip),%rcx
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <pivot_root@plt>
cmp    $0x0,%rax
je     125b <do_pivot+0xeb>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    %rax,%r8
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126e <do_pivot+0xfe>
lea    0xdd5(%rip),%rdi
call   1040 <chdir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_pivot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0xa(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe77(%rip),%rsi
lea    0xe67(%rip),%rcx
mov    %rax,%rdi
mov    %rbx,%rdx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <pivot_root@plt>
test   %rax,%rax
je     1207 <do_pivot+0x97>
mov    0x2e1d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0b(%rip),%r15
mov    (%r15),%edx
lea    0xe37(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%ebp
mov    (%r15),%edi
call   1090 <strerror@plt>
lea    0xe28(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%eax
jmp    1215 <do_pivot+0xa5>
lea    0xe29(%rip),%rdi
call   1040 <chdir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/put_old\00"
@.str.1 = [5 x i8] c"%s%s\00"
@stderr = global i32 0
@.str.2 = [10 x i8] c"errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"failed pivot_root(%s, %s): %s\0A\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @do_pivot(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @strlen(i8* %5)
%7 = call i64 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = add nsw i64 %6, %7
%9 = add nsw i64 %8, 2
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %3
%15 = call i32 @chdir(i8* %14)
%16 = load i8*, i8** %3
%17 = load i8*, i8** %4
%18 = call i64 @pivot_root(i8* %16, i8* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %25, i8* %26, i8* %28)
%30 = load i32, i32* @errno
store i32 %30, i32* %2
br label %33
31: ; preds = %1
%32 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %33
33: ; preds = %31, %20
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@.str = [9 x i8] c"/put_old\00"
@.str.1 = [5 x i8] c"%s%s\00"
@stderr = global i32 0
@.str.2 = [10 x i8] c"errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"failed pivot_root(%s, %s): %s\0A\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @do_pivot(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 10
%4 = tail call i8* @malloc(i64 %3)
%5 = tail call i32 @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @chdir(i8* %0)
%7 = tail call i64 @pivot_root(i8* %0, i8* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %4, i8* %15)
%17 = load i32, i32* @errno
br label %20
18: ; preds = %1
%19 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %9
%21 = phi i32 [ %17, %9 ], [ 0, %18 ]
ret i32 %21
}