#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 wchar_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
size_t wcstombs(char *, int *, size_t);
int wc_to_key(wchar_t wc, char *key, size_t keysz) {
  wchar_t wcs[2];
  size_t written;
  if (!key || keysz != sizeof(wchar_t) + 1)
    return -1;
  wcs[0] = wc;
  wcs[1] = L'\0';
  written = wcstombs(key, wcs, keysz);
  if (!written || written > sizeof(wchar_t) || written == (size_t)-1) {
    perror("wcstombs failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<wc_to_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <wc_to_key+0x29>
cmpq   $0x5,-0x18(%rbp)
je     1155 <wc_to_key+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <wc_to_key+0x97>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <wcstombs@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1198 <wc_to_key+0x78>
cmpq   $0x4,-0x28(%rbp)
ja     1198 <wc_to_key+0x78>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11b0 <wc_to_key+0x90>
lea    0xe61(%rip),%rdi
call   1030 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <wc_to_key+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wc_to_key>:
push   %rax
mov    $0xffffffff,%eax
test   %rsi,%rsi
je     115e <wc_to_key+0x3e>
cmp    $0x5,%rdx
jne    115e <wc_to_key+0x3e>
mov    %edi,(%rsp)
movl   $0x0,0x4(%rsp)
mov    %rsp,%rax
mov    $0x5,%edx
mov    %rsi,%rdi
mov    %rax,%rsi
call   1040 <wcstombs@plt>
mov    %rax,%rcx
add    $0xfffffffffffffffb,%rcx
xor    %eax,%eax
cmp    $0xfffffffffffffffb,%rcx
jbe    1160 <wc_to_key+0x40>
pop    %rcx
ret
lea    0xe99(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"wcstombs failed\00"
define i32 @wc_to_key(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [2 x i32]
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i64, i64* %7
%14 = icmp ne i64 %13, 5
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 -1, i32* %4
br label %35
16: ; preds = %12
%17 = load i32, i32* %5
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
store i32 %17, i32* %18
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
store i32 0, i32* %19
%20 = load i8*, i8** %6
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%22 = load i64, i64* %7
%23 = call i64 @wcstombs(i8* %20, i32* %21, i64 %22)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %16
%27 = load i64, i64* %9
%28 = icmp ugt i64 %27, 4
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i64, i64* %9
%31 = icmp eq i64 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29, %26, %16
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
34: ; preds = %29
store i32 0, i32* %4
br label %35
35: ; preds = %34, %32, %15
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [16 x i8] c"wcstombs failed\00"
define i32 @wc_to_key(i32 %0, i8* %1, i64 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = icmp eq i8* %1, null
%7 = icmp ne i64 %2, 5
%8 = or i1 %6, %7
br i1 %8, label %17, label %9
9: ; preds = %3
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
store i32 %0, i32* %10
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
store i32 0, i32* %11
%12 = call i64 @wcstombs(i8* %1, i32* %10, i64 5)
%13 = add i64 %12, -5
%14 = icmp ult i64 %13, -4
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %9, %3, %15
%18 = phi i32 [ -1, %15 ], [ -1, %3 ], [ 0, %9 ]
ret i32 %18
}