#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 */
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
int wcslen(int /*<<< orphan*/ *);
int /*<<< orphan*/ wcstombs(char *, int /*<<< orphan*/ *, size_t);
char *wcs2mbs(wchar_t *wcs) {
  typedef struct {
    char *mbs;
    size_t size;
  } buf_t;
  static buf_t bufs[32] = {
      {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
      {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
      {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
      {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
  static int bufn = 0;
  buf_t *buf = bufs + bufn++;
  if (bufn == 32)
    bufn = 0;
  size_t len = 6 * wcslen(wcs) + 1;
  if (buf->size < len) {
    buf->mbs = buf->mbs ? (char *)realloc(buf->mbs, len) : (char *)malloc(len);
    buf->size = len;
  }
  wcstombs(buf->mbs, wcs, buf->size);
  return buf->mbs;
}


x86 assembly (clang, -O0):
<wcs2mbs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x30fe(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x30f3(%rip)
movslq %eax,%rcx
lea    0x2ee9(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x20,0x30d7(%rip)
jne    1189 <wcs2mbs+0x49>
movl   $0x0,0x30c7(%rip)
mov    -0x8(%rbp),%rdi
call   1030 <wcslen@plt>
imul   $0x6,%eax,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0x18(%rbp),%rax
jae    11fb <wcs2mbs+0xbb>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d7 <wcs2mbs+0x97>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11e4 <wcs2mbs+0xa4>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1060 <wcstombs@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcs2mbs>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
movslq 0x30fd(%rip),%rbx
lea    0x1(%rbx),%eax
shl    $0x4,%rbx
lea    0x2eef(%rip),%r13
lea    (%rbx,%r13,1),%r15
xor    %ecx,%ecx
cmp    $0x20,%eax
cmovne %eax,%ecx
mov    %ecx,0x30dd(%rip)
call   1030 <wcslen@plt>
add    %eax,%eax
lea    (%rax,%rax,2),%eax
or     $0x1,%eax
movslq %eax,%r12
mov    (%rbx,%r13,1),%rdi
mov    0x8(%rbx,%r13,1),%rax
cmp    %r12,%rax
jae    11a7 <wcs2mbs+0x67>
add    %r13,%rbx
add    $0x8,%rbx
test   %rdi,%rdi
je     11ac <wcs2mbs+0x6c>
mov    %r12,%rsi
call   1050 <realloc@plt>
jmp    11b4 <wcs2mbs+0x74>
mov    %rax,%r12
jmp    11bd <wcs2mbs+0x7d>
mov    %r12,%rdi
call   1040 <malloc@plt>
mov    %rax,%rdi
mov    %rax,(%r15)
mov    %r12,(%rbx)
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <wcstombs@plt>
mov    (%r15),%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.buf_t = type { i8*, i64 }
@wcs2mbs.bufs = internal global [32 x %struct.buf_t] zeroinitializer
@wcs2mbs.bufn = internal global i32 0
define i8* @wcs2mbs(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.buf_t*
%4 = alloca i64
store i32* %0, i32** %2
%5 = load i32, i32* @wcs2mbs.bufn
%6 = add nsw i32 %5, 1
store i32 %6, i32* @wcs2mbs.bufn
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds %struct.buf_t, %struct.buf_t* getelementptr inbounds ([32 x %struct.buf_t], [32 x %struct.buf_t]* @wcs2mbs.bufs, i64 0, i64 0), i64 %7
store %struct.buf_t* %8, %struct.buf_t** %3
%9 = load i32, i32* @wcs2mbs.bufn
%10 = icmp eq i32 %9, 32
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* @wcs2mbs.bufn
br label %12
12: ; preds = %11, %1
%13 = load i32*, i32** %2
%14 = call i32 @wcslen(i32* %13)
%15 = mul nsw i32 6, %14
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
store i64 %17, i64* %4
%18 = load %struct.buf_t*, %struct.buf_t** %3
%19 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %18, i32 0, i32 1
%20 = load i64, i64* %19
%21 = load i64, i64* %4
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %46
23: ; preds = %12
%24 = load %struct.buf_t*, %struct.buf_t** %3
%25 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load %struct.buf_t*, %struct.buf_t** %3
%30 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i64, i64* %4
%33 = call i64 @realloc(i8* %31, i64 %32)
%34 = inttoptr i64 %33 to i8*
br label %39
35: ; preds = %23
%36 = load i64, i64* %4
%37 = call i64 @malloc(i64 %36)
%38 = inttoptr i64 %37 to i8*
br label %39
39: ; preds = %35, %28
%40 = phi i8* [ %34, %28 ], [ %38, %35 ]
%41 = load %struct.buf_t*, %struct.buf_t** %3
%42 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %41, i32 0, i32 0
store i8* %40, i8** %42
%43 = load i64, i64* %4
%44 = load %struct.buf_t*, %struct.buf_t** %3
%45 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %44, i32 0, i32 1
store i64 %43, i64* %45
br label %46
46: ; preds = %39, %12
%47 = load %struct.buf_t*, %struct.buf_t** %3
%48 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = load i32*, i32** %2
%51 = load %struct.buf_t*, %struct.buf_t** %3
%52 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %51, i32 0, i32 1
%53 = load i64, i64* %52
%54 = call i32 @wcstombs(i8* %49, i32* %50, i64 %53)
%55 = load %struct.buf_t*, %struct.buf_t** %3
%56 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %55, i32 0, i32 0
%57 = load i8*, i8** %56
ret i8* %57
}

LLVM IR (-O2):
%struct.buf_t = type { i8*, i64 }
@wcs2mbs.bufs = internal unnamed_addr global [32 x %struct.buf_t] zeroinitializer
@wcs2mbs.bufn = internal unnamed_addr global i32 0
define i8* @wcs2mbs(i32* %0) {
%2 = load i32, i32* @wcs2mbs.bufn
%3 = add nsw i32 %2, 1
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds [32 x %struct.buf_t], [32 x %struct.buf_t]* @wcs2mbs.bufs, i64 0, i64 %4
%6 = icmp eq i32 %3, 32
%7 = select i1 %6, i32 0, i32 %3
store i32 %7, i32* @wcs2mbs.bufn
%8 = tail call i32 @wcslen(i32* %0)
%9 = mul nsw i32 %8, 6
%10 = or i32 %9, 1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [32 x %struct.buf_t], [32 x %struct.buf_t]* @wcs2mbs.bufs, i64 0, i64 %4, i32 1
%13 = load i64, i64* %12
%14 = icmp ult i64 %13, %11
%15 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %5, i64 0, i32 0
%16 = load i8*, i8** %15
br i1 %14, label %17, label %26
17: ; preds = %1
%18 = icmp eq i8* %16, null
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i64 @realloc(i8* %16, i64 %11)
br label %23
21: ; preds = %17
%22 = tail call i64 @malloc(i64 %11)
br label %23
23: ; preds = %21, %19
%24 = phi i64 [ %20, %19 ], [ %22, %21 ]
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %15
store i64 %11, i64* %12
br label %26
26: ; preds = %1, %23
%27 = phi i64 [ %11, %23 ], [ %13, %1 ]
%28 = phi i8* [ %25, %23 ], [ %16, %1 ]
%29 = getelementptr inbounds %struct.buf_t, %struct.buf_t* %5, i64 0, i32 0
%30 = tail call i32 @wcstombs(i8* %28, i32* %0, i64 %27)
%31 = load i8*, i8** %29
ret i8* %31
}