#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*/ free(char *);
char *get_dir_path(char *);
char *get_filename(char *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *pack_path(char *path) {
  if (strcmp(path, "/") == 0) {
    char *packed = malloc(2);
    strcpy(packed, "/");
    return packed;
  }
  char *name = get_filename(path);
  char *dir_path = get_dir_path(path);
  char *packed_dir = pack_path(dir_path);
  free(dir_path);
  if (strcmp(name, ".") == 0) {
    return packed_dir;
  }
  if (strcmp(name, "..") == 0) {
    char *dir_path = get_dir_path(packed_dir);
    free(packed_dir);
    return dir_path;
  }
  char *packed = malloc(strlen(path));
  strcpy(packed, "");
  strcat(packed, packed_dir);
  if (strcmp(packed_dir, "/") != 0)
    strcat(packed, "/");
  strcat(packed, name);
  free(packed_dir);
  return packed;
}


x86 assembly (clang, -O0):
<pack_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11e1 <pack_path+0x51>
mov    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ee <pack_path+0x15e>
mov    -0x10(%rbp),%rdi
call   1050 <get_filename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <get_dir_path@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <pack_path@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1238 <pack_path+0xa8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ee <pack_path+0x15e>
mov    -0x20(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1275 <pack_path+0xe5>
mov    -0x30(%rbp),%rdi
call   1080 <get_dir_path@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ee <pack_path+0x15e>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xd70(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xd4f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12d0 <pack_path+0x140>
mov    -0x40(%rbp),%rdi
lea    0xd35(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
lea    0xe5e(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1261 <pack_path+0xd1>
mov    %r12,%rdi
call   1050 <get_filename@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1080 <get_dir_path@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <pack_path@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe23(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     127f <pack_path+0xef>
lea    0xe0a(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1284 <pack_path+0xf4>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
lea    0xde6(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
lea    0xdc8(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1254 <pack_path+0xc4>
lea    0xdb4(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <strcat@plt>
jmp    128f <pack_path+0xff>
mov    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
lea    0xd8b(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
jmp    1297 <pack_path+0x107>
mov    %r14,%rbx
jmp    1297 <pack_path+0x107>
mov    %r14,%rdi
call   1080 <get_dir_path@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @pack_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = call i8* @malloc(i32 2)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %4
store i8* %17, i8** %2
br label %64
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i8* @get_filename(i8* %19)
store i8* %20, i8** %5
%21 = load i8*, i8** %3
%22 = call i8* @get_dir_path(i8* %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = call i8* @pack_path(i8* %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %5
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = load i8*, i8** %7
store i8* %31, i8** %2
br label %64
32: ; preds = %18
%33 = load i8*, i8** %5
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i8*, i8** %7
%38 = call i8* @get_dir_path(i8* %37)
store i8* %38, i8** %8
%39 = load i8*, i8** %7
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %8
store i8* %41, i8** %2
br label %64
42: ; preds = %32
%43 = load i8*, i8** %3
%44 = call i32 @strlen(i8* %43)
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = call i32 @strcpy(i8* %46, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
%48 = load i8*, i8** %9
%49 = load i8*, i8** %7
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i8*, i8** %7
%52 = call i64 @strcmp(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %42
%55 = load i8*, i8** %9
%56 = call i32 @strcat(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %57
57: ; preds = %54, %42
%58 = load i8*, i8** %9
%59 = load i8*, i8** %5
%60 = call i32 @strcat(i8* %58, i8* %59)
%61 = load i8*, i8** %7
%62 = call i32 @free(i8* %61)
%63 = load i8*, i8** %9
store i8* %63, i8** %2
br label %64
64: ; preds = %57, %36, %30, %13
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @pack_path(i8* %0) {
%2 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%6 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %32
7: ; preds = %1
%8 = tail call i8* @get_filename(i8* %0)
%9 = tail call i8* @get_dir_path(i8* %0)
%10 = tail call i8* @pack_path(i8* %9)
%11 = tail call i32 @free(i8* %9)
%12 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %32, label %14
14: ; preds = %7
%15 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i8* @get_dir_path(i8* %10)
%19 = tail call i32 @free(i8* %10)
br label %32
20: ; preds = %14
%21 = tail call i32 @strlen(i8* %0)
%22 = tail call i8* @malloc(i32 %21)
%23 = tail call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @strcat(i8* %22, i8* %10)
%25 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %20
%28 = tail call i32 @strcat(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
%30 = tail call i32 @strcat(i8* %22, i8* %8)
%31 = tail call i32 @free(i8* %10)
br label %32
32: ; preds = %17, %29, %7, %4
%33 = phi i8* [ %5, %4 ], [ %18, %17 ], [ %22, %29 ], [ %10, %7 ]
ret i8* %33
}