#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 const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *getcwd(char *, int);
char *getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setenv(char *, char const *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int cd(char **args) {
  char cwd[1024];
  if (getcwd(cwd, sizeof(cwd)) == NULL) {
    perror("getcwd()");
  }
  if (args[1] == NULL || strcmp(args[1], "~") == 0) {
    const char *home = getenv("HOME");
    setenv("OLDPWD", cwd, 1);
    if (chdir(home) != 0) {
      fprintf(stderr, "See `man cd` for more informations.\n");
      fprintf(stderr, "HOME may is not set.\n");
    } else {
      setenv("PWD", home, 1);
    }
    return 1;
  } else if (args[2] != NULL && strcmp(args[1], "-L") != 0 &&
             strcmp(args[1], "-P") != 0) {
    fprintf(stderr, "See `man cd` for more informations.\n");
    return 1;
  } else if (strcmp(args[1], "-") == 0) {
    char *oldpwd = getenv("OLDPWD");
    if (chdir(oldpwd) != 0) {
      fprintf(stderr, "See `man cd` for more informations.\n");
      fprintf(stderr, "OLDPWD may is not set.\n");
    } else {
      setenv("OLDPWD", cwd, 1);
      setenv("PWD", oldpwd, 1);
    }
    printf("%s\n", getenv("PWD"));
    return 1;
  } else {
    char *path;
    const char *home = getenv("HOME");
    if (!strchr(args[1], '~')) {
      path = args[1];
    } else {
      int i;
      path = (char *)malloc(strlen(args[1] - 1 + strlen(home)));
      for (i = 0; args[1][i] != '~'; i++) {
        path[i] = args[1][i];
      }
      strncpy(path + i, home, strlen(home));
      for (; i < strlen(args[1]); i++) {
        path[i + strlen(home)] = args[1][i + 1];
      }
    }
    if (chdir(path) != 0) {
      fprintf(stderr, "Path not found.\n");
    } else {
      setenv("OLDPWD", cwd, 1);
      if (getcwd(cwd, sizeof(cwd)) == NULL) {
        fprintf(stderr, "getcwd() error");
      }
      setenv("PWD", cwd, 1);
    }
    return 1;
  }
}


x86 assembly (clang, -O0):
<cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10a0 <getcwd@plt>
cmp    $0x0,%rax
jne    11f6 <cd+0x36>
lea    0xe0f(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1223 <cd+0x63>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf5(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    12b7 <cd+0xf7>
lea    0xde1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x418(%rbp),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
je     1293 <cd+0xd3>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    12ab <cd+0xeb>
mov    -0x418(%rbp),%rsi
lea    0xd72(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
movl   $0x1,-0x4(%rbp)
jmp    164d <cd+0x48d>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1323 <cd+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd7d(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     1323 <cd+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd62(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     1323 <cd+0x163>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xd05(%rip),%rsi
call   10c0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    164d <cd+0x48d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd26(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    13f0 <cd+0x230>
lea    0xcc8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
je     1399 <cd+0x1d9>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
lea    0xc9d(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    0xccb(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    13c9 <cd+0x209>
lea    -0x410(%rbp),%rsi
lea    0xc69(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x420(%rbp),%rsi
lea    0xc54(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xc43(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0xc93(%rip),%rdi
call   1090 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    164d <cd+0x48d>
lea    0xc14(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x7e,%esi
call   1080 <strchr@plt>
cmp    $0x0,%eax
jne    1432 <cd+0x272>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x428(%rbp)
jmp    15b6 <cd+0x3f6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x440(%rbp)
mov    -0x430(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x440(%rbp),%rdi
cltq
add    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x428(%rbp)
movl   $0x0,-0x434(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x434(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x7e,%eax
je     14cd <cd+0x30d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x434(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x428(%rbp),%rax
movslq -0x434(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    147a <cd+0x2ba>
mov    -0x428(%rbp),%rax
movslq -0x434(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x450(%rbp)
mov    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x430(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x450(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x454(%rbp),%eax
cmp    %ecx,%eax
jge    15b1 <cd+0x3f1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x434(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x461(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,-0x468(%rbp)
mov    -0x430(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x468(%rbp),%ecx
mov    -0x461(%rbp),%dl
mov    %eax,%esi
mov    -0x460(%rbp),%rax
add    %esi,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    1514 <cd+0x354>
jmp    15b6 <cd+0x3f6>
mov    -0x428(%rbp),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
je     15e6 <cd+0x426>
mov    0x2a0d(%rip),%rax
mov    (%rax),%edi
lea    0xa9a(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    1646 <cd+0x486>
lea    -0x410(%rbp),%rsi
lea    0xa1c(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10a0 <getcwd@plt>
cmp    $0x0,%rax
jne    162e <cd+0x46e>
mov    0x29c0(%rip),%rax
mov    (%rax),%edi
lea    0xa5e(%rip),%rsi
call   10c0 <fprintf@plt>
lea    -0x410(%rbp),%rsi
lea    0x9d7(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r13
mov    %rsp,%rdi
mov    $0x400,%esi
call   10a0 <getcwd@plt>
test   %rax,%rax
je     1471 <cd+0x2b1>
mov    0x8(%r13),%rdi
test   %rdi,%rdi
je     1257 <cd+0x97>
lea    0xe0f(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1257 <cd+0x97>
cmpq   $0x0,0x10(%r13)
je     12b3 <cd+0xf3>
mov    0x8(%r13),%rdi
lea    0xe38(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     12b3 <cd+0xf3>
mov    0x8(%r13),%rdi
lea    0xe22(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     12b3 <cd+0xf3>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    14c5 <cd+0x305>
lea    0xdad(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xda3(%rip),%rdi
mov    %rsp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
call   1070 <chdir@plt>
test   %rax,%rax
je     13c6 <cd+0x206>
mov    0x2d4e(%rip),%rbx
mov    (%rbx),%edi
lea    0xd7c(%rip),%rsi
call   10c0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd93(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    14c5 <cd+0x305>
mov    0x8(%r13),%rdi
lea    0xd9a(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     13d5 <cd+0x215>
lea    0xd38(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    0x8(%r13),%rdi
mov    $0x7e,%esi
call   1080 <strchr@plt>
mov    0x8(%r13),%r15
test   %eax,%eax
je     139f <cd+0x1df>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
lea    (%r15,%rax,1),%rdi
add    $0xffffffffffffffff,%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,%r15
mov    0x8(%r13),%rax
mov    (%rax),%al
xor    %ebx,%ebx
cmp    $0x7e,%al
je     1345 <cd+0x185>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %al,(%r15,%rbx,1)
mov    0x8(%r13),%rax
movzbl 0x1(%rax,%rbx,1),%eax
add    $0x1,%rbx
cmp    $0x7e,%al
jne    1330 <cd+0x170>
mov    %ebx,%ebp
mov    %r15,%r12
add    %rbp,%r12
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    0x8(%r13),%rdi
call   1060 <strlen@plt>
cmp    %eax,%ebx
jge    139f <cd+0x1df>
nop
mov    0x8(%r13),%rax
lea    0x1(%rbp),%rbx
movzbl 0x1(%rax,%rbp,1),%r12d
mov    %r14,%rdi
call   1060 <strlen@plt>
add    %eax,%ebp
movslq %ebp,%rax
mov    %r12b,(%r15,%rax,1)
mov    0x8(%r13),%rdi
call   1060 <strlen@plt>
mov    %rbx,%rbp
cmp    %ebx,%eax
jg     1370 <cd+0x1b0>
mov    %r15,%rdi
call   1070 <chdir@plt>
test   %rax,%rax
je     141d <cd+0x25d>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
lea    0xcba(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    14c5 <cd+0x305>
lea    0xc46(%rip),%rdi
mov    %rbx,%rsi
jmp    1465 <cd+0x2a5>
lea    0xc34(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <chdir@plt>
test   %rax,%rax
je     1482 <cd+0x2c2>
mov    0x2be4(%rip),%rbx
mov    (%rbx),%edi
lea    0xc12(%rip),%rsi
call   10c0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xc47(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    14aa <cd+0x2ea>
lea    0xbec(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
call   10a0 <getcwd@plt>
test   %rax,%rax
jne    145b <cd+0x29b>
mov    0x2b93(%rip),%rax
mov    (%rax),%edi
lea    0xc31(%rip),%rsi
call   10c0 <fprintf@plt>
lea    0xbb1(%rip),%rdi
mov    %rsp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
jmp    14c5 <cd+0x305>
lea    0xb88(%rip),%rdi
call   10e0 <perror@plt>
jmp    11ea <cd+0x2a>
lea    0xb87(%rip),%rdi
mov    %rsp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xb76(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xb62(%rip),%rdi
call   1030 <getenv@plt>
lea    0xbb5(%rip),%rdi
mov    %rax,%rsi
call   1090 <printf@plt>
mov    $0x1,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"getcwd()\00"
@.str.1 = [2 x i8] c"~\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [7 x i8] c"OLDPWD\00"
@stderr = global i32 0
@.str.4 = [37 x i8] c"See `man cd` for more informations.\0A\00"
@.str.5 = [22 x i8] c"HOME may is not set.\0A\00"
@.str.6 = [4 x i8] c"PWD\00"
@.str.7 = [3 x i8] c"-L\00"
@.str.8 = [3 x i8] c"-P\00"
@.str.9 = [2 x i8] c"-\00"
@.str.10 = [24 x i8] c"OLDPWD may is not set.\0A\00"
@.str.11 = [4 x i8] c"%s\0A\00"
@.str.12 = [17 x i8] c"Path not found.\0A\00"
@.str.13 = [15 x i8] c"getcwd() error\00"
define i32 @cd(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8** %0, i8*** %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i32* @getcwd(i8* %10, i32 1024)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = load i8**, i8*** %3
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = load i8**, i8*** %3
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %42
26: ; preds = %20, %15
%27 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %27, i8** %5
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %28, i32 1)
%30 = load i8*, i8** %5
%31 = call i64 @chdir(i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %41
38: ; preds = %26
%39 = load i8*, i8** %5
%40 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %39, i32 1)
br label %41
41: ; preds = %38, %33
store i32 1, i32* %2
br label %190
42: ; preds = %20
%43 = load i8**, i8*** %3
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %62
47: ; preds = %42
%48 = load i8**, i8*** %3
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %62
53: ; preds = %47
%54 = load i8**, i8*** %3
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %190
62: ; preds = %53, %47, %42
%63 = load i8**, i8*** %3
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = load i8*, i8** %64
%66 = call i64 @strcmp(i8* %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %86
68: ; preds = %62
%69 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i8* %69, i8** %6
%70 = load i8*, i8** %6
%71 = call i64 @chdir(i8* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %68
%74 = load i32, i32* @stderr
%75 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%76 = load i32, i32* @stderr
%77 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %83
78: ; preds = %68
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%80 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %79, i32 1)
%81 = load i8*, i8** %6
%82 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %81, i32 1)
br label %83
83: ; preds = %78, %73
%84 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%85 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i8* %84)
store i32 1, i32* %2
br label %190
86: ; preds = %62
%87 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %87, i8** %8
%88 = load i8**, i8*** %3
%89 = getelementptr inbounds i8*, i8** %88, i64 1
%90 = load i8*, i8** %89
%91 = call i32 @strchr(i8* %90, i8 signext 126)
%92 = icmp ne i32 %91, 0
br i1 %92, label %97, label %93
93: ; preds = %86
%94 = load i8**, i8*** %3
%95 = getelementptr inbounds i8*, i8** %94, i64 1
%96 = load i8*, i8** %95
store i8* %96, i8** %7
br label %170
97: ; preds = %86
%98 = load i8**, i8*** %3
%99 = getelementptr inbounds i8*, i8** %98, i64 1
%100 = load i8*, i8** %99
%101 = getelementptr inbounds i8, i8* %100, i64 -1
%102 = load i8*, i8** %8
%103 = call i32 @strlen(i8* %102)
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i8, i8* %101, i64 %104
%106 = call i32 @strlen(i8* %105)
%107 = call i64 @malloc(i32 %106)
%108 = inttoptr i64 %107 to i8*
store i8* %108, i8** %7
store i32 0, i32* %9
br label %109
109: ; preds = %131, %97
%110 = load i8**, i8*** %3
%111 = getelementptr inbounds i8*, i8** %110, i64 1
%112 = load i8*, i8** %111
%113 = load i32, i32* %9
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds i8, i8* %112, i64 %114
%116 = load i8, i8* %115
%117 = sext i8 %116 to i32
%118 = icmp ne i32 %117, 126
br i1 %118, label %119, label %134
119: ; preds = %109
%120 = load i8**, i8*** %3
%121 = getelementptr inbounds i8*, i8** %120, i64 1
%122 = load i8*, i8** %121
%123 = load i32, i32* %9
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds i8, i8* %122, i64 %124
%126 = load i8, i8* %125
%127 = load i8*, i8** %7
%128 = load i32, i32* %9
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i8, i8* %127, i64 %129
store i8 %126, i8* %130
br label %131
131: ; preds = %119
%132 = load i32, i32* %9
%133 = add nsw i32 %132, 1
store i32 %133, i32* %9
br label %109
134: ; preds = %109
%135 = load i8*, i8** %7
%136 = load i32, i32* %9
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds i8, i8* %135, i64 %137
%139 = load i8*, i8** %8
%140 = load i8*, i8** %8
%141 = call i32 @strlen(i8* %140)
%142 = call i32 @strncpy(i8* %138, i8* %139, i32 %141)
br label %143
143: ; preds = %166, %134
%144 = load i32, i32* %9
%145 = load i8**, i8*** %3
%146 = getelementptr inbounds i8*, i8** %145, i64 1
%147 = load i8*, i8** %146
%148 = call i32 @strlen(i8* %147)
%149 = icmp slt i32 %144, %148
br i1 %149, label %150, label %169
150: ; preds = %143
%151 = load i8**, i8*** %3
%152 = getelementptr inbounds i8*, i8** %151, i64 1
%153 = load i8*, i8** %152
%154 = load i32, i32* %9
%155 = add nsw i32 %154, 1
%156 = sext i32 %155 to i64
%157 = getelementptr inbounds i8, i8* %153, i64 %156
%158 = load i8, i8* %157
%159 = load i8*, i8** %7
%160 = load i32, i32* %9
%161 = load i8*, i8** %8
%162 = call i32 @strlen(i8* %161)
%163 = add nsw i32 %160, %162
%164 = sext i32 %163 to i64
%165 = getelementptr inbounds i8, i8* %159, i64 %164
store i8 %158, i8* %165
br label %166
166: ; preds = %150
%167 = load i32, i32* %9
%168 = add nsw i32 %167, 1
store i32 %168, i32* %9
br label %143
169: ; preds = %143
br label %170
170: ; preds = %169, %93
%171 = load i8*, i8** %7
%172 = call i64 @chdir(i8* %171)
%173 = icmp ne i64 %172, 0
br i1 %173, label %174, label %177
174: ; preds = %170
%175 = load i32, i32* @stderr
%176 = call i32 @fprintf(i32 %175, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
br label %189
177: ; preds = %170
%178 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%179 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %178, i32 1)
%180 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%181 = call i32* @getcwd(i8* %180, i32 1024)
%182 = icmp eq i32* %181, null
br i1 %182, label %183, label %186
183: ; preds = %177
%184 = load i32, i32* @stderr
%185 = call i32 @fprintf(i32 %184, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.13, i64 0, i64 0))
br label %186
186: ; preds = %183, %177
%187 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%188 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %187, i32 1)
br label %189
189: ; preds = %186, %174
store i32 1, i32* %2
br label %190
190: ; preds = %189, %83, %59, %41
%191 = load i32, i32* %2
ret i32 %191
}

LLVM IR (-O2):
@.str = [9 x i8] c"getcwd()\00"
@.str.1 = [2 x i8] c"~\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [7 x i8] c"OLDPWD\00"
@stderr = global i32 0
@.str.4 = [37 x i8] c"See `man cd` for more informations.\0A\00"
@.str.5 = [22 x i8] c"HOME may is not set.\0A\00"
@.str.6 = [4 x i8] c"PWD\00"
@.str.7 = [3 x i8] c"-L\00"
@.str.8 = [3 x i8] c"-P\00"
@.str.9 = [2 x i8] c"-\00"
@.str.10 = [24 x i8] c"OLDPWD may is not set.\0A\00"
@.str.11 = [4 x i8] c"%s\0A\00"
@.str.12 = [17 x i8] c"Path not found.\0A\00"
@.str.13 = [15 x i8] c"getcwd() error\00"
define i32 @cd(i8** nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32* @getcwd(i8* %3, i32 1024)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = getelementptr inbounds i8*, i8** %0, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12, %8
%16 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32 1)
%18 = call i64 @chdir(i8* %16)
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %129
25: ; preds = %15
%26 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %16, i32 1)
br label %129
27: ; preds = %12
%28 = getelementptr inbounds i8*, i8** %0, i64 2
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %42, label %31
31: ; preds = %27
%32 = load i8*, i8** %9
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %31
%36 = load i8*, i8** %9
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %129
42: ; preds = %35, %31, %27
%43 = load i8*, i8** %9
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %61
46: ; preds = %42
%47 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%48 = call i64 @chdir(i8* %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %58
55: ; preds = %46
%56 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32 1)
%57 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %47, i32 1)
br label %58
58: ; preds = %55, %50
%59 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i8* %59)
br label %129
61: ; preds = %42
%62 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%63 = load i8*, i8** %9
%64 = call i32 @strchr(i8* %63, i8 signext 126)
%65 = icmp eq i32 %64, 0
%66 = load i8*, i8** %9
br i1 %65, label %113, label %67
67: ; preds = %61
%68 = getelementptr inbounds i8, i8* %66, i64 -1
%69 = call i32 @strlen(i8* %62)
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = call i32 @strlen(i8* %71)
%73 = call i64 @malloc(i32 %72)
%74 = inttoptr i64 %73 to i8*
%75 = load i8*, i8** %9
%76 = load i8, i8* %75
%77 = icmp eq i8 %76, 126
br i1 %77, label %89, label %78
78: ; preds = %67, %78
%79 = phi i64 [ %82, %78 ], [ 0, %67 ]
%80 = phi i8 [ %85, %78 ], [ %76, %67 ]
%81 = getelementptr inbounds i8, i8* %74, i64 %79
store i8 %80, i8* %81
%82 = add nuw nsw i64 %79, 1
%83 = load i8*, i8** %9
%84 = getelementptr inbounds i8, i8* %83, i64 %82
%85 = load i8, i8* %84
%86 = icmp eq i8 %85, 126
br i1 %86, label %87, label %78
87: ; preds = %78
%88 = trunc i64 %82 to i32
br label %89
89: ; preds = %87, %67
%90 = phi i32 [ 0, %67 ], [ %88, %87 ]
%91 = zext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %74, i64 %91
%93 = call i32 @strlen(i8* %62)
%94 = call i32 @strncpy(i8* %92, i8* %62, i32 %93)
%95 = load i8*, i8** %9
%96 = call i32 @strlen(i8* %95)
%97 = icmp slt i32 %90, %96
br i1 %97, label %98, label %113
98: ; preds = %89, %98
%99 = phi i64 [ %101, %98 ], [ %91, %89 ]
%100 = load i8*, i8** %9
%101 = add nuw nsw i64 %99, 1
%102 = getelementptr inbounds i8, i8* %100, i64 %101
%103 = load i8, i8* %102
%104 = call i32 @strlen(i8* %62)
%105 = trunc i64 %99 to i32
%106 = add nsw i32 %104, %105
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %74, i64 %107
store i8 %103, i8* %108
%109 = load i8*, i8** %9
%110 = call i32 @strlen(i8* %109)
%111 = trunc i64 %101 to i32
%112 = icmp sgt i32 %110, %111
br i1 %112, label %98, label %113
113: ; preds = %98, %61, %89
%114 = phi i8* [ %74, %89 ], [ %66, %61 ], [ %74, %98 ]
%115 = call i64 @chdir(i8* %114)
%116 = icmp eq i64 %115, 0
br i1 %116, label %120, label %117
117: ; preds = %113
%118 = load i32, i32* @stderr
%119 = call i32 @fprintf(i32 %118, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
br label %129
120: ; preds = %113
%121 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32 1)
%122 = call i32* @getcwd(i8* %3, i32 1024)
%123 = icmp eq i32* %122, null
br i1 %123, label %124, label %127
124: ; preds = %120
%125 = load i32, i32* @stderr
%126 = call i32 @fprintf(i32 %125, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.13, i64 0, i64 0))
br label %127
127: ; preds = %124, %120
%128 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %3, i32 1)
br label %129
129: ; preds = %117, %127, %20, %25, %58, %39
ret i32 1
}