#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 */
char *calloc(int, int);
char *strchr(char *, char);
int strlen(char const *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char *, char);
char *flu_simplify_path(const char *s) {
  char *r = calloc(strlen(s) + 1, sizeof(char));
  char *rr = r;
  char *a = (char *)s;
  char *b = NULL;
  if (*s == '/') {
    *r = '/';
    rr = r + 1;
    a = (char *)s + 1;
  }
  while (1) {
    b = strchr(a, '/');
    size_t l = b ? b + 1 - a : strlen(a);
    size_t dots = 0;
    if (l == 2 && strncmp(a, "./", 2) == 0)
      dots = 1;
    else if (l == 1 && strncmp(a, "/", 1) == 0)
      dots = 1;
    else if (l == 1 && strncmp(a, ".\0", 2) == 0)
      dots = 1;
    else if (l >= 2 && a > s && strncmp(a, "..", 2) == 0)
      dots = 2;
    if (dots == 2 && rr > r + 1) {
      *(rr - 1) = 0;
      rr = strrchr(r, '/');
      rr = rr ? rr + 1 : r;
    } else if (dots < 1) {
      strncpy(rr, a, l);
      rr = rr + l;
    }
    *rr = 0;
    if (b == NULL)
      break;
    a = b + 1;
  }
  return r;
}


x86 assembly (clang, -O0):
<flu_simplify_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11cf <flu_simplify_path+0x6f>
mov    -0x10(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d4 <flu_simplify_path+0x74>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1209 <flu_simplify_path+0xa9>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    1218 <flu_simplify_path+0xb8>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x2,-0x30(%rbp)
jne    125f <flu_simplify_path+0xff>
mov    -0x20(%rbp),%rdi
lea    0xdc2(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    125f <flu_simplify_path+0xff>
movq   $0x1,-0x38(%rbp)
jmp    131c <flu_simplify_path+0x1bc>
cmpq   $0x1,-0x30(%rbp)
jne    1296 <flu_simplify_path+0x136>
mov    -0x20(%rbp),%rdi
lea    0xd8c(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1296 <flu_simplify_path+0x136>
movq   $0x1,-0x38(%rbp)
jmp    1317 <flu_simplify_path+0x1b7>
cmpq   $0x1,-0x30(%rbp)
jne    12cd <flu_simplify_path+0x16d>
mov    -0x20(%rbp),%rdi
lea    0xd5a(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12cd <flu_simplify_path+0x16d>
movq   $0x1,-0x38(%rbp)
jmp    1312 <flu_simplify_path+0x1b2>
cmpq   $0x2,-0x30(%rbp)
jb     130d <flu_simplify_path+0x1ad>
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    130d <flu_simplify_path+0x1ad>
mov    -0x20(%rbp),%rdi
lea    0xd12(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    130d <flu_simplify_path+0x1ad>
movq   $0x2,-0x38(%rbp)
jmp    1312 <flu_simplify_path+0x1b2>
jmp    1317 <flu_simplify_path+0x1b7>
jmp    131c <flu_simplify_path+0x1bc>
cmpq   $0x2,-0x38(%rbp)
jne    1387 <flu_simplify_path+0x227>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jbe    1387 <flu_simplify_path+0x227>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1372 <flu_simplify_path+0x212>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    137a <flu_simplify_path+0x21a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    13b4 <flu_simplify_path+0x254>
cmpq   $0x1,-0x38(%rbp)
jae    13af <flu_simplify_path+0x24f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    13b4 <flu_simplify_path+0x254>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    13cb <flu_simplify_path+0x26b>
jmp    13dc <flu_simplify_path+0x27c>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d4 <flu_simplify_path+0x74>
mov    -0x10(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flu_simplify_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r14
cmpb   $0x2f,(%r12)
mov    %r12,%r15
mov    %rax,%rbx
jne    119d <flu_simplify_path+0x3d>
movb   $0x2f,(%r14)
lea    0x1(%r14),%rbx
lea    0x1(%r12),%r15
lea    0x1(%r14),%rax
mov    %rax,(%rsp)
jmp    11d1 <flu_simplify_path+0x71>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
add    %r13,%rbx
lea    0x1(%rbp),%r15
movb   $0x0,(%rbx)
test   %rbp,%rbp
je     12b0 <flu_simplify_path+0x150>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1260 <flu_simplify_path+0x100>
mov    %rbp,%r13
sub    %r15,%r13
add    $0x1,%r13
cmp    $0x1,%r13
je     1271 <flu_simplify_path+0x111>
cmp    $0x2,%r13
jne    1215 <flu_simplify_path+0xb5>
mov    %r15,%rdi
lea    0xdfa(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11c1 <flu_simplify_path+0x61>
cmp    %r12,%r15
jbe    11b0 <flu_simplify_path+0x50>
cmp    $0x2,%r13
jb     11b0 <flu_simplify_path+0x50>
mov    %r15,%rdi
lea    0xdd9(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    12a2 <flu_simplify_path+0x142>
cmp    (%rsp),%rbx
jbe    12a2 <flu_simplify_path+0x142>
movb   $0x0,-0x1(%rbx)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r14,%rbx
jmp    11c1 <flu_simplify_path+0x61>
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%r13
cmp    $0x1,%r13
jne    11f6 <flu_simplify_path+0x96>
mov    %r15,%rdi
lea    0xd86(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11c1 <flu_simplify_path+0x61>
mov    %r15,%rdi
lea    0xd6e(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11b0 <flu_simplify_path+0x50>
jmp    11c1 <flu_simplify_path+0x61>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [3 x i8] c".\00\00"
@.str.3 = [3 x i8] c"..\00"
define i8* @flu_simplify_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = call i8* @calloc(i32 %11, i32 1)
store i8* %12, i8** %3
%13 = load i8*, i8** %3
store i8* %13, i8** %4
%14 = load i8*, i8** %2
store i8* %14, i8** %5
store i8* null, i8** %6
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 47
br i1 %18, label %19, label %25
19: ; preds = %1
%20 = load i8*, i8** %3
store i8 47, i8* %20
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %4
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8* %24, i8** %5
br label %25
25: ; preds = %19, %1
br label %26
26: ; preds = %25, %121
%27 = load i8*, i8** %5
%28 = call i8* @strchr(i8* %27, i8 signext 47)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %26
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8*, i8** %5
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
br label %42
38: ; preds = %26
%39 = load i8*, i8** %5
%40 = call i32 @strlen(i8* %39)
%41 = sext i32 %40 to i64
br label %42
42: ; preds = %38, %31
%43 = phi i64 [ %37, %31 ], [ %41, %38 ]
store i64 %43, i64* %7
store i64 0, i64* %8
%44 = load i64, i64* %7
%45 = icmp eq i64 %44, 2
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i8*, i8** %5
%48 = call i64 @strncmp(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i64 1, i64* %8
br label %82
51: ; preds = %46, %42
%52 = load i64, i64* %7
%53 = icmp eq i64 %52, 1
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i8*, i8** %5
%56 = call i64 @strncmp(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %54
store i64 1, i64* %8
br label %81
59: ; preds = %54, %51
%60 = load i64, i64* %7
%61 = icmp eq i64 %60, 1
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i8*, i8** %5
%64 = call i64 @strncmp(i8* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %62
store i64 1, i64* %8
br label %80
67: ; preds = %62, %59
%68 = load i64, i64* %7
%69 = icmp uge i64 %68, 2
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i8*, i8** %5
%72 = load i8*, i8** %2
%73 = icmp ugt i8* %71, %72
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i8*, i8** %5
%76 = call i64 @strncmp(i8* %75, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %74
store i64 2, i64* %8
br label %79
79: ; preds = %78, %74, %70, %67
br label %80
80: ; preds = %79, %66
br label %81
81: ; preds = %80, %58
br label %82
82: ; preds = %81, %50
%83 = load i64, i64* %8
%84 = icmp eq i64 %83, 2
br i1 %84, label %85, label %104
85: ; preds = %82
%86 = load i8*, i8** %4
%87 = load i8*, i8** %3
%88 = getelementptr inbounds i8, i8* %87, i64 1
%89 = icmp ugt i8* %86, %88
br i1 %89, label %90, label %104
90: ; preds = %85
%91 = load i8*, i8** %4
%92 = getelementptr inbounds i8, i8* %91, i64 -1
store i8 0, i8* %92
%93 = load i8*, i8** %3
%94 = call i8* @strrchr(i8* %93, i8 signext 47)
store i8* %94, i8** %4
%95 = load i8*, i8** %4
%96 = icmp ne i8* %95, null
br i1 %96, label %97, label %100
97: ; preds = %90
%98 = load i8*, i8** %4
%99 = getelementptr inbounds i8, i8* %98, i64 1
br label %102
100: ; preds = %90
%101 = load i8*, i8** %3
br label %102
102: ; preds = %100, %97
%103 = phi i8* [ %99, %97 ], [ %101, %100 ]
store i8* %103, i8** %4
br label %116
104: ; preds = %85, %82
%105 = load i64, i64* %8
%106 = icmp ult i64 %105, 1
br i1 %106, label %107, label %115
107: ; preds = %104
%108 = load i8*, i8** %4
%109 = load i8*, i8** %5
%110 = load i64, i64* %7
%111 = call i32 @strncpy(i8* %108, i8* %109, i64 %110)
%112 = load i8*, i8** %4
%113 = load i64, i64* %7
%114 = getelementptr inbounds i8, i8* %112, i64 %113
store i8* %114, i8** %4
br label %115
115: ; preds = %107, %104
br label %116
116: ; preds = %115, %102
%117 = load i8*, i8** %4
store i8 0, i8* %117
%118 = load i8*, i8** %6
%119 = icmp eq i8* %118, null
br i1 %119, label %120, label %121
120: ; preds = %116
br label %124
121: ; preds = %116
%122 = load i8*, i8** %6
%123 = getelementptr inbounds i8, i8* %122, i64 1
store i8* %123, i8** %5
br label %26
124: ; preds = %120
%125 = load i8*, i8** %3
ret i8* %125
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [3 x i8] c".\00\00"
@.str.3 = [3 x i8] c"..\00"
define i8* @flu_simplify_path(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @calloc(i32 %3, i32 1)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 47
br i1 %6, label %7, label %10
7: ; preds = %1
store i8 47, i8* %4
%8 = getelementptr inbounds i8, i8* %4, i64 1
%9 = getelementptr inbounds i8, i8* %0, i64 1
br label %10
10: ; preds = %7, %1
%11 = phi i8* [ %9, %7 ], [ %0, %1 ]
%12 = phi i8* [ %8, %7 ], [ %4, %1 ]
%13 = getelementptr inbounds i8, i8* %4, i64 1
br label %14
14: ; preds = %57, %10
%15 = phi i8* [ %11, %10 ], [ %59, %57 ]
%16 = phi i8* [ %12, %10 ], [ %58, %57 ]
%17 = tail call i8* @strchr(i8* dereferenceable(1) %15, i8 signext 47)
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %17, i64 1
%21 = ptrtoint i8* %20 to i64
%22 = ptrtoint i8* %15 to i64
%23 = sub i64 %21, %22
br label %27
24: ; preds = %14
%25 = tail call i32 @strlen(i8* %15)
%26 = sext i32 %25 to i64
br label %27
27: ; preds = %24, %19
%28 = phi i64 [ %23, %19 ], [ %26, %24 ]
switch i64 %28, label %38 [
i64 2, label %29
i64 1, label %32
]
29: ; preds = %27
%30 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %57, label %38
32: ; preds = %27
%33 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%34 = icmp eq i64 %33, 0
br i1 %34, label %57, label %35
35: ; preds = %32
%36 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%37 = icmp eq i64 %36, 0
br i1 %37, label %57, label %54
38: ; preds = %27, %29
%39 = icmp ugt i64 %28, 1
%40 = icmp ugt i8* %15, %0
%41 = and i1 %40, %39
br i1 %41, label %42, label %54
42: ; preds = %38
%43 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%44 = icmp eq i64 %43, 0
%45 = icmp ugt i8* %16, %13
%46 = select i1 %44, i1 %45, i1 false
br i1 %46, label %47, label %53
47: ; preds = %42
%48 = getelementptr inbounds i8, i8* %16, i64 -1
store i8 0, i8* %48
%49 = tail call i8* @strrchr(i8* dereferenceable(1) %4, i8 signext 47)
%50 = icmp eq i8* %49, null
%51 = getelementptr inbounds i8, i8* %49, i64 1
%52 = select i1 %50, i8* %4, i8* %51
br label %57
53: ; preds = %42
br i1 %44, label %57, label %54
54: ; preds = %35, %38, %53
%55 = tail call i32 @strncpy(i8* %16, i8* %15, i64 %28)
%56 = getelementptr inbounds i8, i8* %16, i64 %28
br label %57
57: ; preds = %29, %32, %35, %53, %54, %47
%58 = phi i8* [ %52, %47 ], [ %56, %54 ], [ %16, %53 ], [ %16, %35 ], [ %16, %32 ], [ %16, %29 ]
store i8 0, i8* %58
%59 = getelementptr inbounds i8, i8* %17, i64 1
br i1 %18, label %60, label %14
60: ; preds = %57
ret i8* %4
}