#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*/ error(char *);
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ putenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
void register_chdir(void) {
  char *buff1;
  char *buff2;
  char *foo = getenv("PWD");
  char bar[256];
  if (!getcwd(bar, 256)) {
    error("esh: cd: cannot read current directory.");
    return;
  }
  buff2 = (char *)malloc(sizeof(char) * (strlen(bar) + 6));
  sprintf(buff2, "PWD=%s", bar);
  if (foo) {
    buff1 = (char *)malloc(sizeof(char) * (strlen(foo) + 9));
    sprintf(buff1, "OLDPWD=%s", foo);
    putenv(buff1);
  }
  putenv(buff2);
}


x86 assembly (clang, -O0):
<register_chdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%eax
jne    11b6 <register_chdir+0x46>
lea    0xe58(%rip),%rdi
call   1080 <error@plt>
jmp    123b <register_chdir+0xcb>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x6,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    0xe47(%rip),%rsi
call   1090 <sprintf@plt>
cmpq   $0x0,-0x18(%rbp)
je     1232 <register_chdir+0xc2>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x9,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe08(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <putenv@plt>
mov    -0x10(%rbp),%rdi
call   1060 <putenv@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<register_chdir>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xe7b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x100,%esi
call   1050 <getcwd@plt>
test   %eax,%eax
je     1202 <register_chdir+0x92>
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x6(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe74(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1090 <sprintf@plt>
test   %r12,%r12
je     11f8 <register_chdir+0x88>
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    0x9(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe47(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <putenv@plt>
mov    %r14,%rdi
call   1060 <putenv@plt>
jmp    120e <register_chdir+0x9e>
lea    0xdfb(%rip),%rdi
call   1080 <error@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [40 x i8] c"esh: cd: cannot read current directory.\00"
@.str.2 = [7 x i8] c"PWD=%s\00"
@.str.3 = [10 x i8] c"OLDPWD=%s\00"
define void @register_chdir() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 @getcwd(i8* %6, i32 256)
%8 = icmp ne i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %42
11: ; preds = %0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 6
%15 = sext i32 %14 to i64
%16 = mul i64 1, %15
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %2
%20 = load i8*, i8** %2
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %39
25: ; preds = %11
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, 9
%29 = sext i32 %28 to i64
%30 = mul i64 1, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %1
%34 = load i8*, i8** %1
%35 = load i8*, i8** %3
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %1
%38 = call i32 @putenv(i8* %37)
br label %39
39: ; preds = %25, %11
%40 = load i8*, i8** %2
%41 = call i32 @putenv(i8* %40)
br label %42
42: ; preds = %39, %9
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [40 x i8] c"esh: cd: cannot read current directory.\00"
@.str.2 = [7 x i8] c"PWD=%s\00"
@.str.3 = [10 x i8] c"OLDPWD=%s\00"
define void @register_chdir() {
%1 = alloca [256 x i8]
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = call i32 @getcwd(i8* %3, i32 256)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @error(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %24
8: ; preds = %0
%9 = call i32 @strlen(i8* %3)
%10 = add nsw i32 %9, 6
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%14 = icmp eq i8* %2, null
br i1 %14, label %22, label %15
15: ; preds = %8
%16 = call i32 @strlen(i8* %2)
%17 = add nsw i32 %16, 9
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = call i32 @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%21 = call i32 @putenv(i8* %19)
br label %22
22: ; preds = %15, %8
%23 = call i32 @putenv(i8* %12)
br label %24
24: ; preds = %22, %6
ret void
}