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

/* Variables and functions */
long long unicodeToUtf8(int, unsigned int *);
int utf16ToUnicode(unsigned short *, unsigned int *);
int utf16ToUtf8(unsigned char *utf16, unsigned char *utf8,
                unsigned int *sizeUtf16, unsigned int *sizeUtf8) {
  if (!utf16 || !utf8 || !sizeUtf16 || !sizeUtf8)
    return -1;
  int unicode = utf16ToUnicode((unsigned short *)utf16, sizeUtf16);
  if (unicode == -1)
    return -1;
  unsigned char _utf8[sizeof(long long int)];
  if ((*(long long int *)_utf8 = unicodeToUtf8(unicode, sizeUtf8)) == -1LL)
    return -1;
  int i = 0;
  while (i < *sizeUtf8) {
    utf8[i] = _utf8[i];
    ++i;
  }
  return 0;
}


x86 assembly (clang, -O0):
<utf16ToUtf8>:
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,-0x10(%rbp)
je     1164 <utf16ToUtf8+0x44>
cmpq   $0x0,-0x18(%rbp)
je     1164 <utf16ToUtf8+0x44>
cmpq   $0x0,-0x20(%rbp)
je     1164 <utf16ToUtf8+0x44>
cmpq   $0x0,-0x28(%rbp)
jne    1170 <utf16ToUtf8+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <utf16ToUtf8+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <utf16ToUnicode@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1196 <utf16ToUtf8+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <utf16ToUtf8+0xda>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1030 <unicodeToUtf8@plt>
mov    %rax,-0x34(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11bc <utf16ToUtf8+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <utf16ToUtf8+0xda>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%eax
jae    11f3 <utf16ToUtf8+0xd3>
movslq -0x38(%rbp),%rax
mov    -0x34(%rbp,%rax,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11c3 <utf16ToUtf8+0xa3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf16ToUtf8>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11a1 <utf16ToUtf8+0x81>
mov    %rsi,%r15
test   %rsi,%rsi
je     11a1 <utf16ToUtf8+0x81>
test   %rdx,%rdx
je     11a1 <utf16ToUtf8+0x81>
mov    %rcx,%rbx
test   %rcx,%rcx
je     11a1 <utf16ToUtf8+0x81>
mov    %rdx,%rsi
call   1040 <utf16ToUnicode@plt>
cmp    $0xffffffff,%eax
je     11a1 <utf16ToUtf8+0x81>
mov    %eax,%edi
mov    %rbx,%rsi
call   1030 <unicodeToUtf8@plt>
mov    %rax,0x8(%rsp)
cmp    $0xffffffffffffffff,%rax
je     1196 <utf16ToUtf8+0x76>
cmpl   $0x0,(%rbx)
je     119e <utf16ToUtf8+0x7e>
xor    %r14d,%r14d
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x8(%rsp,%rax,1),%ecx
mov    %cl,(%r15,%rax,1)
add    $0x1,%rax
mov    (%rbx),%ecx
cmp    %rcx,%rax
jb     1180 <utf16ToUtf8+0x60>
jmp    11a1 <utf16ToUtf8+0x81>
mov    $0xffffffff,%r14d
jmp    11a1 <utf16ToUtf8+0x81>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @utf16ToUtf8(i8* %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [8 x i8]
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %24
15: ; preds = %4
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21, %18, %15, %4
store i32 -1, i32* %5
br label %59
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = bitcast i8* %26 to i16*
%28 = load i32*, i32** %8
%29 = call i32 @utf16ToUnicode(i16* %27, i32* %28)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %5
br label %59
33: ; preds = %25
%34 = load i32, i32* %10
%35 = load i32*, i32** %9
%36 = call i64 @unicodeToUtf8(i32 %34, i32* %35)
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%38 = bitcast i8* %37 to i64*
store i64 %36, i64* %38
%39 = icmp eq i64 %36, -1
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 -1, i32* %5
br label %59
41: ; preds = %33
store i32 0, i32* %12
br label %42
42: ; preds = %47, %41
%43 = load i32, i32* %12
%44 = load i32*, i32** %9
%45 = load i32, i32* %44
%46 = icmp ult i32 %43, %45
br i1 %46, label %47, label %58
47: ; preds = %42
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = load i8*, i8** %7
%53 = load i32, i32* %12
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 %51, i8* %55
%56 = load i32, i32* %12
%57 = add nsw i32 %56, 1
store i32 %57, i32* %12
br label %42
58: ; preds = %42
store i32 0, i32* %5
br label %59
59: ; preds = %58, %40, %32, %24
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
define i32 @utf16ToUtf8(i8* %0, i8* writeonly %1, i32* %2, i32* %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to [8 x i8]*
%7 = icmp ne i8* %0, null
%8 = icmp ne i8* %1, null
%9 = and i1 %7, %8
%10 = icmp ne i32* %2, null
%11 = and i1 %9, %10
%12 = icmp ne i32* %3, null
%13 = and i1 %11, %12
br i1 %13, label %14, label %36
14: ; preds = %4
%15 = bitcast i8* %0 to i16*
%16 = tail call i32 @utf16ToUnicode(i16* %15, i32* %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %36, label %18
18: ; preds = %14
%19 = bitcast i64* %5 to i8*
%20 = tail call i64 @unicodeToUtf8(i32 %16, i32* %3)
store i64 %20, i64* %5
%21 = icmp eq i64 %20, -1
br i1 %21, label %34, label %22
22: ; preds = %18
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %22, %25
%26 = phi i64 [ %30, %25 ], [ 0, %22 ]
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 %28, i8* %29
%30 = add nuw nsw i64 %26, 1
%31 = load i32, i32* %3
%32 = zext i32 %31 to i64
%33 = icmp ult i64 %30, %32
br i1 %33, label %25, label %34
34: ; preds = %25, %22, %18
%35 = phi i32 [ -1, %18 ], [ 0, %22 ], [ 0, %25 ]
br label %36
36: ; preds = %34, %14, %4
%37 = phi i32 [ -1, %4 ], [ %35, %34 ], [ -1, %14 ]
ret i32 %37
}