#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 */
scalar_t__ chdir(char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *abs_path(const char *path) {
  char *cwd = NULL;
  char *result = NULL;
  cwd = getcwd(NULL, 0);
  if (chdir(path) != 0) {
    free(cwd);
    return NULL;
  }
  result = getcwd(NULL, 0);
  if (chdir(cwd) != 0) {
    fprintf(stderr,
            "ERROR: Unable to change back to original working directory!\n");
    free(cwd);
    exit(3);
  }
  free(cwd);
  return result;
}


x86 assembly (clang, -O0):
<abs_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11a4 <abs_path+0x54>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ff <abs_path+0xaf>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11ee <abs_path+0x9e>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<abs_path>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %r15d,%r15d
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    118d <abs_path+0x3d>
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    119e <abs_path+0x4e>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x3,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [61 x i8] c"ERROR: Unable to change back to original working directory!\0A\00"
define i8* @abs_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
%6 = call i8* @getcwd(i32* null, i32 0)
store i8* %6, i8** %4
%7 = load i8*, i8** %3
%8 = call i64 @chdir(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = call i32 @free(i8* %11)
store i8* null, i8** %2
br label %28
13: ; preds = %1
%14 = call i8* @getcwd(i32* null, i32 0)
store i8* %14, i8** %5
%15 = load i8*, i8** %4
%16 = call i64 @chdir(i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
%23 = call i32 @exit(i32 3)
unreachable
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %5
store i8* %27, i8** %2
br label %28
28: ; preds = %24, %10
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [61 x i8] c"ERROR: Unable to change back to original working directory!\0A\00"
define i8* @abs_path(i8* %0) {
%2 = tail call i8* @getcwd(i32* null, i32 0)
%3 = tail call i64 @chdir(i8* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %1
%6 = tail call i8* @getcwd(i32* null, i32 0)
%7 = tail call i64 @chdir(i8* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @free(i8* %2)
%13 = tail call i32 @exit(i32 3)
unreachable
14: ; preds = %5, %1
%15 = phi i8* [ null, %1 ], [ %6, %5 ]
%16 = tail call i32 @free(i8* %2)
ret i8* %15
}