#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 *path_base(char *);
char *path_str(char *, char *);
char *realpath(char *, int /*<<< orphan*/ *);
size_t strlen(char *);
char *clean_path(char *path) {
  size_t len = strlen(path);
  if (path[len - 1] == '/')
    path[len - 1] = '\0';
  char *base = path_base(path);
  char *dir;
  if (base == path) {
    dir = ".";
  } else {
    dir = path;
    *(base - 1) = '\0';
  }
  dir = realpath(dir, NULL);
  char *result = path_str(dir, base);
  free(dir);
  return result;
}


x86 assembly (clang, -O0):
<clean_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1192 <clean_path+0x42>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1060 <path_base@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11bd <clean_path+0x6d>
lea    0xe4c(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11cd <clean_path+0x7d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <realpath@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <path_str@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clean_path>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    1168 <clean_path+0x18>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rdi
call   1060 <path_base@plt>
mov    %rax,%r14
cmp    %rbx,%rax
je     117f <clean_path+0x2f>
movb   $0x0,-0x1(%r14)
jmp    1186 <clean_path+0x36>
lea    0xe7a(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <realpath@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <path_str@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i8* @clean_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %3
%9 = load i8*, i8** %2
%10 = load i64, i64* %3
%11 = sub i64 %10, 1
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 47
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = load i64, i64* %3
%19 = sub i64 %18, 1
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %16, %1
%22 = load i8*, i8** %2
%23 = call i8* @path_base(i8* %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i8*, i8** %2
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %28
27: ; preds = %21
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5
br label %32
28: ; preds = %21
%29 = load i8*, i8** %2
store i8* %29, i8** %5
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 -1
store i8 0, i8* %31
br label %32
32: ; preds = %28, %27
%33 = load i8*, i8** %5
%34 = call i8* @realpath(i8* %33, i32* null)
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = load i8*, i8** %4
%37 = call i8* @path_str(i8* %35, i8* %36)
store i8* %37, i8** %6
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %6
ret i8* %40
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @clean_path(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, -1
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 47
br i1 %6, label %7, label %8
7: ; preds = %1
store i8 0, i8* %4
br label %8
8: ; preds = %7, %1
%9 = tail call i8* @path_base(i8* %0)
%10 = icmp eq i8* %9, %0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %9, i64 -1
store i8 0, i8* %12
br label %13
13: ; preds = %8, %11
%14 = phi i8* [ %0, %11 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %8 ]
%15 = tail call i8* @realpath(i8* %14, i32* null)
%16 = tail call i8* @path_str(i8* %15, i8* %9)
%17 = tail call i32 @free(i8* %15)
ret i8* %16
}