#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__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *strdup(char const *);
size_t strlen(char const *);
char *rel2abs_path(const char *rel_path) {
  char *buf = NULL, *cwd, *ret;
  size_t len = 128, cwd_len, rel_len, ret_len;
  if (rel_path[0] == '/')
    return strdup(rel_path);
  for (;;) {
    buf = malloc(len);
    if (buf == NULL)
      return NULL;
    cwd = getcwd(buf, len);
    if (cwd == NULL) {
      free(buf);
      if (errno != ERANGE) {
        return NULL;
      }
      len *= 2;
    } else {
      break;
    }
  }
  cwd_len = strlen(cwd);
  rel_len = strlen(rel_path);
  ret_len = cwd_len + 1 + rel_len + 1;
  ret = malloc(ret_len);
  if (ret) {
    memcpy(ret, cwd, cwd_len);
    ret[cwd_len] = '/';
    memcpy(ret + cwd_len + 1, rel_path, rel_len);
    ret[ret_len - 1] = '\0';
  }
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<rel2abs_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x80,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    119e <rel2abs_path+0x3e>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12cc <rel2abs_path+0x16c>
jmp    11a3 <rel2abs_path+0x43>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <rel2abs_path+0x68>
movq   $0x0,-0x8(%rbp)
jmp    12cc <rel2abs_path+0x16c>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <getcwd@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1225 <rel2abs_path+0xc5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <rel2abs_path+0xb4>
movq   $0x0,-0x8(%rbp)
jmp    12cc <rel2abs_path+0x16c>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    122a <rel2abs_path+0xca>
jmp    122f <rel2abs_path+0xcf>
jmp    11a3 <rel2abs_path+0x43>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
add    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12bb <rel2abs_path+0x15b>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
add    -0x38(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rel2abs_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
cmpb   $0x2f,(%rdi)
jne    1189 <rel2abs_path+0x29>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strdup@plt>
mov    $0x80,%ebx
mov    $0x80,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11e4 <rel2abs_path+0x84>
mov    %rax,%r12
mov    0x2e31(%rip),%rbp
mov    0x2e3a(%rip),%r14
xchg   %ax,%ax
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    11e8 <rel2abs_path+0x88>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x0(%rbp),%rax
cmp    (%r14),%rax
jne    11e4 <rel2abs_path+0x84>
add    %rbx,%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b0 <rel2abs_path+0x50>
xor    %ebp,%ebp
jmp    1259 <rel2abs_path+0xf9>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1251 <rel2abs_path+0xf1>
lea    (%rbx,%r14,1),%rax
add    $0x1,%rax
mov    %rax,(%rsp)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
movb   $0x2f,0x0(%rbp,%rbx,1)
lea    (%rbx,%rbp,1),%rdi
add    $0x1,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
mov    (%rsp),%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @rel2abs_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
store i64 128, i64* %7
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 47
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %2
br label %81
19: ; preds = %1
br label %20
20: ; preds = %43, %19
%21 = load i64, i64* %7
%22 = call i8* @malloc(i64 %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
store i8* null, i8** %2
br label %81
26: ; preds = %20
%27 = load i8*, i8** %4
%28 = load i64, i64* %7
%29 = call i8* @getcwd(i8* %27, i64 %28)
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
%35 = load i64, i64* @errno
%36 = load i64, i64* @ERANGE
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %32
store i8* null, i8** %2
br label %81
39: ; preds = %32
%40 = load i64, i64* %7
%41 = mul i64 %40, 2
store i64 %41, i64* %7
br label %43
42: ; preds = %26
br label %44
43: ; preds = %39
br label %20
44: ; preds = %42
%45 = load i8*, i8** %5
%46 = call i64 @strlen(i8* %45)
store i64 %46, i64* %8
%47 = load i8*, i8** %3
%48 = call i64 @strlen(i8* %47)
store i64 %48, i64* %9
%49 = load i64, i64* %8
%50 = add i64 %49, 1
%51 = load i64, i64* %9
%52 = add i64 %50, %51
%53 = add i64 %52, 1
store i64 %53, i64* %10
%54 = load i64, i64* %10
%55 = call i8* @malloc(i64 %54)
store i8* %55, i8** %6
%56 = load i8*, i8** %6
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %77
58: ; preds = %44
%59 = load i8*, i8** %6
%60 = load i8*, i8** %5
%61 = load i64, i64* %8
%62 = call i32 @memcpy(i8* %59, i8* %60, i64 %61)
%63 = load i8*, i8** %6
%64 = load i64, i64* %8
%65 = getelementptr inbounds i8, i8* %63, i64 %64
store i8 47, i8* %65
%66 = load i8*, i8** %6
%67 = load i64, i64* %8
%68 = getelementptr inbounds i8, i8* %66, i64 %67
%69 = getelementptr inbounds i8, i8* %68, i64 1
%70 = load i8*, i8** %3
%71 = load i64, i64* %9
%72 = call i32 @memcpy(i8* %69, i8* %70, i64 %71)
%73 = load i8*, i8** %6
%74 = load i64, i64* %10
%75 = sub i64 %74, 1
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
br label %77
77: ; preds = %58, %44
%78 = load i8*, i8** %4
%79 = call i32 @free(i8* %78)
%80 = load i8*, i8** %6
store i8* %80, i8** %2
br label %81
81: ; preds = %77, %38, %25, %16
%82 = load i8*, i8** %2
ret i8* %82
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @rel2abs_path(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 47
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(128) i8* @malloc(i64 128)
%6 = icmp eq i8* %5, null
br i1 %6, label %39, label %12
7: ; preds = %1
%8 = tail call i8* @strdup(i8* %0)
br label %39
9: ; preds = %17
%10 = tail call i8* @malloc(i64 %22)
%11 = icmp eq i8* %10, null
br i1 %11, label %39, label %12
12: ; preds = %4, %9
%13 = phi i8* [ %10, %9 ], [ %5, %4 ]
%14 = phi i64 [ %22, %9 ], [ 128, %4 ]
%15 = tail call i8* @getcwd(i8* %13, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = tail call i32 @free(i8* %13)
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
%22 = shl i64 %14, 1
br i1 %21, label %9, label %39
23: ; preds = %12
%24 = tail call i64 @strlen(i8* %15)
%25 = tail call i64 @strlen(i8* %0)
%26 = add i64 %24, 1
%27 = add i64 %26, %25
%28 = add i64 %27, 1
%29 = tail call i8* @malloc(i64 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %37, label %31
31: ; preds = %23
%32 = tail call i32 @memcpy(i8* %29, i8* %15, i64 %24)
%33 = getelementptr inbounds i8, i8* %29, i64 %24
store i8 47, i8* %33
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = tail call i32 @memcpy(i8* %34, i8* %0, i64 %25)
%36 = getelementptr inbounds i8, i8* %29, i64 %27
store i8 0, i8* %36
br label %37
37: ; preds = %31, %23
%38 = tail call i32 @free(i8* %13)
br label %39
39: ; preds = %9, %17, %4, %37, %7
%40 = phi i8* [ %8, %7 ], [ %29, %37 ], [ null, %4 ], [ null, %17 ], [ null, %9 ]
ret i8* %40
}