#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*/ O_RDONLY;
int chdir(char *);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fchdir(int);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  int swd_fd, ret;
  char *s;
  swd_fd = open(".", O_RDONLY);
  if (swd_fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  printf("current: %s\n", getcwd(NULL, 0));
  ret = chdir("/");
  if (ret) {
    perror("chdir");
    exit(EXIT_FAILURE);
  }
  printf("changing to: %s\n", getcwd(NULL, 0));
  ret = fchdir(swd_fd);
  if (ret) {
    perror("fchdir");
    exit(EXIT_FAILURE);
  }
  printf("changing back to: %s\n", getcwd(NULL, 0));
  ret = close(swd_fd);
  if (ret) {
    perror("close");
    exit(EXIT_FAILURE);
  }
  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)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d2 <main+0x52>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%rsi
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
lea    0xe21(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     121f <main+0x9f>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%rsi
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <fchdir@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1268 <main+0xe8>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%rsi
lea    0xdb1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12b1 <main+0x131>
lea    0xda6(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     120f <main+0x8f>
mov    %eax,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
lea    0xe5a(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xe58(%rip),%rdi
call   1030 <chdir@plt>
test   %eax,%eax
jne    1218 <main+0x98>
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
lea    0xe41(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <fchdir@plt>
test   %eax,%eax
jne    1221 <main+0xa1>
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
lea    0xe36(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
test   %eax,%eax
jne    122a <main+0xaa>
xor    %eax,%eax
pop    %rbx
ret
lea    0xdec(%rip),%rdi
jmp    1231 <main+0xb1>
lea    0xe09(%rip),%rdi
jmp    1231 <main+0xb1>
lea    0xdff(%rip),%rdi
jmp    1231 <main+0xb1>
lea    0xe13(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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