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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ getcwd(char *, int);
char *homedir();
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
char *strrchr(char *, char);
char *strtok(char *, char *);
char *xmalloc(int);
char *abspath(const char *path) {
  char buf[PATH_MAX];
  if (*path == '~') {
    strcpy(buf, homedir());
    strcat(buf, path + 1);
  } else if (*path != '/') {
    getcwd(buf, sizeof(buf));
    strcat(buf, "/");
    strcat(buf, path);
  } else {
    strcpy(buf, path);
  }
  char *result = xmalloc(strlen(buf) + 1);
  strcpy(result, "/");
  if (!strcmp(path, "/")) {
    return result;
  }
  char *p = result;
  char *prevslash = result;
  char *component = strtok(buf, "/");
  do {
    if (!strcmp(component, "") || !strcmp(component, ".")) {
      continue;
    }
    if (!strcmp(component, "..")) {
      p = prevslash;
      prevslash = result;
      if (p != result) {
        *p = '\0';
        prevslash = strrchr(result, '/');
      } else {
        p[1] = '\0';
      }
      continue;
    }
    prevslash = p;
    *p = '/';
    size_t len = strlen(component);
    memcpy(p + 1, component, len);
    p += len + 1;
    *p = '\0';
  } while ((component = strtok(NULL, "/")));
  return result;
}


x86 assembly (clang, -O0):
<abspath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1215 <abspath+0x75>
mov    $0x0,%al
call   10c0 <homedir@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   10b0 <strcat@plt>
jmp    1268 <abspath+0xc8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1256 <abspath+0xb6>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rax
mov    %eax,%esi
call   1060 <getcwd@plt>
mov    -0x58(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcat@plt>
jmp    1263 <abspath+0xc3>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1268 <abspath+0xc8>
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xd76(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xd66(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    12bc <abspath+0x11c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    13f0 <abspath+0x250>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    0xd29(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xd19(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
je     1312 <abspath+0x172>
mov    -0x48(%rbp),%rdi
lea    0xcff(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1317 <abspath+0x177>
jmp    13c3 <abspath+0x223>
mov    -0x48(%rbp),%rdi
lea    0xce0(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1379 <abspath+0x1d9>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     136c <abspath+0x1cc>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x40(%rbp)
jmp    1374 <abspath+0x1d4>
mov    -0x38(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    13c3 <abspath+0x223>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
lea    0xc32(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    12e0 <abspath+0x140>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<abspath>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r15
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    (%rdi),%al
cmp    $0x2f,%al
je     11f1 <abspath+0x51>
cmp    $0x7e,%al
jne    11fe <abspath+0x5e>
xor    %eax,%eax
call   10c0 <homedir@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
lea    0x1(%r12),%rsi
mov    %r15,%rdi
jmp    121b <abspath+0x7b>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
jmp    1220 <abspath+0x80>
mov    %r15,%rdi
call   1060 <getcwd@plt>
lea    0xdf3(%rip),%rsi
mov    %r15,%rdi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <xmalloc@plt>
lea    0xdc9(%rip),%rbx
mov    %rax,-0x30(%rbp)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     132d <abspath+0x18d>
lea    0xda0(%rip),%r13
mov    %r15,%rdi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r12
mov    -0x30(%rbp),%r15
mov    %r15,%rbx
jmp    12a2 <abspath+0x102>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%r14
mov    %r15,%rbx
mov    %rbx,%r15
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r12
mov    %r14,%rbx
test   %rax,%rax
je     132d <abspath+0x18d>
cmpb   $0x0,(%r12)
je     1280 <abspath+0xe0>
mov    %r12,%rdi
lea    0xd50(%rip),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1280 <abspath+0xe0>
mov    %r12,%rdi
lea    0xd3c(%rip),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     12fc <abspath+0x15c>
movb   $0x2f,(%rbx)
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%r15
lea    0x1(%rbx),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1090 <memcpy@plt>
lea    (%r15,%rbx,1),%r14
add    $0x1,%r14
movb   $0x0,0x1(%r15,%rbx,1)
jmp    1286 <abspath+0xe6>
mov    -0x30(%rbp),%rdi
cmp    %rdi,%r15
je     131e <abspath+0x17e>
movb   $0x0,(%r15)
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbx
mov    %r15,%r14
jmp    1286 <abspath+0xe6>
movb   $0x0,0x1(%rdi)
mov    %rdi,%r14
mov    %rdi,%rbx
jmp    1286 <abspath+0xe6>
mov    -0x30(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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