#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 const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
int do_cd(int argc, const char **argv) {
  const char *path;
  if (argc > 1 && *argv[1] != '~') {
    path = argv[1];
  } else {
    path = getenv("HOME");
    if (path == NULL) {
      fprintf(stderr, "No HOME environment variable\n");
      return 1;
    }
  }
  if (chdir(path) < 0) {
    perror(path);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    117e <do_cd+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     117e <do_cd+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11bf <do_cd+0x7f>
lea    0xe7b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ba <do_cd+0x7a>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ee <do_cd+0xae>
jmp    11bf <do_cd+0x7f>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    11e7 <do_cd+0xa7>
mov    -0x18(%rbp),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ee <do_cd+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cd>:
push   %rbx
cmp    $0x2,%edi
jl     114f <do_cd+0xf>
mov    0x8(%rsi),%rbx
cmpb   $0x7e,(%rbx)
jne    1163 <do_cd+0x23>
lea    0xeaa(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1177 <do_cd+0x37>
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1193 <do_cd+0x53>
pop    %rbx
ret
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    %rbx,%rdi
call   1060 <perror@plt>
jmp    118c <do_cd+0x4c>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"No HOME environment variable\0A\00"
define i32 @do_cd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp sgt i32 %7, 1
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 126
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
br label %28
20: ; preds = %9, %2
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %36
27: ; preds = %20
br label %28
28: ; preds = %27, %16
%29 = load i8*, i8** %6
%30 = call i64 @chdir(i8* %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %6
%34 = call i32 @perror(i8* %33)
store i32 1, i32* %3
br label %36
35: ; preds = %28
store i32 0, i32* %3
br label %36
36: ; preds = %35, %32, %24
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"No HOME environment variable\0A\00"
define i32 @do_cd(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 126
br i1 %8, label %9, label %15
9: ; preds = %4, %2
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %21
15: ; preds = %4, %9
%16 = phi i8* [ %10, %9 ], [ %6, %4 ]
%17 = tail call i64 @chdir(i8* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @perror(i8* %16)
br label %21
21: ; preds = %15, %19, %12
%22 = phi i32 [ 1, %19 ], [ 1, %12 ], [ 0, %15 ]
ret i32 %22
}