#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 int /*<<< orphan*/ buffer;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ chroot(char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  char buffer[PATH_MAX];
  getcwd(buffer, sizeof(buffer));
  printf("Current directory is %s\n", buffer);
  mkdir("dir", 0777);
  chroot("dir");
  getcwd(buffer, sizeof(buffer));
  printf("With chroot to dir, current directory is %s\n", buffer);
  chdir("dir");
  getcwd(buffer, sizeof(buffer));
  printf("After chdir, current directory is %s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x28(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x20(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    %eax,%esi
call   1070 <getcwd@plt>
mov    -0x20(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1060 <printf@plt>
lea    0xe6c(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0xe5b(%rip),%rdi
call   1030 <chroot@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %eax,%esi
call   1070 <getcwd@plt>
mov    -0x20(%rbp),%rsi
lea    0xe40(%rip),%rdi
call   1060 <printf@plt>
lea    0xe30(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %eax,%esi
call   1070 <getcwd@plt>
mov    -0x20(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8f(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <getcwd@plt>
lea    0xe76(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
lea    0xe80(%rip),%r15
mov    %r15,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %r15,%rdi
call   1030 <chroot@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <getcwd@plt>
lea    0xe5d(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1050 <chdir@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <getcwd@plt>
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [25 x i8] c"Current directory is %s\0A\00"
@.str.1 = [4 x i8] c"dir\00"
@.str.2 = [45 x i8] c"With chroot to dir, current directory is %s\0A\00"
@.str.3 = [38 x i8] c"After chdir, current directory is %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = call i8* @llvm.stacksave()
store i8* %6, i8** %2
%7 = alloca i8, i64 %5
store i64 %5, i64* %3
%8 = trunc i64 %5 to i32
%9 = call i32 @getcwd(i8* %7, i32 %8)
%10 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %7)
%11 = call i32 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 511)
%12 = call i32 @chroot(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = trunc i64 %5 to i32
%14 = call i32 @getcwd(i8* %7, i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%16 = call i32 @chdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = trunc i64 %5 to i32
%18 = call i32 @getcwd(i8* %7, i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %7)
store i32 0, i32* %1
%20 = load i8*, i8** %2
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [25 x i8] c"Current directory is %s\0A\00"
@.str.1 = [4 x i8] c"dir\00"
@.str.2 = [45 x i8] c"With chroot to dir, current directory is %s\0A\00"
@.str.3 = [38 x i8] c"After chdir, current directory is %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = call i32 @getcwd(i8* %3, i32 %1)
%5 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %3)
%6 = call i32 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 511)
%7 = call i32 @chroot(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @getcwd(i8* %3, i32 %1)
%9 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%10 = call i32 @chdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @getcwd(i8* %3, i32 %1)
%12 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %3)
ret i32 0
}