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

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *);
scalar_t__ chdir(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int changeDir(char *cdCommand) {
  int i = 0, rc = 0;
  char *cdPathToMoveTo = NULL;
  char *errMsg = NULL;
  cdPathToMoveTo = malloc(sizeof(cdCommand));
  for (; cdCommand[i] != '\0'; i++)
    cdPathToMoveTo[i] = cdCommand[i + 3];
  cdPathToMoveTo[i + 1] = '\0';
  if (chdir(cdPathToMoveTo) < 0) {
    asprintf(&errMsg, "Can't move to '%s'!", cdPathToMoveTo);
    if (errMsg) {
      printf("ERROR: %s\n", errMsg);
      goto cleanup;
    };
  }
cleanup:
  free(cdPathToMoveTo);
  free(errMsg);
  return rc;
}


x86 assembly (clang, -O0):
<changeDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11c6 <changeDir+0x76>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1188 <changeDir+0x38>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1223 <changeDir+0xd3>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1070 <asprintf@plt>
cmpq   $0x0,-0x20(%rbp)
je     121e <changeDir+0xce>
mov    -0x20(%rbp),%rsi
lea    0xe00(%rip),%rdi
call   1050 <printf@plt>
jmp    1228 <changeDir+0xd8>
jmp    1223 <changeDir+0xd3>
jmp    1228 <changeDir+0xd8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<changeDir>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
cmpb   $0x0,(%rbx)
je     1198 <changeDir+0x48>
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
movzbl 0x2(%rbx,%rax,1),%ecx
mov    %cl,-0x1(%r14,%rax,1)
cmpb   $0x0,(%rbx,%rax,1)
lea    0x1(%rax),%rax
jne    1180 <changeDir+0x30>
mov    %eax,%eax
jmp    119d <changeDir+0x4d>
mov    $0x1,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     11b3 <changeDir+0x63>
xor    %ebx,%ebx
jmp    11de <changeDir+0x8e>
lea    0xe46(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
call   1070 <asprintf@plt>
mov    (%rsp),%rsi
test   %rsi,%rsi
je     11af <changeDir+0x5f>
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
mov    (%rsp),%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Can't move to '%s'!\00"
@.str.1 = [11 x i8] c"ERROR: %s\0A\00"
define i32 @changeDir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
store i8* null, i8** %5
store i8* null, i8** %6
%7 = call i8* @malloc(i32 8)
store i8* %7, i8** %5
br label %8
8: ; preds = %27, %1
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %8
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %5
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %16
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %8
30: ; preds = %8
%31 = load i8*, i8** %5
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %5
%37 = call i64 @chdir(i8* %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %30
%40 = load i8*, i8** %5
%41 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %40)
%42 = load i8*, i8** %6
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %45)
br label %49
47: ; preds = %39
br label %48
48: ; preds = %47, %30
br label %49
49: ; preds = %48, %44
%50 = load i8*, i8** %5
%51 = call i32 @free(i8* %50)
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@.str = [20 x i8] c"Can't move to '%s'!\00"
@.str.1 = [11 x i8] c"ERROR: %s\0A\00"
define i32 @changeDir(i8* nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%4 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %1, %7
%8 = phi i64 [ %13, %7 ], [ 0, %1 ]
%9 = add nuw nsw i64 %8, 3
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = getelementptr inbounds i8, i8* %4, i64 %8
store i8 %11, i8* %12
%13 = add nuw nsw i64 %8, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7
%18 = add nuw i64 %8, 2
%19 = and i64 %18, 4294967295
br label %20
20: ; preds = %17, %1
%21 = phi i64 [ 1, %1 ], [ %19, %17 ]
%22 = getelementptr inbounds i8, i8* %4, i64 %21
store i8 0, i8* %22
%23 = tail call i64 @chdir(i8* %4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %4)
%27 = load i8*, i8** %2
%28 = icmp eq i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%31 = load i8*, i8** %2
br label %32
32: ; preds = %20, %25, %29
%33 = phi i8* [ null, %20 ], [ null, %25 ], [ %31, %29 ]
%34 = call i32 @free(i8* %4)
%35 = call i32 @free(i8* %33)
ret i32 0
}