#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 */
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
size_t wcstombs(char *, int /*<<< orphan*/ const *, size_t);
char *wcsdupstr(const wchar_t *path) {
  char *mbs;
  size_t len = wcstombs(NULL, path, 0);
  if (len == (size_t)-1)
    return NULL;
  mbs = malloc(len);
  len = wcstombs(mbs, path, len);
  if (len == (size_t)-1) {
    free(mbs);
    return NULL;
  }
  return mbs;
}


x86 assembly (clang, -O0):
<wcsdupstr>:
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,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1168 <wcsdupstr+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <wcsdupstr+0x83>
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <wcstombs@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11ab <wcsdupstr+0x7b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <wcsdupstr+0x83>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcsdupstr>:
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     117a <wcsdupstr+0x4a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <wcstombs@plt>
cmp    $0xffffffffffffffff,%rax
jne    117a <wcsdupstr+0x4a>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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