#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 */
char *memchr(char const *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps) {
  const char *s = (const char *)*src;
  const char *s2 = memchr(s, 0, len);
  if (s2 != NULL)
    len = (s2 - s) + 1;
  if (dst != NULL)
    memcpy(dst, s, len);
  *src = (wchar_t *)(s + len);
  return len;
}


x86 assembly (clang, -O0):
<wcsrtombs>:
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    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1174 <wcsrtombs+0x54>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1190 <wcsrtombs+0x70>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rcx
add    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsrtombs>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rsi),%r12
mov    %r12,%rdi
xor    %esi,%esi
call   1030 <memchr@plt>
mov    %rax,%rbx
sub    %r12,%rbx
add    $0x1,%rbx
test   %rax,%rax
cmove  %r13,%rbx
test   %r15,%r15
je     1163 <wcsrtombs+0x43>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
add    %rbx,%r12
mov    %r12,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @wcsrtombs(i8* %0, i32** %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i32** %1, i32*** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%11 = load i32**, i32*** %6
%12 = load i32*, i32** %11
%13 = bitcast i32* %12 to i8*
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = load i64, i64* %7
%16 = call i8* @memchr(i8* %14, i32 0, i64 %15)
store i8* %16, i8** %10
%17 = load i8*, i8** %10
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %26
19: ; preds = %4
%20 = load i8*, i8** %10
%21 = load i8*, i8** %9
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = add nsw i64 %24, 1
store i64 %25, i64* %7
br label %26
26: ; preds = %19, %4
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = load i8*, i8** %9
%32 = load i64, i64* %7
%33 = call i32 @memcpy(i8* %30, i8* %31, i64 %32)
br label %34
34: ; preds = %29, %26
%35 = load i8*, i8** %9
%36 = load i64, i64* %7
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = bitcast i8* %37 to i32*
%39 = load i32**, i32*** %6
store i32* %38, i32** %39
%40 = load i64, i64* %7
ret i64 %40
}

LLVM IR (-O2):
define i64 @wcsrtombs(i8* %0, i32** nocapture %1, i64 %2, i32* nocapture readnone %3) {
%5 = bitcast i32** %1 to i8**
%6 = load i8*, i8** %5
%7 = tail call i8* @memchr(i8* %6, i32 0, i64 %2)
%8 = icmp eq i8* %7, null
%9 = ptrtoint i8* %7 to i64
%10 = ptrtoint i8* %6 to i64
%11 = sub i64 1, %10
%12 = add i64 %11, %9
%13 = select i1 %8, i64 %2, i64 %12
%14 = icmp eq i8* %0, null
br i1 %14, label %17, label %15
15: ; preds = %4
%16 = tail call i32 @memcpy(i8* %0, i8* %6, i64 %13)
br label %17
17: ; preds = %15, %4
%18 = getelementptr inbounds i8, i8* %6, i64 %13
store i8* %18, i8** %5
ret i64 %13
}