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

/* Variables and functions */
int /*<<< orphan*/ CODESET;
#define E2BIG 128
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int iconv(int /*<<< orphan*/, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(int /*<<< orphan*/);
int /*<<< orphan*/ iconv_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ nl_langinfo(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int passwd_to_utf16(unsigned char *in_passwd, int length, int max_length,
                    unsigned char *out_passwd) {
  unsigned char *ic_outbuf, *ic_inbuf;
  iconv_t condesc;
  size_t ic_inbytesleft, ic_outbytesleft;
  max_length *= 2;
  ic_inbuf = in_passwd;
  ic_inbytesleft = length;
  ic_outbytesleft = max_length;
  ic_outbuf = out_passwd;
  if ((condesc = iconv_open("UTF-16LE", nl_langinfo(CODESET))) ==
      (iconv_t)(-1)) {
    perror("Error in iconv_open");
    return -1;
  }
  if (iconv(condesc, (char **)&ic_inbuf, &ic_inbytesleft, (char **)&ic_outbuf,
            &ic_outbytesleft) == -1) {
    switch (errno) {
    case E2BIG:
      fprintf(stderr, "Error: password too long\n");
      iconv_close(condesc);
      return -1;
      break;
    default:
      fprintf(stderr, "Error: Invalid or incomplete multibyte sequence\n");
      iconv_close(condesc);
      return -1;
    }
  }
  iconv_close(condesc);
  return (max_length - ic_outbytesleft);
}


x86 assembly (clang, -O0):
<passwd_to_utf16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movslq -0x14(%rbp),%rax
mov    %rax,-0x40(%rbp)
movslq -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1050 <nl_langinfo@plt>
mov    %eax,%esi
lea    0xe4a(%rip),%rdi
call   1080 <iconv_open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11df <passwd_to_utf16+0x7f>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <passwd_to_utf16+0x11e>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1030 <iconv@plt>
cmp    $0xffffffff,%eax
jne    126b <passwd_to_utf16+0x10b>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1242 <passwd_to_utf16+0xe2>
jmp    1219 <passwd_to_utf16+0xb9>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1060 <iconv_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <passwd_to_utf16+0x11e>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1060 <iconv_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <passwd_to_utf16+0x11e>
mov    -0x34(%rbp),%edi
call   1060 <iconv_close@plt>
movslq -0x18(%rbp),%rax
sub    -0x48(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<passwd_to_utf16>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
add    %edx,%ebx
mov    %rdi,0x18(%rsp)
movslq %esi,%rax
mov    %rax,0x10(%rsp)
movslq %ebx,%rax
mov    %rax,0x8(%rsp)
mov    %rcx,0x20(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1050 <nl_langinfo@plt>
lea    0xe67(%rip),%rdi
mov    %eax,%esi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     1216 <passwd_to_utf16+0xb6>
mov    %eax,%ebp
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %eax,%edi
call   1030 <iconv@plt>
cmp    $0xffffffff,%eax
je     11d4 <passwd_to_utf16+0x74>
mov    %ebp,%edi
call   1060 <iconv_close@plt>
sub    0x8(%rsp),%ebx
jmp    120d <passwd_to_utf16+0xad>
mov    0x2df5(%rip),%rax
cmpl   $0x80,(%rax)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
jne    11f5 <passwd_to_utf16+0x95>
lea    0xe2a(%rip),%rsi
jmp    11fc <passwd_to_utf16+0x9c>
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
mov    %ebp,%edi
call   1060 <iconv_close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
jmp    1208 <passwd_to_utf16+0xa8>

LLVM IR (-O0):
@.str = [9 x i8] c"UTF-16LE\00"
@CODESET = global i32 0
@.str.1 = [20 x i8] c"Error in iconv_open\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error: password too long\0A\00"
@.str.3 = [49 x i8] c"Error: Invalid or incomplete multibyte sequence\0A\00"
define i32 @passwd_to_utf16(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%15 = load i32, i32* %8
%16 = mul nsw i32 %15, 2
store i32 %16, i32* %8
%17 = load i8*, i8** %6
store i8* %17, i8** %11
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
store i64 %19, i64* %13
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
store i64 %21, i64* %14
%22 = load i8*, i8** %9
store i8* %22, i8** %10
%23 = load i32, i32* @CODESET
%24 = call i32 @nl_langinfo(i32 %23)
%25 = call i32 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 %25, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %4
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %53
29: ; preds = %4
%30 = load i32, i32* %12
%31 = call i32 @iconv(i32 %30, i8** %11, i64* %13, i8** %10, i64* %14)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %45
33: ; preds = %29
%34 = load i32, i32* @errno
switch i32 %34, label %40 [
i32 128, label %35
]
35: ; preds = %33
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %12
%39 = call i32 @iconv_close(i32 %38)
store i32 -1, i32* %5
br label %53
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* %12
%44 = call i32 @iconv_close(i32 %43)
store i32 -1, i32* %5
br label %53
45: ; preds = %29
%46 = load i32, i32* %12
%47 = call i32 @iconv_close(i32 %46)
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = load i64, i64* %14
%51 = sub i64 %49, %50
%52 = trunc i64 %51 to i32
store i32 %52, i32* %5
br label %53
53: ; preds = %45, %40, %35, %27
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@.str = [9 x i8] c"UTF-16LE\00"
@CODESET = global i32 0
@.str.1 = [20 x i8] c"Error in iconv_open\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error: password too long\0A\00"
@.str.3 = [49 x i8] c"Error: Invalid or incomplete multibyte sequence\0A\00"
define i32 @passwd_to_utf16(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i8** %5 to i8*
%10 = bitcast i8** %6 to i8*
%11 = bitcast i64* %7 to i8*
%12 = bitcast i64* %8 to i8*
%13 = shl nsw i32 %2, 1
store i8* %0, i8** %6
%14 = sext i32 %1 to i64
store i64 %14, i64* %7
%15 = sext i32 %13 to i64
store i64 %15, i64* %8
store i8* %3, i8** %5
%16 = load i32, i32* @CODESET
%17 = tail call i32 @nl_langinfo(i32 %16)
%18 = tail call i32 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %40
22: ; preds = %4
%23 = call i32 @iconv(i32 %18, i8** %6, i64* %7, i8** %5, i64* %8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = icmp eq i32 %26, 128
%28 = load i32, i32* @stderr
br i1 %27, label %29, label %32
29: ; preds = %25
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @iconv_close(i32 %18)
br label %40
32: ; preds = %25
%33 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @iconv_close(i32 %18)
br label %40
35: ; preds = %22
%36 = call i32 @iconv_close(i32 %18)
%37 = load i64, i64* %8
%38 = trunc i64 %37 to i32
%39 = sub i32 %13, %38
br label %40
40: ; preds = %35, %32, %29, %20
%41 = phi i32 [ -1, %20 ], [ -1, %29 ], [ -1, %32 ], [ %39, %35 ]
ret i32 %41
}