#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 EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
char *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ system(char *);
int main(int argc, char **argv) {
  char buf[512];
  if (argc != 2) {
    fprintf(stderr, "%s: requires a directory!\n", argv[0]);
    return (EXIT_FAILURE);
  }
  if (chdir(argv[1]) < 0) {
    fprintf(stderr, "%s: unable to change directory to %s\n", argv[0], argv[1]);
    return (EXIT_FAILURE);
  }
  printf("CWD is now: %s\n", getcwd(buf, 512));
  if (system("ls -l") != 0) {
    perror("unable to run ls(1)");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c2 <main+0x52>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126e <main+0xfe>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    1210 <main+0xa0>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126e <main+0xfe>
lea    -0x210(%rbp),%rdi
mov    $0x200,%esi
call   1060 <getcwd@plt>
mov    %rax,%rsi
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
lea    0xe1a(%rip),%rdi
call   1040 <system@plt>
cmp    $0x0,%rax
je     1260 <main+0xf0>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11ae <main+0x3e>
mov    0x8(%rbx),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jns    11da <main+0x6a>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    0x8(%rbx),%rcx
lea    0xe76(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11c8 <main+0x58>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe3f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
add    $0x200,%rsp
pop    %rbx
ret
mov    %rsp,%rdi
mov    $0x200,%esi
call   1060 <getcwd@plt>
lea    0xe53(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe54(%rip),%rdi
call   1040 <system@plt>
test   %rax,%rax
jne    1215 <main+0xa5>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe3b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

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