#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 */
typedef int wchar_t;

/* Variables and functions */
int /*<<< orphan*/ free(int *);
void *malloc(size_t);
scalar_t__ mbstowcs(int *, char const *, size_t);
int strlen(char const *);
size_t wcscspn(int *, char *);
int /*<<< orphan*/ wcstombs(char *, int *, size_t);
char *mb_clean_name(const char *src) {
  size_t src_size = strlen(src) + 1;
  wchar_t *str_wc = NULL;
  size_t src_chars = mbstowcs(NULL, src, 0);
  if (src_chars == (size_t)-1)
    return NULL;
  src_chars += 1;
  str_wc = malloc(src_chars * sizeof(wchar_t));
  if (str_wc == NULL)
    return NULL;
  if (mbstowcs(str_wc, src, src_chars) <= 0) {
    free(str_wc);
    return NULL;
  }
  str_wc[src_chars - 1] = L'\0';
  wchar_t *wc = str_wc;
  while (*wc) {
    size_t good = wcscspn(wc, L" /:?\\");
    if (good)
      wc += good;
    else
      *wc++ = L'-';
  }
  char *newstr = malloc(src_size);
  if (newstr == NULL) {
    free(str_wc);
    return NULL;
  }
  (void)wcstombs(newstr, str_wc, src_size);
  free(str_wc);
  return newstr;
}


x86 assembly (clang, -O0):
<mb_clean_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1040 <mbstowcs@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11b2 <mb_clean_name+0x52>
movq   $0x0,-0x8(%rbp)
jmp    12de <mb_clean_name+0x17e>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
shl    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <mb_clean_name+0x87>
movq   $0x0,-0x8(%rbp)
jmp    12de <mb_clean_name+0x17e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <mbstowcs@plt>
cmp    $0x0,%rax
jg     1218 <mb_clean_name+0xb8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12de <mb_clean_name+0x17e>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpl   $0x0,(%rax)
je     128e <mb_clean_name+0x12e>
mov    -0x30(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   1060 <wcscspn@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1274 <mb_clean_name+0x114>
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1289 <mb_clean_name+0x129>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x30(%rbp)
movl   $0x2d,(%rax)
jmp    1233 <mb_clean_name+0xd3>
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12bc <mb_clean_name+0x15c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12de <mb_clean_name+0x17e>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <wcstombs@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mb_clean_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
call   1050 <strlen@plt>
mov    %eax,%r14d
xor    %r12d,%r12d
xor    %edi,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1040 <mbstowcs@plt>
cmp    $0xffffffffffffffff,%rax
je     123c <mb_clean_name+0xdc>
mov    %rax,%rbx
lea    0x4(,%rax,4),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     122c <mb_clean_name+0xcc>
mov    %rax,%r15
lea    0x1(%rbx),%rdx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <mbstowcs@plt>
test   %rax,%rax
jle    1231 <mb_clean_name+0xd1>
add    $0x1,%r14d
movslq %r14d,%r14
movl   $0x0,(%r15,%rbx,4)
cmpl   $0x0,(%r15)
je     120c <mb_clean_name+0xac>
lea    0xe28(%rip),%rbx
mov    %r15,%rbp
jmp    11f1 <mb_clean_name+0x91>
nopl   (%rax)
movl   $0x2d,0x0(%rbp)
add    $0x4,%rbp
cmpl   $0x0,0x0(%rbp)
je     120c <mb_clean_name+0xac>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <wcscspn@plt>
test   %rax,%rax
je     11e0 <mb_clean_name+0x80>
lea    0x0(%rbp,%rax,4),%rbp
cmpl   $0x0,0x0(%rbp)
jne    11f1 <mb_clean_name+0x91>
mov    %r14,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1231 <mb_clean_name+0xd1>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <wcstombs@plt>
jmp    1234 <mb_clean_name+0xd4>
xor    %r12d,%r12d
jmp    123c <mb_clean_name+0xdc>
xor    %r12d,%r12d
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i32] [i32 32, i32 47, i32 58, i32 63, i32 92, i32 0]
define i8* @mb_clean_name(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = tail call i64 @mbstowcs(i32* null, i8* %0, i64 0)
%6 = icmp eq i64 %5, -1
br i1 %6, label %40, label %7
7: ; preds = %1
%8 = add nuw i64 %5, 1
%9 = shl i64 %8, 2
%10 = tail call i8* @malloc(i64 %9)
%11 = bitcast i8* %10 to i32*
%12 = icmp eq i8* %10, null
br i1 %12, label %40, label %13
13: ; preds = %7
%14 = tail call i64 @mbstowcs(i32* %11, i8* %0, i64 %8)
%15 = icmp slt i64 %14, 1
br i1 %15, label %37, label %16
16: ; preds = %13
%17 = getelementptr inbounds i32, i32* %11, i64 %5
store i32 0, i32* %17
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %16, %28
%21 = phi i32* [ %29, %28 ], [ %11, %16 ]
%22 = tail call i64 @wcscspn(i32* %21, i8* bitcast ([6 x i32]* @.str to i8*))
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = getelementptr inbounds i32, i32* %21, i64 %22
br label %28
26: ; preds = %20
%27 = getelementptr inbounds i32, i32* %21, i64 1
store i32 45, i32* %21
br label %28
28: ; preds = %26, %24
%29 = phi i32* [ %25, %24 ], [ %27, %26 ]
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %20
32: ; preds = %28, %16
%33 = tail call i8* @malloc(i64 %4)
%34 = icmp eq i8* %33, null
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = tail call i32 @wcstombs(i8* %33, i32* %11, i64 %4)
br label %37
37: ; preds = %32, %13, %35
%38 = phi i8* [ %33, %35 ], [ null, %13 ], [ null, %32 ]
%39 = tail call i32 @free(i32* %11)
br label %40
40: ; preds = %37, %7, %1
%41 = phi i8* [ null, %1 ], [ null, %7 ], [ %38, %37 ]
ret i8* %41
}