#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 */
size_t wcstombs(char *, int /*<<< orphan*/ const *, size_t);
int /*<<< orphan*/ xfree(char *);
char *xmalloc(size_t);
char *wcs_duplicate_as_mbs(const wchar_t *str) {
  size_t bytelen;
  char *output;
  bytelen = wcstombs(NULL, str, 0);
  if (bytelen == (size_t)-1) {
    return (NULL);
  }
  output = xmalloc(bytelen + 1);
  bytelen = wcstombs(output, str, bytelen + 1);
  if (bytelen == (size_t)-1) {
    xfree(output);
    return (NULL);
  }
  return (output);
}


x86 assembly (clang, -O0):
<wcs_duplicate_as_mbs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1050 <wcstombs@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1168 <wcs_duplicate_as_mbs+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11bb <wcs_duplicate_as_mbs+0x8b>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1050 <wcstombs@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11b3 <wcs_duplicate_as_mbs+0x83>
mov    -0x20(%rbp),%rdi
call   1030 <xfree@plt>
movq   $0x0,-0x8(%rbp)
jmp    11bb <wcs_duplicate_as_mbs+0x8b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcs_duplicate_as_mbs>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <wcstombs@plt>
cmp    $0xffffffffffffffff,%rax
je     117e <wcs_duplicate_as_mbs+0x4e>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1040 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <wcstombs@plt>
cmp    $0xffffffffffffffff,%rax
jne    117e <wcs_duplicate_as_mbs+0x4e>
mov    %r15,%rdi
call   1030 <xfree@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @wcs_duplicate_as_mbs(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i64 @wcstombs(i8* null, i32* %6, i64 0)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %27
11: ; preds = %1
%12 = load i64, i64* %4
%13 = add i64 %12, 1
%14 = call i8* @xmalloc(i64 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = load i32*, i32** %3
%17 = load i64, i64* %4
%18 = add i64 %17, 1
%19 = call i64 @wcstombs(i8* %15, i32* %16, i64 %18)
store i64 %19, i64* %4
%20 = load i64, i64* %4
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = load i8*, i8** %5
%24 = call i32 @xfree(i8* %23)
store i8* null, i8** %2
br label %27
25: ; preds = %11
%26 = load i8*, i8** %5
store i8* %26, i8** %2
br label %27
27: ; preds = %25, %22, %10
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
define i8* @wcs_duplicate_as_mbs(i32* %0) {
%2 = tail call i64 @wcstombs(i8* null, i32* %0, i64 0)
%3 = icmp eq i64 %2, -1
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = add nuw i64 %2, 1
%6 = tail call i8* @xmalloc(i64 %5)
%7 = tail call i64 @wcstombs(i8* %6, i32* %0, i64 %5)
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @xfree(i8* %6)
br label %11
11: ; preds = %4, %1, %9
%12 = phi i8* [ null, %9 ], [ null, %1 ], [ %6, %4 ]
ret i8* %12
}