#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 chdir(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
int /*<<< orphan*/ stderr;
int strlen(char const *);
void change_dir(char const *path) {
  int i, path_len = strlen(path);
  char exec_path[(1023) + 1], s[(1023) + 1];
  if (path[0] == '~') {
    for (i = 1; i < path_len + 1; i++)
      s[i - 1] = path[i];
    sprintf(exec_path, "%s%s", getenv("HOME"), s);
  } else {
    sprintf(exec_path, "%s", path);
  }
  if (chdir(exec_path) == -1) {
    fprintf(stderr, "change_dir: No such directory\n");
  }
}


x86 assembly (clang, -O0):
<change_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11f9 <change_dir+0xa9>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11bb <change_dir+0x6b>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %cl,-0x810(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1182 <change_dir+0x32>
lea    -0x410(%rbp),%rax
mov    %rax,-0x818(%rbp)
lea    0xe35(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x818(%rbp),%rdi
mov    %rax,%rdx
lea    -0x810(%rbp),%rcx
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    1212 <change_dir+0xc2>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    123c <change_dir+0xec>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_dir>:
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmpb   $0x7e,(%rbx)
jne    11c9 <change_dir+0x59>
test   %eax,%eax
jle    119f <change_dir+0x2f>
add    $0x1,%rbx
mov    %eax,%edx
lea    0x400(%rsp),%rdi
mov    %rbx,%rsi
call   1080 <memcpy@plt>
lea    0xe5f(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe4e(%rip),%rsi
mov    %rsp,%rdi
lea    0x400(%rsp),%rcx
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
jmp    11d4 <change_dir+0x64>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rsp,%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11f6 <change_dir+0x86>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x800,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"change_dir: No such directory\0A\00"
define void @change_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %4
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 126
br i1 %13, label %14, label %38
14: ; preds = %1
store i32 1, i32* %3
br label %15
15: ; preds = %30, %14
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %33
20: ; preds = %15
%21 = load i8*, i8** %2
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = load i32, i32* %3
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %28
store i8 %25, i8* %29
br label %30
30: ; preds = %20
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %15
33: ; preds = %15
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%35 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %36)
br label %42
38: ; preds = %1
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%40 = load i8*, i8** %2
%41 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38, %33
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = call i32 @chdir(i8* %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %46, %42
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"change_dir: No such directory\0A\00"
define void @change_dir(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %0)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 126
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = icmp slt i32 %5, 1
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = getelementptr i8, i8* %0, i64 1
%14 = zext i32 %5 to i64
br label %15
15: ; preds = %12, %10
%16 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %7)
br label %20
18: ; preds = %1
%19 = call i8* @strcpy(i8* %6, i8* %0)
br label %20
20: ; preds = %18, %15
%21 = call i32 @chdir(i8* %6)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %20
ret void
}