#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__ MAXPATHLEN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int remove_double_slashes_and_single_dots(char *);
int resolve_tail_double_dots(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int remove_all_dots_and_double_slashes(char const *nm, char *resolved_path) {
  int res = -1;
  char *orig;
  if (!nm || nm[0] == 0 || strlen(nm) >= MAXPATHLEN) {
    fprintf(stderr, "Wrong file name parameter\n");
    return res;
  }
  orig = malloc(MAXPATHLEN);
  if (!orig) {
    fprintf(stderr, "Failed to allocate memory\n");
    return res;
  }
  strcpy(orig, nm);
  strcpy(resolved_path, "<null>");
  res = remove_double_slashes_and_single_dots(orig);
  if (res == 0)
    res = resolve_tail_double_dots(orig, resolved_path, 0);
  free(orig);
  return res;
}


x86 assembly (clang, -O0):
<remove_all_dots_and_double_slashes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bb <remove_all_dots_and_double_slashes+0x4b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11bb <remove_all_dots_and_double_slashes+0x4b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jl     11db <remove_all_dots_and_double_slashes+0x6b>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <remove_all_dots_and_double_slashes+0xfd>
mov    0x2e06(%rip),%rax
mov    (%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1219 <remove_all_dots_and_double_slashes+0xa9>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <remove_all_dots_and_double_slashes+0xfd>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1090 <remove_double_slashes_and_single_dots@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    125e <remove_all_dots_and_double_slashes+0xee>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1060 <resolve_tail_double_dots@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_all_dots_and_double_slashes>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11ea <remove_all_dots_and_double_slashes+0x7a>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11ea <remove_all_dots_and_double_slashes+0x7a>
mov    %rsi,%r15
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0x2e54(%rip),%rcx
mov    (%rcx),%rdi
cmp    %rdi,%rax
jge    11ea <remove_all_dots_and_double_slashes+0x7a>
call   1080 <malloc@plt>
test   %rax,%rax
je     120c <remove_all_dots_and_double_slashes+0x9c>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe7b(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1090 <remove_double_slashes_and_single_dots@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11e0 <remove_all_dots_and_double_slashes+0x70>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <resolve_tail_double_dots@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1204 <remove_all_dots_and_double_slashes+0x94>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
jmp    11fa <remove_all_dots_and_double_slashes+0x8a>

LLVM IR (-O0):
@MAXPATHLEN = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"Wrong file name parameter\0A\00"
@.str.1 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.2 = [7 x i8] c"<null>\00"
define i32 @remove_all_dots_and_double_slashes(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = call i64 @strlen(i8* %17)
%19 = load i64, i64* @MAXPATHLEN
%20 = icmp sge i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %16, %10, %2
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %52
25: ; preds = %16
%26 = load i64, i64* @MAXPATHLEN
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %52
34: ; preds = %25
%35 = load i8*, i8** %7
%36 = load i8*, i8** %4
%37 = call i32 @strcpy(i8* %35, i8* %36)
%38 = load i8*, i8** %5
%39 = call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** %7
%41 = call i32 @remove_double_slashes_and_single_dots(i8* %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %34
%45 = load i8*, i8** %7
%46 = load i8*, i8** %5
%47 = call i32 @resolve_tail_double_dots(i8* %45, i8* %46, i32 0)
store i32 %47, i32* %6
br label %48
48: ; preds = %44, %34
%49 = load i8*, i8** %7
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %6
store i32 %51, i32* %3
br label %52
52: ; preds = %48, %30, %21
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@MAXPATHLEN = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"Wrong file name parameter\0A\00"
@.str.1 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.2 = [7 x i8] c"<null>\00"
define i32 @remove_all_dots_and_double_slashes(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = tail call i64 @strlen(i8* %0)
%9 = load i64, i64* @MAXPATHLEN
%10 = icmp slt i64 %8, %9
br i1 %10, label %14, label %11
11: ; preds = %7, %4, %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %30
14: ; preds = %7
%15 = tail call i8* @malloc(i64 %9)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %30
20: ; preds = %14
%21 = tail call i32 @strcpy(i8* %15, i8* %0)
%22 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @remove_double_slashes_and_single_dots(i8* %15)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @resolve_tail_double_dots(i8* %15, i8* %1, i32 0)
br label %27
27: ; preds = %25, %20
%28 = phi i32 [ %26, %25 ], [ %23, %20 ]
%29 = tail call i32 @free(i8* %15)
br label %30
30: ; preds = %27, %17, %11
%31 = phi i32 [ -1, %11 ], [ %28, %27 ], [ -1, %17 ]
ret i32 %31
}