#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 */
int sscanf(char *, char *, int *);
size_t hexstr_to_ucs2char(char *in, size_t in_length, char *out,
                          size_t out_length) {
  size_t i = 0;
  size_t x = 0;
  int hexval = 0;
  char buf[] = "  ";
  if ((out_length * 2) < in_length)
    return -1;
  for (i = 0; i < in_length / 2; i++) {
    buf[0] = in[(i * 2)];
    buf[1] = in[(i * 2) + 1];
    if (sscanf(buf, "%x", &hexval) != 1) {
      return -1;
    }
    out[x] = hexval;
    x++;
  }
  x = (x - (x % 2));
  return x;
}


x86 assembly (clang, -O0):
<hexstr_to_ucs2char>:
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)
movl   $0x0,-0x3c(%rbp)
mov    0xeba(%rip),%ax
mov    %ax,-0x3f(%rbp)
mov    0xeb2(%rip),%al
mov    %al,-0x3d(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
cmp    -0x18(%rbp),%rax
jae    1172 <hexstr_to_ucs2char+0x62>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1226 <hexstr_to_ucs2char+0x116>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    120b <hexstr_to_ucs2char+0xfb>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x1,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x3f(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x1,%rcx
mov    0x1(%rax,%rcx,1),%al
mov    %al,-0x3e(%rbp)
lea    -0x3f(%rbp),%rdi
lea    0xe44(%rip),%rsi
lea    -0x3c(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     11de <hexstr_to_ucs2char+0xce>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1226 <hexstr_to_ucs2char+0x116>
mov    -0x3c(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    117a <hexstr_to_ucs2char+0x6a>
mov    -0x38(%rbp),%rax
mov    -0x38(%rbp),%rcx
and    $0x1,%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr_to_ucs2char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
movb   $0x0,0x2(%rsp)
movw   $0x2020,(%rsp)
add    %rcx,%rcx
mov    $0xffffffffffffffff,%rax
cmp    %rsi,%rcx
jb     11ab <hexstr_to_ucs2char+0x9b>
mov    %rsi,%r12
cmp    $0x2,%rsi
jae    114a <hexstr_to_ucs2char+0x3a>
xor    %eax,%eax
jmp    11ab <hexstr_to_ucs2char+0x9b>
mov    %rdx,%r15
mov    %rdi,%rbx
shr    %r12
xor    %r13d,%r13d
mov    %rsp,%rbp
lea    0x4(%rsp),%r14
xchg   %ax,%ax
movzbl (%rbx,%r13,2),%eax
mov    %al,(%rsp)
movzbl 0x1(%rbx,%r13,2),%eax
mov    %al,0x1(%rsp)
mov    %rbp,%rdi
lea    0xe87(%rip),%rsi
mov    %r14,%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    11a4 <hexstr_to_ucs2char+0x94>
movzbl 0x4(%rsp),%eax
mov    %al,(%r15,%r13,1)
add    $0x1,%r13
cmp    %r13,%r12
jne    1160 <hexstr_to_ucs2char+0x50>
and    $0xfffffffffffffffe,%r12
mov    %r12,%rax
jmp    11ab <hexstr_to_ucs2char+0x9b>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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