#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 */
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, 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 == '/')
    strcpy(str, str + 1);
  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     11e1 <sanitize+0xb1>
mov    -0x10(%rbp),%rdi
lea    0xea0(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1185 <sanitize+0x55>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
jmp    11dc <sanitize+0xac>
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11b5 <sanitize+0x85>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11d7 <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    11dc <sanitize+0xac>
jmp    1148 <sanitize+0x18>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1209 <sanitize+0xd9>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x1,%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     124d <sanitize+0x11d>
mov    -0x8(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     124d <sanitize+0x11d>
mov    -0x8(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    125d <sanitize+0x12d>
mov    -0x8(%rbp),%rdi
lea    0xdb3(%rip),%rsi
call   1040 <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     11aa <sanitize+0x7a>
lea    0xeb5(%rip),%r12
lea    0xeb3(%rip),%r15
mov    %r14,%r13
mov    %r14,%rbx
jmp    116f <sanitize+0x3f>
nopw   0x0(%rax,%rax,1)
add    $0x3,%rbx
mov    %rbx,%rcx
cmpb   $0x0,(%rcx)
mov    %rcx,%rbx
je     11aa <sanitize+0x7a>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1160 <sanitize+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
lea    0x1(%rbx),%rcx
test   %rax,%rax
je     1167 <sanitize+0x37>
movzbl (%rbx),%eax
mov    %al,0x0(%r13)
add    $0x1,%r13
jmp    1167 <sanitize+0x37>
movb   $0x0,0x0(%r13)
mov    (%r14),%al
cmp    $0x2f,%al
jne    11c5 <sanitize+0x95>
lea    0x1(%r14),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    (%r14),%al
test   %al,%al
je     11fb <sanitize+0xcb>
lea    0xe32(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11fb <sanitize+0xcb>
lea    0xe24(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11fb <sanitize+0xcb>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe09(%rip),%rsi
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <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*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %4
store i8* %5, i8** %3
br label %6
6: ; preds = %31, %1
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = icmp ne i8 %8, 0
br i1 %9, label %10, label %32
10: ; preds = %6
%11 = load i8*, i8** %3
%12 = call i64 @strncmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 3
store i8* %16, i8** %3
br label %31
17: ; preds = %10
%18 = load i8*, i8** %3
%19 = call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %3
br label %30
24: ; preds = %17
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %3
%27 = load i8, i8* %25
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %4
store i8 %27, i8* %28
br label %30
30: ; preds = %24, %21
br label %31
31: ; preds = %30, %14
br label %6
32: ; preds = %6
%33 = load i8*, i8** %4
store i8 0, i8* %33
%34 = load i8*, i8** %2
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 47
br i1 %37, label %38, label %43
38: ; preds = %32
%39 = load i8*, i8** %2
%40 = load i8*, i8** %2
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = call i32 @strcpy(i8* %39, i8* %41)
br label %43
43: ; preds = %38, %32
%44 = load i8*, i8** %2
%45 = getelementptr inbounds i8, i8* %44, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 0
br i1 %48, label %57, label %49
49: ; preds = %43
%50 = load i8*, i8** %2
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %49
%54 = load i8*, i8** %2
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %53, %49, %43
%58 = load i8*, i8** %2
%59 = call i32 @strcpy(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %57, %53
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* %6, 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* %6, i64 3
br label %18
11: ; preds = %4
%12 = tail call i64 @strncmp(i8* %6, 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* %6, i64 1
br i1 %13, label %18, label %15
15: ; preds = %11
%16 = load i8, i8* %6
%17 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %16, i8* %5
br label %18
18: ; preds = %11, %15, %9
%19 = phi i8* [ %10, %9 ], [ %14, %15 ], [ %14, %11 ]
%20 = phi i8* [ %5, %9 ], [ %17, %15 ], [ %5, %11 ]
%21 = load i8, i8* %19
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %4
23: ; preds = %18, %1
%24 = phi i8* [ %0, %1 ], [ %20, %18 ]
store i8 0, i8* %24
%25 = load i8, i8* %0
%26 = icmp eq i8 %25, 47
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = getelementptr inbounds i8, i8* %0, i64 1
%29 = tail call i32 @strcpy(i8* %0, i8* %28)
%30 = load i8, i8* %0
br label %31
31: ; preds = %27, %23
%32 = phi i8 [ %30, %27 ], [ %25, %23 ]
%33 = icmp eq i8 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %31
%35 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37, %34, %31
%41 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
ret void
}