#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 */
size_t wcsnlen(int /*<<< orphan*/ const *, size_t);
int wmemcmp(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *, size_t);
int wcsncmp(const wchar_t *a, const wchar_t *b, size_t length) {
  size_t n = wcsnlen(a, length);
  size_t m = wcsnlen(b, length);
  int r = wmemcmp(a, b, (n < m ? n : m));
  return r ? r : n == m ? 0 : n < m ? -1 : +1;
}


x86 assembly (clang, -O0):
<wcsncmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <wcsnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <wcsnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1181 <wcsncmp+0x61>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1189 <wcsncmp+0x69>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
call   1040 <wmemcmp@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11b2 <wcsncmp+0x92>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    11eb <wcsncmp+0xcb>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11ca <wcsncmp+0xaa>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    11e5 <wcsncmp+0xc5>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rsi
mov    $0x1,%eax
mov    $0xffffffff,%ecx
cmp    %rsi,%rdx
cmovb  %ecx,%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsncmp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rsi
call   1030 <wcsnlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <wcsnlen@plt>
xor    %ecx,%ecx
xor    %edx,%edx
cmp    %rax,%rbx
cmovae %rax,%rbx
setae  %dl
lea    -0x1(%rdx,%rdx,1),%ebp
cmove  %ecx,%ebp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <wmemcmp@plt>
test   %eax,%eax
cmove  %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @wcsncmp(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%10 = load i32*, i32** %4
%11 = load i64, i64* %6
%12 = call i64 @wcsnlen(i32* %10, i64 %11)
store i64 %12, i64* %7
%13 = load i32*, i32** %5
%14 = load i64, i64* %6
%15 = call i64 @wcsnlen(i32* %13, i64 %14)
store i64 %15, i64* %8
%16 = load i32*, i32** %4
%17 = load i32*, i32** %5
%18 = load i64, i64* %7
%19 = load i64, i64* %8
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i64, i64* %7
br label %25
23: ; preds = %3
%24 = load i64, i64* %8
br label %25
25: ; preds = %23, %21
%26 = phi i64 [ %22, %21 ], [ %24, %23 ]
%27 = call i32 @wmemcmp(i32* %16, i32* %17, i64 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* %9
br label %45
32: ; preds = %25
%33 = load i64, i64* %7
%34 = load i64, i64* %8
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
br label %43
37: ; preds = %32
%38 = load i64, i64* %7
%39 = load i64, i64* %8
%40 = icmp ult i64 %38, %39
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 -1, i32 1
br label %43
43: ; preds = %37, %36
%44 = phi i32 [ 0, %36 ], [ %42, %37 ]
br label %45
45: ; preds = %43, %30
%46 = phi i32 [ %31, %30 ], [ %44, %43 ]
ret i32 %46
}

LLVM IR (-O2):
define i32 @wcsncmp(i32* %0, i32* %1, i64 %2) {
%4 = tail call i64 @wcsnlen(i32* %0, i64 %2)
%5 = tail call i64 @wcsnlen(i32* %1, i64 %2)
%6 = icmp ult i64 %4, %5
%7 = select i1 %6, i64 %4, i64 %5
%8 = tail call i32 @wmemcmp(i32* %0, i32* %1, i64 %7)
%9 = icmp eq i32 %8, 0
%10 = icmp eq i64 %4, %5
%11 = select i1 %6, i32 -1, i32 1
%12 = select i1 %10, i32 0, i32 %11
%13 = select i1 %9, i32 %12, i32 %8
ret i32 %13
}