#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, char);
size_t ucs2char_to_hexstr(char *in, size_t in_length, char *out,
                          size_t out_length) {
  size_t i = 0;
  size_t x = 0;
  char buf[] = "  ";
  if ((out_length * 4) < in_length)
    return -1;
  for (i = 0; i < in_length; i++) {
    snprintf(buf, sizeof(buf), "%X", in[i]);
    if (buf[1] == '\0') {
      buf[1] = buf[0];
      buf[0] = '0';
    }
    out[x] = buf[0];
    out[x + 1] = buf[1];
    x = x + 2;
  }
  out[x] = '\0';
  return x;
}


x86 assembly (clang, -O0):
<ucs2char_to_hexstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0xec1(%rip),%ax
mov    %ax,-0x3b(%rbp)
mov    0xeb9(%rip),%al
mov    %al,-0x39(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
cmp    -0x18(%rbp),%rax
jae    116b <ucs2char_to_hexstr+0x5b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1207 <ucs2char_to_hexstr+0xf7>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f3 <ucs2char_to_hexstr+0xe3>
lea    -0x3b(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    $0x3,%esi
lea    0xe6a(%rip),%rdx
movsbl (%rax,%rcx,1),%ecx
call   1030 <snprintf@plt>
movsbl -0x3a(%rbp),%eax
cmp    $0x0,%eax
jne    11b9 <ucs2char_to_hexstr+0xa9>
mov    -0x3b(%rbp),%al
mov    %al,-0x3a(%rbp)
movb   $0x30,-0x3b(%rbp)
mov    -0x3b(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3a(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,0x1(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1173 <ucs2char_to_hexstr+0x63>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ucs2char_to_hexstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x0,0x6(%rsp)
movw   $0x2020,0x4(%rsp)
shl    $0x2,%rcx
mov    $0xffffffffffffffff,%rbx
cmp    %rsi,%rcx
jb     11b3 <ucs2char_to_hexstr+0xa3>
mov    %rdx,%r15
mov    %rsi,%r14
test   %rsi,%rsi
je     11ac <ucs2char_to_hexstr+0x9c>
mov    %rdi,%r12
xor    %ebx,%ebx
lea    0x4(%rsp),%rbp
xor    %r13d,%r13d
jmp    1176 <ucs2char_to_hexstr+0x66>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %al,(%r15,%rbx,1)
mov    %cl,0x1(%r15,%rbx,1)
add    $0x2,%rbx
add    $0x1,%r13
cmp    %r13,%r14
je     11ae <ucs2char_to_hexstr+0x9e>
movsbl (%r12,%r13,1),%ecx
mov    %rbp,%rdi
mov    $0x3,%esi
lea    0xe79(%rip),%rdx
call   1030 <snprintf@plt>
movzbl 0x4(%rsp),%eax
movzbl 0x5(%rsp),%ecx
test   %cl,%cl
jne    1160 <ucs2char_to_hexstr+0x50>
mov    %al,0x5(%rsp)
movb   $0x30,0x4(%rsp)
mov    %eax,%ecx
mov    $0x30,%al
jmp    1160 <ucs2char_to_hexstr+0x50>
xor    %ebx,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.ucs2char_to_hexstr.buf = [3 x i8] c" \00"
@.str = [3 x i8] c"%X\00"
define i64 @ucs2char_to_hexstr(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 i64
%11 = alloca i64
%12 = alloca [3 x i8]
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i64 0, i64* %10
store i64 0, i64* %11
%13 = bitcast [3 x i8]* %12 to i8*
%14 = load i64, i64* %9
%15 = mul i64 %14, 4
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store i64 -1, i64* %5
br label %62
19: ; preds = %4
store i64 0, i64* %10
br label %20
20: ; preds = %54, %19
%21 = load i64, i64* %10
%22 = load i64, i64* %7
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %57
24: ; preds = %20
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 0
%26 = load i8*, i8** %6
%27 = load i64, i64* %10
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @snprintf(i8* %25, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %29)
%31 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 1
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %24
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 0
%37 = load i8, i8* %36
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 1
store i8 %37, i8* %38
%39 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 0
store i8 48, i8* %39
br label %40
40: ; preds = %35, %24
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 0
%42 = load i8, i8* %41
%43 = load i8*, i8** %8
%44 = load i64, i64* %11
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 %42, i8* %45
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 1
%47 = load i8, i8* %46
%48 = load i8*, i8** %8
%49 = load i64, i64* %11
%50 = add i64 %49, 1
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
%52 = load i64, i64* %11
%53 = add i64 %52, 2
store i64 %53, i64* %11
br label %54
54: ; preds = %40
%55 = load i64, i64* %10
%56 = add i64 %55, 1
store i64 %56, i64* %10
br label %20
57: ; preds = %20
%58 = load i8*, i8** %8
%59 = load i64, i64* %11
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8 0, i8* %60
%61 = load i64, i64* %11
store i64 %61, i64* %5
br label %62
62: ; preds = %57, %18
%63 = load i64, i64* %5
ret i64 %63
}

LLVM IR (-O2):
@__const.ucs2char_to_hexstr.buf = [3 x i8] c" \00"
@.str = [3 x i8] c"%X\00"
define i64 @ucs2char_to_hexstr(i8* nocapture %0, i64 %1, i8* nocapture writeonly %2, i64 %3) {
%5 = alloca [3 x i8]
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%7 = shl i64 %3, 2
%8 = icmp ult i64 %7, %1
br i1 %8, label %35, label %9
9: ; preds = %4
%10 = icmp eq i64 %1, 0
br i1 %10, label %32, label %11
11: ; preds = %9
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 1
br label %13
13: ; preds = %11, %23
%14 = phi i64 [ 0, %11 ], [ %29, %23 ]
%15 = phi i64 [ 0, %11 ], [ %30, %23 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = call i32 @snprintf(i8* %6, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %17)
%19 = load i8, i8* %12
%20 = icmp eq i8 %19, 0
%21 = load i8, i8* %6
br i1 %20, label %22, label %23
22: ; preds = %13
store i8 %21, i8* %12
store i8 48, i8* %6
br label %23
23: ; preds = %13, %22
%24 = phi i8 [ %21, %22 ], [ %19, %13 ]
%25 = phi i8 [ 48, %22 ], [ %21, %13 ]
%26 = getelementptr inbounds i8, i8* %2, i64 %14
store i8 %25, i8* %26
%27 = or i64 %14, 1
%28 = getelementptr inbounds i8, i8* %2, i64 %27
store i8 %24, i8* %28
%29 = add i64 %14, 2
%30 = add nuw i64 %15, 1
%31 = icmp eq i64 %30, %1
br i1 %31, label %32, label %13
32: ; preds = %23, %9
%33 = phi i64 [ 0, %9 ], [ %29, %23 ]
%34 = getelementptr inbounds i8, i8* %2, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %4, %32
%36 = phi i64 [ %33, %32 ], [ -1, %4 ]
ret i64 %36
}