#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 chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  char *swd;
  int ret;
  swd = getcwd(NULL, 0);
  if (!swd) {
    perror("getcwd");
    exit(EXIT_FAILURE);
  }
  printf("current: %s\n", swd);
  ret = chdir("/");
  if (ret) {
    perror("chdir");
    exit(EXIT_FAILURE);
  }
  printf("changing to %s\n", getcwd(NULL, 0));
  ret = chdir(swd);
  if (ret) {
    perror("chdir");
    exit(EXIT_FAILURE);
  }
  printf("current: %s\n", swd);
  free(swd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11aa <main+0x4a>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
lea    0xe53(%rip),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ed <main+0x8d>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,%rsi
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1237 <main+0xd7>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xdc5(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     11d9 <main+0x79>
mov    %rax,%rbx
lea    0xe8e(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe8c(%rip),%rdi
call   1040 <chdir@plt>
test   %eax,%eax
jne    11d0 <main+0x70>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
lea    0xe7b(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %eax,%eax
jne    11d0 <main+0x70>
lea    0xe4b(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe3f(%rip),%rdi
jmp    11e0 <main+0x80>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"getcwd\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [13 x i8] c"current: %s\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [16 x i8] c"changing to %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i8* @getcwd(i32* null, i32 0)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %15
%26 = call i8* @getcwd(i32* null, i32 0)
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %6
%29 = call i32 @chdir(i8* %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
%37 = load i8*, i8** %6
%38 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %6
%40 = call i32 @free(i8* %39)
ret i32 0
}

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