#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 */
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct printf_info {
  int dummy;
};
struct TYPE_4__ {
  int /*<<< orphan*/ member_0;
};
struct TYPE_5__ {
  TYPE_1__ member_1;
  int /*<<< orphan*/ member_0;
};
typedef TYPE_2__ mbstate_t;
typedef int char16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int MB_CUR_MAX;
int c16rtomb(char *, int const, TYPE_2__ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int const);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int print_char16(FILE *stream, const struct printf_info *info,
                 const void *const *args) {
  const char16_t *str = *((const char16_t **)(args[0]));
  int r, l, n = 0;
  char mb[MB_CUR_MAX];
  while (*str) {
    mbstate_t ps = {0, {0}};
    n += (l = c16rtomb(mb, *str, &ps));
    if (l == (size_t)-1) {
      fprintf(stream, "\\u%4.4X", *str);
    } else if ((r = fwrite(mb, l, 1, stream)) < 0)
      return r;
    ++str;
  }
  return n;
}


x86 assembly (clang, -O0):
<print_char16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
je     1240 <print_char16+0x100>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1050 <c16rtomb@plt>
mov    %eax,-0x30(%rbp)
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
movslq -0x30(%rbp),%rax
cmp    $0xffffffffffffffff,%rax
jne    11f7 <print_char16+0xb7>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xe13(%rip),%rsi
call   1040 <fprintf@plt>
jmp    122f <print_char16+0xef>
mov    -0x60(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    122a <print_char16+0xea>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    124d <print_char16+0x10d>
jmp    122f <print_char16+0xef>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    1195 <print_char16+0x55>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_char16>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rdx),%rax
mov    (%rax),%r12
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmpl   $0x0,(%r12)
je     11ce <print_char16+0x9e>
mov    %rdi,%r14
xor    %ebx,%ebx
jmp    1193 <print_char16+0x63>
nopl   0x0(%rax)
mov    (%r12),%edx
mov    %r14,%rdi
lea    0xe82(%rip),%rsi
call   1030 <fprintf@plt>
add    %r13d,%ebx
cmpl   $0x0,0x4(%r12)
lea    0x4(%r12),%r12
je     11d0 <print_char16+0xa0>
movq   $0x0,-0x30(%rbp)
mov    (%r12),%esi
mov    %r15,%rdi
lea    -0x30(%rbp),%rdx
call   1040 <c16rtomb@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1170 <print_char16+0x40>
mov    %r15,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
test   %eax,%eax
jns    1183 <print_char16+0x53>
mov    %eax,%ebx
jmp    11d0 <print_char16+0xa0>
xor    %ebx,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.printf_info = type { i32 }
%struct.TYPE_5__ = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32 }
@MB_CUR_MAX = global i32 0
@.str = [8 x i8] c"\\u%4.4X\00"
define i32 @print_char16(i32* %0, %struct.printf_info* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.printf_info*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca %struct.TYPE_5__
%15 = alloca i32
store i32* %0, i32** %5
store %struct.printf_info* %1, %struct.printf_info** %6
store i8** %2, i8*** %7
%16 = load i8**, i8*** %7
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = bitcast i8* %18 to i32**
%20 = load i32*, i32** %19
store i32* %20, i32** %8
store i32 0, i32* %11
%21 = load i32, i32* @MB_CUR_MAX
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %12
%24 = alloca i8, i64 %22
store i64 %22, i64* %13
br label %25
25: ; preds = %52, %3
%26 = load i32*, i32** %8
%27 = load i32, i32* %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %55
29: ; preds = %25
%30 = bitcast %struct.TYPE_5__* %14 to i8*
%31 = load i32*, i32** %8
%32 = load i32, i32* %31
%33 = call i32 @c16rtomb(i8* %24, i32 %32, %struct.TYPE_5__* %14)
store i32 %33, i32* %10
%34 = load i32, i32* %11
%35 = add nsw i32 %34, %33
store i32 %35, i32* %11
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = icmp eq i64 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %29
%40 = load i32*, i32** %5
%41 = load i32*, i32** %8
%42 = load i32, i32* %41
%43 = call i32 @fprintf(i32* %40, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %42)
br label %52
44: ; preds = %29
%45 = load i32, i32* %10
%46 = load i32*, i32** %5
%47 = call i32 @fwrite(i8* %24, i32 %45, i32 1, i32* %46)
store i32 %47, i32* %9
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = load i32, i32* %9
store i32 %50, i32* %4
store i32 1, i32* %15
br label %57
51: ; preds = %44
br label %52
52: ; preds = %51, %39
%53 = load i32*, i32** %8
%54 = getelementptr inbounds i32, i32* %53, i32 1
store i32* %54, i32** %8
br label %25
55: ; preds = %25
%56 = load i32, i32* %11
store i32 %56, i32* %4
store i32 1, i32* %15
br label %57
57: ; preds = %55, %49
%58 = load i8*, i8** %12
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.printf_info = type { i32 }
%struct.TYPE_5__ = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32 }
@MB_CUR_MAX = global i32 0
@.str = [8 x i8] c"\\u%4.4X\00"
define i32 @print_char16(i32* nocapture %0, %struct.printf_info* nocapture readnone %1, i8** nocapture %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.TYPE_5__*
%6 = bitcast i8** %2 to i32***
%7 = load i32**, i32*** %6
%8 = load i32*, i32** %7
%9 = load i32, i32* @MB_CUR_MAX
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, 0
br i1 %13, label %34, label %14
14: ; preds = %3
%15 = bitcast i64* %4 to i8*
br label %16
16: ; preds = %14, %30
%17 = phi i32* [ %8, %14 ], [ %31, %30 ]
%18 = phi i32 [ 0, %14 ], [ %21, %30 ]
store i64 0, i64* %4
%19 = load i32, i32* %17
%20 = call i32 @c16rtomb(i8* %11, i32 %19, %struct.TYPE_5__* %5)
%21 = add nsw i32 %20, %18
%22 = icmp eq i32 %20, -1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* %17
%25 = call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %30
26: ; preds = %16
%27 = call i32 @fwrite(i8* %11, i32 %20, i32 1, i32* %0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
br label %34
30: ; preds = %23, %26
%31 = getelementptr inbounds i32, i32* %17, i64 1
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %16
34: ; preds = %30, %3, %29
%35 = phi i32 [ %27, %29 ], [ 0, %3 ], [ %21, %30 ]
ret i32 %35
}