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

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *);
size_t wcslcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
size_t wcslen(int /*<<< orphan*/ *);
void path_quickcut(wchar_t *out, wchar_t *path, size_t len, size_t maxlen,
                   wchar_t *filler) {
  size_t plen, offset, excess;
  if (len == 0 || out == NULL)
    errx(1, "path_quickcut: invalid output");
  if (path == NULL)
    errx(1, "path_quickcut: null input");
  if (maxlen + 1 > len)
    errx(1, "path_quickcut: maxlen too long");
  len = maxlen + 1;
  plen = wcslen(path);
  if (plen <= maxlen) {
    wcslcpy(out, path, len);
    return;
  }
  offset = wcslcpy(out, filler, len);
  if (offset >= maxlen)
    return;
  excess = plen - maxlen;
  path += excess + offset;
  wcslcpy(out + offset, path, len - offset);
}


x86 assembly (clang, -O0):
<path_quickcut>:
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     1162 <path_quickcut+0x32>
cmpq   $0x0,-0x8(%rbp)
jne    1173 <path_quickcut+0x43>
mov    $0x1,%edi
lea    0xe92(%rip),%rsi
call   1030 <errx@plt>
cmpq   $0x0,-0x10(%rbp)
jne    118f <path_quickcut+0x5f>
mov    $0x1,%edi
lea    0xe94(%rip),%rsi
call   1030 <errx@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    11b2 <path_quickcut+0x82>
mov    $0x1,%edi
lea    0xe8b(%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>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
ja     11ef <path_quickcut+0xbf>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <wcslcpy@plt>
jmp    1257 <path_quickcut+0x127>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <wcslcpy@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1217 <path_quickcut+0xe7>
jmp    1257 <path_quickcut+0x127>
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1050 <wcslcpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_quickcut>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r14
test   %rdi,%rdi
je     11ad <path_quickcut+0x7d>
test   %rbp,%rbp
je     11ad <path_quickcut+0x7d>
test   %r13,%r13
je     11c3 <path_quickcut+0x93>
lea    0x1(%r15),%rbx
cmp    %rbp,%rbx
jbe    1173 <path_quickcut+0x43>
lea    0xecf(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    %r13,%rdi
call   1040 <wcslen@plt>
mov    %rax,%rbp
sub    %r15,%rbp
jbe    11d6 <path_quickcut+0xa6>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <wcslcpy@plt>
cmp    %r15,%rax
jae    11f2 <path_quickcut+0xc2>
add    %rax,%rbp
lea    0x0(,%rbp,4),%rsi
add    %r13,%rsi
lea    (%r14,%rax,4),%rdi
sub    %rax,%rbx
jmp    11dc <path_quickcut+0xac>
lea    0xe4c(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
test   %r13,%r13
jne    1159 <path_quickcut+0x29>
lea    0xe54(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
jmp    1159 <path_quickcut+0x29>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <wcslcpy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"path_quickcut: invalid output\00"
@.str.1 = [26 x i8] c"path_quickcut: null input\00"
@.str.2 = [31 x i8] c"path_quickcut: maxlen too long\00"
define void @path_quickcut(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 i64
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)
store i64 %37, i64* %11
%38 = load i64, i64* %11
%39 = load i64, i64* %9
%40 = icmp ule i64 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %33
%42 = load i32*, i32** %6
%43 = load i32*, i32** %7
%44 = load i64, i64* %8
%45 = call i64 @wcslcpy(i32* %42, i32* %43, i64 %44)
br label %72
46: ; preds = %33
%47 = load i32*, i32** %6
%48 = load i32*, i32** %10
%49 = load i64, i64* %8
%50 = call i64 @wcslcpy(i32* %47, i32* %48, i64 %49)
store i64 %50, i64* %12
%51 = load i64, i64* %12
%52 = load i64, i64* %9
%53 = icmp uge i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %46
br label %72
55: ; preds = %46
%56 = load i64, i64* %11
%57 = load i64, i64* %9
%58 = sub i64 %56, %57
store i64 %58, i64* %13
%59 = load i64, i64* %13
%60 = load i64, i64* %12
%61 = add i64 %59, %60
%62 = load i32*, i32** %7
%63 = getelementptr inbounds i32, i32* %62, i64 %61
store i32* %63, i32** %7
%64 = load i32*, i32** %6
%65 = load i64, i64* %12
%66 = getelementptr inbounds i32, i32* %64, i64 %65
%67 = load i32*, i32** %7
%68 = load i64, i64* %8
%69 = load i64, i64* %12
%70 = sub i64 %68, %69
%71 = call i64 @wcslcpy(i32* %66, i32* %67, i64 %70)
br label %72
72: ; preds = %55, %54, %41
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"path_quickcut: invalid output\00"
@.str.1 = [26 x i8] c"path_quickcut: null input\00"
@.str.2 = [31 x i8] c"path_quickcut: maxlen too long\00"
define void @path_quickcut(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 %35
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 %35
28: ; preds = %25
%29 = sub i64 %21, %3
%30 = add i64 %29, %26
%31 = getelementptr inbounds i32, i32* %1, i64 %30
%32 = getelementptr inbounds i32, i32* %0, i64 %26
%33 = sub i64 %16, %26
%34 = tail call i64 @wcslcpy(i32* %32, i32* %31, i64 %33)
br label %35
35: ; preds = %25, %28, %23
ret void
}