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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
size_t iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char *, char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *, size_t);
wchar_t *utf82wc(char *utf8, size_t len) {
  if (len == 0)
    return 0;
  iconv_t cd = iconv_open("WCHAR_T", "UTF8");
  size_t outlen = len * 4;
  char *out = malloc(outlen);
  size_t nconv = 0;
  char *ptr = out;
  nconv = iconv(cd, &utf8, &len, &ptr, &outlen);
  iconv_close(cd);
  if (nconv != 0) {
    printf("string:[%s],len:[%zd] is not a valid utf8 string\n", utf8, len);
    free(out);
    return 0;
  }
  return (wchar_t *)out;
}


x86 assembly (clang, -O0):
<utf82wc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <utf82wc+0x28>
movq   $0x0,-0x8(%rbp)
jmp    1228 <utf82wc+0xc8>
lea    0xe71(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1040 <iconv@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <iconv_close@plt>
cmpq   $0x0,-0x38(%rbp)
je     1220 <utf82wc+0xc0>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1228 <utf82wc+0xc8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf82wc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,0x8(%rsp)
mov    %rsi,(%rsp)
test   %rsi,%rsi
je     11ef <utf82wc+0x8f>
mov    %rsi,%rbx
lea    0xe80(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %eax,%r14d
shl    $0x2,%rbx
mov    %rbx,0x18(%rsp)
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
lea    0x10(%rsp),%rcx
lea    0x18(%rsp),%r8
mov    %r14d,%edi
call   1040 <iconv@plt>
mov    %rax,%rbp
mov    %r14d,%edi
call   1070 <iconv_close@plt>
test   %rbp,%rbp
je     11f1 <utf82wc+0x91>
mov    0x8(%rsp),%rsi
mov    (%rsp),%rdx
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"WCHAR_T\00"
@.str.1 = [5 x i8] c"UTF8\00"
@.str.2 = [50 x i8] c"string:[%s],len:[%zd] is not a valid utf8 string\0A\00"
define i32* @utf82wc(i8* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32* null, i32** %3
br label %36
14: ; preds = %2
%15 = call i32 @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %15, i32* %6
%16 = load i64, i64* %5
%17 = mul i64 %16, 4
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %8
store i64 0, i64* %9
%20 = load i8*, i8** %8
store i8* %20, i8** %10
%21 = load i32, i32* %6
%22 = call i64 @iconv(i32 %21, i8** %4, i64* %5, i8** %10, i64* %7)
store i64 %22, i64* %9
%23 = load i32, i32* %6
%24 = call i32 @iconv_close(i32 %23)
%25 = load i64, i64* %9
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %14
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %28, i64 %29)
%31 = load i8*, i8** %8
%32 = call i32 @free(i8* %31)
store i32* null, i32** %3
br label %36
33: ; preds = %14
%34 = load i8*, i8** %8
%35 = bitcast i8* %34 to i32*
store i32* %35, i32** %3
br label %36
36: ; preds = %33, %27, %13
%37 = load i32*, i32** %3
ret i32* %37
}

LLVM IR (-O2):
@.str = [8 x i8] c"WCHAR_T\00"
@.str.1 = [5 x i8] c"UTF8\00"
@.str.2 = [50 x i8] c"string:[%s],len:[%zd] is not a valid utf8 string\0A\00"
define i32* @utf82wc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = icmp eq i64 %1, 0
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = tail call i32 @iconv_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = bitcast i64* %5 to i8*
%11 = shl i64 %1, 2
store i64 %11, i64* %5
%12 = tail call i8* @malloc(i64 %11)
%13 = bitcast i8** %6 to i8*
store i8* %12, i8** %6
%14 = call i64 @iconv(i32 %9, i8** %3, i64* %4, i8** %6, i64* %5)
%15 = call i32 @iconv_close(i32 %9)
%16 = icmp eq i64 %14, 0
br i1 %16, label %22, label %17
17: ; preds = %8
%18 = load i8*, i8** %3
%19 = load i64, i64* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %18, i64 %19)
%21 = call i32 @free(i8* %12)
br label %24
22: ; preds = %8
%23 = bitcast i8* %12 to i32*
br label %24
24: ; preds = %22, %17
%25 = phi i32* [ null, %17 ], [ %23, %22 ]
br label %26
26: ; preds = %2, %24
%27 = phi i32* [ %25, %24 ], [ null, %2 ]
ret i32* %27
}