#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 unicodeToUtf16(int, unsigned int *);
int utf8ToUnicode(unsigned char *, unsigned int *);
int utf8ToUtf16(unsigned char *utf8, unsigned char *utf16,
                unsigned int *sizeUtf8, unsigned int *sizeUtf16) {
  if (!utf8 || !utf16 || !sizeUtf8 || !sizeUtf16)
    return -1;
  int unicode = utf8ToUnicode(utf8, sizeUtf8);
  if (unicode == -1)
    return -1;
  unsigned char _utf16[sizeof(long long int)];
  if ((*(long long int *)_utf16 = unicodeToUtf16(unicode, sizeUtf16)) == -1LL)
    return -1;
  int i = 0;
  while (i < *sizeUtf16) {
    utf16[i] = _utf16[i];
    ++i;
  }
  return 0;
}


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

x86 assembly (clang, -O2):
<utf8ToUtf16>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11a1 <utf8ToUtf16+0x81>
mov    %rsi,%r15
test   %rsi,%rsi
je     11a1 <utf8ToUtf16+0x81>
test   %rdx,%rdx
je     11a1 <utf8ToUtf16+0x81>
mov    %rcx,%rbx
test   %rcx,%rcx
je     11a1 <utf8ToUtf16+0x81>
mov    %rdx,%rsi
call   1040 <utf8ToUnicode@plt>
cmp    $0xffffffff,%eax
je     11a1 <utf8ToUtf16+0x81>
mov    %eax,%edi
mov    %rbx,%rsi
call   1030 <unicodeToUtf16@plt>
mov    %rax,0x8(%rsp)
cmp    $0xffffffffffffffff,%rax
je     1196 <utf8ToUtf16+0x76>
cmpl   $0x0,(%rbx)
je     119e <utf8ToUtf16+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 <utf8ToUtf16+0x60>
jmp    11a1 <utf8ToUtf16+0x81>
mov    $0xffffffff,%r14d
jmp    11a1 <utf8ToUtf16+0x81>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @utf8ToUtf16(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 %58
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = load i32*, i32** %8
%28 = call i32 @utf8ToUnicode(i8* %26, i32* %27)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %5
br label %58
32: ; preds = %25
%33 = load i32, i32* %10
%34 = load i32*, i32** %9
%35 = call i64 @unicodeToUtf16(i32 %33, i32* %34)
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%37 = bitcast i8* %36 to i64*
store i64 %35, i64* %37
%38 = icmp eq i64 %35, -1
br i1 %38, label %39, label %40
39: ; preds = %32
store i32 -1, i32* %5
br label %58
40: ; preds = %32
store i32 0, i32* %12
br label %41
41: ; preds = %46, %40
%42 = load i32, i32* %12
%43 = load i32*, i32** %9
%44 = load i32, i32* %43
%45 = icmp ult i32 %42, %44
br i1 %45, label %46, label %57
46: ; preds = %41
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = load i8*, i8** %7
%52 = load i32, i32* %12
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 1
store i32 %56, i32* %12
br label %41
57: ; preds = %41
store i32 0, i32* %5
br label %58
58: ; preds = %57, %39, %31, %24
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
define i32 @utf8ToUtf16(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 %35
14: ; preds = %4
%15 = tail call i32 @utf8ToUnicode(i8* %0, i32* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %35, label %17
17: ; preds = %14
%18 = bitcast i64* %5 to i8*
%19 = tail call i64 @unicodeToUtf16(i32 %15, i32* %3)
store i64 %19, i64* %5
%20 = icmp eq i64 %19, -1
br i1 %20, label %33, label %21
21: ; preds = %17
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %21, %24
%25 = phi i64 [ %29, %24 ], [ 0, %21 ]
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 %27, i8* %28
%29 = add nuw nsw i64 %25, 1
%30 = load i32, i32* %3
%31 = zext i32 %30 to i64
%32 = icmp ult i64 %29, %31
br i1 %32, label %24, label %33
33: ; preds = %24, %21, %17
%34 = phi i32 [ -1, %17 ], [ 0, %21 ], [ 0, %24 ]
br label %35
35: ; preds = %33, %14, %4
%36 = phi i32 [ -1, %4 ], [ %34, %33 ], [ -1, %14 ]
ret i32 %36
}