#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__ calloc(int, int);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
void change_directory(char *buffer) {
  char delimiter[] = " ";
  char *path;
  char *inptcpy;
  int inptlen;
  inptlen = strlen(buffer);
  inptcpy = (char *)calloc(inptlen + 1, sizeof(char));
  strncpy(inptcpy, buffer, inptlen);
  strtok(inptcpy, delimiter);
  path = strtok(NULL, delimiter);
  if (strcmp(buffer, "cd ..") == 0)
    chdir("../");
  else if (strcmp(buffer, "cd") == 0 || strcmp(buffer, "cd ~") == 0)
    chdir(getenv("HOME"));
  else
    chdir(path);
  if (inptcpy)
    free(inptcpy);
}


x86 assembly (clang, -O0):
<change_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xe6d(%rip),%ax
mov    %ax,-0xa(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x20(%rbp),%rdi
lea    -0xa(%rbp),%rsi
call   10a0 <strtok@plt>
lea    -0xa(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1210 <change_directory+0x90>
lea    0xe02(%rip),%rdi
call   1070 <chdir@plt>
jmp    126b <change_directory+0xeb>
mov    -0x8(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1244 <change_directory+0xc4>
mov    -0x8(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    125d <change_directory+0xdd>
lea    0xdc9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1070 <chdir@plt>
jmp    1266 <change_directory+0xe6>
mov    -0x18(%rbp),%rdi
call   1070 <chdir@plt>
jmp    126b <change_directory+0xeb>
cmpq   $0x0,-0x20(%rbp)
je     127f <change_directory+0xff>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_directory>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
movw   $0x20,0x6(%rsp)
call   1060 <strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <strncpy@plt>
lea    0x6(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r15
lea    0xe26(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <change_directory+0xa0>
lea    0xe1c(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     120f <change_directory+0x8f>
lea    0xe0b(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1227 <change_directory+0xa7>
lea    0xdfc(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
jmp    1227 <change_directory+0xa7>
lea    0xddf(%rip),%r15
mov    %r15,%rdi
call   1070 <chdir@plt>
test   %r14,%r14
je     1247 <change_directory+0xc7>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.change_directory.delimiter = [2 x i8] c" \00"
@.str = [6 x i8] c"cd ..\00"
@.str.1 = [4 x i8] c"../\00"
@.str.2 = [3 x i8] c"cd\00"
@.str.3 = [5 x i8] c"cd ~\00"
@.str.4 = [5 x i8] c"HOME\00"
define void @change_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = bitcast [2 x i8]* %3 to i8*
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = call i64 @calloc(i32 %11, i32 1)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = load i8*, i8** %2
%16 = load i32, i32* %6
%17 = call i32 @strncpy(i8* %14, i8* %15, i32 %16)
%18 = load i8*, i8** %5
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%20 = call i8* @strtok(i8* %18, i8* %19)
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%22 = call i8* @strtok(i8* null, i8* %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %2
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %1
%27 = call i32 @chdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %43
28: ; preds = %1
%29 = load i8*, i8** %2
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i8*, i8** %2
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32, %28
%37 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @chdir(i8* %37)
br label %42
39: ; preds = %32
%40 = load i8*, i8** %4
%41 = call i32 @chdir(i8* %40)
br label %42
42: ; preds = %39, %36
br label %43
43: ; preds = %42, %26
%44 = load i8*, i8** %5
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %46, %43
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"cd ..\00"
@.str.1 = [4 x i8] c"../\00"
@.str.2 = [3 x i8] c"cd\00"
@.str.3 = [5 x i8] c"cd ~\00"
@.str.4 = [5 x i8] c"HOME\00"
define void @change_directory(i8* %0) {
%2 = alloca i16
%3 = bitcast i16* %2 to i8*
store i16 32, i16* %2
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i64 @calloc(i32 %5, i32 1)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @strncpy(i8* %7, i8* %0, i32 %4)
%9 = call i8* @strtok(i8* %7, i8* %3)
%10 = call i8* @strtok(i8* null, i8* %3)
%11 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %1
%14 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16, %13
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %16, %1, %19
%22 = phi i8* [ %20, %19 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %1 ], [ %10, %16 ]
%23 = tail call i32 @chdir(i8* %22)
%24 = icmp eq i64 %6, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = tail call i32 @free(i8* %7)
br label %27
27: ; preds = %25, %21
ret void
}