#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 ssize_t;

/* Variables and functions */
int chars8bit_to_hexstr(char *, int, char *, size_t);
int convert_string(char const *, size_t, char *, size_t, char *, char *);
ssize_t utf8_to_hexstr_ucs2(const char *in, size_t in_length, char *out,
                            size_t out_size) {
  char buf[out_size];
  ssize_t res;
  if (out_size - 1 < in_length * 4) {
    return -1;
  }
  res = convert_string(in, in_length, buf, out_size, "UTF-8", "UCS-2BE");
  if (res < 0) {
    return res;
  }
  res = chars8bit_to_hexstr(buf, res, out, out_size);
  return res;
}


x86 assembly (clang, -O0):
<utf8_to_hexstr_ucs2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    -0x18(%rbp),%rcx
shl    $0x2,%rcx
cmp    %rcx,%rax
jae    118b <utf8_to_hexstr_ucs2+0x6b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    11f1 <utf8_to_hexstr_ucs2+0xd1>
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xe5e(%rip),%r8
lea    0xe5d(%rip),%r9
call   1040 <convert_string@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11cd <utf8_to_hexstr_ucs2+0xad>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    11f1 <utf8_to_hexstr_ucs2+0xd1>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <chars8bit_to_hexstr@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_to_hexstr_ucs2>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsp,%rbx
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
add    $0xffffffffffffffff,%rcx
lea    0x0(,%rsi,4),%rdx
mov    $0xffffffff,%eax
cmp    %rdx,%rcx
jb     1184 <utf8_to_hexstr_ucs2+0x64>
lea    0xea2(%rip),%r8
lea    0xea1(%rip),%r9
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <convert_string@plt>
test   %eax,%eax
js     1184 <utf8_to_hexstr_ucs2+0x64>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <chars8bit_to_hexstr@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [8 x i8] c"UCS-2BE\00"
define i32 @utf8_to_hexstr_ucs2(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%14 = load i64, i64* %9
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i64, i64* %9
%18 = sub i64 %17, 1
%19 = load i64, i64* %7
%20 = mul i64 %19, 4
%21 = icmp ult i64 %18, %20
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 -1, i32* %5
store i32 1, i32* %13
br label %38
23: ; preds = %4
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = load i64, i64* %9
%27 = call i32 @convert_string(i8* %24, i64 %25, i8* %16, i64 %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i32, i32* %12
store i32 %31, i32* %5
store i32 1, i32* %13
br label %38
32: ; preds = %23
%33 = load i32, i32* %12
%34 = load i8*, i8** %8
%35 = load i64, i64* %9
%36 = call i32 @chars8bit_to_hexstr(i8* %16, i32 %33, i8* %34, i64 %35)
store i32 %36, i32* %12
%37 = load i32, i32* %12
store i32 %37, i32* %5
store i32 1, i32* %13
br label %38
38: ; preds = %32, %30, %22
%39 = load i8*, i8** %10
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [8 x i8] c"UCS-2BE\00"
define i32 @utf8_to_hexstr_ucs2(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8, i64 %3
%6 = add i64 %3, -1
%7 = shl i64 %1, 2
%8 = icmp ult i64 %6, %7
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = call i32 @convert_string(i8* %0, i64 %1, i8* %5, i64 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp slt i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @chars8bit_to_hexstr(i8* %5, i32 %10, i8* %2, i64 %3)
br label %14
14: ; preds = %9, %4, %12
%15 = phi i32 [ %13, %12 ], [ -1, %4 ], [ %10, %9 ]
ret i32 %15
}