#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*/ st;
typedef int /*<<< orphan*/ mbstate_t;
typedef int /*<<< orphan*/ __gnuc_va_list;

/* Variables and functions */
size_t __mbsrtowcs(int /*<<< orphan*/ *, char const **, size_t,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ __vfwprintf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/);
scalar_t__ alloca(size_t);
int /*<<< orphan*/ fputws_unlocked(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
scalar_t__ realloc(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ stderr;
int strlen(char const *);
void convert_and_print(const char *format, __gnuc_va_list ap) {
  size_t len;
  wchar_t *wformat = NULL;
  mbstate_t st;
  size_t res;
  const char *tmp;
  if (format == NULL)
    return;
  len = strlen(format) + 1;
  do {
    if (len < 2000)
      wformat = (wchar_t *)alloca(len * sizeof(wchar_t));
    else {
      if (wformat != NULL && len / 2 < 2000)
        wformat = NULL;
      wformat = (wchar_t *)realloc(wformat, len * sizeof(wchar_t));
      if (wformat == NULL) {
        fputws_unlocked(L"out of memory\n", stderr);
        return;
      }
    }
    memset(&st, '\0', sizeof(st));
    tmp = format;
  } while ((res = __mbsrtowcs(wformat, &tmp, len, &st)) == len);
  if (res == (size_t)-1)
    wformat = (wchar_t *)L"???";
  __vfwprintf(stderr, wformat, ap);
}


x86 assembly (clang, -O0):
<convert_and_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1197 <convert_and_print+0x27>
jmp    1299 <convert_and_print+0x129>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x7d0,-0x18(%rbp)
jae    11cd <convert_and_print+0x5d>
mov    -0x18(%rbp),%rdi
shl    $0x2,%rdi
call   1070 <alloca@plt>
mov    %rax,-0x20(%rbp)
jmp    1233 <convert_and_print+0xc3>
cmpq   $0x0,-0x20(%rbp)
je     11f4 <convert_and_print+0x84>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
cmp    $0x7d0,%rax
jae    11f4 <convert_and_print+0x84>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x2,%rsi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122e <convert_and_print+0xbe>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xddc(%rip),%rdi
call   1030 <fputws_unlocked@plt>
jmp    1299 <convert_and_print+0x129>
jmp    1233 <convert_and_print+0xc3>
lea    -0x24(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x38(%rbp),%rsi
lea    -0x24(%rbp),%rcx
call   1050 <__mbsrtowcs@plt>
mov    %rax,-0x30(%rbp)
cmp    -0x18(%rbp),%rax
je     11a9 <convert_and_print+0x39>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1284 <convert_and_print+0x114>
lea    0xdbc(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1090 <__vfwprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_and_print>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     1261 <convert_and_print+0xf1>
mov    %rdi,%r12
mov    %esi,0x4(%rsp)
call   1040 <strlen@plt>
movslq %eax,%rbp
lea    0x4(,%rbp,4),%r15
add    $0x1,%rbp
lea    0x14(%rsp),%rbx
lea    0x8(%rsp),%r13
xor    %r14d,%r14d
jmp    11f5 <convert_and_print+0x85>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <alloca@plt>
mov    %rax,%r14
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %r12,0x8(%rsp)
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1050 <__mbsrtowcs@plt>
cmp    %rbp,%rax
jne    123d <convert_and_print+0xcd>
cmp    $0x7cf,%ebp
jbe    11c0 <convert_and_print+0x50>
cmp    $0xfa0,%ebp
mov    %r14,%rdi
mov    $0x0,%eax
cmovb  %rax,%rdi
test   %r14,%r14
cmove  %r14,%rdi
mov    %r15,%rsi
call   1080 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11cb <convert_and_print+0x5b>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    0xdca(%rip),%rdi
call   1030 <fputws_unlocked@plt>
jmp    1261 <convert_and_print+0xf1>
cmp    $0xffffffffffffffff,%rax
lea    0xdf4(%rip),%rax
cmove  %rax,%r14
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    0x4(%rsp),%edx
call   1090 <__vfwprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i32] [i32 111, i32 117, i32 116, i32 32, i32 111, i32 102, i32 32, i32 109, i32 101, i32 109, i32 111, i32 114, i32 121, i32 10, i32 0]
@stderr = global i32 0
@.str.1 = [4 x i32] [i32 63, i32 63, i32 63, i32 0]
define void @convert_and_print(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32* null, i32** %6
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
br label %64
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
store i64 %17, i64* %5
br label %18
18: ; preds = %49, %13
%19 = load i64, i64* %5
%20 = icmp ult i64 %19, 2000
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i64, i64* %5
%23 = mul i64 %22, 4
%24 = call i64 @alloca(i64 %23)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %6
br label %46
26: ; preds = %18
%27 = load i32*, i32** %6
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i64, i64* %5
%31 = udiv i64 %30, 2
%32 = icmp ult i64 %31, 2000
br i1 %32, label %33, label %34
33: ; preds = %29
store i32* null, i32** %6
br label %34
34: ; preds = %33, %29, %26
%35 = load i32*, i32** %6
%36 = load i64, i64* %5
%37 = mul i64 %36, 4
%38 = call i64 @realloc(i32* %35, i64 %37)
%39 = inttoptr i64 %38 to i32*
store i32* %39, i32** %6
%40 = load i32*, i32** %6
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %45
42: ; preds = %34
%43 = load i32, i32* @stderr
%44 = call i32 @fputws_unlocked(i8* bitcast ([15 x i32]* @.str to i8*), i32 %43)
br label %64
45: ; preds = %34
br label %46
46: ; preds = %45, %21
%47 = call i32 @memset(i32* %7, i8 signext 0, i32 4)
%48 = load i8*, i8** %3
store i8* %48, i8** %9
br label %49
49: ; preds = %46
%50 = load i32*, i32** %6
%51 = load i64, i64* %5
%52 = call i64 @__mbsrtowcs(i32* %50, i8** %9, i64 %51, i32* %7)
store i64 %52, i64* %8
%53 = load i64, i64* %5
%54 = icmp eq i64 %52, %53
br i1 %54, label %18, label %55
55: ; preds = %49
%56 = load i64, i64* %8
%57 = icmp eq i64 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %55
store i32* getelementptr inbounds ([4 x i32], [4 x i32]* @.str.1, i64 0, i64 0), i32** %6
br label %59
59: ; preds = %58, %55
%60 = load i32, i32* @stderr
%61 = load i32*, i32** %6
%62 = load i32, i32* %4
%63 = call i32 @__vfwprintf(i32 %60, i32* %61, i32 %62)
br label %64
64: ; preds = %59, %42, %12
ret void
}

LLVM IR (-O2):
@.str = [15 x i32] [i32 111, i32 117, i32 116, i32 32, i32 111, i32 102, i32 32, i32 109, i32 101, i32 109, i32 111, i32 114, i32 121, i32 10, i32 0]
@stderr = global i32 0
@.str.1 = [4 x i32] [i32 63, i32 63, i32 63, i32 0]
define void @convert_and_print(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = bitcast i32* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = icmp eq i8* %0, null
br i1 %7, label %40, label %8
8: ; preds = %2
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = icmp ult i32 %10, 2000
%13 = icmp ult i32 %10, 4000
%14 = shl nsw i64 %11, 2
br label %15
15: ; preds = %28, %8
%16 = phi i32* [ null, %8 ], [ %30, %28 ]
br i1 %12, label %17, label %19
17: ; preds = %15
%18 = call i64 @alloca(i64 %14)
br label %28
19: ; preds = %15
%20 = icmp eq i32* %16, null
%21 = select i1 %20, i1 true, i1 %13
%22 = select i1 %21, i32* null, i32* %16
%23 = call i64 @realloc(i32* %22, i64 %14)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 @fputws_unlocked(i8* bitcast ([15 x i32]* @.str to i8*), i32 %26)
br label %40
28: ; preds = %19, %17
%29 = phi i64 [ %18, %17 ], [ %23, %19 ]
%30 = inttoptr i64 %29 to i32*
%31 = call i32 @memset(i32* %3, i8 signext 0, i32 4)
store i8* %0, i8** %4
%32 = call i64 @__mbsrtowcs(i32* %30, i8** %4, i64 %11, i32* %3)
%33 = icmp eq i64 %32, %11
br i1 %33, label %15, label %34
34: ; preds = %28
%35 = inttoptr i64 %29 to i32*
%36 = icmp eq i64 %32, -1
%37 = select i1 %36, i32* getelementptr inbounds ([4 x i32], [4 x i32]* @.str.1, i64 0, i64 0), i32* %35
%38 = load i32, i32* @stderr
%39 = call i32 @__vfwprintf(i32 %38, i32* %37, i32 %1)
br label %40
40: ; preds = %2, %34, %25
ret void
}