#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 */
size_t mbrtowc(int /*<<< orphan*/ *, char const *, size_t,
               int /*<<< orphan*/ *);
size_t mbsrtowcs(wchar_t *dest, const char **src, size_t len, mbstate_t *ps) {
  size_t i;
  if (!dest)
    len = (size_t)-1;
  for (i = 0; i < len; ++i) {
    size_t n = mbrtowc(dest ? dest + i : 0, *src, len, ps);
    if (n == (size_t)-1)
      return -1;
    if (!n)
      break;
    *src += n;
  }
  return i;
}


x86 assembly (clang, -O0):
<mbsrtowcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113b <mbsrtowcs+0x2b>
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11e2 <mbsrtowcs+0xd2>
cmpq   $0x0,-0x10(%rbp)
je     1174 <mbsrtowcs+0x64>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    117f <mbsrtowcs+0x6f>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    117f <mbsrtowcs+0x6f>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <mbrtowc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    11b3 <mbsrtowcs+0xa3>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ea <mbsrtowcs+0xda>
cmpq   $0x0,-0x38(%rbp)
jne    11c3 <mbsrtowcs+0xb3>
jmp    11e2 <mbsrtowcs+0xd2>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1143 <mbsrtowcs+0x33>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbsrtowcs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%rdi
sbb    %r13,%r13
or     %rdx,%r13
je     117b <mbsrtowcs+0x6b>
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    (%rsi),%rax
xor    %ebx,%ebx
mov    %rdi,%rbp
nopl   0x0(%rax,%rax,1)
test   %r12,%r12
mov    %rbp,%rdi
cmove  %r12,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1030 <mbrtowc@plt>
cmp    $0xffffffffffffffff,%rax
je     117f <mbsrtowcs+0x6f>
test   %rax,%rax
je     1186 <mbsrtowcs+0x76>
add    (%r15),%rax
mov    %rax,(%r15)
add    $0x1,%rbx
add    $0x4,%rbp
cmp    %rbx,%r13
jne    1140 <mbsrtowcs+0x30>
mov    %r13,%rbx
jmp    1186 <mbsrtowcs+0x76>
xor    %ebx,%ebx
jmp    1186 <mbsrtowcs+0x76>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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