#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 */
size_t convert_string(char *, size_t, char *, size_t, char *, char *);
size_t hexstr_to_ucs2char(char *, size_t, char *, size_t);
size_t hexstr_ucs2_to_utf8(char *in, size_t in_length, char *out,
                           size_t out_length) {
  char buf[out_length];
  size_t res;
  if ((out_length * 2) < in_length)
    return -1;
  if ((res = hexstr_to_ucs2char(in, in_length, buf, out_length)) < 0)
    return res;
  res = convert_string(buf, res, out, out_length, "UCS-2BE", "UTF-8");
  return res;
}


x86 assembly (clang, -O0):
<hexstr_ucs2_to_utf8>:
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,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
cmp    -0x18(%rbp),%rax
jae    1185 <hexstr_ucs2_to_utf8+0x65>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
movl   $0x1,-0x44(%rbp)
jmp    11f2 <hexstr_ucs2_to_utf8+0xd2>
mov    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
call   1040 <hexstr_to_ucs2char@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jae    11bc <hexstr_ucs2_to_utf8+0x9c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    11f2 <hexstr_ucs2_to_utf8+0xd2>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe2d(%rip),%r8
lea    0xe2e(%rip),%r9
call   1030 <convert_string@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr_ucs2_to_utf8>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rsp,%rbx
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
add    %rcx,%rcx
mov    $0xffffffffffffffff,%rax
cmp    %rsi,%rcx
jb     117a <hexstr_ucs2_to_utf8+0x5a>
mov    %rdx,%r14
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <hexstr_to_ucs2char@plt>
lea    0xe9e(%rip),%r8
lea    0xe9f(%rip),%r9
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <convert_string@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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