#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) {
  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,-0x18(%rbp)
jne    1165 <mbrtoc32+0x45>
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
lea    0xeae(%rip),%rsi
mov    $0x1,%edx
call   1040 <mbrtoc32@plt>
mov    %rax,-0x8(%rbp)
jmp    11a5 <mbrtoc32+0x85>
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     119d <mbrtoc32+0x7d>
cmpq   $0x0,-0x10(%rbp)
je     119d <mbrtoc32+0x7d>
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
test   %rsi,%rsi
je     113a <mbrtoc32+0x2a>
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1030 <mbrtowc@plt>
test   %rbx,%rbx
je     1150 <mbrtoc32+0x40>
cmp    $0x4,%rax
ja     1150 <mbrtoc32+0x40>
mov    0x8(%rsp),%ecx
mov    %ecx,(%rbx)
jmp    1150 <mbrtoc32+0x40>
lea    0xebf(%rip),%rsi
lea    0xc(%rsp),%rdi
mov    $0x1,%edx
call   1030 <mbrtowc@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.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 i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %4
%15 = load i32*, i32** %9
%16 = call i64 @mbrtoc32(i32* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i64 1, i32* %15)
store i64 %16, i64* %5
br label %32
17: ; preds = %4
%18 = load i8*, i8** %7
%19 = load i64, i64* %8
%20 = load i32*, i32** %9
%21 = call i64 @mbrtowc(i32* %10, i8* %18, i64 %19, i32* %20)
store i64 %21, i64* %11
%22 = load i64, i64* %11
%23 = icmp ule i64 %22, 4
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32*, i32** %6
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %10
%29 = load i32*, i32** %6
store i32 %28, i32* %29
br label %30
30: ; preds = %27, %24, %17
%31 = load i64, i64* %11
store i64 %31, i64* %5
br label %32
32: ; preds = %30, %14
%33 = load i64, i64* %5
ret i64 %33
}

LLVM IR (-O2):
@.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 i8* %1, null
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = bitcast i32* %5 to i8*
%10 = call i64 @mbrtowc(i32* %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i64 1, i32* %3)
br label %20
11: ; preds = %4
%12 = bitcast i32* %6 to i8*
%13 = call i64 @mbrtowc(i32* %6, i8* %1, i64 %2, i32* %3)
%14 = icmp ult i64 %13, 5
%15 = icmp ne i32* %0, null
%16 = and i1 %15, %14
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i32, i32* %6
store i32 %18, i32* %0
br label %19
19: ; preds = %17, %11
br label %20
20: ; preds = %19, %8
%21 = phi i64 [ %13, %19 ], [ %10, %8 ]
ret i64 %21
}