#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 */
size_t iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char *, char *);
int wcslen(int /*<<< orphan*/ *);
size_t wc2utf8(wchar_t *wcstr, char *out, size_t outbuf) {
  if (wcstr == NULL) {
    return 0;
  }
  iconv_t cd = iconv_open("UTF8", "WCHAR_T");
  size_t len = (wcslen(wcstr) + 1) * sizeof(wchar_t);
  size_t nconv = iconv(cd, (char **)&wcstr, &len, &out, &outbuf);
  iconv_close(cd);
  return nconv;
}


x86 assembly (clang, -O0):
<wc2utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <wc2utf8+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <wc2utf8+0x84>
lea    0xe8d(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <iconv_open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <wcslen@plt>
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <iconv_close@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wc2utf8>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rdx,0x10(%rsp)
test   %rdi,%rdi
je     11b0 <wc2utf8+0x70>
mov    %rdi,%rbx
lea    0xe9c(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <iconv_open@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <wcslen@plt>
cltq
lea    0x4(,%rax,4),%rax
mov    %rax,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebp,%edi
call   1030 <iconv@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <iconv_close@plt>
jmp    11b2 <wc2utf8+0x72>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"UTF8\00"
@.str.1 = [8 x i8] c"WCHAR_T\00"
define i64 @wc2utf8(i32* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i64 0, i64* %4
br label %27
14: ; preds = %3
%15 = call i32 @iconv_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %15, i32* %8
%16 = load i32*, i32** %5
%17 = call i32 @wcslen(i32* %16)
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
store i64 %20, i64* %9
%21 = load i32, i32* %8
%22 = bitcast i32** %5 to i8**
%23 = call i64 @iconv(i32 %21, i8** %22, i64* %9, i8** %6, i64* %7)
store i64 %23, i64* %10
%24 = load i32, i32* %8
%25 = call i32 @iconv_close(i32 %24)
%26 = load i64, i64* %10
store i64 %26, i64* %4
br label %27
27: ; preds = %14, %13
%28 = load i64, i64* %4
ret i64 %28
}

LLVM IR (-O2):
@.str = [5 x i8] c"UTF8\00"
@.str.1 = [8 x i8] c"WCHAR_T\00"
define i64 @wc2utf8(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = icmp eq i32* %0, null
br i1 %8, label %19, label %9
9: ; preds = %3
%10 = tail call i32 @iconv_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%11 = bitcast i64* %7 to i8*
%12 = tail call i32 @wcslen(i32* %0)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = shl nsw i64 %14, 2
store i64 %15, i64* %7
%16 = bitcast i32** %4 to i8**
%17 = call i64 @iconv(i32 %10, i8** %16, i64* %7, i8** %5, i64* %6)
%18 = call i32 @iconv_close(i32 %10)
br label %19
19: ; preds = %3, %9
%20 = phi i64 [ %17, %9 ], [ 0, %3 ]
ret i64 %20
}