#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*/ bd;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int MAXPATHLEN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
scalar_t__ rfile_fn_is_abs(char const *);
char *rfile_fn_tidy(char *);
int strlen(char const *);
char *rfile_fn_rel2abs(const char *rel, const char *base) {
  char bd[MAXPATHLEN * 2];
  size_t blen, rlen;
  if (rfile_fn_is_abs(rel)) {
    blen = 0;
  } else {
    if (base) {
      char *ab = rfile_fn_rel2abs(base, NULL);
      blen = strlen(ab);
      if (blen + 1 > sizeof(bd)) {
        errno = EINVAL;
        return NULL;
      }
      memcpy(bd, ab, blen);
      free(ab);
    } else {
      if (!getcwd(bd, sizeof(bd) - 1))
        return NULL;
      blen = strlen(bd);
    }
    if (bd[blen - 1] != '/')
      bd[blen++] = '/';
  }
  bd[blen] = '\0';
  rlen = strlen(rel) + 1;
  if (blen + rlen > sizeof(bd)) {
    errno = EINVAL;
    return NULL;
  }
  memcpy(bd + blen, rel, rlen);
  return rfile_fn_tidy(bd);
}


x86 assembly (clang, -O0):
<rfile_fn_rel2abs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
add    %eax,%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <rfile_fn_is_abs@plt>
cmp    $0x0,%rax
je     11d2 <rfile_fn_rel2abs+0x62>
movq   $0x0,-0x30(%rbp)
jmp    12cb <rfile_fn_rel2abs+0x15b>
cmpq   $0x0,-0x18(%rbp)
je     1257 <rfile_fn_rel2abs+0xe7>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <rfile_fn_rel2abs@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rcx
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    %rcx,%rax
jbe    1238 <rfile_fn_rel2abs+0xc8>
mov    0x2db7(%rip),%rax
mov    (%rax),%ecx
mov    0x2db6(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    134d <rfile_fn_rel2abs+0x1dd>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
jmp    1296 <rfile_fn_rel2abs+0x126>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
call   1070 <getcwd@plt>
cmp    $0x0,%eax
jne    1287 <rfile_fn_rel2abs+0x117>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    134d <rfile_fn_rel2abs+0x1dd>
mov    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x50(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12c6 <rfile_fn_rel2abs+0x156>
mov    -0x50(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x2f,(%rax,%rcx,1)
jmp    12cb <rfile_fn_rel2abs+0x15b>
mov    -0x50(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rcx
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
cmp    %rcx,%rax
jbe    1324 <rfile_fn_rel2abs+0x1b4>
mov    0x2ccb(%rip),%rax
mov    (%rax),%ecx
mov    0x2cca(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    134d <rfile_fn_rel2abs+0x1dd>
mov    -0x50(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x50(%rbp),%rdi
call   1030 <rfile_fn_tidy@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rfile_fn_rel2abs>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e5d(%rip),%rax
mov    (%rax),%r13d
add    %r13d,%r13d
mov    %rsp,%r15
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
call   1090 <rfile_fn_is_abs@plt>
test   %rax,%rax
je     11e7 <rfile_fn_rel2abs+0x77>
xor    %ebx,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    %r14,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdx
lea    (%rbx,%rdx,1),%rax
add    $0x1,%rax
cmp    %r13,%rax
ja     120d <rfile_fn_rel2abs+0x9d>
add    %r15,%rbx
add    $0x1,%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <memcpy@plt>
mov    %r15,%rdi
call   1030 <rfile_fn_tidy@plt>
jmp    1221 <rfile_fn_rel2abs+0xb1>
test   %rbx,%rbx
je     1230 <rfile_fn_rel2abs+0xc0>
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <rfile_fn_rel2abs@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbx
lea    0x1(%rbx),%rax
cmp    %r13,%rax
jbe    1259 <rfile_fn_rel2abs+0xe9>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    0x2dbb(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    -0x1(%r13),%esi
mov    %r15,%rdi
call   1070 <getcwd@plt>
test   %eax,%eax
je     121f <rfile_fn_rel2abs+0xaf>
mov    %r15,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbx
cmpb   $0x2f,-0x1(%rbx,%r15,1)
je     11ae <rfile_fn_rel2abs+0x3e>
jmp    127b <rfile_fn_rel2abs+0x10b>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <memcpy@plt>
mov    %r12,%rdi
call   1040 <free@plt>
cmpb   $0x2f,-0x1(%rbx,%r15,1)
je     11ae <rfile_fn_rel2abs+0x3e>
movb   $0x2f,(%r15,%rbx,1)
add    $0x1,%rbx
jmp    11ae <rfile_fn_rel2abs+0x3e>

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

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i8* @rfile_fn_rel2abs(i8* %0, i8* %1) {
%3 = load i32, i32* @MAXPATHLEN
%4 = shl nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i64 @rfile_fn_is_abs(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %38
9: ; preds = %2
%10 = icmp eq i8* %1, null
br i1 %10, label %22, label %11
11: ; preds = %9
%12 = tail call i8* @rfile_fn_rel2abs(i8* %1, i8* null)
%13 = tail call i32 @strlen(i8* %12)
%14 = sext i32 %13 to i64
%15 = add nsw i64 %14, 1
%16 = icmp ugt i64 %15, %5
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
br label %51
19: ; preds = %11
%20 = call i32 @memcpy(i8* %6, i8* %12, i64 %14)
%21 = call i32 @free(i8* %12)
br label %29
22: ; preds = %9
%23 = add i32 %4, -1
%24 = call i32 @getcwd(i8* %6, i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %51, label %26
26: ; preds = %22
%27 = call i32 @strlen(i8* %6)
%28 = sext i32 %27 to i64
br label %29
29: ; preds = %19, %26
%30 = phi i64 [ %14, %19 ], [ %28, %26 ]
%31 = add nsw i64 %30, -1
%32 = getelementptr inbounds i8, i8* %6, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 47
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = add nsw i64 %30, 1
%37 = getelementptr inbounds i8, i8* %6, i64 %30
store i8 47, i8* %37
br label %38
38: ; preds = %2, %29, %35
%39 = phi i64 [ %36, %35 ], [ %30, %29 ], [ 0, %2 ]
%40 = getelementptr inbounds i8, i8* %6, i64 %39
store i8 0, i8* %40
%41 = call i32 @strlen(i8* %0)
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = add nsw i64 %39, %43
%45 = icmp ugt i64 %44, %5
br i1 %45, label %46, label %48
46: ; preds = %38
%47 = load i32, i32* @EINVAL
store i32 %47, i32* @errno
br label %51
48: ; preds = %38
%49 = call i32 @memcpy(i8* %40, i8* %0, i64 %43)
%50 = call i8* @rfile_fn_tidy(i8* %6)
br label %51
51: ; preds = %17, %22, %48, %46
%52 = phi i8* [ null, %46 ], [ %50, %48 ], [ null, %22 ], [ null, %17 ]
ret i8* %52
}