#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 */
char *__get_home_dir();
scalar_t__ chdir(char *);
int /*<<< orphan*/ perror(char *);
int psh_cd(char **args) {
  char *dir;
  dir = args[1];
  if (!dir) {
    dir = __get_home_dir();
  }
  if (dir == NULL) {
    perror("cd");
    return 1;
  }
  if (chdir(dir) != 0) {
    perror("cd");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<psh_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115e <psh_cd+0x2e>
mov    $0x0,%al
call   1040 <__get_home_dir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <psh_cd+0x51>
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b3 <psh_cd+0x83>
mov    -0x18(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11ac <psh_cd+0x7c>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b3 <psh_cd+0x83>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psh_cd>:
push   %rax
mov    0x8(%rdi),%rdi
test   %rdi,%rdi
jne    1149 <psh_cd+0x19>
xor    %eax,%eax
call   1040 <__get_home_dir@plt>
mov    %rax,%rdi
test   %rax,%rax
je     115a <psh_cd+0x2a>
call   1030 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    115a <psh_cd+0x2a>
pop    %rcx
ret
lea    0xe9f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
define i32 @psh_cd(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i8*
store i8** %0, i8*** %3
%5 = load i8**, i8*** %3
%6 = getelementptr inbounds i8*, i8** %5, i64 1
%7 = load i8*, i8** %6
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i8* (...) @__get_home_dir()
store i8* %11, i8** %4
br label %12
12: ; preds = %10, %1
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %24
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i64 @chdir(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %24
23: ; preds = %17
store i32 0, i32* %2
br label %24
24: ; preds = %23, %21, %15
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
define i32 @psh_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 %8
5: ; preds = %1
%6 = tail call i8* (...) @__get_home_dir()
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %1, %5
%9 = phi i8* [ %6, %5 ], [ %3, %1 ]
%10 = tail call i64 @chdir(i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8, %5
%13 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = phi i32 [ 0, %8 ], [ 1, %12 ]
ret i32 %15
}