#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*/ cwd;

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setenv(char *, char *, int);
void builtin_cd(char *c_argv[], int c_argc) {
  if (c_argc != 1) {
    printf("usage: cd dir\n");
  } else {
    if (chdir(c_argv[1]) == 0) {
      char cwd[256];
      getcwd(cwd, sizeof(cwd));
      setenv("PWD", cwd, 1);
    } else {
      printf("Could not change to directory \"%s\"\n", c_argv[1]);
    }
  }
}


x86 assembly (clang, -O0):
<builtin_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
je     116f <builtin_cd+0x2f>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11cf <builtin_cd+0x8f>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jne    11b4 <builtin_cd+0x74>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   1060 <getcwd@plt>
lea    -0x110(%rbp),%rsi
lea    0xe6a(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
jmp    11ca <builtin_cd+0x8a>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11cf <builtin_cd+0x8f>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_cd>:
push   %rbx
sub    $0x100,%rsp
cmp    $0x1,%esi
jne    1188 <builtin_cd+0x38>
mov    %rdi,%rbx
mov    0x8(%rdi),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     119c <builtin_cd+0x4c>
mov    0x8(%rbx),%rsi
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
jmp    1060 <printf@plt>
lea    0xe99(%rip),%rdi
add    $0x100,%rsp
pop    %rbx
jmp    1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
lea    0xe4d(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"usage: cd dir\0A\00"
@.str.1 = [4 x i8] c"PWD\00"
@.str.2 = [36 x i8] c"Could not change to directory \22%s\22\0A\00"
define void @builtin_cd(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca [256 x i8]
store i8** %0, i8*** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %27
10: ; preds = %2
%11 = load i8**, i8*** %3
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @chdir(i8* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%18 = call i32 @getcwd(i8* %17, i32 256)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 1)
br label %26
21: ; preds = %10
%22 = load i8**, i8*** %3
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %21, %16
br label %27
27: ; preds = %26, %8
ret void
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"PWD\00"
@.str.2 = [36 x i8] c"Could not change to directory \22%s\22\0A\00"
@str = [14 x i8] c"usage: cd dir\00"
define void @builtin_cd(i8** nocapture %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = icmp eq i32 %1, 1
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %19
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %0, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @chdir(i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%14 = call i32 @getcwd(i8* %13, i32 256)
%15 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 1)
br label %19
16: ; preds = %7
%17 = load i8*, i8** %8
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %12, %16, %5
ret void
}