#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  char *pw_dir;
};

/* Variables and functions */
scalar_t__ chdir(char const *);
char *getenv(char *);
TYPE_1__ *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ printf(char *);
void bi_cd(char *dir) {
  if (dir != NULL) {
    if (chdir(dir))
      printf("cd failed\n");
  } else {
    const char *homedir;
    if ((homedir = getenv("HOME")) == NULL) {
      homedir = getpwuid(getuid())->pw_dir;
    }
    if (chdir(homedir))
      printf("cd failed\n");
  }
}


x86 assembly (clang, -O0):
<bi_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     118b <bi_cd+0x3b>
mov    -0x8(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     1186 <bi_cd+0x36>
lea    0xe7f(%rip),%rdi
call   1070 <printf@plt>
jmp    11de <bi_cd+0x8e>
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ba <bi_cd+0x6a>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     11d9 <bi_cd+0x89>
lea    0xe2c(%rip),%rdi
call   1070 <printf@plt>
jmp    11de <bi_cd+0x8e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bi_cd>:
push   %rax
test   %rdi,%rdi
jne    117b <bi_cd+0x2b>
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1178 <bi_cd+0x28>
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
je     1192 <bi_cd+0x42>
lea    0xe79(%rip),%rdi
pop    %rax
jmp    1040 <puts@plt>
pop    %rax
ret

LLVM IR (-O0):
%struct.TYPE_2__ = type { i8* }
@.str = [11 x i8] c"cd failed\0A\00"
@.str.1 = [5 x i8] c"HOME\00"
define void @bi_cd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = load i8*, i8** %2
%8 = call i64 @chdir(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
br label %28
13: ; preds = %1
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = call i32 (...) @getuid()
%18 = call %struct.TYPE_2__* @getpwuid(i32 %17)
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %3
br label %21
21: ; preds = %16, %13
%22 = load i8*, i8** %3
%23 = call i64 @chdir(i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
br label %28
28: ; preds = %27, %12
ret void
}

LLVM IR (-O2):
%struct.TYPE_2__ = type { i8* }
@.str.1 = [5 x i8] c"HOME\00"
@str.2 = [10 x i8] c"cd failed\00"
define void @bi_cd(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = tail call i64 @chdir(i8* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %21, label %18
6: ; preds = %1
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = tail call i32 (...) @getuid()
%11 = tail call %struct.TYPE_2__* @getpwuid(i32 %10)
%12 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %11, i64 0, i32 0
%13 = load i8*, i8** %12
br label %14
14: ; preds = %9, %6
%15 = phi i8* [ %13, %9 ], [ %7, %6 ]
%16 = tail call i64 @chdir(i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14, %3
%19 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @str.2, i64 0, i64 0), %3 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @str.2, i64 0, i64 0), %14 ]
%20 = tail call i32 @puts(i8* dereferenceable(1) %19)
br label %21
21: ; preds = %18, %14, %3
ret void
}