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

/* Variables and functions */
int BUFSIZ;
scalar_t__ chdir(char *);
int errno;
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strtok(char *, char *);
int cd(const char *pCmd) {
  char strCmd[BUFSIZ];
  char *p = NULL;
  strncpy(strCmd, pCmd, sizeof(strCmd));
  strtok(strCmd, " \t");
  p = strtok(NULL, " \t");
  if (p == NULL) {
    struct passwd *pPw = NULL;
    pPw = getpwuid(getuid());
    if (pPw == NULL) {
      perror("getpwuid() failed.");
      return errno;
    }
    p = pPw->pw_dir;
  }
  if (chdir(p) != 0) {
    perror("cd ");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1080 <strtok@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe3a(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <cd+0xce>
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1223 <cd+0xc3>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1273 <cd+0x113>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     1265 <cd+0x105>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1273 <cd+0x113>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cd>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdi,%rsi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1030 <strncpy@plt>
lea    0xe6a(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strtok@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
jne    11c8 <cd+0x68>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     11e9 <cd+0x89>
mov    (%rax),%rax
mov    %rax,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    11e0 <cd+0x80>
mov    %ebx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2f(%rip),%rdi
jmp    11f0 <cd+0x90>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebx
jmp    11d5 <cd+0x75>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@BUFSIZ = global i32 0
@.str = [3 x i8] c" \09\00"
@.str.1 = [19 x i8] c"getpwuid() failed.\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"cd \00"
define i32 @cd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
store i8* null, i8** %6
%13 = load i8*, i8** %3
%14 = trunc i64 %10 to i32
%15 = call i32 @strncpy(i8* %12, i8* %13, i32 %14)
%16 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%17 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %32
20: ; preds = %1
store %struct.passwd* null, %struct.passwd** %7
%21 = call i32 (...) @getuid()
%22 = call %struct.passwd* @getpwuid(i32 %21)
store %struct.passwd* %22, %struct.passwd** %7
%23 = load %struct.passwd*, %struct.passwd** %7
%24 = icmp eq %struct.passwd* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @errno
store i32 %27, i32* %2
store i32 1, i32* %8
br label %40
28: ; preds = %20
%29 = load %struct.passwd*, %struct.passwd** %7
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %29, i32 0, i32 0
%31 = load i8*, i8** %30
store i8* %31, i8** %6
br label %32
32: ; preds = %28, %1
%33 = load i8*, i8** %6
%34 = call i64 @chdir(i8* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @errno
store i32 %38, i32* %2
store i32 1, i32* %8
br label %40
39: ; preds = %32
store i32 0, i32* %2
store i32 1, i32* %8
br label %40
40: ; preds = %39, %36, %25
%41 = load i8*, i8** %4
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@BUFSIZ = global i32 0
@.str = [3 x i8] c" \09\00"
@.str.1 = [19 x i8] c"getpwuid() failed.\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"cd \00"
define i32 @cd(i8* %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @strncpy(i8* %4, i8* %0, i32 %2)
%6 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = call i32 (...) @getuid()
%11 = call %struct.passwd* @getpwuid(i32 %10)
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %20, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 0
%15 = load i8*, i8** %14
br label %16
16: ; preds = %13, %1
%17 = phi i8* [ %15, %13 ], [ %7, %1 ]
%18 = call i64 @chdir(i8* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16, %9
%21 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %16 ]
%22 = call i32 @perror(i8* %21)
%23 = load i32, i32* @errno
br label %24
24: ; preds = %20, %16
%25 = phi i32 [ 0, %16 ], [ %23, %20 ]
ret i32 %25
}