#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 */
int /*<<< orphan*/ GETCWD(char *, int);
char const *_PATHSEP();
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strncmp(char const *, char *, int);
char *strrchr(char const *, char);
char *to_fullpath(const char *path, char *fullpath, int size) {
  const char *dp;
  char dc;
  const char *path1;
  char cwd[1024];
  GETCWD(fullpath, 1024);
  if ((dp = strrchr(path, '/')) || (dp = strrchr(path, '\\'))) {
    path1 = dp + 1;
    strcpy(cwd, fullpath);
    dc = *dp;
    *(char *)dp = 0;
    chdir(path);
    *(char *)dp = dc;
    GETCWD(fullpath, 1024);
    chdir(cwd);
  } else
    path1 = path;
  strcat(fullpath, _PATHSEP());
  if (strncmp(path1, "./", 2) == 0 || strncmp(path1, ".\\", 2) == 0)
    path1 += 2;
  strcat(fullpath, path1);
  return fullpath;
}


x86 assembly (clang, -O0):
<to_fullpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x400,%esi
call   1050 <GETCWD@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11cc <to_fullpath+0x5c>
mov    -0x8(%rbp),%rdi
mov    $0x5c,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1229 <to_fullpath+0xb9>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1060 <chdir@plt>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x400,%esi
call   1050 <GETCWD@plt>
lea    -0x430(%rbp),%rdi
call   1060 <chdir@plt>
jmp    1231 <to_fullpath+0xc1>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    $0x0,%al
call   1080 <_PATHSEP@plt>
mov    -0x438(%rbp),%rdi
mov    %rax,%rsi
call   1090 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1290 <to_fullpath+0x120>
mov    -0x30(%rbp),%rdi
lea    0xd87(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    129c <to_fullpath+0x12c>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_fullpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
mov    $0x400,%esi
call   1050 <GETCWD@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11be <to_fullpath+0x4e>
mov    %r15,%rdi
mov    $0x5c,%esi
call   1070 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f9 <to_fullpath+0x89>
lea    0x1(%rbx),%r13
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    (%rbx),%bpl
movb   $0x0,(%rbx)
mov    %r15,%rdi
call   1060 <chdir@plt>
mov    %bpl,(%rbx)
mov    %r14,%rdi
mov    $0x400,%esi
call   1050 <GETCWD@plt>
mov    %r12,%rdi
call   1060 <chdir@plt>
mov    %r13,%r15
xor    %eax,%eax
call   1080 <_PATHSEP@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1090 <strcat@plt>
lea    0xdee(%rip),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     123d <to_fullpath+0xcd>
lea    0xdd8(%rip),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1241 <to_fullpath+0xd1>
add    $0x2,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
mov    %r14,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [3 x i8] c".\\\00"
define i8* @to_fullpath(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8*
%10 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = call i32 @GETCWD(i8* %11, i32 1024)
%13 = load i8*, i8** %4
%14 = call i8* @strrchr(i8* %13, i8 signext 47)
store i8* %14, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = call i8* @strrchr(i8* %17, i8 signext 92)
store i8* %18, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %37
20: ; preds = %16, %3
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %9
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%24 = load i8*, i8** %5
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = load i8*, i8** %7
%27 = load i8, i8* %26
store i8 %27, i8* %8
%28 = load i8*, i8** %7
store i8 0, i8* %28
%29 = load i8*, i8** %4
%30 = call i32 @chdir(i8* %29)
%31 = load i8, i8* %8
%32 = load i8*, i8** %7
store i8 %31, i8* %32
%33 = load i8*, i8** %5
%34 = call i32 @GETCWD(i8* %33, i32 1024)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%36 = call i32 @chdir(i8* %35)
br label %39
37: ; preds = %16
%38 = load i8*, i8** %4
store i8* %38, i8** %9
br label %39
39: ; preds = %37, %20
%40 = load i8*, i8** %5
%41 = call i8* (...) @_PATHSEP()
%42 = call i32 @strcat(i8* %40, i8* %41)
%43 = load i8*, i8** %9
%44 = call i64 @strncmp(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%45 = icmp eq i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %39
%47 = load i8*, i8** %9
%48 = call i64 @strncmp(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46, %39
%51 = load i8*, i8** %9
%52 = getelementptr inbounds i8, i8* %51, i64 2
store i8* %52, i8** %9
br label %53
53: ; preds = %50, %46
%54 = load i8*, i8** %5
%55 = load i8*, i8** %9
%56 = call i32 @strcat(i8* %54, i8* %55)
%57 = load i8*, i8** %5
ret i8* %57
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [3 x i8] c".\\\00"
define i8* @to_fullpath(i8* %0, i8* returned %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @GETCWD(i8* %1, i32 1024)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 92)
%11 = icmp eq i8* %10, null
br i1 %11, label %20, label %12
12: ; preds = %9, %3
%13 = phi i8* [ %7, %3 ], [ %10, %9 ]
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = call i32 @strcpy(i8* %5, i8* %1)
%16 = load i8, i8* %13
store i8 0, i8* %13
%17 = call i32 @chdir(i8* %0)
store i8 %16, i8* %13
%18 = call i32 @GETCWD(i8* %1, i32 1024)
%19 = call i32 @chdir(i8* %5)
br label %20
20: ; preds = %9, %12
%21 = phi i8* [ %14, %12 ], [ %0, %9 ]
%22 = call i8* (...) @_PATHSEP()
%23 = call i32 @strcat(i8* %1, i8* %22)
%24 = call i64 @strncmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = call i64 @strncmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26, %20
%30 = getelementptr inbounds i8, i8* %21, i64 2
br label %31
31: ; preds = %29, %26
%32 = phi i8* [ %30, %29 ], [ %21, %26 ]
%33 = call i32 @strcat(i8* %1, i8* %32)
ret i8* %1
}