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

/* Variables and functions */
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) {
  static unsigned internal_state;
  if (!ps)
    ps = (void *)&internal_state;
  if (!s)
    return mbrtoc32(0, "", 1, ps);
  wchar_t wc;
  size_t ret = mbrtowc(&wc, s, n, ps);
  if (ret <= 4 && pc32)
    *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    114e <mbrtoc32+0x2e>
lea    0x2eea(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117b <mbrtoc32+0x5b>
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
lea    0xe98(%rip),%rsi
mov    $0x1,%edx
call   1040 <mbrtoc32@plt>
mov    %rax,-0x8(%rbp)
jmp    11bb <mbrtoc32+0x9b>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
call   1030 <mbrtowc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x4,-0x38(%rbp)
ja     11b3 <mbrtoc32+0x93>
cmpq   $0x0,-0x10(%rbp)
je     11b3 <mbrtoc32+0x93>
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>:
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rax
test   %rcx,%rcx
lea    0x2f0a(%rip),%rcx
cmovne %rax,%rcx
test   %rsi,%rsi
je     114b <mbrtoc32+0x3b>
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1030 <mbrtowc@plt>
test   %rbx,%rbx
je     1161 <mbrtoc32+0x51>
cmp    $0x4,%rax
ja     1161 <mbrtoc32+0x51>
mov    0x8(%rsp),%ecx
mov    %ecx,(%rbx)
jmp    1161 <mbrtoc32+0x51>
lea    0xeae(%rip),%rsi
lea    0xc(%rsp),%rdi
mov    $0x1,%edx
call   1030 <mbrtowc@plt>
add    $0x10,%rsp
pop    %rbx
ret

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