#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 */
scalar_t__ mbrtowc(int /*<<< orphan*/ *, char const *, int,
                   int /*<<< orphan*/ *);
size_t mbsrtowcs(wchar_t *__restrict pwcs, const char **__restrict s, size_t n,
                 mbstate_t *__restrict ps) {
  const char *p;
  wchar_t *d;
  size_t count;
  for (p = *s, d = pwcs, count = 0; count <= n; count++, d++, p++) {
    if (mbrtowc(d, p, 1, ps) == 0)
      break;
  }
  return count;
}


x86 assembly (clang, -O0):
<mbsrtowcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11a4 <mbsrtowcs+0x94>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <mbrtowc@plt>
cmp    $0x0,%rax
jne    1176 <mbsrtowcs+0x66>
jmp    11a4 <mbsrtowcs+0x94>
jmp    117b <mbsrtowcs+0x6b>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1143 <mbsrtowcs+0x33>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbsrtowcs>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%r12
mov    (%rsi),%r13
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%rbx,%r13,1),%rsi
mov    %r12,%rdi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <mbrtowc@plt>
test   %rax,%rax
je     1156 <mbsrtowcs+0x46>
add    $0x1,%rbx
add    $0x4,%r12
cmp    %r15,%rbx
jbe    1130 <mbsrtowcs+0x20>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @mbsrtowcs(i32* noalias %0, i8** noalias %1, i64 %2, i32* noalias %3) {
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i64
store i32* %0, i32** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
store i8* %13, i8** %9
%14 = load i32*, i32** %5
store i32* %14, i32** %10
store i64 0, i64* %11
br label %15
15: ; preds = %27, %4
%16 = load i64, i64* %11
%17 = load i64, i64* %7
%18 = icmp ule i64 %16, %17
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = load i32*, i32** %10
%21 = load i8*, i8** %9
%22 = load i32*, i32** %8
%23 = call i64 @mbrtowc(i32* %20, i8* %21, i32 1, i32* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %34
26: ; preds = %19
br label %27
27: ; preds = %26
%28 = load i64, i64* %11
%29 = add i64 %28, 1
store i64 %29, i64* %11
%30 = load i32*, i32** %10
%31 = getelementptr inbounds i32, i32* %30, i32 1
store i32* %31, i32** %10
%32 = load i8*, i8** %9
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %9
br label %15
34: ; preds = %25, %15
%35 = load i64, i64* %11
ret i64 %35
}

LLVM IR (-O2):
define i64 @mbsrtowcs(i32* noalias %0, i8** noalias nocapture %1, i64 %2, i32* noalias %3) {
%5 = load i8*, i8** %1
br label %6
6: ; preds = %4, %12
%7 = phi i64 [ 0, %4 ], [ %13, %12 ]
%8 = phi i32* [ %0, %4 ], [ %14, %12 ]
%9 = phi i8* [ %5, %4 ], [ %15, %12 ]
%10 = tail call i64 @mbrtowc(i32* %8, i8* %9, i32 1, i32* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = add i64 %7, 1
%14 = getelementptr inbounds i32, i32* %8, i64 1
%15 = getelementptr inbounds i8, i8* %9, i64 1
%16 = icmp ugt i64 %13, %2
br i1 %16, label %17, label %6
17: ; preds = %6, %12
%18 = phi i64 [ %7, %6 ], [ %13, %12 ]
ret i64 %18
}