#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*/ assert(int);
int /*<<< orphan*/ *makebuf(size_t, int);
int wcslen(int /*<<< orphan*/ const *);
size_t wcsnlen(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ wmemcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                           size_t);
void test_wcsnlen(const wchar_t *s) {
  wchar_t *s1;
  size_t size, len, bufsize;
  int i;
  size = wcslen(s) + 1;
  for (i = 0; i <= 1; i++) {
    for (bufsize = 0; bufsize <= size + 10; bufsize++) {
      s1 = makebuf(bufsize * sizeof(wchar_t), i);
      wmemcpy(s1, s, bufsize);
      len = (size > bufsize) ? bufsize : size - 1;
      assert(wcsnlen(s1, bufsize) == len);
    }
  }
}


x86 assembly (clang, -O0):
<test_wcsnlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <wcslen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jg     1232 <test_wcsnlen+0xe2>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0xa,%rcx
cmp    %rcx,%rax
ja     121f <test_wcsnlen+0xcf>
mov    -0x28(%rbp),%rdi
shl    $0x2,%rdi
mov    -0x2c(%rbp),%esi
call   1070 <makebuf@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <wmemcpy@plt>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    11dc <test_wcsnlen+0x8c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11e8 <test_wcsnlen+0x98>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <wcsnlen@plt>
cmp    -0x20(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1187 <test_wcsnlen+0x37>
jmp    1224 <test_wcsnlen+0xd4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1175 <test_wcsnlen+0x25>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_wcsnlen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
call   1040 <wcslen@plt>
cltq
lea    0x1(%rax),%rbp
mov    %rax,0x10(%rsp)
add    $0xb,%rax
xor    %r15d,%r15d
xor    %ebx,%ebx
mov    %rax,0x8(%rsp)
nop
mov    %r15,%rdi
xor    %esi,%esi
call   1070 <makebuf@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1030 <wmemcpy@plt>
cmp    %rbp,%rbx
mov    %rbp,%r14
mov    %r13,%rbp
mov    0x10(%rsp),%r13
cmovb  %rbx,%r13
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <wcsnlen@plt>
xor    %edi,%edi
cmp    %r13,%rax
mov    %rbp,%r13
mov    %r14,%rbp
sete   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%rax
add    $0x1,%rbx
add    $0x4,%r15
cmp    %rax,%rbx
jbe    1180 <test_wcsnlen+0x30>
xor    %r15d,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    $0x1,%esi
call   1070 <makebuf@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1030 <wmemcpy@plt>
cmp    %rbp,%rbx
mov    0x10(%rsp),%rbp
cmovb  %rbx,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <wcsnlen@plt>
xor    %edi,%edi
cmp    %rbp,%rax
mov    %r14,%rbp
sete   %dil
call   1050 <assert@plt>
add    $0x1,%rbx
add    $0x4,%r15
cmp    0x8(%rsp),%rbx
jbe    11f0 <test_wcsnlen+0xa0>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @test_wcsnlen(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %2
%8 = load i32*, i32** %2
%9 = call i32 @wcslen(i32* %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
store i64 %11, i64* %4
store i32 0, i32* %7
br label %12
12: ; preds = %51, %1
%13 = load i32, i32* %7
%14 = icmp sle i32 %13, 1
br i1 %14, label %15, label %54
15: ; preds = %12
store i64 0, i64* %6
br label %16
16: ; preds = %47, %15
%17 = load i64, i64* %6
%18 = load i64, i64* %4
%19 = add i64 %18, 10
%20 = icmp ule i64 %17, %19
br i1 %20, label %21, label %50
21: ; preds = %16
%22 = load i64, i64* %6
%23 = mul i64 %22, 4
%24 = load i32, i32* %7
%25 = call i32* @makebuf(i64 %23, i32 %24)
store i32* %25, i32** %3
%26 = load i32*, i32** %3
%27 = load i32*, i32** %2
%28 = load i64, i64* %6
%29 = call i32 @wmemcpy(i32* %26, i32* %27, i64 %28)
%30 = load i64, i64* %4
%31 = load i64, i64* %6
%32 = icmp ugt i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = load i64, i64* %6
br label %38
35: ; preds = %21
%36 = load i64, i64* %4
%37 = sub i64 %36, 1
br label %38
38: ; preds = %35, %33
%39 = phi i64 [ %34, %33 ], [ %37, %35 ]
store i64 %39, i64* %5
%40 = load i32*, i32** %3
%41 = load i64, i64* %6
%42 = call i64 @wcsnlen(i32* %40, i64 %41)
%43 = load i64, i64* %5
%44 = icmp eq i64 %42, %43
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
br label %47
47: ; preds = %38
%48 = load i64, i64* %6
%49 = add i64 %48, 1
store i64 %49, i64* %6
br label %16
50: ; preds = %16
br label %51
51: ; preds = %50
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %12
54: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @test_wcsnlen(i32* %0) {
%2 = tail call i32 @wcslen(i32* %0)
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = add nsw i64 %4, 10
%6 = add nsw i64 %4, -1
br label %7
7: ; preds = %1, %7
%8 = phi i64 [ 0, %1 ], [ %18, %7 ]
%9 = shl i64 %8, 2
%10 = tail call i32* @makebuf(i64 %9, i32 0)
%11 = tail call i32 @wmemcpy(i32* %10, i32* %0, i64 %8)
%12 = icmp ult i64 %8, %4
%13 = select i1 %12, i64 %8, i64 %6
%14 = tail call i64 @wcsnlen(i32* %10, i64 %8)
%15 = icmp eq i64 %14, %13
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = add i64 %8, 1
%19 = icmp ugt i64 %18, %5
br i1 %19, label %20, label %7
20: ; preds = %7, %20
%21 = phi i64 [ %31, %20 ], [ 0, %7 ]
%22 = shl i64 %21, 2
%23 = tail call i32* @makebuf(i64 %22, i32 1)
%24 = tail call i32 @wmemcpy(i32* %23, i32* %0, i64 %21)
%25 = icmp ult i64 %21, %4
%26 = select i1 %25, i64 %21, i64 %6
%27 = tail call i64 @wcsnlen(i32* %23, i64 %21)
%28 = icmp eq i64 %27, %26
%29 = zext i1 %28 to i32
%30 = tail call i32 @assert(i32 %29)
%31 = add i64 %21, 1
%32 = icmp ugt i64 %31, %5
br i1 %32, label %33, label %20
33: ; preds = %20
ret void
}