#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 EXIT_SUCCESS;
int /*<<< orphan*/ change_dir();
scalar_t__ chdir(char *);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ get_working_dir();
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main(int argc, char *argv[]) {
  char *buf;
  get_working_dir();
  change_dir();
  if ((buf = getcwd(NULL, 0)) == NULL) {
    fprintf(stderr, "Err.:(%d) - %s: buf\n", errno, strerror(errno));
    exit(EXIT_FAILURE);
  }
  get_working_dir();
  change_dir();
  get_working_dir();
  if (chdir(buf) < 0) {
    fprintf(stderr, "Err.:(%d) - %s; %s\n", errno, strerror(errno), buf);
    exit(EXIT_FAILURE);
  }
  printf("%s\n", buf);
  free(buf);
  return (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    $0x0,%al
call   1080 <get_working_dir@plt>
mov    $0x0,%al
call   1090 <change_dir@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1218 <main+0x88>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1080 <get_working_dir@plt>
mov    $0x0,%al
call   1090 <change_dir@plt>
mov    $0x0,%al
call   1080 <get_working_dir@plt>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    128f <main+0xff>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
mov    -0x18(%rbp),%r8
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xd8b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <get_working_dir@plt>
xor    %eax,%eax
call   1090 <change_dir@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     11fa <main+0x6a>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <get_working_dir@plt>
xor    %eax,%eax
call   1090 <change_dir@plt>
xor    %eax,%eax
call   1080 <get_working_dir@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     122a <main+0x9a>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dce(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    125d <main+0xcd>
mov    0x2daf(%rip),%rax
mov    (%rax),%r14d
mov    0x2d9d(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xdca(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Err.:(%d) - %s: buf\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [20 x i8] c"Err.:(%d) - %s; %s\0A\00"
@.str.2 = [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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @get_working_dir()
%8 = call i32 (...) @change_dir()
%9 = call i8* @getcwd(i32* null, i32 0)
store i8* %9, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = call i32 (...) @get_working_dir()
%21 = call i32 (...) @change_dir()
%22 = call i32 (...) @get_working_dir()
%23 = load i8*, i8** %6
%24 = call i64 @chdir(i8* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = load i8*, i8** %6
%32 = call i32 (i32, i8*, i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %28, i8* %30, i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %19
%36 = load i8*, i8** %6
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %6
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* @EXIT_SUCCESS
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Err.:(%d) - %s: buf\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [20 x i8] c"Err.:(%d) - %s; %s\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @get_working_dir()
%4 = tail call i32 (...) @change_dir()
%5 = tail call i8* @getcwd(i32* null, i32 0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %14
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = tail call i32 (...) @get_working_dir()
%16 = tail call i32 (...) @change_dir()
%17 = tail call i32 (...) @get_working_dir()
%18 = tail call i64 @chdir(i8* %5)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %23, i8* %5)
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %14
%28 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%29 = tail call i32 @free(i8* %5)
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}