#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*/ va_list;
typedef scalar_t__ uint32_t;

/* Variables and functions */
scalar_t__ alloca(size_t);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/ *);
size_t u8_toucs(scalar_t__ *, size_t, char *, size_t);
size_t vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
size_t u8_vprintf(const char *fmt, va_list ap) {
  size_t cnt, sz = 0, nc, needfree = 0;
  char *buf;
  uint32_t *wcs;
  sz = 512;
  buf = (char *)alloca(sz);
  cnt = vsnprintf(buf, sz, fmt, ap);
  if ((intptr_t)cnt < 0)
    return 0;
  if (cnt >= sz) {
    buf = (char *)malloc(cnt + 1);
    needfree = 1;
    vsnprintf(buf, cnt + 1, fmt, ap);
  }
  wcs = (uint32_t *)alloca((cnt + 1) * sizeof(uint32_t));
  nc = u8_toucs(wcs, cnt + 1, buf, cnt);
  wcs[nc] = 0;
  printf("%ls", (wchar_t *)wcs);
  if (needfree)
    free(buf);
  return nc;
}


x86 assembly (clang, -O0):
<u8_vprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x200,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <alloca@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c4 <u8_vprintf+0x64>
movq   $0x0,-0x8(%rbp)
jmp    1271 <u8_vprintf+0x111>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     1203 <u8_vprintf+0xa3>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
movq   $0x1,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x3,%rdi
call   1060 <alloca@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x40(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <u8_toucs@plt>
mov    %rax,-0x30(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x30(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x48(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
je     1269 <u8_vprintf+0x109>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u8_vprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r12
mov    $0x200,%edi
call   1060 <alloca@plt>
mov    %rax,%r14
mov    $0x200,%esi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
test   %rax,%rax
js     1210 <u8_vprintf+0xb0>
mov    %rax,%rbx
lea    0x1(%rax),%r13
cmp    $0x200,%rax
jb     11c1 <u8_vprintf+0x61>
mov    %r13,%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
lea    0x0(,%r13,8),%rdi
call   1060 <alloca@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1070 <u8_toucs@plt>
mov    %rax,%r15
movq   $0x0,0x0(%rbp,%rax,8)
lea    0xe0b(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
cmp    $0x200,%rbx
jb     1213 <u8_vprintf+0xb3>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1213 <u8_vprintf+0xb3>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%ls\00"
define i64 @u8_vprintf(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 0, i64* %7
store i64 0, i64* %9
store i64 512, i64* %7
%12 = load i64, i64* %7
%13 = call i64 @alloca(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = load i64, i64* %7
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = call i64 @vsnprintf(i8* %15, i64 %16, i8* %17, i32 %18)
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i64 0, i64* %3
br label %63
23: ; preds = %2
%24 = load i64, i64* %6
%25 = load i64, i64* %7
%26 = icmp uge i64 %24, %25
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i64, i64* %6
%29 = add i64 %28, 1
%30 = call i64 @malloc(i64 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %10
store i64 1, i64* %9
%32 = load i8*, i8** %10
%33 = load i64, i64* %6
%34 = add i64 %33, 1
%35 = load i8*, i8** %4
%36 = load i32, i32* %5
%37 = call i64 @vsnprintf(i8* %32, i64 %34, i8* %35, i32 %36)
br label %38
38: ; preds = %27, %23
%39 = load i64, i64* %6
%40 = add i64 %39, 1
%41 = mul i64 %40, 8
%42 = call i64 @alloca(i64 %41)
%43 = inttoptr i64 %42 to i64*
store i64* %43, i64** %11
%44 = load i64*, i64** %11
%45 = load i64, i64* %6
%46 = add i64 %45, 1
%47 = load i8*, i8** %10
%48 = load i64, i64* %6
%49 = call i64 @u8_toucs(i64* %44, i64 %46, i8* %47, i64 %48)
store i64 %49, i64* %8
%50 = load i64*, i64** %11
%51 = load i64, i64* %8
%52 = getelementptr inbounds i64, i64* %50, i64 %51
store i64 0, i64* %52
%53 = load i64*, i64** %11
%54 = bitcast i64* %53 to i32*
%55 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %54)
%56 = load i64, i64* %9
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %38
%59 = load i8*, i8** %10
%60 = call i32 @free(i8* %59)
br label %61
61: ; preds = %58, %38
%62 = load i64, i64* %8
store i64 %62, i64* %3
br label %63
63: ; preds = %61, %22
%64 = load i64, i64* %3
ret i64 %64
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ls\00"
define i64 @u8_vprintf(i8* nocapture %0, i32 %1) {
%3 = tail call i64 @alloca(i64 512)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @vsnprintf(i8* %4, i64 512, i8* %0, i32 %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = icmp ugt i64 %5, 511
%9 = add nuw i64 %5, 1
br i1 %8, label %10, label %14
10: ; preds = %7
%11 = tail call i64 @malloc(i64 %9)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i64 @vsnprintf(i8* %12, i64 %9, i8* %0, i32 %1)
br label %14
14: ; preds = %7, %10
%15 = phi i8* [ %12, %10 ], [ %4, %7 ]
%16 = shl i64 %9, 3
%17 = tail call i64 @alloca(i64 %16)
%18 = inttoptr i64 %17 to i64*
%19 = tail call i64 @u8_toucs(i64* %18, i64 %9, i8* %15, i64 %5)
%20 = getelementptr inbounds i64, i64* %18, i64 %19
store i64 0, i64* %20
%21 = inttoptr i64 %17 to i32*
%22 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %21)
br i1 %8, label %23, label %25
23: ; preds = %14
%24 = tail call i32 @free(i8* %15)
br label %25
25: ; preds = %14, %23, %2
%26 = phi i64 [ 0, %2 ], [ %19, %23 ], [ %19, %14 ]
ret i64 %26
}