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

/* Variables and functions */
int ENOBUFS;
scalar_t__ ERANGE;
int PATH_MAX;
int /*<<< orphan*/ dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ *getcwd(char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int dot_to_absolute(char *rel_path, int rel_path_size) {
  char tmp_path[PATH_MAX];
  if (strcmp(rel_path, ".") == 0) {
    if (getcwd(rel_path, rel_path_size) == NULL) {
      if (errno == ERANGE)
        return ENOBUFS;
      else
        return errno;
    }
  } else if (strcmp(rel_path, "..") == 0) {
    if (getcwd(tmp_path, sizeof(tmp_path)) == NULL) {
      if (errno == ERANGE)
        return ENOBUFS;
      else
        return errno;
    }
    strncpy(rel_path, dirname(tmp_path), rel_path_size);
    if (rel_path[rel_path_size - 1] != 0)
      return ENOBUFS;
  }
  return 0;
}


x86 assembly (clang, -O0):
<dot_to_absolute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%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,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11ff <dot_to_absolute+0xbf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    11fa <dot_to_absolute+0xba>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11e1 <dot_to_absolute+0xa1>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e6 <dot_to_absolute+0x1a6>
mov    0x2de0(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e6 <dot_to_absolute+0x1a6>
jmp    12d8 <dot_to_absolute+0x198>
mov    -0x10(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12d3 <dot_to_absolute+0x193>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    127d <dot_to_absolute+0x13d>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
jne    1264 <dot_to_absolute+0x124>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e6 <dot_to_absolute+0x1a6>
mov    0x2d5d(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e6 <dot_to_absolute+0x1a6>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
call   1040 <dirname@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     12ce <dot_to_absolute+0x18e>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12e6 <dot_to_absolute+0x1a6>
jmp    12d3 <dot_to_absolute+0x193>
jmp    12d8 <dot_to_absolute+0x198>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dot_to_absolute>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e90(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe8d(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11cb <dot_to_absolute+0x8b>
lea    0xe7b(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11ff <dot_to_absolute+0xbf>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11e0 <dot_to_absolute+0xa0>
mov    %rbx,%rdi
call   1040 <dirname@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq %r14d,%rcx
xor    %eax,%eax
cmpb   $0x0,-0x1(%rcx,%r15,1)
jne    11f6 <dot_to_absolute+0xb6>
jmp    11ff <dot_to_absolute+0xbf>
mov    %r15,%rdi
mov    %r14d,%esi
call   1050 <getcwd@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11ff <dot_to_absolute+0xbf>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    11ff <dot_to_absolute+0xbf>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@errno = global i64 0
@ERANGE = global i64 0
@ENOBUFS = global i32 0
@.str.1 = [3 x i8] c"..\00"
define i32 @dot_to_absolute(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i8*, i8** %4
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = call i32* @getcwd(i8* %17, i32 %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %30
21: ; preds = %16
%22 = load i64, i64* @errno
%23 = load i64, i64* @ERANGE
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* @ENOBUFS
store i32 %26, i32* %3
store i32 1, i32* %8
br label %66
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = trunc i64 %28 to i32
store i32 %29, i32* %3
store i32 1, i32* %8
br label %66
30: ; preds = %16
br label %65
31: ; preds = %2
%32 = load i8*, i8** %4
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %64
35: ; preds = %31
%36 = trunc i64 %10 to i32
%37 = call i32* @getcwd(i8* %12, i32 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = load i64, i64* @ERANGE
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = load i32, i32* @ENOBUFS
store i32 %44, i32* %3
store i32 1, i32* %8
br label %66
45: ; preds = %39
%46 = load i64, i64* @errno
%47 = trunc i64 %46 to i32
store i32 %47, i32* %3
store i32 1, i32* %8
br label %66
48: ; preds = %35
%49 = load i8*, i8** %4
%50 = call i32 @dirname(i8* %12)
%51 = load i32, i32* %5
%52 = call i32 @strncpy(i8* %49, i32 %50, i32 %51)
%53 = load i8*, i8** %4
%54 = load i32, i32* %5
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %48
%62 = load i32, i32* @ENOBUFS
store i32 %62, i32* %3
store i32 1, i32* %8
br label %66
63: ; preds = %48
br label %64
64: ; preds = %63, %31
br label %65
65: ; preds = %64, %30
store i32 0, i32* %3
store i32 1, i32* %8
br label %66
66: ; preds = %65, %61, %45, %43, %27, %25
%67 = load i8*, i8** %6
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@errno = global i64 0
@ERANGE = global i64 0
@ENOBUFS = global i32 0
@.str.1 = [3 x i8] c"..\00"
define i32 @dot_to_absolute(i8* %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %2
%9 = tail call i32* @getcwd(i8* %0, i32 %1)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %43
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* @ENOBUFS
br label %43
17: ; preds = %11
%18 = trunc i64 %12 to i32
br label %43
19: ; preds = %2
%20 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %43
22: ; preds = %19
%23 = call i32* @getcwd(i8* %5, i32 %3)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @ERANGE
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* @ENOBUFS
br label %43
31: ; preds = %25
%32 = trunc i64 %26 to i32
br label %43
33: ; preds = %22
%34 = call i32 @dirname(i8* %5)
%35 = call i32 @strncpy(i8* %0, i32 %34, i32 %1)
%36 = add nsw i32 %1, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %0, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 0
%41 = load i32, i32* @ENOBUFS
%42 = select i1 %40, i32 0, i32 %41
br label %43
43: ; preds = %33, %8, %19, %31, %29, %17, %15
%44 = phi i32 [ %16, %15 ], [ %18, %17 ], [ %30, %29 ], [ %32, %31 ], [ 0, %19 ], [ 0, %8 ], [ %42, %33 ]
ret i32 %44
}