#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 EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int MAXPATHLEN;
int chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
char *getcwd(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char buf[MAXPATHLEN];
  if (argc != 2) {
    fprintf(stderr, "%s: need a directory!\n", argv[0]);
    return (EXIT_FAILURE);
  }
  if (chdir(argv[1]) == -1) {
    fprintf(stderr, "%s: unable to chdir to %s\n", argv[0], argv[1]);
    return (EXIT_FAILURE);
  }
  printf("Ok, cwd is now: %s\n", getcwd(buf, MAXPATHLEN));
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d2 <main+0x82>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1255 <main+0x105>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1226 <main+0xd6>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1255 <main+0x105>
mov    -0x30(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1050 <getcwd@plt>
mov    %rax,%rsi
lea    0xdf0(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e84(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
jne    11ab <main+0x5b>
mov    0x8(%r14),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11d9 <main+0x89>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x8(%r14),%rcx
lea    0xe75(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11c5 <main+0x75>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe42(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <getcwd@plt>
lea    0xe47(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: need a directory!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"%s: unable to chdir to %s\0A\00"
@.str.2 = [20 x i8] c"Ok, cwd is now: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @MAXPATHLEN
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
store i32 %21, i32* %3
store i32 1, i32* %8
br label %44
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @chdir(i8* %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %38
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
store i32 1, i32* %8
br label %44
38: ; preds = %22
%39 = load i32, i32* @MAXPATHLEN
%40 = call i8* @getcwd(i8* %12, i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %28, %15
%45 = load i8*, i8** %6
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: need a directory!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"%s: unable to chdir to %s\0A\00"
@.str.2 = [20 x i8] c"Ok, cwd is now: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @MAXPATHLEN
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i32 %0, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %27
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @chdir(i8* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %1
%19 = load i8*, i8** %12
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %19)
br label %27
21: ; preds = %11
%22 = load i32, i32* @MAXPATHLEN
%23 = call i8* @getcwd(i8* %5, i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_SUCCESS
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16, %7
%28 = load i32, i32* @EXIT_FAILURE
ret i32 %28
}