#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*/ errx(int, char *);
int /*<<< orphan*/ path_quickcut(int *, int *, size_t, size_t, int *);
int *wcschr(int *, char);
size_t wcslcpy(int *, int *, size_t);
size_t wcslen(int *);
int /*<<< orphan*/ wcsncpy(int *, int *, size_t);
void path_cleancut(wchar_t *out, wchar_t *path, size_t len, size_t maxlen,
                   wchar_t *filler) {
  size_t maxplen, flen;
  wchar_t *c;
  if (len == 0 || out == NULL)
    errx(1, "path_cleancut: invalid output");
  if (path == NULL)
    errx(1, "path_cleancut: null input");
  if (maxlen + 1 > len)
    errx(1, "path_cleancut: maxlen too long");
  len = maxlen + 1;
  if (wcslen(path) <= maxlen) {
    wcslcpy(out, path, len);
    return;
  }
  flen = wcslcpy(out, filler, len);
  if (flen >= maxlen)
    return;
  maxplen = maxlen - flen;
  for (c = path; wcslen(c) > maxplen;) {
    if (*c == L'/')
      c++;
    c = wcschr(c, '/');
    if (c == NULL) {
      path_quickcut(out, path, len, maxlen, filler);
      return;
    }
  }
  wcsncpy(out + flen, c, len - flen);
}


x86 assembly (clang, -O0):
<path_cleancut>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1192 <path_cleancut+0x32>
cmpq   $0x0,-0x8(%rbp)
jne    11a3 <path_cleancut+0x43>
mov    $0x1,%edi
lea    0xe62(%rip),%rsi
call   1030 <errx@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11bf <path_cleancut+0x5f>
mov    $0x1,%edi
lea    0xe64(%rip),%rsi
call   1030 <errx@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    11e2 <path_cleancut+0x82>
mov    $0x1,%edi
lea    0xe5b(%rip),%rsi
call   1030 <errx@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <wcslen@plt>
cmp    -0x20(%rbp),%rax
ja     1217 <path_cleancut+0xb7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <wcslcpy@plt>
jmp    12df <path_cleancut+0x17f>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <wcslcpy@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     123f <path_cleancut+0xdf>
jmp    12df <path_cleancut+0x17f>
mov    -0x20(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <wcslen@plt>
cmp    -0x30(%rbp),%rax
jbe    12bf <path_cleancut+0x15f>
mov    -0x40(%rbp),%rax
cmpl   $0x2f,(%rax)
jne    127f <path_cleancut+0x11f>
mov    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <wcschr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12ba <path_cleancut+0x15a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1080 <path_quickcut@plt>
jmp    12df <path_cleancut+0x17f>
jmp    1253 <path_cleancut+0xf3>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1050 <wcsncpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_cleancut>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r13
test   %rdi,%rdi
je     125f <path_cleancut+0xff>
test   %rbp,%rbp
je     125f <path_cleancut+0xff>
test   %r14,%r14
je     1279 <path_cleancut+0x119>
lea    0x1(%rbx),%r12
cmp    %rbp,%r12
jbe    11b2 <path_cleancut+0x52>
lea    0xe90(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    %r14,%rdi
call   1040 <wcslen@plt>
cmp    %rbx,%rax
jbe    128f <path_cleancut+0x12f>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <wcslcpy@plt>
cmp    %rbx,%rax
jae    12ab <path_cleancut+0x14b>
mov    %rax,%rbp
mov    %r13,(%rsp)
mov    %r15,0x10(%rsp)
mov    %rbx,0x8(%rsp)
sub    %rax,%rbx
mov    %r14,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <wcslen@plt>
cmp    %rbx,%rax
jbe    12ba <path_cleancut+0x15a>
xor    %eax,%eax
cmpl   $0x2f,0x0(%r13)
sete   %al
lea    0x0(,%rax,4),%rdi
add    %r13,%rdi
mov    $0x2f,%esi
call   1070 <wcschr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1200 <path_cleancut+0xa0>
mov    (%rsp),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    0x8(%rsp),%rcx
mov    0x10(%rsp),%r8
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <path_quickcut@plt>
lea    0xd9a(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
test   %r14,%r14
jne    1198 <path_cleancut+0x38>
lea    0xd9e(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
jmp    1198 <path_cleancut+0x38>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <wcslcpy@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rax
lea    (%rax,%rbp,4),%rdi
sub    %rbp,%r12
mov    %r13,%rsi
mov    %r12,%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <wcsncpy@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"path_cleancut: invalid output\00"
@.str.1 = [26 x i8] c"path_cleancut: null input\00"
@.str.2 = [31 x i8] c"path_cleancut: maxlen too long\00"
define void @path_cleancut(i32* %0, i32* %1, i64 %2, i64 %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32* %4, i32** %10
%14 = load i64, i64* %8
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %5
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16, %5
%20 = call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32*, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i64, i64* %9
%28 = add i64 %27, 1
%29 = load i64, i64* %8
%30 = icmp ugt i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i64, i64* %9
%35 = add i64 %34, 1
store i64 %35, i64* %8
%36 = load i32*, i32** %7
%37 = call i64 @wcslen(i32* %36)
%38 = load i64, i64* %9
%39 = icmp ule i64 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = load i32*, i32** %6
%42 = load i32*, i32** %7
%43 = load i64, i64* %8
%44 = call i64 @wcslcpy(i32* %41, i32* %42, i64 %43)
br label %93
45: ; preds = %33
%46 = load i32*, i32** %6
%47 = load i32*, i32** %10
%48 = load i64, i64* %8
%49 = call i64 @wcslcpy(i32* %46, i32* %47, i64 %48)
store i64 %49, i64* %12
%50 = load i64, i64* %12
%51 = load i64, i64* %9
%52 = icmp uge i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %45
br label %93
54: ; preds = %45
%55 = load i64, i64* %9
%56 = load i64, i64* %12
%57 = sub i64 %55, %56
store i64 %57, i64* %11
%58 = load i32*, i32** %7
store i32* %58, i32** %13
br label %59
59: ; preds = %83, %54
%60 = load i32*, i32** %13
%61 = call i64 @wcslen(i32* %60)
%62 = load i64, i64* %11
%63 = icmp ugt i64 %61, %62
br i1 %63, label %64, label %84
64: ; preds = %59
%65 = load i32*, i32** %13
%66 = load i32, i32* %65
%67 = icmp eq i32 %66, 47
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = load i32*, i32** %13
%70 = getelementptr inbounds i32, i32* %69, i32 1
store i32* %70, i32** %13
br label %71
71: ; preds = %68, %64
%72 = load i32*, i32** %13
%73 = call i32* @wcschr(i32* %72, i8 signext 47)
store i32* %73, i32** %13
%74 = load i32*, i32** %13
%75 = icmp eq i32* %74, null
br i1 %75, label %76, label %83
76: ; preds = %71
%77 = load i32*, i32** %6
%78 = load i32*, i32** %7
%79 = load i64, i64* %8
%80 = load i64, i64* %9
%81 = load i32*, i32** %10
%82 = call i32 @path_quickcut(i32* %77, i32* %78, i64 %79, i64 %80, i32* %81)
br label %93
83: ; preds = %71
br label %59
84: ; preds = %59
%85 = load i32*, i32** %6
%86 = load i64, i64* %12
%87 = getelementptr inbounds i32, i32* %85, i64 %86
%88 = load i32*, i32** %13
%89 = load i64, i64* %8
%90 = load i64, i64* %12
%91 = sub i64 %89, %90
%92 = call i32 @wcsncpy(i32* %87, i32* %88, i64 %91)
br label %93
93: ; preds = %84, %76, %53, %40
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"path_cleancut: invalid output\00"
@.str.1 = [26 x i8] c"path_cleancut: null input\00"
@.str.2 = [31 x i8] c"path_cleancut: maxlen too long\00"
define void @path_cleancut(i32* %0, i32* %1, i64 %2, i64 %3, i32* %4) {
%6 = icmp eq i64 %2, 0
%7 = icmp eq i32* %0, null
%8 = or i1 %7, %6
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %5, %9
%12 = icmp eq i32* %1, null
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
%16 = add i64 %3, 1
%17 = icmp ugt i64 %16, %2
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = tail call i64 @wcslen(i32* %1)
%22 = icmp ugt i64 %21, %3
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i64 @wcslcpy(i32* %0, i32* %1, i64 %16)
br label %47
25: ; preds = %20
%26 = tail call i64 @wcslcpy(i32* %0, i32* %4, i64 %16)
%27 = icmp ult i64 %26, %3
br i1 %27, label %28, label %47
28: ; preds = %25
%29 = sub i64 %3, %26
br label %30
30: ; preds = %34, %28
%31 = phi i32* [ %1, %28 ], [ %39, %34 ]
%32 = tail call i64 @wcslen(i32* %31)
%33 = icmp ugt i64 %32, %29
br i1 %33, label %34, label %43
34: ; preds = %30
%35 = load i32, i32* %31
%36 = icmp eq i32 %35, 47
%37 = zext i1 %36 to i64
%38 = getelementptr i32, i32* %31, i64 %37
%39 = tail call i32* @wcschr(i32* %38, i8 signext 47)
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %30
41: ; preds = %34
%42 = tail call i32 @path_quickcut(i32* %0, i32* %1, i64 %16, i64 %3, i32* %4)
br label %47
43: ; preds = %30
%44 = getelementptr inbounds i32, i32* %0, i64 %26
%45 = sub i64 %16, %26
%46 = tail call i32 @wcsncpy(i32* %44, i32* %31, i64 %45)
br label %47
47: ; preds = %25, %43, %41, %23
ret void
}