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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ *realpath(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int relative2real(char *relative, char *real) {
  int ret;
  char *p;
  char *slash;
  char dir[PATH_MAX];
  char buf[PATH_MAX];
  ret = 0;
  strncpy(buf, relative, PATH_MAX);
  slash = NULL;
  p = buf;
  while (*p != '\0') {
    if (*p == '/') {
      slash = p;
    }
    p++;
  }
  if (slash == NULL) {
    if (getcwd(dir, sizeof(dir)) == NULL) {
      goto fail;
    };
    (void)strncat(dir, "/", PATH_MAX - 1);
    (void)strncat(dir, relative, PATH_MAX - strlen(dir));
    if (realpath(dir, real) == NULL) {
      goto fail;
    };
  } else if (*buf == '/') {
    (void)strncat(real, buf, PATH_MAX);
  } else {
    *slash = '\0';
    if (realpath(buf, real) == NULL) {
      goto fail;
    };
    if (*(slash + 1) != '\0') {
      (void)strncat(real, "/", PATH_MAX - 1);
      (void)strncat(real, slash + 1, PATH_MAX - strlen(real));
    }
  }
  ret = 1;
fail:
  return (ret);
}


x86 assembly (clang, -O0):
<relative2real>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x58(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x38(%rbp)
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%rax
movq   $0x0,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1213 <relative2real+0xc3>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1202 <relative2real+0xb2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11da <relative2real+0x8a>
cmpq   $0x0,-0x28(%rbp)
jne    12a8 <relative2real+0x158>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    123c <relative2real+0xec>
jmp    136f <relative2real+0x21f>
mov    -0x50(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
lea    0xdad(%rip),%rsi
call   1060 <strncat@plt>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edx
mov    -0x60(%rbp),%rsi
mov    -0x50(%rbp),%rdi
sub    %eax,%edx
call   1060 <strncat@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <realpath@plt>
cmp    $0x0,%rax
jne    12a3 <relative2real+0x153>
jmp    136f <relative2real+0x21f>
jmp    1368 <relative2real+0x218>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    12d3 <relative2real+0x183>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
call   1060 <strncat@plt>
jmp    1363 <relative2real+0x213>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rsi
call   1070 <realpath@plt>
cmp    $0x0,%rax
jne    12f6 <relative2real+0x1a6>
jmp    136f <relative2real+0x21f>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     135e <relative2real+0x20e>
mov    -0x10(%rbp),%rdi
mov    0x2cde(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
lea    0xce2(%rip),%rsi
call   1060 <strncat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x70(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%edx
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
sub    %eax,%edx
call   1060 <strncat@plt>
jmp    1363 <relative2real+0x213>
jmp    1368 <relative2real+0x218>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relative2real>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    0x2e84(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r12
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
xor    %ebx,%ebx
mov    %r14,%rax
jmp    11b7 <relative2real+0x67>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbx
add    $0x1,%rax
movzbl (%rax),%ecx
cmp    $0x2f,%cl
je     11b0 <relative2real+0x60>
test   %cl,%cl
jne    11b3 <relative2real+0x63>
test   %rbx,%rbx
je     11e3 <relative2real+0x93>
cmpb   $0x2f,(%r14)
jne    1244 <relative2real+0xf4>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%rdi
mov    %r14,%rsi
jmp    1298 <relative2real+0x148>
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <getcwd@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12a2 <relative2real+0x152>
mov    0x2df0(%rip),%r14
mov    (%r14),%edx
add    $0xffffffff,%edx
lea    0xdf3(%rip),%rsi
mov    %r12,%rdi
call   1060 <strncat@plt>
mov    (%r14),%r14d
mov    %r12,%rdi
call   1040 <strlen@plt>
sub    %eax,%r14d
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <strncat@plt>
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
call   1070 <realpath@plt>
test   %rax,%rax
jne    129d <relative2real+0x14d>
jmp    12a2 <relative2real+0x152>
movb   $0x0,(%rbx)
mov    %r14,%rdi
mov    -0x30(%rbp),%r15
mov    %r15,%rsi
call   1070 <realpath@plt>
test   %rax,%rax
je     12b3 <relative2real+0x163>
cmpb   $0x0,0x1(%rbx)
je     129d <relative2real+0x14d>
add    $0x1,%rbx
mov    0x2d84(%rip),%r14
mov    (%r14),%edx
add    $0xffffffff,%edx
lea    0xd87(%rip),%rsi
mov    %r15,%rdi
call   1060 <strncat@plt>
mov    (%r14),%r14d
mov    %r15,%rdi
call   1040 <strlen@plt>
sub    %eax,%r14d
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <strncat@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    12a2 <relative2real+0x152>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @relative2real(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %10
store i32 0, i32* %5
%18 = load i8*, i8** %3
%19 = load i32, i32* @PATH_MAX
%20 = call i32 @strncpy(i8* %17, i8* %18, i32 %19)
store i8* null, i8** %7
store i8* %17, i8** %6
br label %21
21: ; preds = %33, %2
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 47
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = load i8*, i8** %6
store i8* %32, i8** %7
br label %33
33: ; preds = %31, %26
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
br label %21
36: ; preds = %21
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %58
39: ; preds = %36
%40 = trunc i64 %12 to i32
%41 = call i32* @getcwd(i8* %14, i32 %40)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %44
43: ; preds = %39
br label %94
44: ; preds = %39
%45 = load i32, i32* @PATH_MAX
%46 = sub nsw i32 %45, 1
%47 = call i32 @strncat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = load i8*, i8** %3
%49 = load i32, i32* @PATH_MAX
%50 = call i32 @strlen(i8* %14)
%51 = sub nsw i32 %49, %50
%52 = call i32 @strncat(i8* %14, i8* %48, i32 %51)
%53 = load i8*, i8** %4
%54 = call i32* @realpath(i8* %14, i8* %53)
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %57
56: ; preds = %44
br label %94
57: ; preds = %44
br label %93
58: ; preds = %36
%59 = load i8, i8* %17
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 47
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = load i8*, i8** %4
%64 = load i32, i32* @PATH_MAX
%65 = call i32 @strncat(i8* %63, i8* %17, i32 %64)
br label %92
66: ; preds = %58
%67 = load i8*, i8** %7
store i8 0, i8* %67
%68 = load i8*, i8** %4
%69 = call i32* @realpath(i8* %17, i8* %68)
%70 = icmp eq i32* %69, null
br i1 %70, label %71, label %72
71: ; preds = %66
br label %94
72: ; preds = %66
%73 = load i8*, i8** %7
%74 = getelementptr inbounds i8, i8* %73, i64 1
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %91
78: ; preds = %72
%79 = load i8*, i8** %4
%80 = load i32, i32* @PATH_MAX
%81 = sub nsw i32 %80, 1
%82 = call i32 @strncat(i8* %79, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %81)
%83 = load i8*, i8** %4
%84 = load i8*, i8** %7
%85 = getelementptr inbounds i8, i8* %84, i64 1
%86 = load i32, i32* @PATH_MAX
%87 = load i8*, i8** %4
%88 = call i32 @strlen(i8* %87)
%89 = sub nsw i32 %86, %88
%90 = call i32 @strncat(i8* %83, i8* %85, i32 %89)
br label %91
91: ; preds = %78, %72
br label %92
92: ; preds = %91, %62
br label %93
93: ; preds = %92, %57
store i32 1, i32* %5
br label %94
94: ; preds = %93, %71, %56, %43
%95 = load i32, i32* %5
%96 = load i8*, i8** %8
ret i32 %95
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @relative2real(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = call i32 @strncpy(i8* %6, i8* %0, i32 %3)
br label %8
8: ; preds = %13, %2
%9 = phi i8* [ null, %2 ], [ %14, %13 ]
%10 = phi i8* [ %6, %2 ], [ %15, %13 ]
%11 = load i8, i8* %10
switch i8 %11, label %13 [
i8 0, label %16
i8 47, label %12
]
12: ; preds = %8
br label %13
13: ; preds = %8, %12
%14 = phi i8* [ %10, %12 ], [ %9, %8 ]
%15 = getelementptr inbounds i8, i8* %10, i64 1
br label %8
16: ; preds = %8
%17 = icmp eq i8* %9, null
br i1 %17, label %18, label %31
18: ; preds = %16
%19 = call i32* @getcwd(i8* %5, i32 %3)
%20 = icmp eq i32* %19, null
br i1 %20, label %53, label %21
21: ; preds = %18
%22 = load i32, i32* @PATH_MAX
%23 = add nsw i32 %22, -1
%24 = call i32 @strncat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @PATH_MAX
%26 = call i32 @strlen(i8* %5)
%27 = sub nsw i32 %25, %26
%28 = call i32 @strncat(i8* %5, i8* %0, i32 %27)
%29 = call i32* @realpath(i8* %5, i8* %1)
%30 = icmp eq i32* %29, null
br i1 %30, label %53, label %52
31: ; preds = %16
%32 = load i8, i8* %6
%33 = icmp eq i8 %32, 47
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @PATH_MAX
%36 = call i32 @strncat(i8* %1, i8* %6, i32 %35)
br label %52
37: ; preds = %31
store i8 0, i8* %9
%38 = call i32* @realpath(i8* %6, i8* %1)
%39 = icmp eq i32* %38, null
br i1 %39, label %53, label %40
40: ; preds = %37
%41 = getelementptr inbounds i8, i8* %9, i64 1
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 0
br i1 %43, label %52, label %44
44: ; preds = %40
%45 = load i32, i32* @PATH_MAX
%46 = add nsw i32 %45, -1
%47 = call i32 @strncat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = load i32, i32* @PATH_MAX
%49 = call i32 @strlen(i8* %1)
%50 = sub nsw i32 %48, %49
%51 = call i32 @strncat(i8* %1, i8* %41, i32 %50)
br label %52
52: ; preds = %34, %44, %40, %21
br label %53
53: ; preds = %37, %21, %18, %52
%54 = phi i32 [ 0, %18 ], [ 0, %21 ], [ 1, %52 ], [ 0, %37 ]
ret i32 %54
}