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


x86 assembly (clang, -O0):
<mbsrtowcs>:
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 <mbsrtowcs+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 <mbsrtowcs+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):
<mbsrtowcs>:
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 <mbsrtowcs+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 @mbsrtowcs(i32* %0, i8** %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
store i32* %0, i32** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%11 = load i8**, i8*** %6
%12 = load i8*, i8** %11
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = load i64, i64* %7
%15 = call i8* @memchr(i8* %13, i32 0, i64 %14)
store i8* %15, i8** %10
%16 = load i8*, i8** %10
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %25
18: ; preds = %4
%19 = load i8*, i8** %10
%20 = load i8*, i8** %9
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = add i64 %23, 1
store i64 %24, i64* %7
br label %25
25: ; preds = %18, %4
%26 = load i32*, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32*, i32** %5
%30 = bitcast i32* %29 to i8*
%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 = %28, %25
%35 = load i8*, i8** %9
%36 = load i64, i64* %7
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = load i8**, i8*** %6
store i8* %37, i8** %38
%39 = load i64, i64* %7
ret i64 %39
}

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