#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;

/* Variables and functions */
int /*<<< orphan*/ MB_LEN_MAX;
int mbtowc(int /*<<< orphan*/, char const *, int /*<<< orphan*/);
size_t mbstowcs(wchar_t *restrict pwcs, const char *restrict s, size_t n) {
  size_t m = 0;
  while (n--) {
    int b = mbtowc(pwcs++, s, MB_LEN_MAX);
    if (!b)
      break;
    if (b < 0)
      return (-1);
    s += b;
    m++;
  }
  return (m);
}


x86 assembly (clang, -O0):
<mbstowcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
je     11b3 <mbstowcs+0xa3>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
call   1030 <mbtowc@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    117a <mbstowcs+0x6a>
jmp    11b3 <mbstowcs+0xa3>
cmpl   $0x0,-0x2c(%rbp)
jge    1191 <mbstowcs+0x81>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11bb <mbstowcs+0xab>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112c <mbstowcs+0x1c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbstowcs>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdx,%rdx
je     116a <mbstowcs+0x5a>
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %r15d,%r15d
mov    0x2eaf(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1030 <mbtowc@plt>
test   %eax,%eax
je     1176 <mbstowcs+0x66>
js     116f <mbstowcs+0x5f>
mov    %eax,%eax
add    %rax,%r12
add    $0x1,%r15
add    $0x4,%ebx
cmp    %r15,%r14
jne    1140 <mbstowcs+0x30>
mov    %r14,%r15
jmp    1176 <mbstowcs+0x66>
xor    %r15d,%r15d
jmp    1176 <mbstowcs+0x66>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@MB_LEN_MAX = global i32 0
define i64 @mbstowcs(i32* noalias %0, i8* noalias %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %3, %19
%6 = phi i64 [ %10, %19 ], [ %2, %3 ]
%7 = phi i64 [ %22, %19 ], [ 0, %3 ]
%8 = phi i8* [ %21, %19 ], [ %1, %3 ]
%9 = phi i32* [ %11, %19 ], [ %0, %3 ]
%10 = add i64 %6, -1
%11 = getelementptr inbounds i32, i32* %9, i64 1
%12 = ptrtoint i32* %9 to i64
%13 = trunc i64 %12 to i32
%14 = load i32, i32* @MB_LEN_MAX
%15 = tail call i32 @mbtowc(i32 %13, i8* %8, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %5
%18 = icmp slt i32 %15, 0
br i1 %18, label %24, label %19
19: ; preds = %17
%20 = zext i32 %15 to i64
%21 = getelementptr inbounds i8, i8* %8, i64 %20
%22 = add nuw i64 %7, 1
%23 = icmp eq i64 %10, 0
br i1 %23, label %24, label %5
24: ; preds = %19, %5, %17, %3
%25 = phi i64 [ 0, %3 ], [ -1, %17 ], [ %7, %5 ], [ %2, %19 ]
ret i64 %25
}