#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*/ const wchar_t;
typedef int /*<<< orphan*/ *iconv_t;

/* Variables and functions */
scalar_t__ E2BIG;
int /*<<< orphan*/ const *calloc(int, size_t);
scalar_t__ errno;
size_t iconv(int /*<<< orphan*/ *, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ *iconv_open(char *, char *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ const *, int /*<<< orphan*/,
                          size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ const *realloc(int /*<<< orphan*/ const *, size_t);
const wchar_t *utf8_to_wchar(char *in, size_t count, size_t *size) {
  static iconv_t utf8_to_wchar_conv = NULL;
  static wchar_t *out = NULL;
  static size_t out_size = 16;
  size_t inbytesleft;
  size_t outbytesleft;
  if (utf8_to_wchar_conv == NULL) {
    if ((utf8_to_wchar_conv = iconv_open("wchar_t", "UTF-8")) == (iconv_t)-1) {
      perror("utf8_to_wchar: iconv_open");
      utf8_to_wchar_conv = NULL;
    }
  }
  if (out == NULL) {
    out = calloc(1, out_size);
  } else {
    memset(out, 0, out_size);
  }
  size_t in_size = sizeof(char) * count;
  while (1) {
    inbytesleft = in_size;
    outbytesleft = out_size;
    char *in_ptr = in;
    char *out_ptr = (char *)out;
    size_t r = iconv(utf8_to_wchar_conv, &in_ptr, &inbytesleft, &out_ptr,
                     &outbytesleft);
    if (r != -1) {
      break;
    } else if (errno == E2BIG) {
      out_size *= 2;
      out = realloc(out, out_size);
    } else {
      return NULL;
    }
  }
  *size = (out_size - outbytesleft) / sizeof(wchar_t);
  return out;
}


x86 assembly (clang, -O0):
<utf8_to_wchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,0x2ee4(%rip)
jne    11c8 <utf8_to_wchar+0x68>
lea    0xe77(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,0x2ec4(%rip)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11c3 <utf8_to_wchar+0x63>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,0x2e9d(%rip)
jmp    11c8 <utf8_to_wchar+0x68>
cmpq   $0x0,0x2e98(%rip)
jne    11f3 <utf8_to_wchar+0x93>
mov    0x2e73(%rip),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,0x2e7a(%rip)
jmp    1208 <utf8_to_wchar+0xa8>
mov    0x2e6e(%rip),%rdi
mov    0x2e4f(%rip),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e2d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e32(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e1f(%rip),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x48(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0xffffffffffffffff,-0x50(%rbp)
je     126a <utf8_to_wchar+0x10a>
jmp    12cc <utf8_to_wchar+0x16c>
mov    0x2d67(%rip),%rax
mov    (%rax),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <utf8_to_wchar+0x155>
mov    0x2dc5(%rip),%rax
shl    $0x1,%rax
mov    %rax,0x2dba(%rip)
mov    0x2dcb(%rip),%rdi
mov    0x2dac(%rip),%rsi
call   1060 <realloc@plt>
mov    %rax,0x2db8(%rip)
jmp    12c2 <utf8_to_wchar+0x162>
movq   $0x0,-0x8(%rbp)
jmp    12ed <utf8_to_wchar+0x18d>
jmp    12c7 <utf8_to_wchar+0x167>
jmp    1214 <utf8_to_wchar+0xb4>
mov    0x2d7d(%rip),%rcx
sub    -0x30(%rbp),%rcx
shr    $0x2,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d7f(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_to_wchar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %rsi,%r15
mov    %rdi,%rbx
cmpq   $0x0,0x2edf(%rip)
jne    11a7 <utf8_to_wchar+0x47>
lea    0xe76(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,0x2ec3(%rip)
cmp    $0xffffffffffffffff,%rax
je     12d5 <utf8_to_wchar+0x175>
mov    0x2eba(%rip),%rdi
mov    0x2e9b(%rip),%rdx
test   %rdi,%rdi
je     11ca <utf8_to_wchar+0x6a>
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x2ea0(%rip),%rax
jmp    11de <utf8_to_wchar+0x7e>
mov    $0x1,%edi
mov    %rdx,%rsi
call   1050 <calloc@plt>
mov    %rax,0x2e8a(%rip)
mov    %r15,0x18(%rsp)
mov    0x2e66(%rip),%rcx
mov    %rcx,(%rsp)
mov    %rbx,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2e61(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rsp,%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
jne    12a4 <utf8_to_wchar+0x144>
lea    0x18(%rsp),%r13
lea    0x8(%rsp),%rbp
mov    %rsp,%r14
nopl   (%rax)
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    12d1 <utf8_to_wchar+0x171>
mov    0x2dff(%rip),%r12
add    %r12,%r12
mov    %r12,0x2df5(%rip)
mov    0x2e06(%rip),%rdi
mov    %r12,%rsi
call   1060 <realloc@plt>
mov    %rax,0x2df7(%rip)
mov    %r15,0x18(%rsp)
mov    %r12,(%rsp)
mov    %rbx,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2dd5(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     1230 <utf8_to_wchar+0xd0>
mov    0x2da5(%rip),%rax
sub    (%rsp),%rax
shr    $0x2,%rax
mov    0x20(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x2da6(%rip),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12c2 <utf8_to_wchar+0x162>
lea    0xd32(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,0x2d74(%rip)
jmp    11a7 <utf8_to_wchar+0x47>

LLVM IR (-O0):
@utf8_to_wchar.utf8_to_wchar_conv = internal global i32* null
@utf8_to_wchar.out = internal global i32* null
@utf8_to_wchar.out_size = internal global i64 16
@.str = [8 x i8] c"wchar_t\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [26 x i8] c"utf8_to_wchar: iconv_open\00"
@errno = global i64 0
@E2BIG = global i64 0
define i32* @utf8_to_wchar(i8* %0, i64 %1, i64* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%14 = load i32*, i32** @utf8_to_wchar.utf8_to_wchar_conv
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = call i32* @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** @utf8_to_wchar.utf8_to_wchar_conv
%18 = icmp eq i32* %17, inttoptr (i64 -1 to i32*)
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** @utf8_to_wchar.utf8_to_wchar_conv
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %21, %3
%23 = load i32*, i32** @utf8_to_wchar.out
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i64, i64* @utf8_to_wchar.out_size
%27 = call i32* @calloc(i32 1, i64 %26)
store i32* %27, i32** @utf8_to_wchar.out
br label %32
28: ; preds = %22
%29 = load i32*, i32** @utf8_to_wchar.out
%30 = load i64, i64* @utf8_to_wchar.out_size
%31 = call i32 @memset(i32* %29, i32 0, i64 %30)
br label %32
32: ; preds = %28, %25
%33 = load i64, i64* %6
%34 = mul i64 1, %33
store i64 %34, i64* %10
br label %35
35: ; preds = %32, %58
%36 = load i64, i64* %10
store i64 %36, i64* %8
%37 = load i64, i64* @utf8_to_wchar.out_size
store i64 %37, i64* %9
%38 = load i8*, i8** %5
store i8* %38, i8** %11
%39 = load i32*, i32** @utf8_to_wchar.out
%40 = bitcast i32* %39 to i8*
store i8* %40, i8** %12
%41 = load i32*, i32** @utf8_to_wchar.utf8_to_wchar_conv
%42 = call i64 @iconv(i32* %41, i8** %11, i64* %8, i8** %12, i64* %9)
store i64 %42, i64* %13
%43 = load i64, i64* %13
%44 = icmp ne i64 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %35
br label %59
46: ; preds = %35
%47 = load i64, i64* @errno
%48 = load i64, i64* @E2BIG
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i64, i64* @utf8_to_wchar.out_size
%52 = mul i64 %51, 2
store i64 %52, i64* @utf8_to_wchar.out_size
%53 = load i32*, i32** @utf8_to_wchar.out
%54 = load i64, i64* @utf8_to_wchar.out_size
%55 = call i32* @realloc(i32* %53, i64 %54)
store i32* %55, i32** @utf8_to_wchar.out
br label %57
56: ; preds = %46
store i32* null, i32** %4
br label %66
57: ; preds = %50
br label %58
58: ; preds = %57
br label %35
59: ; preds = %45
%60 = load i64, i64* @utf8_to_wchar.out_size
%61 = load i64, i64* %9
%62 = sub i64 %60, %61
%63 = udiv i64 %62, 4
%64 = load i64*, i64** %7
store i64 %63, i64* %64
%65 = load i32*, i32** @utf8_to_wchar.out
store i32* %65, i32** %4
br label %66
66: ; preds = %59, %56
%67 = load i32*, i32** %4
ret i32* %67
}

LLVM IR (-O2):
@utf8_to_wchar.utf8_to_wchar_conv = internal unnamed_addr global i32* null
@utf8_to_wchar.out = internal unnamed_addr global i32* null
@utf8_to_wchar.out_size = internal unnamed_addr global i64 16
@.str = [8 x i8] c"wchar_t\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [26 x i8] c"utf8_to_wchar: iconv_open\00"
@errno = global i64 0
@E2BIG = global i64 0
define i32* @utf8_to_wchar(i8* %0, i64 %1, i64* nocapture writeonly %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = load i32*, i32** @utf8_to_wchar.utf8_to_wchar_conv
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = tail call i32* @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** @utf8_to_wchar.utf8_to_wchar_conv
%14 = icmp eq i32* %13, inttoptr (i64 -1 to i32*)
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** @utf8_to_wchar.utf8_to_wchar_conv
br label %17
17: ; preds = %12, %15, %3
%18 = load i32*, i32** @utf8_to_wchar.out
%19 = icmp eq i32* %18, null
%20 = load i64, i64* @utf8_to_wchar.out_size
br i1 %19, label %21, label %24
21: ; preds = %17
%22 = tail call i32* @calloc(i32 1, i64 %20)
store i32* %22, i32** @utf8_to_wchar.out
%23 = bitcast i32* %22 to i8*
br label %27
24: ; preds = %17
%25 = tail call i32 @memset(i32* %18, i32 0, i64 %20)
%26 = load i8*, i8** bitcast (i32** @utf8_to_wchar.out to i8**)
br label %27
27: ; preds = %24, %21
%28 = phi i8* [ %26, %24 ], [ %23, %21 ]
%29 = bitcast i8** %6 to i8*
%30 = bitcast i8** %7 to i8*
store i64 %1, i64* %4
%31 = load i64, i64* @utf8_to_wchar.out_size
store i64 %31, i64* %5
store i8* %0, i8** %6
store i8* %28, i8** %7
%32 = load i32*, i32** @utf8_to_wchar.utf8_to_wchar_conv
%33 = call i64 @iconv(i32* %32, i8** %6, i64* %4, i8** %7, i64* %5)
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %50
35: ; preds = %27
%36 = bitcast i8** %7 to i32**
br label %37
37: ; preds = %35, %42
%38 = load i64, i64* @errno
%39 = load i64, i64* @E2BIG
%40 = icmp eq i64 %38, %39
br i1 %40, label %42, label %41
41: ; preds = %37
br label %56
42: ; preds = %37
%43 = load i64, i64* @utf8_to_wchar.out_size
%44 = shl i64 %43, 1
store i64 %44, i64* @utf8_to_wchar.out_size
%45 = load i32*, i32** @utf8_to_wchar.out
%46 = call i32* @realloc(i32* %45, i64 %44)
store i32* %46, i32** @utf8_to_wchar.out
store i64 %1, i64* %4
store i64 %44, i64* %5
store i8* %0, i8** %6
store i32* %46, i32** %36
%47 = load i32*, i32** @utf8_to_wchar.utf8_to_wchar_conv
%48 = call i64 @iconv(i32* %47, i8** %6, i64* %4, i8** %7, i64* %5)
%49 = icmp eq i64 %48, -1
br i1 %49, label %37, label %50
50: ; preds = %42, %27
%51 = load i64, i64* @utf8_to_wchar.out_size
%52 = load i64, i64* %5
%53 = sub i64 %51, %52
%54 = lshr i64 %53, 2
store i64 %54, i64* %2
%55 = load i32*, i32** @utf8_to_wchar.out
br label %56
56: ; preds = %41, %50
%57 = phi i32* [ %55, %50 ], [ null, %41 ]
ret i32* %57
}