#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 */
typedef int /*<<< orphan*/ save_cwd;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int errno;
int /*<<< orphan*/ *getcwd(char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
int absolutize(char *to, size_t size, char *from) {
  char save_cwd[256], *slash;
  size_t remaining;
  if (getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
    printf("absolutize : unable to get cwd - errno = %d\n", errno);
    return (-1);
  }
  slash = strrchr(from, '/');
  if (slash != NULL) {
    *slash = '\0';
    if (chdir(from)) {
      *slash = '/';
      printf("absolutize : Can't cd to '%s' - "
             "errno = %d\n",
             from, errno);
      return (-1);
    }
    *slash = '/';
    if (getcwd(to, size) == NULL) {
      printf("absolutize : unable to get cwd of '%s' - "
             "errno = %d\n",
             from, errno);
      return (-1);
    }
    remaining = size - strlen(to);
    if (strlen(slash) + 1 > remaining) {
      printf("absolutize : unable to fit '%s' into %zd "
             "chars\n",
             from, size);
      return (-1);
    }
    strcat(to, slash);
  } else {
    if (strlen(save_cwd) + 1 + strlen(from) + 1 > size) {
      printf("absolutize : unable to fit '%s' into %zd "
             "chars\n",
             from, size);
      return (-1);
    }
    strcpy(to, save_cwd);
    strcat(to, "/");
    strcat(to, from);
  }
  chdir(save_cwd);
  return (0);
}


x86 assembly (clang, -O0):
<absolutize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
cmp    $0x0,%rax
jne    11c5 <absolutize+0x55>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <absolutize+0x225>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
je     12fc <absolutize+0x18c>
mov    -0x128(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%eax
je     1235 <absolutize+0xc5>
mov    -0x128(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x20(%rbp),%rsi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <absolutize+0x225>
mov    -0x128(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <getcwd@plt>
cmp    $0x0,%rax
jne    127d <absolutize+0x10d>
mov    -0x20(%rbp),%rsi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <absolutize+0x225>
mov    -0x18(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x138(%rbp),%rax
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %rax,-0x130(%rbp)
mov    -0x128(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x130(%rbp),%rax
jbe    12e7 <absolutize+0x177>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <absolutize+0x225>
mov    -0x10(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1090 <strcat@plt>
jmp    1382 <absolutize+0x212>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x13c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x13c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    1355 <absolutize+0x1e5>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1395 <absolutize+0x225>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xd4e(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x120(%rbp),%rdi
call   1050 <chdir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<absolutize>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
test   %rax,%rax
je     11ea <absolutize+0x7a>
mov    %r12,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     120b <absolutize+0x9b>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r12,%rdi
call   1050 <chdir@plt>
movb   $0x2f,(%rbx)
test   %eax,%eax
je     124b <absolutize+0xdb>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12c2 <absolutize+0x152>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12c2 <absolutize+0x152>
mov    %rsp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
cltq
cmp    %r15,%rax
ja     127d <absolutize+0x10d>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xe83(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
mov    %r12,%rsi
jmp    12b3 <absolutize+0x143>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <getcwd@plt>
test   %rax,%rax
je     1298 <absolutize+0x128>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
mov    %r15,%r13
sub    %rax,%r13
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %rax,%r13
jae    12ad <absolutize+0x13d>
lea    0xe0a(%rip),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12c2 <absolutize+0x152>
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    0xdb1(%rip),%rdi
jmp    11d6 <absolutize+0x66>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %rsp,%rdi
call   1050 <chdir@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [45 x i8] c"absolutize : unable to get cwd - errno = %d\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"absolutize : Can't cd to '%s' - errno = %d\0A\00"
@.str.2 = [53 x i8] c"absolutize : unable to get cwd of '%s' - errno = %d\0A\00"
@.str.3 = [48 x i8] c"absolutize : unable to fit '%s' into %zd chars\0A\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @absolutize(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%12 = call i32* @getcwd(i8* %11, i64 256)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 -1, i32* %4
br label %89
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %62
22: ; preds = %17
%23 = load i8*, i8** %9
store i8 0, i8* %23
%24 = load i8*, i8** %7
%25 = call i32 @chdir(i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i8*, i8** %9
store i8 47, i8* %28
%29 = load i8*, i8** %7
%30 = load i32, i32* @errno
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30)
store i32 -1, i32* %4
br label %89
32: ; preds = %22
%33 = load i8*, i8** %9
store i8 47, i8* %33
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = call i32* @getcwd(i8* %34, i64 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i8*, i8** %7
%40 = load i32, i32* @errno
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %39, i32 %40)
store i32 -1, i32* %4
br label %89
42: ; preds = %32
%43 = load i64, i64* %6
%44 = load i8*, i8** %5
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = sub i64 %43, %46
store i64 %47, i64* %10
%48 = load i8*, i8** %9
%49 = call i32 @strlen(i8* %48)
%50 = add nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = load i64, i64* %10
%53 = icmp ugt i64 %51, %52
br i1 %53, label %54, label %58
54: ; preds = %42
%55 = load i8*, i8** %7
%56 = load i64, i64* %6
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %55, i64 %56)
store i32 -1, i32* %4
br label %89
58: ; preds = %42
%59 = load i8*, i8** %5
%60 = load i8*, i8** %9
%61 = call i32 @strcat(i8* %59, i8* %60)
br label %86
62: ; preds = %17
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = add nsw i32 %64, 1
%66 = load i8*, i8** %7
%67 = call i32 @strlen(i8* %66)
%68 = add nsw i32 %65, %67
%69 = add nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = load i64, i64* %6
%72 = icmp ugt i64 %70, %71
br i1 %72, label %73, label %77
73: ; preds = %62
%74 = load i8*, i8** %7
%75 = load i64, i64* %6
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %74, i64 %75)
store i32 -1, i32* %4
br label %89
77: ; preds = %62
%78 = load i8*, i8** %5
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%80 = call i32 @strcpy(i8* %78, i8* %79)
%81 = load i8*, i8** %5
%82 = call i32 @strcat(i8* %81, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%83 = load i8*, i8** %5
%84 = load i8*, i8** %7
%85 = call i32 @strcat(i8* %83, i8* %84)
br label %86
86: ; preds = %77, %58
%87 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%88 = call i32 @chdir(i8* %87)
store i32 0, i32* %4
br label %89
89: ; preds = %86, %73, %54, %38, %27, %14
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
@.str = [45 x i8] c"absolutize : unable to get cwd - errno = %d\0A\00"
@errno = global i32 0
@.str.1 = [44 x i8] c"absolutize : Can't cd to '%s' - errno = %d\0A\00"
@.str.2 = [53 x i8] c"absolutize : unable to get cwd of '%s' - errno = %d\0A\00"
@.str.3 = [48 x i8] c"absolutize : unable to fit '%s' into %zd chars\0A\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @absolutize(i8* %0, i64 %1, i8* %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32* @getcwd(i8* %5, i64 256)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %53
11: ; preds = %3
%12 = call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 47)
%13 = icmp eq i8* %12, null
br i1 %13, label %38, label %14
14: ; preds = %11
store i8 0, i8* %12
%15 = call i32 @chdir(i8* %2)
%16 = icmp eq i32 %15, 0
store i8 47, i8* %12
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 %18)
br label %53
20: ; preds = %14
%21 = call i32* @getcwd(i8* %0, i64 %1)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 %24)
br label %53
26: ; preds = %20
%27 = call i32 @strlen(i8* %0)
%28 = sext i32 %27 to i64
%29 = sub i64 %1, %28
%30 = call i32 @strlen(i8* %12)
%31 = add nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = icmp ult i64 %29, %32
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %2, i64 %1)
br label %53
36: ; preds = %26
%37 = call i32 @strcat(i8* %0, i8* %12)
br label %51
38: ; preds = %11
%39 = call i32 @strlen(i8* %5)
%40 = call i32 @strlen(i8* %2)
%41 = add i32 %39, 2
%42 = add i32 %41, %40
%43 = sext i32 %42 to i64
%44 = icmp ugt i64 %43, %1
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %2, i64 %1)
br label %53
47: ; preds = %38
%48 = call i32 @strcpy(i8* %0, i8* %5)
%49 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @strcat(i8* %0, i8* %2)
br label %51
51: ; preds = %47, %36
%52 = call i32 @chdir(i8* %5)
br label %53
53: ; preds = %51, %45, %34, %23, %17, %8
%54 = phi i32 [ -1, %8 ], [ -1, %17 ], [ -1, %23 ], [ -1, %34 ], [ 0, %51 ], [ -1, %45 ]
ret i32 %54
}