#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*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
void set_cwd(void) {
  char *cwd, *old_cwd;
  if ((cwd = getcwd(NULL, 0))) {
    if ((old_cwd = getenv("PWD"))) {
      setenv("OLDPWD", old_cwd, 1);
    } else {
      setenv("OLDPWD", cwd, 1);
    }
    setenv("PWD", cwd, 1);
    free(cwd);
  } else {
    perror("pwd");
  }
}


x86 assembly (clang, -O0):
<set_cwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11dd <set_cwd+0x8d>
lea    0xe8b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11a5 <set_cwd+0x55>
mov    -0x10(%rbp),%rsi
lea    0xe6a(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
jmp    11ba <set_cwd+0x6a>
mov    -0x8(%rbp),%rsi
lea    0xe50(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x8(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x8(%rbp),%rdi
call   1040 <free@plt>
jmp    11e9 <set_cwd+0x99>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cwd>:
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     11ae <set_cwd+0x5e>
mov    %rax,%rbx
lea    0xe97(%rip),%r14
mov    %r14,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
mov    %rbx,%rsi
cmovne %rax,%rsi
lea    0xe7b(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <free@plt>
lea    0xe52(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <perror@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [7 x i8] c"OLDPWD\00"
@.str.2 = [4 x i8] c"pwd\00"
define void @set_cwd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i8* @getcwd(i32* null, i32 0)
store i8* %3, i8** %1
%4 = icmp ne i8* %3, null
br i1 %4, label %5, label %19
5: ; preds = %0
%6 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i8*, i8** %2
%10 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %9, i32 1)
br label %14
11: ; preds = %5
%12 = load i8*, i8** %1
%13 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 1)
br label %14
14: ; preds = %11, %8
%15 = load i8*, i8** %1
%16 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %15, i32 1)
%17 = load i8*, i8** %1
%18 = call i32 @free(i8* %17)
br label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"PWD\00"
@.str.1 = [7 x i8] c"OLDPWD\00"
@.str.2 = [4 x i8] c"pwd\00"
define void @set_cwd() {
%1 = tail call i8* @getcwd(i32* null, i32 0)
%2 = icmp eq i8* %1, null
br i1 %2, label %10, label %3
3: ; preds = %0
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
%6 = select i1 %5, i8* %1, i8* %4
%7 = tail call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %6, i32 1)
%8 = tail call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1, i32 1)
%9 = tail call i32 @free(i8* %1)
br label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
ret void
}