#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*/ char32_t;

/* Variables and functions */
size_t MB_LEN_MAX;
size_t mbrtowc(int /*<<< orphan*/ *, char const *, size_t,
               int /*<<< orphan*/ *);
size_t mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n,
                mbstate_t *restrict ps) {
  char32_t wc;
  size_t ret;
  static mbstate_t sps;
  if (!ps)
    ps = &sps;
  if (!pc32)
    return (mbrtowc(0, s, n, ps));
  ret = mbrtowc(&wc, s, n, ps);
  if (ret <= MB_LEN_MAX)
    *pc32 = wc;
  return (ret);
}


x86 assembly (clang, -O0):
<mbrtoc32>:
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,-0x28(%rbp)
jne    113e <mbrtoc32+0x2e>
lea    0x2ef6(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <mbrtoc32+0x57>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mbrtowc@plt>
mov    %rax,-0x8(%rbp)
jmp    11a5 <mbrtoc32+0x95>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
call   1030 <mbrtowc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
ja     119d <mbrtoc32+0x8d>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbrtoc32>:
mov    %rcx,%rax
test   %rcx,%rcx
lea    0x2f13(%rip),%rcx
cmovne %rax,%rcx
test   %rdi,%rdi
je     1150 <mbrtoc32+0x40>
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xc(%rsp),%rdi
call   1030 <mbrtowc@plt>
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%rax
ja     114a <mbrtoc32+0x3a>
mov    0xc(%rsp),%ecx
mov    %ecx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret
xor    %edi,%edi
jmp    1030 <mbrtowc@plt>

LLVM IR (-O0):
@mbrtoc32.sps = internal global i32 0
@MB_LEN_MAX = global i64 0
define i64 @mbrtoc32(i32* noalias %0, i8* noalias %1, i64 %2, i32* noalias %3) {
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%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** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %4
store i32* @mbrtoc32.sps, i32** %9
br label %15
15: ; preds = %14, %4
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = load i32*, i32** %9
%22 = call i64 @mbrtowc(i32* null, i8* %19, i64 %20, i32* %21)
store i64 %22, i64* %5
br label %36
23: ; preds = %15
%24 = load i8*, i8** %7
%25 = load i64, i64* %8
%26 = load i32*, i32** %9
%27 = call i64 @mbrtowc(i32* %10, i8* %24, i64 %25, i32* %26)
store i64 %27, i64* %11
%28 = load i64, i64* %11
%29 = load i64, i64* @MB_LEN_MAX
%30 = icmp ule i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = load i32, i32* %10
%33 = load i32*, i32** %6
store i32 %32, i32* %33
br label %34
34: ; preds = %31, %23
%35 = load i64, i64* %11
store i64 %35, i64* %5
br label %36
36: ; preds = %34, %18
%37 = load i64, i64* %5
ret i64 %37
}

LLVM IR (-O2):
@mbrtoc32.sps = internal global i32 0
@MB_LEN_MAX = global i64 0
define i64 @mbrtoc32(i32* noalias writeonly %0, i8* noalias %1, i64 %2, i32* noalias %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = icmp eq i32* %3, null
%8 = select i1 %7, i32* @mbrtoc32.sps, i32* %3
%9 = icmp eq i32* %0, null
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i64 @mbrtowc(i32* null, i8* %1, i64 %2, i32* %8)
br label %18
12: ; preds = %4
%13 = call i64 @mbrtowc(i32* %5, i8* %1, i64 %2, i32* %8)
%14 = load i64, i64* @MB_LEN_MAX
%15 = icmp ugt i64 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = load i32, i32* %5
store i32 %17, i32* %0
br label %18
18: ; preds = %12, %16, %10
%19 = phi i64 [ %11, %10 ], [ %13, %16 ], [ %13, %12 ]
ret i64 %19
}