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

/* Variables and functions */
int chdir(char *);
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, scalar_t__);
int executeCdCommand(char *arg) {
  int status = 0;
  char destination[1024], currPos[1024];
  if (getcwd(currPos, sizeof(currPos)) == NULL) {
    perror("getcwd() error");
  }
  strcpy(destination, currPos);
  strcat(destination, "/");
  strncat(destination, arg, strlen(arg) - 1);
  status = chdir(destination);
  if (status != 0) {
    perror("Error");
    return -1;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<executeCdCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x820(%rbp),%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
jne    11ad <executeCdCommand+0x3d>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
lea    -0x420(%rbp),%rdi
lea    -0x820(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x830(%rbp),%rdi
mov    -0x828(%rbp),%rsi
mov    %rax,%rdx
sub    $0x1,%rdx
call   1070 <strncat@plt>
lea    -0x420(%rbp),%rdi
call   1050 <chdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1240 <executeCdCommand+0xd0>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <executeCdCommand+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executeCdCommand>:
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     11e3 <executeCdCommand+0x73>
lea    0x400(%rsp),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    -0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11f1 <executeCdCommand+0x81>
add    $0x808,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
jmp    118f <executeCdCommand+0x1f>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    11d8 <executeCdCommand+0x68>

LLVM IR (-O0):
@.str = [15 x i8] c"getcwd() error\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"Error\00"
define i32 @executeCdCommand(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%8 = call i32* @getcwd(i8* %7, i32 1024)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = call i32 @strcpy(i8* %13, i8* %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%17 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = load i8*, i8** %3
%21 = call i64 @strlen(i8* %20)
%22 = sub nsw i64 %21, 1
%23 = call i32 @strncat(i8* %18, i8* %19, i64 %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = call i32 @chdir(i8* %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %12
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %31
30: ; preds = %12
store i32 0, i32* %2
br label %31
31: ; preds = %30, %28
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [15 x i8] c"getcwd() error\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"Error\00"
define i32 @executeCdCommand(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32* @getcwd(i8* %5, i32 1024)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = call i32 @strcpy(i8* %4, i8* %5)
%12 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call i64 @strlen(i8* dereferenceable(1) %0)
%14 = add nsw i64 %13, -1
%15 = call i32 @strncat(i8* %4, i8* %0, i64 %14)
%16 = call i32 @chdir(i8* %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %10, %18
%21 = phi i32 [ -1, %18 ], [ 0, %10 ]
ret i32 %21
}