#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 scalar_t__ iconv_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ perror(char *);
scalar_t__ strlen(char *);
int utf8ToUtf16LE(char *from, char **to) {
  iconv_t conv = iconv_open("UTF-16LE", "UTF-8");
  if (conv == (iconv_t)-1) {
    perror("load_config:iconv_open");
    exit(1);
  }
  char *inbuf = from;
  size_t inbytesleft = (size_t)strlen(from);
  size_t outbytesleft = inbytesleft * 2, avail = outbytesleft;
  char *outbuf = (char *)malloc(outbytesleft), *outptr = outbuf;
  memset(outbuf, 0, outbytesleft);
  if (iconv(conv, &inbuf, &inbytesleft, &outptr, &avail) == (size_t)-1) {
    perror("load_config:iconv");
    exit(1);
  }
  iconv_close(conv);
  *to = outbuf;
  return (outbytesleft - avail);
}


x86 assembly (clang, -O0):
<utf8ToUtf16LE>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe69(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   10a0 <iconv_open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11c8 <utf8ToUtf16LE+0x48>
lea    0xe56(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x48(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
jne    124e <utf8ToUtf16LE+0xce>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1070 <iconv_close@plt>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
sub    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8ToUtf16LE>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe68(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   10a0 <iconv_open@plt>
cmp    $0xffffffffffffffff,%rax
je     1226 <utf8ToUtf16LE+0xa6>
mov    %rax,%r15
mov    %rbx,0x20(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %rax,0x18(%rsp)
add    %rax,%rbx
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r15,%rdi
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     122f <utf8ToUtf16LE+0xaf>
mov    %r15,%rdi
call   1070 <iconv_close@plt>
mov    %r12,(%r14)
sub    0x8(%rsp),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xde2(%rip),%rdi
jmp    1236 <utf8ToUtf16LE+0xb6>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"UTF-16LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [23 x i8] c"load_config:iconv_open\00"
@.str.3 = [18 x i8] c"load_config:iconv\00"
define i32 @utf8ToUtf16LE(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store i8** %1, i8*** %4
%12 = call i64 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8*, i8** %3
store i8* %19, i8** %6
%20 = load i8*, i8** %3
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = mul i64 %22, 2
store i64 %23, i64* %8
%24 = load i64, i64* %8
store i64 %24, i64* %9
%25 = load i64, i64* %8
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %10
%28 = load i8*, i8** %10
store i8* %28, i8** %11
%29 = load i8*, i8** %10
%30 = load i64, i64* %8
%31 = call i32 @memset(i8* %29, i32 0, i64 %30)
%32 = load i64, i64* %5
%33 = call i64 @iconv(i64 %32, i8** %6, i64* %7, i8** %11, i64* %9)
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %18
%36 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %18
%39 = load i64, i64* %5
%40 = call i32 @iconv_close(i64 %39)
%41 = load i8*, i8** %10
%42 = load i8**, i8*** %4
store i8* %41, i8** %42
%43 = load i64, i64* %8
%44 = load i64, i64* %9
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
ret i32 %46
}

LLVM IR (-O2):
@.str = [9 x i8] c"UTF-16LE\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@.str.2 = [23 x i8] c"load_config:iconv_open\00"
@.str.3 = [18 x i8] c"load_config:iconv\00"
define i32 @utf8ToUtf16LE(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = tail call i64 @iconv_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = bitcast i8** %3 to i8*
store i8* %0, i8** %3
%14 = bitcast i64* %4 to i8*
%15 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %15, i64* %4
%16 = shl i64 %15, 1
%17 = bitcast i64* %5 to i8*
store i64 %16, i64* %5
%18 = tail call i64 @malloc(i64 %16)
%19 = inttoptr i64 %18 to i8*
%20 = bitcast i8** %6 to i8*
store i8* %19, i8** %6
%21 = tail call i32 @memset(i8* %19, i32 0, i64 %16)
%22 = call i64 @iconv(i64 %7, i8** %3, i64* %4, i8** %6, i64* %5)
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %12
%28 = call i32 @iconv_close(i64 %7)
store i8* %19, i8** %1
%29 = load i64, i64* %5
%30 = sub i64 %16, %29
%31 = trunc i64 %30 to i32
ret i32 %31
}