#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 */
struct stat {
  int /*<<< orphan*/ st_ino;
};

/* Variables and functions */
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ free(char *);
char *getcomponent(char *, char *);
char *ino2name(int /*<<< orphan*/, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
char *strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ xchdir(char *);
void reversepath(char *inpath, char *name, int len, char *outpath) {
  char *cp, *cp2;
  char buf[256];
  struct stat sb;
  cp = strcpy(outpath + 1024 - (len + 1), name);
  cp2 = inpath;
  while ((cp2 = getcomponent(cp2, buf)) != 0) {
    if (strcmp(buf, ".") == 0)
      continue;
    if (strcmp(buf, "..") == 0) {
      if (stat(".", &sb) < 0)
        fail("cannot stat current directory");
      name = ino2name(sb.st_ino, "..");
      len = strlen(name);
      cp -= len + 1;
      strcpy(cp, name);
      cp[len] = '/';
      free(name);
      xchdir("..");
    } else {
      cp -= 3;
      strncpy(cp, "../", 3);
      xchdir(buf);
    }
  }
  strcpy(outpath, cp);
}


x86 assembly (clang, -O0):
<reversepath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x400,%rdi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   10c0 <getcomponent@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1311 <reversepath+0x171>
lea    -0x130(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    122b <reversepath+0x8b>
jmp    11eb <reversepath+0x4b>
lea    -0x130(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    12df <reversepath+0x13f>
lea    0xdb2(%rip),%rdi
lea    -0x138(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jge    1271 <reversepath+0xd1>
lea    0xd97(%rip),%rdi
call   1080 <fail@plt>
mov    -0x138(%rbp),%edi
lea    0xd82(%rip),%rsi
call   1060 <ino2name@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xd2b(%rip),%rdi
call   1050 <xchdir@plt>
jmp    130c <reversepath+0x16c>
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffffd,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xd2b(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
lea    -0x130(%rbp),%rdi
call   1050 <xchdir@plt>
jmp    11eb <reversepath+0x4b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strcpy@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reversepath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rcx,%rbp
mov    %rdi,%rbx
not    %edx
movslq %edx,%rax
lea    (%rcx,%rax,1),%r15
add    $0x400,%r15
mov    %r15,%rdi
call   1070 <strcpy@plt>
lea    0x10(%rsp),%r12
lea    0xe26(%rip),%r13
lea    0xe1e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   10c0 <getcomponent@plt>
test   %rax,%rax
je     12be <reversepath+0x11e>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r13,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     11f0 <reversepath+0x50>
mov    %r12,%rdi
mov    %r14,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1249 <reversepath+0xa9>
add    $0xfffffffffffffffd,%r15
mov    %r15,%rdi
lea    0xdec(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %r12,%rdi
call   1050 <xchdir@plt>
jmp    11f0 <reversepath+0x50>
mov    %rbp,0x8(%rsp)
mov    %r13,%rdi
mov    %rsp,%rsi
call   10b0 <stat@plt>
test   %rax,%rax
jns    126a <reversepath+0xca>
lea    0xd9e(%rip),%rdi
call   1080 <fail@plt>
mov    (%rsp),%edi
mov    %r14,%rsi
call   1060 <ino2name@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1090 <strlen@plt>
mov    %eax,%r14d
not    %eax
cltq
add    %rax,%r15
mov    %r15,%rdi
mov    %rbp,%rsi
call   1070 <strcpy@plt>
movslq %r14d,%rax
lea    0xd61(%rip),%r14
movb   $0x2f,(%rax,%r15,1)
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <xchdir@plt>
mov    0x8(%rsp),%rbp
jmp    11f0 <reversepath+0x50>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <strcpy@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [30 x i8] c"cannot stat current directory\00"
@.str.3 = [4 x i8] c"../\00"
define void @reversepath(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [256 x i8]
%12 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%13 = load i8*, i8** %8
%14 = getelementptr inbounds i8, i8* %13, i64 1024
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = sub i64 0, %17
%19 = getelementptr inbounds i8, i8* %14, i64 %18
%20 = load i8*, i8** %6
%21 = call i8* @strcpy(i8* %19, i8* %20)
store i8* %21, i8** %9
%22 = load i8*, i8** %5
store i8* %22, i8** %10
br label %23
23: ; preds = %71, %32, %4
%24 = load i8*, i8** %10
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%26 = call i8* @getcomponent(i8* %24, i8* %25)
store i8* %26, i8** %10
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %72
28: ; preds = %23
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %23
33: ; preds = %28
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %64
37: ; preds = %33
%38 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @fail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i8* @ino2name(i32 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
%50 = load i8*, i8** %9
%51 = sext i32 %49 to i64
%52 = sub i64 0, %51
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8* %53, i8** %9
%54 = load i8*, i8** %9
%55 = load i8*, i8** %6
%56 = call i8* @strcpy(i8* %54, i8* %55)
%57 = load i8*, i8** %9
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 47, i8* %60
%61 = load i8*, i8** %6
%62 = call i32 @free(i8* %61)
%63 = call i32 @xchdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %71
64: ; preds = %33
%65 = load i8*, i8** %9
%66 = getelementptr inbounds i8, i8* %65, i64 -3
store i8* %66, i8** %9
%67 = load i8*, i8** %9
%68 = call i32 @strncpy(i8* %67, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%70 = call i32 @xchdir(i8* %69)
br label %71
71: ; preds = %64, %42
br label %23
72: ; preds = %23
%73 = load i8*, i8** %8
%74 = load i8*, i8** %9
%75 = call i8* @strcpy(i8* %73, i8* %74)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [30 x i8] c"cannot stat current directory\00"
@.str.3 = [4 x i8] c"../\00"
define void @reversepath(i8* %0, i8* nocapture %1, i32 %2, i8* %3) {
%5 = alloca [256 x i8]
%6 = alloca %struct.stat
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = bitcast %struct.stat* %6 to i8*
%9 = getelementptr inbounds i8, i8* %3, i64 1024
%10 = xor i32 %2, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = tail call i8* @strcpy(i8* %12, i8* dereferenceable(1) %1)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
br label %15
15: ; preds = %49, %4
%16 = phi i8* [ %12, %4 ], [ %50, %49 ]
%17 = phi i8* [ %0, %4 ], [ %20, %49 ]
br label %18
18: ; preds = %15, %22
%19 = phi i8* [ %20, %22 ], [ %17, %15 ]
%20 = call i8* @getcomponent(i8* %19, i8* %7)
%21 = icmp eq i8* %20, null
br i1 %21, label %51, label %22
22: ; preds = %18
%23 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %18, label %25
25: ; preds = %22
%26 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %45
28: ; preds = %25
%29 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @fail(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %14
%35 = call i8* @ino2name(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @strlen(i8* %35)
%37 = xor i32 %36, -1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %16, i64 %38
%40 = call i8* @strcpy(i8* dereferenceable(1) %39, i8* dereferenceable(1) %35)
%41 = sext i32 %36 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 47, i8* %42
%43 = call i32 @free(i8* %35)
%44 = call i32 @xchdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %49
45: ; preds = %25
%46 = getelementptr inbounds i8, i8* %16, i64 -3
%47 = call i32 @strncpy(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%48 = call i32 @xchdir(i8* %7)
br label %49
49: ; preds = %45, %33
%50 = phi i8* [ %39, %33 ], [ %46, %45 ]
br label %15
51: ; preds = %18
%52 = call i8* @strcpy(i8* dereferenceable(1) %3, i8* dereferenceable(1) %16)
ret void
}