#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
int /*<<< orphan*/ run_test(char *, int, char **);
int strcmp(char *, char *);
int test_cd(int argc, char **argv) {
  char *args[] = {"./shell", NULL};
  char *old_dir_name, *new_dir_name;
  int result;
  old_dir_name = get_current_dir_name();
  run_test("cd /usr\nexit\n", 1, args);
  new_dir_name = get_current_dir_name();
  result = strcmp("/usr", new_dir_name);
  chdir(old_dir_name);
  free(old_dir_name);
  free(new_dir_name);
  do {
    if (result)
      exit(EXIT_FAILURE);
  } while (0);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<test_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c8a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c87(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <get_current_dir_name@plt>
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdx
lea    0xe6d(%rip),%rdi
mov    $0x1,%esi
call   1060 <run_test@plt>
mov    $0x0,%al
call   1070 <get_current_dir_name@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe5b(%rip),%rdi
call   1050 <strcmp@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x34(%rbp)
je     11f6 <test_cd+0x96>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11fb <test_cd+0x9b>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movaps 0x2c91(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1070 <get_current_dir_name@plt>
mov    %rax,%r14
lea    0xe84(%rip),%rdi
mov    %rsp,%rdx
mov    $0x1,%esi
call   1060 <run_test@plt>
xor    %eax,%eax
call   1070 <get_current_dir_name@plt>
mov    %rax,%rbx
lea    0xe74(%rip),%rdi
mov    %rax,%rsi
call   1050 <strcmp@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <chdir@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    11da <test_cd+0x7a>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"./shell\00"
@__const.test_cd.args = [2 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [14 x i8] c"cd /usr\0Aexit\0A\00"
@.str.2 = [5 x i8] c"/usr\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @test_cd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [2 x i8*]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = bitcast [2 x i8*]* %5 to i8*
%10 = call i8* (...) @get_current_dir_name()
store i8* %10, i8** %6
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%12 = call i32 @run_test(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 1, i8** %11)
%13 = call i8* (...) @get_current_dir_name()
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = call i32 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %14)
store i32 %15, i32* %8
%16 = load i8*, i8** %6
%17 = call i32 @chdir(i8* %16)
%18 = load i8*, i8** %6
%19 = call i32 @free(i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @free(i8* %20)
br label %22
22: ; preds = %2
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %22
br label %29
29: ; preds = %28
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}

LLVM IR (-O2):
@.str = [8 x i8] c"./shell\00"
@__const.test_cd.args = [2 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [14 x i8] c"cd /usr\0Aexit\0A\00"
@.str.2 = [5 x i8] c"/usr\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @test_cd(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i8*]
%4 = bitcast [2 x i8*]* %3 to i8*
%5 = tail call i8* (...) @get_current_dir_name()
%6 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%7 = call i32 @run_test(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 1, i8** %6)
%8 = call i8* (...) @get_current_dir_name()
%9 = call i32 @strcmp(i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* dereferenceable(1) %8)
%10 = call i32 @chdir(i8* %5)
%11 = call i32 @free(i8* %5)
%12 = call i32 @free(i8* %8)
%13 = icmp eq i32 %9, 0
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32, i32* @EXIT_SUCCESS
ret i32 %18
}