#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__ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned int strlen(char *);
void mrsh_cd(char **args) {
  if (args[1] == NULL) {
    do {
      fprintf(stderr, "mrsh: ");
      fprintf(stderr, "mrsh: expected arg to 'cd'\n");
      fprintf(stderr, "\n");
    } while (0);
    return;
  }
  char *dir = args[1];
  unsigned len = strlen(dir);
  if (dir[len - 1] == '\n') {
    dir[len - 1] = '\0';
  }
  if (chdir(dir) != 0) {
    do {
      fprintf(stderr, "mrsh: ");
      fprintf(stderr, "mrsh: chdir(%s): %s", dir, strerror(errno));
      fprintf(stderr, "\n");
    } while (0);
  }
}


x86 assembly (clang, -O0):
<mrsh_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11aa <mrsh_cd+0x6a>
jmp    1160 <mrsh_cd+0x20>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1270 <mrsh_cd+0x130>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11eb <mrsh_cd+0xab>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1270 <mrsh_cd+0x130>
jmp    1203 <mrsh_cd+0xc3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1270 <mrsh_cd+0x130>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrsh_cd>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rdi),%rbx
test   %rbx,%rbx
je     11b3 <mrsh_cd+0x73>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%eax
cmpb   $0xa,(%rbx,%rax,1)
jne    1161 <mrsh_cd+0x21>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     11f7 <mrsh_cd+0xb7>
mov    0x2e67(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe7d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%ebp
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe81(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11da <mrsh_cd+0x9a>
mov    0x2e26(%rip),%rbx
mov    (%rbx),%edi
lea    0xe3d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe34(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [7 x i8] c"mrsh: \00"
@.str.1 = [28 x i8] c"mrsh: expected arg to 'cd'\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [20 x i8] c"mrsh: chdir(%s): %s\00"
@errno = global i32 0
define void @mrsh_cd(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i32
store i8** %0, i8*** %2
%5 = load i8**, i8*** %2
%6 = getelementptr inbounds i8*, i8** %5, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %18
9: ; preds = %1
br label %10
10: ; preds = %9
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %10
br label %54
18: ; preds = %1
%19 = load i8**, i8*** %2
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %4
%24 = load i8*, i8** %3
%25 = load i32, i32* %4
%26 = sub i32 %25, 1
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %38
32: ; preds = %18
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = sub i32 %34, 1
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %32, %18
%39 = load i8*, i8** %3
%40 = call i64 @chdir(i8* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %38
br label %43
43: ; preds = %42
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %3
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %47, i8* %49)
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %43
br label %54
54: ; preds = %17, %53, %38
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [7 x i8] c"mrsh: \00"
@.str.1 = [28 x i8] c"mrsh: expected arg to 'cd'\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [20 x i8] c"mrsh: chdir(%s): %s\00"
@errno = global i32 0
define void @mrsh_cd(i8** nocapture %0) {
%2 = getelementptr inbounds i8*, i8** %0, i64 1
%3 = load i8*, i8** %2
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %3)
%12 = add i32 %11, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %3, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %17, label %18
17: ; preds = %10
store i8 0, i8* %14
br label %18
18: ; preds = %17, %10
%19 = tail call i64 @chdir(i8* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* %26)
br label %28
28: ; preds = %5, %21
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %28, %18
ret void
}