#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 const *);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *getenv(char *);
scalar_t__ setenv(char *, char const *, int);
int sh_error(char const *, int);
int _cd_chdir(int *prefs, char const *path) {
  char *p;
  char const *oldpwd;
  if (chdir(path) != 0)
    return sh_error(path, 125);
  if ((oldpwd = getenv("PWD")) != NULL)
    if (setenv("OLDPWD", oldpwd, 1) != 0)
      sh_error("setenv OLDPWD", 0);
  if ((p = getcwd(NULL, 0)) != NULL)
    path = p;
  if (setenv("PWD", path, 1) != 0)
    sh_error("setenv PWD", 0);
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<_cd_chdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
je     1199 <_cd_chdir+0x39>
mov    -0x18(%rbp),%rdi
mov    $0x7d,%esi
call   1080 <sh_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1243 <_cd_chdir+0xe3>
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11e5 <_cd_chdir+0x85>
mov    -0x28(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
cmp    $0x0,%rax
je     11e0 <_cd_chdir+0x80>
lea    0xe27(%rip),%rdi
xor    %esi,%esi
call   1080 <sh_error@plt>
jmp    11e5 <_cd_chdir+0x85>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1070 <getcwd@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1206 <_cd_chdir+0xa6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe04(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
cmp    $0x0,%rax
je     1233 <_cd_chdir+0xd3>
lea    0xde2(%rip),%rdi
xor    %esi,%esi
call   1080 <sh_error@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_cd_chdir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
je     1188 <_cd_chdir+0x28>
mov    %rbx,%rdi
mov    $0x7d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <sh_error@plt>
lea    0xe86(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11c0 <_cd_chdir+0x60>
lea    0xe67(%rip),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
test   %rax,%rax
je     11c0 <_cd_chdir+0x60>
lea    0xe47(%rip),%rdi
xor    %esi,%esi
call   1080 <sh_error@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1070 <getcwd@plt>
mov    %rax,%r14
test   %rax,%rax
cmovne %rax,%rbx
lea    0xe3b(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
test   %rax,%rax
je     11fa <_cd_chdir+0x9a>
lea    0xe1b(%rip),%rdi
xor    %esi,%esi
call   1080 <sh_error@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [7 x i8] c"OLDPWD\00"
@.str.2 = [14 x i8] c"setenv OLDPWD\00"
@.str.3 = [11 x i8] c"setenv PWD\00"
define i32 @_cd_chdir(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @chdir(i8* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = call i32 @sh_error(i8* %12, i32 125)
store i32 %13, i32* %3
br label %38
14: ; preds = %2
%15 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i8*, i8** %7
%19 = call i64 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 1)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @sh_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %23
23: ; preds = %21, %17
br label %24
24: ; preds = %23, %14
%25 = call i8* @getcwd(i32* null, i32 0)
store i8* %25, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i8*, i8** %6
store i8* %28, i8** %5
br label %29
29: ; preds = %27, %24
%30 = load i8*, i8** %5
%31 = call i64 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %30, i32 1)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @sh_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %35
35: ; preds = %33, %29
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
store i32 0, i32* %3
br label %38
38: ; preds = %35, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [7 x i8] c"OLDPWD\00"
@.str.2 = [14 x i8] c"setenv OLDPWD\00"
@.str.3 = [11 x i8] c"setenv PWD\00"
define i32 @_cd_chdir(i32* nocapture readnone %0, i8* %1) {
%3 = tail call i64 @chdir(i8* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @sh_error(i8* %1, i32 125)
br label %25
7: ; preds = %2
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = tail call i64 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @sh_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %15
15: ; preds = %10, %13, %7
%16 = tail call i8* @getcwd(i32* null, i32 0)
%17 = icmp eq i8* %16, null
%18 = select i1 %17, i8* %1, i8* %16
%19 = tail call i64 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %18, i32 1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = tail call i32 @sh_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %23
23: ; preds = %21, %15
%24 = tail call i32 @free(i8* %16)
br label %25
25: ; preds = %23, %5
%26 = phi i32 [ %6, %5 ], [ 0, %23 ]
ret i32 %26
}