#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_DIRECTORY;
int PATH_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fchdir(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *get_current_dir_name();
int /*<<< orphan*/ *getcwd(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char path[PATH_MAX];
  int status;
  int fd;
  if (argc < 2) {
    fprintf(stderr, "missing argument\n");
    exit(EXIT_FAILURE);
  }
  fd = open(argv[1], O_DIRECTORY);
  if (fd < 0) {
    perror("open()");
    exit(EXIT_FAILURE);
  }
  status = fchdir(fd);
  if (status < 0) {
    perror("fchdir()");
    exit(EXIT_FAILURE);
  }
  if (getcwd(path, PATH_MAX) == NULL) {
    perror("getcwd()");
    exit(EXIT_FAILURE);
  }
  printf("%s\n", get_current_dir_name());
  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    0x2e53(%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)
jge    11ef <main+0x6f>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    122c <main+0xac>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1070 <fchdir@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    125b <main+0xdb>
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    1291 <main+0x111>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <get_current_dir_name@plt>
mov    %rax,%rsi
lea    0xd89(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e62(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
jg     11be <main+0x3e>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
jmp    120d <main+0x8d>
mov    0x8(%rsi),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    11dd <main+0x5d>
lea    0xe37(%rip),%rdi
jmp    1208 <main+0x88>
mov    %eax,%edi
call   1070 <fchdir@plt>
test   %eax,%eax
jns    11f1 <main+0x71>
lea    0xe2a(%rip),%rdi
jmp    1208 <main+0x88>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    121b <main+0x9b>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   1060 <get_current_dir_name@plt>
lea    0xe02(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"missing argument\0A\00"
@EXIT_FAILURE = global i32 0
@O_DIRECTORY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [9 x i8] c"fchdir()\00"
@.str.3 = [9 x i8] c"getcwd()\00"
@.str.4 = [4 x i8] c"%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
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_DIRECTORY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %21
%34 = load i32, i32* %9
%35 = call i32 @fchdir(i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = load i32, i32* @PATH_MAX
%44 = call i32* @getcwd(i8* %13, i32 %43)
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %42
%51 = call i8* (...) @get_current_dir_name()
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @EXIT_SUCCESS
%54 = call i32 @exit(i32 %53)
unreachable
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"missing argument\0A\00"
@EXIT_FAILURE = global i32 0
@O_DIRECTORY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [9 x i8] c"fchdir()\00"
@.str.3 = [9 x i8] c"getcwd()\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_DIRECTORY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = tail call i32 @fchdir(i32 %16)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %22
%30 = load i32, i32* @PATH_MAX
%31 = call i32* @getcwd(i8* %5, i32 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = call i8* (...) @get_current_dir_name()
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
}