#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 *);
char *getenv(char *);
int /*<<< orphan*/ printf(char *, int);
void dir_cmd(int argc, char **argv) {
  char *dir;
  if (argc == 1) {
    dir = getenv("HOME");
  } else {
    dir = argv[1];
  }
  int err = chdir(dir);
  if (err != 0) {
    printf("Unable to change directory, error: %d\n", err);
  }
}


x86 assembly (clang, -O0):
<dir_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    115e <dir_cmd+0x2e>
lea    0xeb0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
jmp    116a <dir_cmd+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     118f <dir_cmd+0x5f>
mov    -0x1c(%rbp),%esi
lea    0xe7b(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_cmd>:
push   %rax
cmp    $0x1,%edi
jne    1147 <dir_cmd+0x17>
lea    0xec3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
jmp    114b <dir_cmd+0x1b>
mov    0x8(%rsi),%rdi
call   1040 <chdir@plt>
test   %eax,%eax
je     1163 <dir_cmd+0x33>
lea    0xeaa(%rip),%rdi
mov    %eax,%esi
pop    %rax
jmp    1050 <printf@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [39 x i8] c"Unable to change directory, error: %d\0A\00"
define void @dir_cmd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %5
br label %15
11: ; preds = %2
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %5
br label %15
15: ; preds = %11, %9
%16 = load i8*, i8** %5
%17 = call i32 @chdir(i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %6
%22 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %15
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [39 x i8] c"Unable to change directory, error: %d\0A\00"
define void @dir_cmd(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %4
%10 = phi i8* [ %5, %4 ], [ %8, %6 ]
%11 = tail call i32 @chdir(i8* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %15
15: ; preds = %13, %9
ret void
}