#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;

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ wcstombs(char *, int /*<<< orphan*/ const *, int);
const char *my_wcstombs(const wchar_t *wstr) {
  static struct {
    char *str;
    int len;
  } buf;
  int needed = wcstombs(0, wstr, 0) + 1;
  if (needed > buf.len) {
    buf.str = malloc(needed);
    buf.len = needed;
  }
  wcstombs(buf.str, wstr, needed);
  buf.str[needed - 1] = 0;
  return buf.str;
}


x86 assembly (clang, -O0):
<my_wcstombs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1040 <wcstombs@plt>
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    0x2ef6(%rip),%eax
jle    1168 <my_wcstombs+0x48>
mov    -0xc(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,0x2ed9(%rip)
mov    -0xc(%rbp),%eax
mov    %eax,0x2ed8(%rip)
mov    0x2ec9(%rip),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <wcstombs@plt>
mov    0x2eb6(%rip),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2ea2(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_wcstombs>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <wcstombs@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%ebp
cmp    0x2f02(%rip),%eax
jge    1149 <my_wcstombs+0x29>
mov    0x2ef1(%rip),%rdi
jmp    1160 <my_wcstombs+0x40>
mov    %ebp,%edi
call   1030 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ede(%rip)
mov    %ebp,0x2ee0(%rip)
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <wcstombs@plt>
mov    0x2ec7(%rip),%rax
movslq %ebx,%rcx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { i8*, i32 }
@my_wcstombs.buf = internal global %struct.anon zeroinitializer
define i8* @my_wcstombs(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
store i32* %0, i32** %2
%4 = load i32*, i32** %2
%5 = call i32 @wcstombs(i8* null, i32* %4, i32 0)
%6 = add nsw i32 %5, 1
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @my_wcstombs.buf, i32 0, i32 1)
%9 = icmp sgt i32 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** getelementptr inbounds (%struct.anon, %struct.anon* @my_wcstombs.buf, i32 0, i32 0)
%13 = load i32, i32* %3
store i32 %13, i32* getelementptr inbounds (%struct.anon, %struct.anon* @my_wcstombs.buf, i32 0, i32 1)
br label %14
14: ; preds = %10, %1
%15 = load i8*, i8** getelementptr inbounds (%struct.anon, %struct.anon* @my_wcstombs.buf, i32 0, i32 0)
%16 = load i32*, i32** %2
%17 = load i32, i32* %3
%18 = call i32 @wcstombs(i8* %15, i32* %16, i32 %17)
%19 = load i8*, i8** getelementptr inbounds (%struct.anon, %struct.anon* @my_wcstombs.buf, i32 0, i32 0)
%20 = load i32, i32* %3
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** getelementptr inbounds (%struct.anon, %struct.anon* @my_wcstombs.buf, i32 0, i32 0)
ret i8* %24
}

LLVM IR (-O2):
@my_wcstombs.buf.0 = internal unnamed_addr global i8* null
@my_wcstombs.buf.1 = internal unnamed_addr global i32 0
define i8* @my_wcstombs(i32* %0) {
%2 = tail call i32 @wcstombs(i8* null, i32* %0, i32 0)
%3 = add nsw i32 %2, 1
%4 = load i32, i32* @my_wcstombs.buf.1
%5 = icmp slt i32 %2, %4
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i8*, i8** @my_wcstombs.buf.0
br label %10
8: ; preds = %1
%9 = tail call i8* @malloc(i32 %3)
store i8* %9, i8** @my_wcstombs.buf.0
store i32 %3, i32* @my_wcstombs.buf.1
br label %10
10: ; preds = %6, %8
%11 = phi i8* [ %7, %6 ], [ %9, %8 ]
%12 = tail call i32 @wcstombs(i8* %11, i32* %0, i32 %3)
%13 = load i8*, i8** @my_wcstombs.buf.0
%14 = sext i32 %2 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
store i8 0, i8* %15
ret i8* %13
}