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

/* Variables and functions */
scalar_t__ calloc(int, int);
int strlen(char const *);
int utf8_count(char const *, int);
int /*<<< orphan*/ utf8_to_utf16be_sub(int /*<<< orphan*/ *, char const *, int,
                                       int);
wchar_t *utf8_to_utf16be(const char *p_utf8_str, int *n_num_out,
                         bool b_big_endian) {
  int n_utf8_num;
  wchar_t *pUcsStr;
  n_utf8_num = strlen(p_utf8_str);
  *n_num_out = utf8_count(p_utf8_str, n_utf8_num);
  pUcsStr = (wchar_t *)calloc((*n_num_out + 1), sizeof(wchar_t));
  utf8_to_utf16be_sub(pUcsStr, p_utf8_str, n_utf8_num, b_big_endian);
  return pUcsStr;
}


x86 assembly (clang, -O0):
<utf8_to_utf16be>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <utf8_count@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1060 <utf8_to_utf16be_sub@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_to_utf16be>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
mov    %eax,%esi
call   1030 <utf8_count@plt>
mov    %eax,(%r15)
lea    0x1(%rax),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
mov    %r14d,%ecx
call   1060 <utf8_to_utf16be_sub@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @utf8_to_utf16be(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %7
%11 = load i8*, i8** %4
%12 = load i32, i32* %7
%13 = call i32 @utf8_count(i8* %11, i32 %12)
%14 = load i32*, i32** %5
store i32 %13, i32* %14
%15 = load i32*, i32** %5
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1
%18 = call i64 @calloc(i32 %17, i32 4)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = load i8*, i8** %4
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = call i32 @utf8_to_utf16be_sub(i32* %20, i8* %21, i32 %22, i32 %23)
%25 = load i32*, i32** %8
ret i32* %25
}

LLVM IR (-O2):
define i32* @utf8_to_utf16be(i8* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @utf8_count(i8* %0, i32 %4)
store i32 %5, i32* %1
%6 = add nsw i32 %5, 1
%7 = tail call i64 @calloc(i32 %6, i32 4)
%8 = inttoptr i64 %7 to i32*
%9 = tail call i32 @utf8_to_utf16be_sub(i32* %8, i8* %0, i32 %4, i32 %2)
ret i32* %8
}