#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*/ 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 mblen(const char *s, size_t n) {
  static mbstate_t mbs;
  size_t rval;
  if (s == NULL) {
    memset(&mbs, 0, sizeof(mbs));
    return (0);
  }
  rval = mbrtowc(NULL, s, n, &mbs);
  if (rval == (size_t)-1 || rval == (size_t)-2)
    return (-1);
  return ((int)rval);
}


x86 assembly (clang, -O0):
<mblen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115a <mblen+0x3a>
lea    0x2ef2(%rip),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x0,-0x4(%rbp)
jmp    119f <mblen+0x7f>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
lea    0x2ec7(%rip),%rcx
call   1030 <mbrtowc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
je     118c <mblen+0x6c>
cmpq   $0xfffffffffffffffe,-0x20(%rbp)
jne    1198 <mblen+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <mblen+0x7f>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mblen>:
push   %rbx
test   %rdi,%rdi
je     1148 <mblen+0x28>
mov    %rsi,%rdx
mov    %rdi,%rsi
lea    0x2f01(%rip),%rcx
xor    %edi,%edi
call   1030 <mbrtowc@plt>
cmp    $0xfffffffffffffffe,%rax
mov    $0xffffffff,%ebx
cmovb  %eax,%ebx
jmp    115d <mblen+0x3d>
lea    0x2ee5(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %ebx,%eax
pop    %rbx
ret

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

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