#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ wchar_t;
struct TYPE_3__ {
  int /*<<< orphan*/ member_0;
};
typedef TYPE_1__ mbstate_t;

/* Variables and functions */
int /*<<< orphan*/ EILSEQ;
int /*<<< orphan*/ errno;
size_t mbrtowc(int /*<<< orphan*/ *, char const *, size_t, TYPE_1__ *);
int mbtowc(wchar_t *restrict wc, const char *restrict s, size_t size) {
  if (!s)
    return 0;
  mbstate_t ps = {0};
  size_t result = mbrtowc(wc, s, size, &ps);
  if (result == (size_t)-2) {
    errno = EILSEQ;
    return -1;
  }
  return result;
}


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    114b <mbtowc+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <mbtowc+0x84>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1030 <mbrtowc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xfffffffffffffffe,-0x30(%rbp)
jne    119d <mbtowc+0x7d>
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e49(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <mbtowc+0x84>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbtowc>:
test   %rsi,%rsi
je     1147 <mbtowc+0x37>
push   %rax
movl   $0x0,(%rsp)
mov    %rsp,%rcx
call   1030 <mbrtowc@plt>
cmp    $0xfffffffffffffffe,%rax
jne    1142 <mbtowc+0x32>
mov    0x2ebe(%rip),%rax
mov    (%rax),%eax
mov    0x2e9d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@EILSEQ = global i32 0
@errno = 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 %struct.TYPE_3__
%9 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 0, i32* %4
br label %26
13: ; preds = %3
%14 = bitcast %struct.TYPE_3__* %8 to i8*
%15 = load i32*, i32** %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i64 @mbrtowc(i32* %15, i8* %16, i64 %17, %struct.TYPE_3__* %8)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp eq i64 %19, -2
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i32, i32* @EILSEQ
store i32 %22, i32* @errno
store i32 -1, i32* %4
br label %26
23: ; preds = %13
%24 = load i64, i64* %9
%25 = trunc i64 %24 to i32
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %21, %12
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@EILSEQ = global i32 0
@errno = global i32 0
define i32 @mbtowc(i32* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca %struct.TYPE_3__
%5 = icmp eq i8* %1, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = bitcast %struct.TYPE_3__* %4 to i8*
%8 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i64 0, i32 0
store i32 0, i32* %8
%9 = call i64 @mbrtowc(i32* %0, i8* %1, i64 %2, %struct.TYPE_3__* %4)
%10 = icmp eq i64 %9, -2
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = load i32, i32* @EILSEQ
store i32 %12, i32* @errno
br label %15
13: ; preds = %6
%14 = trunc i64 %9 to i32
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ -1, %11 ], [ %14, %13 ]
br label %17
17: ; preds = %3, %15
%18 = phi i32 [ %16, %15 ], [ 0, %3 ]
ret i32 %18
}