#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;
typedef int /*<<< orphan*/ mbs;

/* Variables and functions */
size_t mbrtowc(int /*<<< orphan*/ *, char const *, size_t,
               int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int mbtowc(wchar_t *__restrict pwc, const char *__restrict s, size_t n) {
  static mbstate_t mbs;
  size_t rval;
  if (s == NULL) {
    memset(&mbs, 0, sizeof(mbs));
    return (0);
  }
  rval = mbrtowc(pwc, s, n, &mbs);
  if (rval == (size_t)-1 || rval == (size_t)-2)
    return (-1);
  return ((int)rval);
}


x86 assembly (clang, -O0):
<mbtowc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115e <mbtowc+0x3e>
lea    0x2eee(%rip),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <mbtowc+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0x2ec3(%rip),%rcx
call   1030 <mbrtowc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
je     1190 <mbtowc+0x70>
cmpq   $0xfffffffffffffffe,-0x28(%rbp)
jne    119c <mbtowc+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <mbtowc+0x83>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbtowc>:
push   %rbx
test   %rsi,%rsi
je     1140 <mbtowc+0x20>
lea    0x2f07(%rip),%rcx
call   1030 <mbrtowc@plt>
cmp    $0xfffffffffffffffe,%rax
mov    $0xffffffff,%ebx
cmovb  %eax,%ebx
jmp    1155 <mbtowc+0x35>
lea    0x2eed(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@mbtowc.mbs = internal global i32 0
define i32 @mbtowc(i32* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @memset(i32* @mbtowc.mbs, i32 0, i32 4)
store i32 0, i32* %4
br label %27
13: ; preds = %3
%14 = load i32*, i32** %5
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i64 @mbrtowc(i32* %14, i8* %15, i64 %16, i32* @mbtowc.mbs)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp eq i64 %18, -1
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = load i64, i64* %8
%22 = icmp eq i64 %21, -2
br i1 %22, label %23, label %24
23: ; preds = %20, %13
store i32 -1, i32* %4
br label %27
24: ; preds = %20
%25 = load i64, i64* %8
%26 = trunc i64 %25 to i32
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %23, %11
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@mbtowc.mbs = internal global i32 0
define i32 @mbtowc(i32* noalias %0, i8* noalias %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @memset(i32* @mbtowc.mbs, i32 0, i32 4)
br label %12
7: ; preds = %3
%8 = tail call i64 @mbrtowc(i32* %0, i8* %1, i64 %2, i32* @mbtowc.mbs)
%9 = icmp ugt i64 %8, -3
%10 = trunc i64 %8 to i32
%11 = select i1 %9, i32 -1, i32 %10
br label %12
12: ; preds = %7, %5
%13 = phi i32 [ 0, %5 ], [ %11, %7 ]
ret i32 %13
}