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

/* Variables and functions */
int /*<<< orphan*/ __chk_fail();
scalar_t__ __glibc_unlikely(int);
size_t __wcsrtombs(char *, int /*<<< orphan*/ const **, size_t,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
size_t __wcstombs_chk(char *dst, const wchar_t *src, size_t len,
                      size_t dstlen) {
  if (__glibc_unlikely(dstlen < len))
    __chk_fail();
  mbstate_t state;
  memset(&state, '\0', sizeof state);
  return __wcsrtombs(dst, &src, len, &state);
}


x86 assembly (clang, -O0):
<__wcstombs_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     117e <__wcstombs_chk+0x3e>
mov    $0x0,%al
call   1050 <__chk_fail@plt>
lea    -0x24(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x10(%rbp),%rsi
lea    -0x24(%rbp),%rcx
call   1030 <__wcsrtombs@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__wcstombs_chk>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rdi,%r14
mov    %rsi,(%rsp)
xor    %edi,%edi
cmp    %rdx,%rcx
setb   %dil
call   1060 <__glibc_unlikely@plt>
test   %rax,%rax
je     116d <__wcstombs_chk+0x2d>
xor    %eax,%eax
call   1050 <__chk_fail@plt>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <__wcsrtombs@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @__wcstombs_chk(i8* %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%10 = load i64, i64* %8
%11 = load i64, i64* %7
%12 = icmp ult i64 %10, %11
%13 = zext i1 %12 to i32
%14 = call i64 @__glibc_unlikely(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 (...) @__chk_fail()
br label %18
18: ; preds = %16, %4
%19 = call i32 @memset(i32* %9, i8 signext 0, i32 4)
%20 = load i8*, i8** %5
%21 = load i64, i64* %7
%22 = call i64 @__wcsrtombs(i8* %20, i32** %6, i64 %21, i32* %9)
ret i64 %22
}

LLVM IR (-O2):
define i64 @__wcstombs_chk(i8* %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i32
store i32* %1, i32** %5
%7 = icmp ult i64 %3, %2
%8 = zext i1 %7 to i32
%9 = tail call i64 @__glibc_unlikely(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %4
%12 = tail call i32 (...) @__chk_fail()
br label %13
13: ; preds = %11, %4
%14 = bitcast i32* %6 to i8*
%15 = call i32 @memset(i32* %6, i8 signext 0, i32 4)
%16 = call i64 @__wcsrtombs(i8* %0, i32** %5, i64 %2, i32* %6)
ret i64 %16
}