#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;
struct nuts_lv {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ nuts_lv_set(struct nuts_lv *, char *, size_t);
size_t wcstombs(char *, int /*<<< orphan*/ const *, size_t);
int nuts_lv_set_wstr(struct nuts_lv *lv, const wchar_t *wstr) {
  size_t n = wcstombs(NULL, wstr, 0);
  if (n == (size_t)-1) {
    return -1;
  }
  char buf[n];
  n = wcstombs(buf, wstr, n);
  if (n == (size_t)-1) {
    return -1;
  }
  nuts_lv_set(lv, buf, n);
  return 0;
}


x86 assembly (clang, -O0):
<nuts_lv_set_wstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1040 <wcstombs@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    115b <nuts_lv_set_wstr+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <nuts_lv_set_wstr+0xb7>
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <wcstombs@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11b1 <nuts_lv_set_wstr+0x91>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11d0 <nuts_lv_set_wstr+0xb0>
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
call   1030 <nuts_lv_set@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nuts_lv_set_wstr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
xor    %edx,%edx
call   1040 <wcstombs@plt>
cmp    $0xffffffffffffffff,%rax
je     117d <nuts_lv_set_wstr+0x5d>
mov    %rsp,%r12
mov    %rsp,%rbx
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <wcstombs@plt>
cmp    $0xffffffffffffffff,%rax
je     1184 <nuts_lv_set_wstr+0x64>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <nuts_lv_set@plt>
xor    %eax,%eax
jmp    1189 <nuts_lv_set_wstr+0x69>
mov    $0xffffffff,%eax
jmp    118c <nuts_lv_set_wstr+0x6c>
mov    $0xffffffff,%eax
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nuts_lv = type { i32 }
define i32 @nuts_lv_set_wstr(%struct.nuts_lv* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.nuts_lv*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store %struct.nuts_lv* %0, %struct.nuts_lv** %4
store i32* %1, i32** %5
%10 = load i32*, i32** %5
%11 = call i64 @wcstombs(i8* null, i32* %10, i64 0)
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %31
15: ; preds = %2
%16 = load i64, i64* %6
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %7
%18 = alloca i8, i64 %16
store i64 %16, i64* %8
%19 = load i32*, i32** %5
%20 = load i64, i64* %6
%21 = call i64 @wcstombs(i8* %18, i32* %19, i64 %20)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %15
store i32 -1, i32* %3
store i32 1, i32* %9
br label %29
25: ; preds = %15
%26 = load %struct.nuts_lv*, %struct.nuts_lv** %4
%27 = load i64, i64* %6
%28 = call i32 @nuts_lv_set(%struct.nuts_lv* %26, i8* %18, i64 %27)
store i32 0, i32* %3
store i32 1, i32* %9
br label %29
29: ; preds = %25, %24
%30 = load i8*, i8** %7
br label %31
31: ; preds = %29, %14
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.nuts_lv = type { i32 }
define i32 @nuts_lv_set_wstr(%struct.nuts_lv* %0, i32* %1) {
%3 = tail call i64 @wcstombs(i8* null, i32* %1, i64 0)
%4 = icmp eq i64 %3, -1
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %3
%8 = call i64 @wcstombs(i8* %7, i32* %1, i64 %3)
%9 = icmp eq i64 %8, -1
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = call i32 @nuts_lv_set(%struct.nuts_lv* %0, i8* %7, i64 %8)
br label %12
12: ; preds = %5, %10
%13 = phi i32 [ 0, %10 ], [ -1, %5 ]
br label %14
14: ; preds = %2, %12
%15 = phi i32 [ %13, %12 ], [ -1, %2 ]
ret i32 %15
}