#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 PATH_MAX;
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
int /*<<< orphan*/ remove_dot_dot(char *);
int /*<<< orphan*/ remove_path_string(char *, char *);
char *strdup(char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *resolve_path(const char *p) {
  char path[PATH_MAX];
  char *path_ptr;
  size_t path_len;
  if (p == NULL || p[0] == 0) {
    return NULL;
  }
  if (p[0] != '/') {
    char *cwd = get_current_dir_name();
    size_t p_pos = strlen(cwd);
    path_ptr = path;
    strncpy(path, cwd, PATH_MAX);
    path_ptr += p_pos;
    strncpy(path_ptr, "/", PATH_MAX - p_pos);
    p_pos += 1;
    path_ptr += 1;
    strncpy(path_ptr, p, PATH_MAX - p_pos);
    free(cwd);
  } else {
    strncpy(path, p, PATH_MAX);
  }
  path_len = strlen(path);
  remove_path_string(path, "/./");
  remove_path_string(path, "//");
  remove_dot_dot(path);
  return strdup(path);
}


x86 assembly (clang, -O0):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c3 <resolve_path+0x53>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11d7 <resolve_path+0x67>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12e8 <resolve_path+0x178>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1288 <resolve_path+0x118>
mov    $0x0,%al
call   1080 <get_current_dir_name@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x48(%rbp)
mov    %rdi,-0x28(%rbp)
mov    -0x40(%rbp),%rsi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dc0(%rip),%rax
movslq (%rax),%rax
sub    -0x48(%rbp),%rax
mov    %eax,%edx
lea    0xdc2(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d84(%rip),%rax
movslq (%rax),%rax
sub    -0x48(%rbp),%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    129e <resolve_path+0x12e>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,-0x30(%rbp)
lea    0xd4a(%rip),%rsi
call   1070 <remove_path_string@plt>
mov    -0x50(%rbp),%rdi
lea    0xd3e(%rip),%rsi
call   1070 <remove_path_string@plt>
mov    -0x50(%rbp),%rdi
call   1060 <remove_dot_dot@plt>
mov    -0x50(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6b(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     11b9 <resolve_path+0x49>
mov    %rdi,%r14
mov    (%rdi),%al
test   %al,%al
je     11b9 <resolve_path+0x49>
cmp    $0x2f,%al
jne    11c0 <resolve_path+0x50>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strncpy@plt>
jmp    121d <resolve_path+0xad>
xor    %eax,%eax
jmp    124b <resolve_path+0xdb>
xor    %eax,%eax
call   1080 <get_current_dir_name@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strncpy@plt>
lea    (%rbx,%r12,1),%rdi
mov    0x0(%r13),%edx
sub    %r12d,%edx
lea    0xe0c(%rip),%rsi
call   1040 <strncpy@plt>
lea    (%rbx,%r12,1),%rdi
add    $0x1,%rdi
not    %r12d
add    0x0(%r13),%r12d
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xddc(%rip),%rsi
mov    %rbx,%rdi
call   1070 <remove_path_string@plt>
lea    0xdd1(%rip),%rsi
mov    %rbx,%rdi
call   1070 <remove_path_string@plt>
mov    %rbx,%rdi
call   1060 <remove_dot_dot@plt>
mov    %rbx,%rdi
call   1090 <strdup@plt>
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 = [4 x i8] c"/./\00"
@.str.2 = [3 x i8] c"//\00"
define i8* @resolve_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
%15 = load i8*, i8** %3
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17, %1
store i8* null, i8** %2
store i32 1, i32* %8
br label %71
24: ; preds = %17
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 47
br i1 %29, label %30, label %61
30: ; preds = %24
%31 = call i8* (...) @get_current_dir_name()
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = call i64 @strlen(i8* %32)
store i64 %33, i64* %10
store i8* %14, i8** %6
%34 = load i8*, i8** %9
%35 = load i32, i32* @PATH_MAX
%36 = call i32 @strncpy(i8* %14, i8* %34, i32 %35)
%37 = load i64, i64* %10
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 %37
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = load i32, i32* @PATH_MAX
%42 = sext i32 %41 to i64
%43 = load i64, i64* %10
%44 = sub i64 %42, %43
%45 = trunc i64 %44 to i32
%46 = call i32 @strncpy(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = load i64, i64* %10
%48 = add i64 %47, 1
store i64 %48, i64* %10
%49 = load i8*, i8** %6
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = load i8*, i8** %3
%53 = load i32, i32* @PATH_MAX
%54 = sext i32 %53 to i64
%55 = load i64, i64* %10
%56 = sub i64 %54, %55
%57 = trunc i64 %56 to i32
%58 = call i32 @strncpy(i8* %51, i8* %52, i32 %57)
%59 = load i8*, i8** %9
%60 = call i32 @free(i8* %59)
br label %65
61: ; preds = %24
%62 = load i8*, i8** %3
%63 = load i32, i32* @PATH_MAX
%64 = call i32 @strncpy(i8* %14, i8* %62, i32 %63)
br label %65
65: ; preds = %61, %30
%66 = call i64 @strlen(i8* %14)
store i64 %66, i64* %7
%67 = call i32 @remove_path_string(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%68 = call i32 @remove_path_string(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%69 = call i32 @remove_dot_dot(i8* %14)
%70 = call i8* @strdup(i8* %14)
store i8* %70, i8** %2
store i32 1, i32* %8
br label %71
71: ; preds = %65, %23
%72 = load i8*, i8** %4
%73 = load i8*, i8** %2
ret i8* %73
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"/./\00"
@.str.2 = [3 x i8] c"//\00"
define noalias i8* @resolve_path(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = icmp eq i8* %0, null
br i1 %5, label %31, label %6
6: ; preds = %1
%7 = load i8, i8* %0
switch i8 %7, label %8 [
i8 0, label %31
i8 47, label %24
]
8: ; preds = %6
%9 = tail call i8* (...) @get_current_dir_name()
%10 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%11 = load i32, i32* @PATH_MAX
%12 = call i32 @strncpy(i8* %4, i8* %9, i32 %11)
%13 = getelementptr inbounds i8, i8* %4, i64 %10
%14 = load i32, i32* @PATH_MAX
%15 = trunc i64 %10 to i32
%16 = sub i32 %14, %15
%17 = call i32 @strncpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds i8, i8* %13, i64 1
%19 = load i32, i32* @PATH_MAX
%20 = xor i32 %15, -1
%21 = add i32 %19, %20
%22 = call i32 @strncpy(i8* %18, i8* %0, i32 %21)
%23 = call i32 @free(i8* %9)
br label %26
24: ; preds = %6
%25 = call i32 @strncpy(i8* %4, i8* %0, i32 %2)
br label %26
26: ; preds = %24, %8
%27 = call i32 @remove_path_string(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @remove_path_string(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @remove_dot_dot(i8* %4)
%30 = call i8* @strdup(i8* %4)
br label %31
31: ; preds = %6, %1, %26
%32 = phi i8* [ %30, %26 ], [ null, %6 ], [ null, %1 ]
ret i8* %32
}