#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 *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int cd_b(int argc, char **argv) {
  int val = 0;
  if (argc == 0) {
    val = chdir(getenv("HOME"));
  } else if (argc == 1) {
    val = chdir(argv[1]);
  } else if (argc > 1) {
    fprintf(stderr, "cd: too many arguments\n");
    return -1;
  }
  if (val != 0) {
    fprintf(stderr, "cd: error changing directories, errno: %d\n", errno);
  }
  return val;
}


x86 assembly (clang, -O0):
<cd_b>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    116c <cd_b+0x3c>
lea    0xea9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
mov    %eax,-0x14(%rbp)
jmp    11c2 <cd_b+0x92>
cmpl   $0x1,-0x8(%rbp)
jne    118b <cd_b+0x5b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x14(%rbp)
jmp    11bd <cd_b+0x8d>
cmpl   $0x1,-0x8(%rbp)
jle    11b8 <cd_b+0x88>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <cd_b+0xc2>
jmp    11bd <cd_b+0x8d>
jmp    11c2 <cd_b+0x92>
cmpl   $0x0,-0x14(%rbp)
je     11ec <cd_b+0xbc>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd_b>:
push   %rbx
cmp    $0x1,%edi
je     114b <cd_b+0x1b>
test   %edi,%edi
jne    117c <cd_b+0x4c>
lea    0xebf(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
jmp    114f <cd_b+0x1f>
mov    0x8(%rsi),%rdi
call   1040 <chdir@plt>
test   %eax,%eax
je     11a1 <cd_b+0x71>
mov    %eax,%ebx
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
lea    0xeaa(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11a3 <cd_b+0x73>
xor    %ebx,%ebx
cmp    $0x2,%edi
jl     11a3 <cd_b+0x73>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11a3 <cd_b+0x73>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"cd: too many arguments\0A\00"
@.str.2 = [43 x i8] c"cd: error changing directories, errno: %d\0A\00"
@errno = global i32 0
define i32 @cd_b(i32 %0, i8** nocapture %1) {
switch i32 %0, label %8 [
i32 0, label %3
i32 1, label %5
]
3: ; preds = %2
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
br label %13
8: ; preds = %2
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %21
10: ; preds = %8
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %21
13: ; preds = %5, %3
%14 = phi i8* [ %7, %5 ], [ %4, %3 ]
%15 = tail call i32 @chdir(i8* %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %8, %13, %17, %10
%22 = phi i32 [ -1, %10 ], [ %15, %17 ], [ 0, %13 ], [ 0, %8 ]
ret i32 %22
}