#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 *malloc(int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
void sanitize(char *str) {
  char *src, *dest;
  src = dest = str;
  while (*src) {
    if (strncmp(src, "/../", 4) == 0) {
      src += 3;
    } else if (strncmp(src, "//", 2) == 0) {
      src++;
    } else {
      *dest++ = *src++;
    }
  }
  *dest = '\0';
  if (*str == '/') {
    char *temp = malloc(sizeof(char) * strlen(str));
    strcpy(temp, str + 1);
    strcpy(str, temp);
    free(temp);
  }
  if (str[0] == '\0' || strcmp(str, "./") == 0 || strcmp(str, "./..") == 0) {
    strcpy(str, ".");
  }
}


x86 assembly (clang, -O0):
<sanitize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1211 <sanitize+0xb1>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11b5 <sanitize+0x55>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
jmp    120c <sanitize+0xac>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11e5 <sanitize+0x85>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1207 <sanitize+0xa7>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    120c <sanitize+0xac>
jmp    1178 <sanitize+0x18>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1269 <sanitize+0x109>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x1,%rsi
call   1050 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ad <sanitize+0x14d>
mov    -0x8(%rbp),%rdi
lea    0xd7e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12ad <sanitize+0x14d>
mov    -0x8(%rbp),%rdi
lea    0xd6a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12bd <sanitize+0x15d>
mov    -0x8(%rbp),%rdi
lea    0xd53(%rip),%rsi
call   1050 <strcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sanitize>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
mov    %rdi,%r13
je     11da <sanitize+0x7a>
lea    0xe85(%rip),%r12
lea    0xe83(%rip),%r15
mov    %r14,%rbx
mov    %r14,%r13
jmp    119f <sanitize+0x3f>
nopw   0x0(%rax,%rax,1)
add    $0x3,%rbx
mov    %rbx,%rcx
cmpb   $0x0,(%rcx)
mov    %rcx,%rbx
je     11da <sanitize+0x7a>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1190 <sanitize+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
lea    0x1(%rbx),%rcx
test   %rax,%rax
je     1197 <sanitize+0x37>
movzbl (%rbx),%eax
mov    %al,0x0(%r13)
add    $0x1,%r13
jmp    1197 <sanitize+0x37>
movb   $0x0,0x0(%r13)
mov    (%r14),%al
cmp    $0x2f,%al
jne    121a <sanitize+0xba>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0x1(%r14),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%r14),%al
test   %al,%al
je     1250 <sanitize+0xf0>
lea    0xddd(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1250 <sanitize+0xf0>
lea    0xdcf(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1250 <sanitize+0xf0>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdb4(%rip),%rsi
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <strcpy@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"/../\00"
@.str.1 = [3 x i8] c"//\00"
@.str.2 = [3 x i8] c"./\00"
@.str.3 = [5 x i8] c"./..\00"
@.str.4 = [2 x i8] c".\00"
define void @sanitize(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %23, label %4
4: ; preds = %1, %18
%5 = phi i8* [ %20, %18 ], [ %0, %1 ]
%6 = phi i8* [ %19, %18 ], [ %0, %1 ]
%7 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %5, i64 3
br label %18
11: ; preds = %4
%12 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%13 = icmp eq i64 %12, 0
%14 = getelementptr inbounds i8, i8* %5, i64 1
br i1 %13, label %18, label %15
15: ; preds = %11
%16 = load i8, i8* %5
%17 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %16, i8* %6
br label %18
18: ; preds = %11, %15, %9
%19 = phi i8* [ %6, %9 ], [ %17, %15 ], [ %6, %11 ]
%20 = phi i8* [ %10, %9 ], [ %14, %15 ], [ %14, %11 ]
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %4
23: ; preds = %18, %1
%24 = phi i8* [ %0, %1 ], [ %19, %18 ]
store i8 0, i8* %24
%25 = load i8, i8* %0
%26 = icmp eq i8 %25, 47
br i1 %26, label %27, label %35
27: ; preds = %23
%28 = tail call i32 @strlen(i8* %0)
%29 = tail call i8* @malloc(i32 %28)
%30 = getelementptr inbounds i8, i8* %0, i64 1
%31 = tail call i32 @strcpy(i8* %29, i8* %30)
%32 = tail call i32 @strcpy(i8* %0, i8* %29)
%33 = tail call i32 @free(i8* %29)
%34 = load i8, i8* %0
br label %35
35: ; preds = %27, %23
%36 = phi i8 [ %34, %27 ], [ %25, %23 ]
%37 = icmp eq i8 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %35
%39 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41, %38, %35
%45 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
ret void
}