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

/* Variables and functions */
scalar_t__ E2BIG;
char *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(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
const char *wchar_to_utf8(const wchar_t *in, size_t count, uint32_t *size) {
  static iconv_t wchar_to_utf8_conv = NULL;
  static char *out = NULL;
  static size_t out_size = 16;
  size_t inbytesleft;
  size_t outbytesleft;
  if (wchar_to_utf8_conv == NULL) {
    if ((wchar_to_utf8_conv = iconv_open("UTF-8", "wchar_t")) == (iconv_t)-1) {
      perror("wchar_to_utf8: iconv_open");
      wchar_to_utf8_conv = NULL;
    }
  }
  if (out == NULL) {
    out = calloc(1, out_size);
  } else {
    memset(out, 0, out_size);
  }
  size_t in_size = sizeof(wchar_t) * count;
  while (1) {
    inbytesleft = in_size;
    outbytesleft = out_size;
    char *in_ptr = (char *)in;
    char *out_ptr = out;
    size_t r = iconv(wchar_to_utf8_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;
  return out;
}


x86 assembly (clang, -O0):
<wchar_to_utf8>:
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 <wchar_to_utf8+0x68>
lea    0xe77(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,0x2ec4(%rip)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11c3 <wchar_to_utf8+0x63>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,0x2e9d(%rip)
jmp    11c8 <wchar_to_utf8+0x68>
cmpq   $0x0,0x2e98(%rip)
jne    11f3 <wchar_to_utf8+0x93>
mov    0x2e73(%rip),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,0x2e7a(%rip)
jmp    1208 <wchar_to_utf8+0xa8>
mov    0x2e6e(%rip),%rdi
mov    0x2e4f(%rip),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x18(%rbp),%rax
shl    $0x2,%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 <wchar_to_utf8+0x10a>
jmp    12cc <wchar_to_utf8+0x16c>
mov    0x2d67(%rip),%rax
mov    (%rax),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <wchar_to_utf8+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 <wchar_to_utf8+0x162>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <wchar_to_utf8+0x189>
jmp    12c7 <wchar_to_utf8+0x167>
jmp    1214 <wchar_to_utf8+0xb4>
mov    0x2d7d(%rip),%rcx
sub    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d83(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wchar_to_utf8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r15
cmpq   $0x0,0x2edf(%rip)
jne    11a7 <wchar_to_utf8+0x47>
lea    0xe76(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1080 <iconv_open@plt>
mov    %rax,0x2ec3(%rip)
cmp    $0xffffffffffffffff,%rax
je     12e1 <wchar_to_utf8+0x181>
mov    0x2eba(%rip),%rdi
mov    0x2e9b(%rip),%rdx
test   %rdi,%rdi
je     11ca <wchar_to_utf8+0x6a>
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x2ea0(%rip),%rax
jmp    11de <wchar_to_utf8+0x7e>
mov    $0x1,%edi
mov    %rdx,%rsi
call   1050 <calloc@plt>
mov    %rax,0x2e8a(%rip)
shl    $0x2,%rbx
mov    %rbx,0x18(%rsp)
mov    0x2e62(%rip),%rcx
mov    %rcx,(%rsp)
mov    %r15,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2e5d(%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    12b4 <wchar_to_utf8+0x154>
lea    0x18(%rsp),%r13
lea    0x8(%rsp),%rbp
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2d91(%rip),%rax
mov    (%rax),%rax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
jne    12dd <wchar_to_utf8+0x17d>
mov    0x2def(%rip),%r12
add    %r12,%r12
mov    %r12,0x2de5(%rip)
mov    0x2df6(%rip),%rdi
mov    %r12,%rsi
call   1060 <realloc@plt>
mov    %rax,0x2de7(%rip)
mov    %rbx,0x18(%rsp)
mov    %r12,(%rsp)
mov    %r15,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2dc5(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     1240 <wchar_to_utf8+0xe0>
mov    0x2d95(%rip),%rax
sub    (%rsp),%rax
mov    0x20(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x2d9a(%rip),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12ce <wchar_to_utf8+0x16e>
lea    0xd26(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,0x2d68(%rip)
jmp    11a7 <wchar_to_utf8+0x47>

LLVM IR (-O0):
@wchar_to_utf8.wchar_to_utf8_conv = internal global i32* null
@wchar_to_utf8.out = internal global i8* null
@wchar_to_utf8.out_size = internal global i64 16
@.str = [6 x i8] c"UTF-8\00"
@.str.1 = [8 x i8] c"wchar_t\00"
@.str.2 = [26 x i8] c"wchar_to_utf8: iconv_open\00"
@errno = global i64 0
@E2BIG = global i64 0
define i8* @wchar_to_utf8(i32* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i32*
%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 i32* %0, i32** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%14 = load i32*, i32** @wchar_to_utf8.wchar_to_utf8_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 ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** @wchar_to_utf8.wchar_to_utf8_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** @wchar_to_utf8.wchar_to_utf8_conv
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %21, %3
%23 = load i8*, i8** @wchar_to_utf8.out
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i64, i64* @wchar_to_utf8.out_size
%27 = call i8* @calloc(i32 1, i64 %26)
store i8* %27, i8** @wchar_to_utf8.out
br label %32
28: ; preds = %22
%29 = load i8*, i8** @wchar_to_utf8.out
%30 = load i64, i64* @wchar_to_utf8.out_size
%31 = call i32 @memset(i8* %29, i32 0, i64 %30)
br label %32
32: ; preds = %28, %25
%33 = load i64, i64* %6
%34 = mul i64 4, %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* @wchar_to_utf8.out_size
store i64 %37, i64* %9
%38 = load i32*, i32** %5
%39 = bitcast i32* %38 to i8*
store i8* %39, i8** %11
%40 = load i8*, i8** @wchar_to_utf8.out
store i8* %40, i8** %12
%41 = load i32*, i32** @wchar_to_utf8.wchar_to_utf8_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* @wchar_to_utf8.out_size
%52 = mul i64 %51, 2
store i64 %52, i64* @wchar_to_utf8.out_size
%53 = load i8*, i8** @wchar_to_utf8.out
%54 = load i64, i64* @wchar_to_utf8.out_size
%55 = call i8* @realloc(i8* %53, i64 %54)
store i8* %55, i8** @wchar_to_utf8.out
br label %57
56: ; preds = %46
store i8* null, i8** %4
br label %65
57: ; preds = %50
br label %58
58: ; preds = %57
br label %35
59: ; preds = %45
%60 = load i64, i64* @wchar_to_utf8.out_size
%61 = load i64, i64* %9
%62 = sub i64 %60, %61
%63 = load i64*, i64** %7
store i64 %62, i64* %63
%64 = load i8*, i8** @wchar_to_utf8.out
store i8* %64, i8** %4
br label %65
65: ; preds = %59, %56
%66 = load i8*, i8** %4
ret i8* %66
}

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