#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 wchar_t;
typedef int /*<<< orphan*/ mbstate_t;
typedef int char16_t;

/* Variables and functions */
int /*<<< orphan*/ EILSEQ;
int /*<<< orphan*/ errno;
size_t wcrtomb(char *, int, int /*<<< orphan*/);
size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps) {
  static unsigned internal_state;
  if (!ps)
    ps = (void *)&internal_state;
  unsigned *x = (unsigned *)ps;
  wchar_t wc;
  if (!s) {
    if (*x)
      goto ilseq;
    return 1;
  }
  if (!*x && c16 - 0xd800u < 0x400) {
    *x = c16 - 0xd7c0 << 10;
    return 0;
  }
  if (*x) {
    if (c16 - 0xdc00u >= 0x400)
      goto ilseq;
    else
      wc = *x + c16 - 0xdc00;
    *x = 0;
  } else {
    wc = c16;
  }
  return wcrtomb(s, wc, 0);
ilseq:
  *x = 0;
  errno = EILSEQ;
  return -1;
}


x86 assembly (clang, -O0):
<c16rtomb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1139 <c16rtomb+0x29>
lea    0x2ef7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <c16rtomb+0x5b>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
je     115e <c16rtomb+0x4e>
jmp    120c <c16rtomb+0xfc>
movq   $0x1,-0x8(%rbp)
jmp    1230 <c16rtomb+0x120>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11aa <c16rtomb+0x9a>
mov    -0x14(%rbp),%eax
sub    $0xd800,%eax
cmp    $0x400,%eax
jae    11aa <c16rtomb+0x9a>
mov    -0x14(%rbp),%ecx
sub    $0xd7c0,%ecx
shl    $0xa,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1230 <c16rtomb+0x120>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
je     11ef <c16rtomb+0xdf>
mov    -0x14(%rbp),%eax
sub    $0xdc00,%eax
cmp    $0x400,%eax
jb     11cf <c16rtomb+0xbf>
jmp    120c <c16rtomb+0xfc>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
add    -0x14(%rbp),%eax
sub    $0xdc00,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
jmp    11f5 <c16rtomb+0xe5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
xor    %edx,%edx
call   1030 <wcrtomb@plt>
mov    %rax,-0x8(%rbp)
jmp    1230 <c16rtomb+0x120>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    0x2db2(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c16rtomb>:
test   %rdx,%rdx
lea    0x2f12(%rip),%rax
cmovne %rdx,%rax
mov    (%rax),%ecx
test   %rdi,%rdi
je     1147 <c16rtomb+0x37>
mov    %esi,%edx
and    $0xfffffc00,%edx
cmp    $0xd800,%edx
jne    1151 <c16rtomb+0x41>
test   %ecx,%ecx
jne    1151 <c16rtomb+0x41>
shl    $0xa,%esi
add    $0xfca10000,%esi
mov    %esi,(%rax)
xor    %eax,%eax
ret
test   %ecx,%ecx
jne    1172 <c16rtomb+0x62>
mov    $0x1,%eax
ret
test   %ecx,%ecx
je     116b <c16rtomb+0x5b>
cmp    $0xdc00,%edx
jne    1172 <c16rtomb+0x62>
add    %ecx,%esi
add    $0xffff2400,%esi
movl   $0x0,(%rax)
xor    %edx,%edx
jmp    1030 <wcrtomb@plt>
movl   $0x0,(%rax)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
ret

LLVM IR (-O0):
@c16rtomb.internal_state = internal global i32 0
@EILSEQ = global i32 0
@errno = global i32 0
define i64 @c16rtomb(i8* noalias %0, i32 %1, i32* noalias %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32* @c16rtomb.internal_state, i32** %7
br label %13
13: ; preds = %12, %3
%14 = load i32*, i32** %7
store i32* %14, i32** %8
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = load i32*, i32** %8
%19 = load i32, i32* %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
br label %59
22: ; preds = %17
store i64 1, i64* %4
br label %62
23: ; preds = %13
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %23
%28 = load i32, i32* %6
%29 = sub i32 %28, 55296
%30 = icmp ult i32 %29, 1024
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 55232
%34 = shl i32 %33, 10
%35 = load i32*, i32** %8
store i32 %34, i32* %35
store i64 0, i64* %4
br label %62
36: ; preds = %27, %23
%37 = load i32*, i32** %8
%38 = load i32, i32* %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %36
%41 = load i32, i32* %6
%42 = sub i32 %41, 56320
%43 = icmp uge i32 %42, 1024
br i1 %43, label %44, label %45
44: ; preds = %40
br label %59
45: ; preds = %40
%46 = load i32*, i32** %8
%47 = load i32, i32* %46
%48 = load i32, i32* %6
%49 = add i32 %47, %48
%50 = sub i32 %49, 56320
store i32 %50, i32* %9
br label %51
51: ; preds = %45
%52 = load i32*, i32** %8
store i32 0, i32* %52
br label %55
53: ; preds = %36
%54 = load i32, i32* %6
store i32 %54, i32* %9
br label %55
55: ; preds = %53, %51
%56 = load i8*, i8** %5
%57 = load i32, i32* %9
%58 = call i64 @wcrtomb(i8* %56, i32 %57, i32 0)
store i64 %58, i64* %4
br label %62
59: ; preds = %44, %21
%60 = load i32*, i32** %8
store i32 0, i32* %60
%61 = load i32, i32* @EILSEQ
store i32 %61, i32* @errno
store i64 -1, i64* %4
br label %62
62: ; preds = %59, %55, %31, %22
%63 = load i64, i64* %4
ret i64 %63
}

LLVM IR (-O2):
@c16rtomb.internal_state = internal unnamed_addr global i32 0
@EILSEQ = global i32 0
@errno = global i32 0
define i64 @c16rtomb(i8* noalias %0, i32 %1, i32* noalias %2) {
%4 = icmp eq i32* %2, null
%5 = select i1 %4, i32* @c16rtomb.internal_state, i32* %2
%6 = icmp eq i8* %0, null
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %6, label %9, label %10
9: ; preds = %3
br i1 %8, label %28, label %26
10: ; preds = %3
%11 = and i32 %1, -1024
%12 = icmp eq i32 %11, 55296
%13 = and i1 %12, %8
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = shl nuw nsw i32 %1, 10
%16 = add nsw i32 %15, -56557568
store i32 %16, i32* %5
br label %28
17: ; preds = %10
br i1 %8, label %23, label %18
18: ; preds = %17
%19 = icmp eq i32 %11, 56320
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = add nsw i32 %1, -56320
%22 = add i32 %21, %7
store i32 0, i32* %5
br label %23
23: ; preds = %17, %20
%24 = phi i32 [ %22, %20 ], [ %1, %17 ]
%25 = tail call i64 @wcrtomb(i8* %0, i32 %24, i32 0)
br label %28
26: ; preds = %18, %9
store i32 0, i32* %5
%27 = load i32, i32* @EILSEQ
store i32 %27, i32* @errno
br label %28
28: ; preds = %9, %26, %23, %14
%29 = phi i64 [ -1, %26 ], [ %25, %23 ], [ 0, %14 ], [ 1, %9 ]
ret i64 %29
}