#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 char uint8_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ str;

/* Variables and functions */
int /*<<< orphan*/ _encoder_ucs2_to_utf8(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcat(char *, char *);
int _encoder_ucs2str_to_utf8str(uint8_t *in, int insize, uint8_t *out) {
  uint8_t str[16] = {0};
  uint16_t *tmp = (uint16_t *)malloc(insize);
  int i;
  *out = '\0';
  memcpy(tmp, in, insize);
  for (i = 0; i < insize / 2; i++) {
    memset(str, 0, sizeof(str));
    _encoder_ucs2_to_utf8(tmp[i], str);
    strcat(out, str);
  }
  return 0;
}


x86 assembly (clang, -O0):
<_encoder_ucs2str_to_utf8str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
cmp    %ecx,%eax
jge    11fa <_encoder_ucs2str_to_utf8str+0xaa>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x30(%rbp),%rsi
call   1030 <_encoder_ucs2_to_utf8@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    119d <_encoder_ucs2str_to_utf8str+0x4d>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_encoder_ucs2str_to_utf8str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    %esi,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%r14)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
cmp    $0x2,%r12d
jl     11cf <_encoder_ucs2str_to_utf8str+0x7f>
shr    %r12d
xor    %ebx,%ebx
mov    %rsp,%r13
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    (%r15,%rbx,4),%edi
mov    %r13,%rsi
call   1030 <_encoder_ucs2_to_utf8@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1070 <strcat@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11a0 <_encoder_ucs2str_to_utf8str+0x50>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @_encoder_ucs2str_to_utf8str(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [16 x i8]
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = bitcast [16 x i8]* %7 to i8*
%11 = load i32, i32* %5
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %8
%14 = load i8*, i8** %6
store i8 0, i8* %14
%15 = load i32*, i32** %8
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = call i32 @memcpy(i32* %15, i8* %16, i32 %17)
store i32 0, i32* %9
br label %19
19: ; preds = %37, %3
%20 = load i32, i32* %9
%21 = load i32, i32* %5
%22 = sdiv i32 %21, 2
%23 = icmp slt i32 %20, %22
br i1 %23, label %24, label %40
24: ; preds = %19
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 16)
%27 = load i32*, i32** %8
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%33 = call i32 @_encoder_ucs2_to_utf8(i32 %31, i8* %32)
%34 = load i8*, i8** %6
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%36 = call i32 @strcat(i8* %34, i8* %35)
br label %37
37: ; preds = %24
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
store i32 %39, i32* %9
br label %19
40: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
define i32 @_encoder_ucs2str_to_utf8str(i8* %0, i32 %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = tail call i64 @malloc(i32 %1)
%7 = inttoptr i64 %6 to i32*
store i8 0, i8* %2
%8 = tail call i32 @memcpy(i32* %7, i8* %0, i32 %1)
%9 = icmp sgt i32 %1, 1
br i1 %9, label %10, label %22
10: ; preds = %3
%11 = lshr i32 %1, 1
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %13
%14 = phi i64 [ 0, %10 ], [ %20, %13 ]
%15 = call i32 @memset(i8* %5, i32 0, i32 16)
%16 = getelementptr inbounds i32, i32* %7, i64 %14
%17 = load i32, i32* %16
%18 = call i32 @_encoder_ucs2_to_utf8(i32 %17, i8* %5)
%19 = call i32 @strcat(i8* %2, i8* %5)
%20 = add nuw nsw i64 %14, 1
%21 = icmp eq i64 %20, %12
br i1 %21, label %22, label %13
22: ; preds = %13, %3
ret i32 0
}