#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*/ mbstate_t;

/* Variables and functions */
char *malloc(size_t);
size_t wcsrtombs(char *, int /*<<< orphan*/ const **, size_t,
                 int /*<<< orphan*/ *);
char *__mbsdup(const wchar_t *ws) {
  static const mbstate_t initial;
  mbstate_t st;
  const wchar_t *wcp;
  size_t len;
  char *mbs;
  wcp = ws;
  st = initial;
  if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1)
    return (NULL);
  if ((mbs = malloc(len + 1)) == NULL)
    return (NULL);
  st = initial;
  wcsrtombs(mbs, &ws, len + 1, &st);
  return (mbs);
}


x86 assembly (clang, -O0):
<__mbsdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0xec6(%rip),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
lea    -0x14(%rbp),%rcx
mov    %rdx,%rdi
call   1030 <wcsrtombs@plt>
mov    %rax,-0x28(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    116c <__mbsdup+0x4c>
movq   $0x0,-0x8(%rbp)
jmp    11be <__mbsdup+0x9e>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1194 <__mbsdup+0x74>
movq   $0x0,-0x8(%rbp)
jmp    11be <__mbsdup+0x9e>
mov    0xe66(%rip),%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rcx
call   1030 <wcsrtombs@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mbsdup>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x10(%rsp)
mov    %rdi,0x8(%rsp)
movl   $0x0,0x4(%rsp)
xor    %r14d,%r14d
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rcx
xor    %edi,%edi
xor    %edx,%edx
call   1030 <wcsrtombs@plt>
cmp    $0xffffffffffffffff,%rax
je     118e <__mbsdup+0x6e>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     118b <__mbsdup+0x6b>
mov    %rax,%r14
movl   $0x0,0x4(%rsp)
lea    0x10(%rsp),%rsi
lea    0x4(%rsp),%rcx
mov    %rax,%rdi
mov    %rbx,%rdx
call   1030 <wcsrtombs@plt>
jmp    118e <__mbsdup+0x6e>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @__mbsdup(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
store i32* %0, i32** %2
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32** %4 to i8*
store i32* %0, i32** %4
store i32 0, i32* %3
%7 = call i64 @wcsrtombs(i8* null, i32** %4, i64 0, i32* %3)
%8 = icmp eq i64 %7, -1
br i1 %8, label %15, label %9
9: ; preds = %1
%10 = add nuw i64 %7, 1
%11 = call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %9
store i32 0, i32* %3
%14 = call i64 @wcsrtombs(i8* %11, i32** %2, i64 %10, i32* %3)
br label %15
15: ; preds = %9, %1, %13
%16 = phi i8* [ %11, %13 ], [ null, %1 ], [ null, %9 ]
ret i8* %16
}