#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 ucs2char_to_hexstr(char *, size_t, char *, size_t);
size_t utf8_to_hexstr_ucs2(char *in, size_t in_length, char *out,
                           size_t out_length) {
  char buf[out_length];
  size_t res;
  if (out_length < in_length)
    return -1;
  if ((res = convert_string(in, in_length, buf, out_length, "UTF-8",
                            "UCS-2BE")) < 0)
    return res;
  res = ucs2char_to_hexstr(buf, res, out, out_length);
  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,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1181 <utf8_to_hexstr_ucs2+0x61>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
movl   $0x1,-0x44(%rbp)
jmp    11ee <utf8_to_hexstr_ucs2+0xce>
mov    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xe68(%rip),%r8
lea    0xe67(%rip),%r9
call   1030 <convert_string@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jae    11c6 <utf8_to_hexstr_ucs2+0xa6>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    11ee <utf8_to_hexstr_ucs2+0xce>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <ucs2char_to_hexstr@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):
<utf8_to_hexstr_ucs2>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rbx
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0xffffffffffffffff,%rax
cmp    %rsi,%rcx
jb     1174 <utf8_to_hexstr_ucs2+0x54>
mov    %rcx,%r15
mov    %rdx,%r14
lea    0xeac(%rip),%r8
lea    0xeab(%rip),%r9
mov    %rbx,%rdx
call   1030 <convert_string@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1040 <ucs2char_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 i64 @utf8_to_hexstr_ucs2(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 = load i64, i64* %7
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %4
store i64 -1, i64* %5
store i32 1, i32* %13
br label %35
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = load i64, i64* %9
%25 = call i64 @convert_string(i8* %22, i64 %23, i8* %16, i64 %24, 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 i64 %25, i64* %12
%26 = icmp ult i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i64, i64* %12
store i64 %28, i64* %5
store i32 1, i32* %13
br label %35
29: ; preds = %21
%30 = load i64, i64* %12
%31 = load i8*, i8** %8
%32 = load i64, i64* %9
%33 = call i64 @ucs2char_to_hexstr(i8* %16, i64 %30, i8* %31, i64 %32)
store i64 %33, i64* %12
%34 = load i64, i64* %12
store i64 %34, i64* %5
store i32 1, i32* %13
br label %35
35: ; preds = %29, %27, %20
%36 = load i8*, i8** %10
%37 = load i64, i64* %5
ret i64 %37
}

LLVM IR (-O2):
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [8 x i8] c"UCS-2BE\00"
define i64 @utf8_to_hexstr_ucs2(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8, i64 %3
%6 = icmp ult i64 %3, %1
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = call i64 @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))
%9 = call i64 @ucs2char_to_hexstr(i8* %5, i64 %8, i8* %2, i64 %3)
br label %10
10: ; preds = %4, %7
%11 = phi i64 [ %9, %7 ], [ -1, %4 ]
ret i64 %11
}