#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*/ uint64_t;
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
unsigned int
unicode_code_point_byte_requirement_for_string_utf16(int /*<<< orphan*/ *,
                                                     size_t);
int /*<<< orphan*/ unicode_reverse_byte_order_utf16(int, int);
scalar_t__ unicode_write_utf16_with_endianness(int /*<<< orphan*/, char *, int);
char *unicode_write_string_utf16_imp(uint32_t *code_points,
                                     size_t code_points_length,
                                     size_t *strings_length_ptr, bool reverse,
                                     bool include_bom) {
  assert(code_points);
  unsigned int string_length =
                   unicode_code_point_byte_requirement_for_string_utf16(
                       code_points, code_points_length),
               string_top;
  if (include_bom) {
    string_length += 2;
    string_top = 2;
  } else
    string_top = 0;
  char *ret = (char *)malloc(sizeof(char) * (string_length + 1));
  if (include_bom)
    *((uint64_t *)ret) = unicode_reverse_byte_order_utf16(0xFEFF, reverse);
  for (unsigned int k = 0; k < code_points_length; k++) {
    string_top += unicode_write_utf16_with_endianness(
        code_points[k], &ret[string_top], !reverse);
  }
  *strings_length_ptr = string_length;
  return ret;
}


x86 assembly (clang, -O0):
<unicode_write_string_utf16_imp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <unicode_code_point_byte_requirement_for_string_utf16@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11a3 <unicode_write_string_utf16_imp+0x53>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
movl   $0x2,-0x28(%rbp)
jmp    11aa <unicode_write_string_utf16_imp+0x5a>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11e0 <unicode_write_string_utf16_imp+0x90>
mov    -0x1c(%rbp),%esi
mov    $0xfeff,%edi
call   1070 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    1235 <unicode_write_string_utf16_imp+0xe5>
mov    -0x8(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%eax
add    %rax,%rsi
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edx
call   1060 <unicode_write_utf16_with_endianness@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e7 <unicode_write_string_utf16_imp+0x97>
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_write_string_utf16_imp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %ecx,%r13d
mov    %rdx,0x10(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <assert@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <unicode_code_point_byte_requirement_for_string_utf16@plt>
lea    0x2(%rax),%ecx
xor    %ebx,%ebx
test   %ebp,%ebp
setne  %r14b
cmove  %eax,%ecx
mov    %rcx,0x8(%rsp)
lea    0x1(%rcx),%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
test   %ebp,%ebp
mov    %rax,(%rsp)
je     11b8 <unicode_write_string_utf16_imp+0x68>
mov    $0xfeff,%edi
mov    %r13d,%esi
call   1070 <unicode_reverse_byte_order_utf16@plt>
mov    (%rsp),%rdx
mov    %eax,(%rdx)
test   %r15,%r15
je     1201 <unicode_write_string_utf16_imp+0xb1>
mov    %r14b,%bl
add    %rbx,%rbx
xor    %r14d,%r14d
test   %r13d,%r13d
sete   %r14b
xor    %ebp,%ebp
nop
mov    (%r12,%rbp,4),%edi
mov    %r12,%r13
mov    %r15,%r12
mov    %ebx,%r15d
lea    (%rdx,%r15,1),%rsi
mov    %r14d,%edx
call   1060 <unicode_write_utf16_with_endianness@plt>
mov    (%rsp),%rdx
mov    %rax,%rbx
add    %r15,%rbx
mov    %r12,%r15
mov    %r13,%r12
add    $0x1,%ebp
cmp    %r15,%rbp
jb     11d0 <unicode_write_string_utf16_imp+0x80>
mov    0x8(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
mov    %rdx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @unicode_write_string_utf16_imp(i32* %0, i64 %1, i64* %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i64* %2, i64** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i32*, i32** %6
%16 = call i32 @assert(i32* %15)
%17 = load i32*, i32** %6
%18 = load i64, i64* %7
%19 = call i32 @unicode_code_point_byte_requirement_for_string_utf16(i32* %17, i64 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %10
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %5
%23 = load i32, i32* %11
%24 = add i32 %23, 2
store i32 %24, i32* %11
store i32 2, i32* %12
br label %26
25: ; preds = %5
store i32 0, i32* %12
br label %26
26: ; preds = %25, %22
%27 = load i32, i32* %11
%28 = add i32 %27, 1
%29 = zext i32 %28 to i64
%30 = mul i64 1, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %13
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %26
%37 = load i32, i32* %9
%38 = call i32 @unicode_reverse_byte_order_utf16(i32 65279, i32 %37)
%39 = load i8*, i8** %13
%40 = bitcast i8* %39 to i32*
store i32 %38, i32* %40
br label %41
41: ; preds = %36, %26
store i32 0, i32* %14
br label %42
42: ; preds = %66, %41
%43 = load i32, i32* %14
%44 = zext i32 %43 to i64
%45 = load i64, i64* %7
%46 = icmp ult i64 %44, %45
br i1 %46, label %47, label %69
47: ; preds = %42
%48 = load i32*, i32** %6
%49 = load i32, i32* %14
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = load i32, i32* %51
%53 = load i8*, i8** %13
%54 = load i32, i32* %12
%55 = zext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i32, i32* %9
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
%60 = zext i1 %59 to i32
%61 = call i64 @unicode_write_utf16_with_endianness(i32 %52, i8* %56, i32 %60)
%62 = load i32, i32* %12
%63 = zext i32 %62 to i64
%64 = add nsw i64 %63, %61
%65 = trunc i64 %64 to i32
store i32 %65, i32* %12
br label %66
66: ; preds = %47
%67 = load i32, i32* %14
%68 = add i32 %67, 1
store i32 %68, i32* %14
br label %42
69: ; preds = %42
%70 = load i32, i32* %11
%71 = zext i32 %70 to i64
%72 = load i64*, i64** %8
store i64 %71, i64* %72
%73 = load i8*, i8** %13
ret i8* %73
}

LLVM IR (-O2):
define i8* @unicode_write_string_utf16_imp(i32* %0, i64 %1, i64* nocapture writeonly %2, i32 %3, i32 %4) {
%6 = tail call i32 @assert(i32* %0)
%7 = tail call i32 @unicode_code_point_byte_requirement_for_string_utf16(i32* %0, i64 %1)
%8 = icmp eq i32 %4, 0
%9 = add i32 %7, 2
%10 = select i1 %8, i64 0, i64 2
%11 = select i1 %8, i32 %7, i32 %9
%12 = add i32 %11, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
br i1 %8, label %18, label %15
15: ; preds = %5
%16 = tail call i32 @unicode_reverse_byte_order_utf16(i32 65279, i32 %3)
%17 = inttoptr i64 %13 to i32*
store i32 %16, i32* %17
br label %18
18: ; preds = %15, %5
%19 = icmp eq i64 %1, 0
br i1 %19, label %23, label %20
20: ; preds = %18
%21 = icmp eq i32 %3, 0
%22 = zext i1 %21 to i32
br label %25
23: ; preds = %25, %18
%24 = zext i32 %11 to i64
store i64 %24, i64* %2
ret i8* %14
25: ; preds = %20, %25
%26 = phi i64 [ 0, %20 ], [ %35, %25 ]
%27 = phi i64 [ %10, %20 ], [ %33, %25 ]
%28 = getelementptr inbounds i32, i32* %0, i64 %26
%29 = load i32, i32* %28
%30 = and i64 %27, 4294967295
%31 = getelementptr inbounds i8, i8* %14, i64 %30
%32 = tail call i64 @unicode_write_utf16_with_endianness(i32 %29, i8* %31, i32 %22)
%33 = add nsw i64 %32, %30
%34 = add nuw nsw i64 %26, 1
%35 = and i64 %34, 4294967295
%36 = icmp ult i64 %35, %1
br i1 %36, label %25, label %23
}