#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(char *in_passwd, int length, int max_length,
                    char *out_passwd) {
  char *ic_outbuf, *ic_inbuf;
  iconv_t condesc;
  size_t ic_inbytesleft, ic_outbytesleft;
  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, &ic_inbuf, &ic_inbytesleft, &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    -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    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1050 <nl_langinfo@plt>
mov    %eax,%esi
lea    0xe53(%rip),%rdi
call   1080 <iconv_open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11d6 <passwd_to_utf16+0x76>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <passwd_to_utf16+0x115>
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    1262 <passwd_to_utf16+0x102>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1239 <passwd_to_utf16+0xd9>
jmp    1210 <passwd_to_utf16+0xb0>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1060 <iconv_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <passwd_to_utf16+0x115>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1060 <iconv_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <passwd_to_utf16+0x115>
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
mov    %rdi,0x18(%rsp)
movslq %esi,%rax
mov    %rax,0x10(%rsp)
movslq %edx,%rax
mov    %rax,0x8(%rsp)
mov    %rcx,0x20(%rsp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1050 <nl_langinfo@plt>
lea    0xe69(%rip),%rdi
mov    %eax,%esi
call   1080 <iconv_open@plt>
cmp    $0xffffffff,%eax
je     1214 <passwd_to_utf16+0xb4>
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     11d2 <passwd_to_utf16+0x72>
mov    %ebp,%edi
call   1060 <iconv_close@plt>
sub    0x8(%rsp),%ebx
jmp    120b <passwd_to_utf16+0xab>
mov    0x2df7(%rip),%rax
cmpl   $0x80,(%rax)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
jne    11f3 <passwd_to_utf16+0x93>
lea    0xe2c(%rip),%rsi
jmp    11fa <passwd_to_utf16+0x9a>
lea    0xe3d(%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    0xdee(%rip),%rdi
call   1070 <perror@plt>
jmp    1206 <passwd_to_utf16+0xa6>

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 i8*, i8** %6
store i8* %15, i8** %11
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
store i64 %17, i64* %13
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
store i64 %19, i64* %14
%20 = load i8*, i8** %9
store i8* %20, i8** %10
%21 = load i32, i32* @CODESET
%22 = call i32 @nl_langinfo(i32 %21)
%23 = call i32 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %12
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %4
%26 = 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 %51
27: ; preds = %4
%28 = load i32, i32* %12
%29 = call i32 @iconv(i32 %28, i8** %11, i64* %13, i8** %10, i64* %14)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %43
31: ; preds = %27
%32 = load i32, i32* @errno
switch i32 %32, label %38 [
i32 128, label %33
]
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %12
%37 = call i32 @iconv_close(i32 %36)
store i32 -1, i32* %5
br label %51
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %12
%42 = call i32 @iconv_close(i32 %41)
store i32 -1, i32* %5
br label %51
43: ; preds = %27
%44 = load i32, i32* %12
%45 = call i32 @iconv_close(i32 %44)
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = load i64, i64* %14
%49 = sub i64 %47, %48
%50 = trunc i64 %49 to i32
store i32 %50, i32* %5
br label %51
51: ; preds = %43, %38, %33, %25
%52 = load i32, i32* %5
ret i32 %52
}

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*
store i8* %0, i8** %6
%13 = sext i32 %1 to i64
store i64 %13, i64* %7
%14 = sext i32 %2 to i64
store i64 %14, i64* %8
store i8* %3, i8** %5
%15 = load i32, i32* @CODESET
%16 = tail call i32 @nl_langinfo(i32 %15)
%17 = tail call i32 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %39
21: ; preds = %4
%22 = call i32 @iconv(i32 %17, i8** %6, i64* %7, i8** %5, i64* %8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = icmp eq i32 %25, 128
%27 = load i32, i32* @stderr
br i1 %26, label %28, label %31
28: ; preds = %24
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @iconv_close(i32 %17)
br label %39
31: ; preds = %24
%32 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @iconv_close(i32 %17)
br label %39
34: ; preds = %21
%35 = call i32 @iconv_close(i32 %17)
%36 = load i64, i64* %8
%37 = trunc i64 %36 to i32
%38 = sub i32 %2, %37
br label %39
39: ; preds = %34, %31, %28, %19
%40 = phi i32 [ -1, %19 ], [ -1, %28 ], [ -1, %31 ], [ %38, %34 ]
ret i32 %40
}