#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 */
typedef int /*<<< orphan*/ time_t;
struct passwd {
  char *pw_dir;
};

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ ftsh_error(int, int, char *, ...);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int builtin_cd(int line, int argc, char **argv, time_t stoptime) {
  int result;
  char *dirname;
  if (argc == 2) {
    dirname = argv[1];
  } else if (argc == 1) {
    struct passwd *p;
    p = getpwuid(getuid());
    if (p) {
      dirname = p->pw_dir;
    } else {
      ftsh_error(10, line, "cd: couldn't determine your home directory");
      dirname = 0;
    }
  } else {
    ftsh_error(0, line, "cd: too many arguments");
    result = 0;
  }
  if (dirname) {
    ftsh_error(20, line, "CD %s", dirname);
    if (chdir(dirname) == 0) {
      result = 1;
    } else {
      ftsh_error(10, line, "cd: couldn't move to %s: %s", dirname,
                 strerror(errno));
      result = 0;
    }
  } else {
    result = 0;
  }
  return result;
}


x86 assembly (clang, -O0):
<builtin_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1180 <builtin_cd+0x30>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11f9 <builtin_cd+0xa9>
cmpl   $0x1,-0x8(%rbp)
jne    11da <builtin_cd+0x8a>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b7 <builtin_cd+0x67>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11d5 <builtin_cd+0x85>
mov    -0x4(%rbp),%esi
mov    $0xa,%edi
lea    0xe3a(%rip),%rdx
mov    $0x0,%al
call   1060 <ftsh_error@plt>
movq   $0x0,-0x20(%rbp)
jmp    11f4 <builtin_cd+0xa4>
mov    -0x4(%rbp),%esi
xor    %edi,%edi
lea    0xe45(%rip),%rdx
mov    $0x0,%al
call   1060 <ftsh_error@plt>
movl   $0x0,-0x18(%rbp)
jmp    11f9 <builtin_cd+0xa9>
cmpq   $0x0,-0x20(%rbp)
je     1282 <builtin_cd+0x132>
mov    -0x4(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x14,%edi
lea    0xe2b(%rip),%rdx
mov    $0x0,%al
call   1060 <ftsh_error@plt>
mov    -0x20(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jne    123d <builtin_cd+0xed>
movl   $0x1,-0x18(%rbp)
jmp    127d <builtin_cd+0x12d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    %eax,%r8d
mov    $0xa,%edi
lea    0xdd9(%rip),%rdx
mov    $0x0,%al
call   1060 <ftsh_error@plt>
movl   $0x0,-0x18(%rbp)
jmp    1289 <builtin_cd+0x139>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_cd>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
cmp    $0x1,%esi
jne    1176 <builtin_cd+0x26>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     11ea <builtin_cd+0x9a>
mov    %rax,%rdx
jmp    1183 <builtin_cd+0x33>
cmp    $0x2,%esi
jne    1207 <builtin_cd+0xb7>
add    $0x8,%rdx
mov    (%rdx),%rbx
test   %rbx,%rbx
je     11df <builtin_cd+0x8f>
lea    0xeb0(%rip),%rdx
xor    %ebp,%ebp
mov    $0x14,%edi
mov    %r14d,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <ftsh_error@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     11e3 <builtin_cd+0x93>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe80(%rip),%rdx
mov    $0xa,%edi
mov    %r14d,%esi
mov    %rbx,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1060 <ftsh_error@plt>
jmp    1200 <builtin_cd+0xb0>
xor    %ebp,%ebp
jmp    1200 <builtin_cd+0xb0>
mov    $0x1,%ebp
jmp    1200 <builtin_cd+0xb0>
lea    0xe0f(%rip),%rdx
mov    $0xa,%edi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <ftsh_error@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1d(%rip),%rdx
xor    %edi,%edi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <ftsh_error@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [43 x i8] c"cd: couldn't determine your home directory\00"
@.str.1 = [23 x i8] c"cd: too many arguments\00"
@.str.2 = [6 x i8] c"CD %s\00"
@.str.3 = [28 x i8] c"cd: couldn't move to %s: %s\00"
@errno = global i32 0
define i32 @builtin_cd(i32 %0, i32 %1, i8** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.passwd*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32 %3, i32* %8
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i8**, i8*** %7
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %10
br label %38
18: ; preds = %4
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = call i32 (...) @getuid()
%23 = call %struct.passwd* @getpwuid(i32 %22)
store %struct.passwd* %23, %struct.passwd** %11
%24 = load %struct.passwd*, %struct.passwd** %11
%25 = icmp ne %struct.passwd* %24, null
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load %struct.passwd*, %struct.passwd** %11
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 0
%29 = load i8*, i8** %28
store i8* %29, i8** %10
br label %33
30: ; preds = %21
%31 = load i32, i32* %5
%32 = call i32 (i32, i32, i8*, ...) @ftsh_error(i32 10, i32 %31, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %10
br label %33
33: ; preds = %30, %26
br label %37
34: ; preds = %18
%35 = load i32, i32* %5
%36 = call i32 (i32, i32, i8*, ...) @ftsh_error(i32 0, i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %9
br label %37
37: ; preds = %34, %33
br label %38
38: ; preds = %37, %14
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %56
41: ; preds = %38
%42 = load i32, i32* %5
%43 = load i8*, i8** %10
%44 = call i32 (i32, i32, i8*, ...) @ftsh_error(i32 20, i32 %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i8*, i8** %10
%46 = call i64 @chdir(i8* %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %41
store i32 1, i32* %9
br label %55
49: ; preds = %41
%50 = load i32, i32* %5
%51 = load i8*, i8** %10
%52 = load i32, i32* @errno
%53 = call i32 @strerror(i32 %52)
%54 = call i32 (i32, i32, i8*, ...) @ftsh_error(i32 10, i32 %50, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %53)
store i32 0, i32* %9
br label %55
55: ; preds = %49, %48
br label %57
56: ; preds = %38
store i32 0, i32* %9
br label %57
57: ; preds = %56, %55
%58 = load i32, i32* %9
ret i32 %58
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [43 x i8] c"cd: couldn't determine your home directory\00"
@.str.1 = [23 x i8] c"cd: too many arguments\00"
@.str.2 = [6 x i8] c"CD %s\00"
@.str.3 = [28 x i8] c"cd: couldn't move to %s: %s\00"
@errno = global i32 0
define i32 @builtin_cd(i32 %0, i32 %1, i8** nocapture %2, i32 %3) {
switch i32 %1, label %15 [
i32 2, label %5
i32 1, label %7
]
5: ; preds = %4
%6 = getelementptr inbounds i8*, i8** %2, i64 1
br label %17
7: ; preds = %4
%8 = tail call i32 (...) @getuid()
%9 = tail call %struct.passwd* @getpwuid(i32 %8)
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i64 0, i32 0
br label %17
13: ; preds = %7
%14 = tail call i32 (i32, i32, i8*, ...) @ftsh_error(i32 10, i32 %0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %29
15: ; preds = %4
%16 = tail call i32 (i32, i32, i8*, ...) @ftsh_error(i32 0, i32 %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
unreachable
17: ; preds = %11, %5
%18 = phi i8** [ %6, %5 ], [ %12, %11 ]
%19 = load i8*, i8** %18
%20 = icmp eq i8* %19, null
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = tail call i32 (i32, i32, i8*, ...) @ftsh_error(i32 20, i32 %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%23 = tail call i64 @chdir(i8* %19)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* @errno
%27 = tail call i32 @strerror(i32 %26)
%28 = tail call i32 (i32, i32, i8*, ...) @ftsh_error(i32 10, i32 %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %19, i32 %27)
br label %29
29: ; preds = %13, %17, %21, %25
%30 = phi i32 [ 0, %25 ], [ 1, %21 ], [ 0, %17 ], [ 0, %13 ]
ret i32 %30
}