#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 convert_string(char *, int, char *, size_t, char *, char *);
int hexstr_to_8bitchars(char const *, size_t, char *, size_t);
ssize_t hexstr_ucs2_to_utf8(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 / 2) {
    return -1;
  }
  res = hexstr_to_8bitchars(in, in_length, buf, out_size);
  if (res < 0) {
    return res;
  }
  res = convert_string(buf, res, out, out_size, "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,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    -0x18(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    118b <hexstr_ucs2_to_utf8+0x6b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    11f1 <hexstr_ucs2_to_utf8+0xd1>
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
call   1040 <hexstr_to_8bitchars@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11bf <hexstr_ucs2_to_utf8+0x9f>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    11f1 <hexstr_ucs2_to_utf8+0xd1>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe2b(%rip),%r8
lea    0xe2c(%rip),%r9
call   1030 <convert_string@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):
<hexstr_ucs2_to_utf8>:
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
mov    %rsi,%rdx
shr    %rdx
mov    $0xffffffff,%eax
cmp    %rdx,%rcx
jb     1182 <hexstr_ucs2_to_utf8+0x62>
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <hexstr_to_8bitchars@plt>
test   %eax,%eax
js     1182 <hexstr_ucs2_to_utf8+0x62>
lea    0xe95(%rip),%r8
lea    0xe96(%rip),%r9
mov    %rbx,%rdi
mov    %eax,%esi
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 i32 @hexstr_ucs2_to_utf8(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 = udiv i64 %19, 2
%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 @hexstr_to_8bitchars(i8* %24, i64 %25, i8* %16, i64 %26)
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 @convert_string(i8* %16, i32 %33, i8* %34, i64 %35, 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 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 = [8 x i8] c"UCS-2BE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
define i32 @hexstr_ucs2_to_utf8(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8, i64 %3
%6 = add i64 %3, -1
%7 = lshr i64 %1, 1
%8 = icmp ult i64 %6, %7
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = call i32 @hexstr_to_8bitchars(i8* %0, i64 %1, i8* %5, i64 %3)
%11 = icmp slt i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @convert_string(i8* %5, i32 %10, 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 %14
14: ; preds = %9, %4, %12
%15 = phi i32 [ %13, %12 ], [ -1, %4 ], [ %10, %9 ]
ret i32 %15
}