#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char *, char *);
int chkbuiltin(char **arguments) {
  if (strcmp(arguments[0], "cd") == 0) {
    if (arguments[1] == NULL) {
      chdir(getenv("HOME"));
    } else if (chdir(arguments[1]) == -1) {
      printf("%s: no such directory\n", arguments[1]);
    }
    return 1;
  } else if (strcmp(arguments[0], "exit") == 0) {
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<chkbuiltin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe96(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11dc <chkbuiltin+0x8c>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11a1 <chkbuiltin+0x51>
lea    0xe74(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
jmp    11d0 <chkbuiltin+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11cb <chkbuiltin+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe42(%rip),%rdi
call   1050 <printf@plt>
jmp    11d0 <chkbuiltin+0x80>
movl   $0x1,-0x4(%rbp)
jmp    120c <chkbuiltin+0xbc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe35(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1200 <chkbuiltin+0xb0>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1205 <chkbuiltin+0xb5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chkbuiltin>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%rdi
lea    0xea0(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1189 <chkbuiltin+0x39>
mov    (%rbx),%rdi
lea    0xeab(%rip),%rsi
call   1060 <strcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11ce <chkbuiltin+0x7e>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11b3 <chkbuiltin+0x63>
call   1040 <chdir@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
jne    1180 <chkbuiltin+0x30>
mov    0x8(%rbx),%rsi
lea    0xe5c(%rip),%rdi
call   1050 <printf@plt>
jmp    1180 <chkbuiltin+0x30>
lea    0xe49(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <chdir@plt>
mov    $0x1,%ebp
jmp    1180 <chkbuiltin+0x30>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [23 x i8] c"%s: no such directory\0A\00"
@.str.3 = [5 x i8] c"exit\00"
define i32 @chkbuiltin(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
store i8** %0, i8*** %3
%4 = load i8**, i8*** %3
%5 = getelementptr inbounds i8*, i8** %4, i64 0
%6 = load i8*, i8** %5
%7 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %1
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @chdir(i8* %15)
br label %29
17: ; preds = %9
%18 = load i8**, i8*** %3
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @chdir(i8* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8**, i8*** %3
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %17
br label %29
29: ; preds = %28, %14
store i32 1, i32* %2
br label %40
30: ; preds = %1
%31 = load i8**, i8*** %3
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %30
br label %39
39: ; preds = %38
store i32 0, i32* %2
br label %40
40: ; preds = %39, %29
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [23 x i8] c"%s: no such directory\0A\00"
@.str.3 = [5 x i8] c"exit\00"
define i32 @chkbuiltin(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %18
5: ; preds = %1
%6 = getelementptr inbounds i8*, i8** %0, i64 1
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @chdir(i8* %10)
br label %24
12: ; preds = %5
%13 = tail call i32 @chdir(i8* %7)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i8*, i8** %6
%17 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %24
18: ; preds = %1
%19 = load i8*, i8** %0
%20 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %18, %9, %15, %12
%25 = phi i32 [ 1, %12 ], [ 1, %15 ], [ 1, %9 ], [ 0, %18 ]
ret i32 %25
}