#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 *);
char *getenv(char *);
int /*<<< orphan*/ perror(char const *);
int cmd_cd(int argc, char *argv[]) {
  const char *dir;
  if (argc > 0)
    dir = argv[0];
  else {
    dir = getenv("HOME");
    if (dir == NULL)
      dir = "/";
  }
  if (chdir(dir)) {
    perror(dir);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cmd_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1159 <cmd_cd+0x29>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1184 <cmd_cd+0x54>
lea    0xea0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117f <cmd_cd+0x4f>
lea    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1184 <cmd_cd+0x54>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11ac <cmd_cd+0x7c>
mov    -0x18(%rbp),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b3 <cmd_cd+0x83>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_cd>:
push   %rbx
test   %edi,%edi
jle    113a <cmd_cd+0xa>
mov    (%rsi),%rbx
jmp    1154 <cmd_cd+0x24>
lea    0xebf(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xeb5(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1040 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1168 <cmd_cd+0x38>
pop    %rbx
ret
mov    %rbx,%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @cmd_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, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
store i8* %12, i8** %6
br label %19
13: ; preds = %2
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %13
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %18
18: ; preds = %17, %13
br label %19
19: ; preds = %18, %9
%20 = load i8*, i8** %6
%21 = call i64 @chdir(i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = call i32 @perror(i8* %24)
store i32 1, i32* %3
br label %27
26: ; preds = %19
store i32 0, i32* %3
br label %27
27: ; preds = %26, %23
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @cmd_cd(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i8*, i8** %1
br label %10
6: ; preds = %2
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
%9 = select i1 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %7
br label %10
10: ; preds = %6, %4
%11 = phi i8* [ %5, %4 ], [ %9, %6 ]
%12 = tail call i64 @chdir(i8* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* %11)
br label %16
16: ; preds = %10, %14
%17 = phi i32 [ 1, %14 ], [ 0, %10 ]
ret i32 %17
}