#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 scalar_t__ wchar_t;
typedef scalar_t__ char16_t;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(scalar_t__ *);
scalar_t__ malloc(int);
int mbstowcs(scalar_t__ *, char const *, int);
int strlen(char const *);
int multibyte_to_16bit(char16_t **dest, const char *source) {
  wchar_t *temp_name;
  int length = mbstowcs(NULL, source, strlen(source));
  temp_name = (wchar_t *)malloc((length + 1) * sizeof(wchar_t));
  mbstowcs(temp_name, source, length + 1);
  if (sizeof(wchar_t) == 4) {
    int i;
    *dest = (char16_t *)calloc(length + 1, sizeof(char16_t));
    for (i = 0; i < length; i++) {
      (*dest)[i] = (char16_t)temp_name[i];
    }
    free(temp_name);
  } else {
    *dest = temp_name;
  }
  return length;
}


x86 assembly (clang, -O0):
<multibyte_to_16bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mbstowcs@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <mbstowcs@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<multibyte_to_16bit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <mbstowcs@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%r12d
lea    0x8(,%rbx,8),%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <mbstowcs@plt>
mov    %rbp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @multibyte_to_16bit(i64** nocapture writeonly %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i32 @mbstowcs(i64* null, i8* %1, i32 %3)
%5 = add nsw i32 %4, 1
%6 = shl i32 %5, 3
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i64*
%9 = tail call i32 @mbstowcs(i64* %8, i8* %1, i32 %5)
store i64* %8, i64** %0
ret i32 %4
}