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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getenv(char *);
int /*<<< orphan*/ stderr;
size_t utf16le_encode(unsigned char *, size_t, unsigned int,
                      unsigned char const *);
size_t utf8_decode(unsigned char const *, unsigned int *);
int mms_utf8_to_utf16le(char *dest, size_t dest_size, const char *str) {
  unsigned char *op = (unsigned char *)dest;
  const unsigned char *prev_op = NULL;
  unsigned int cp;
  size_t slen, olen;
  if (dest_size < 2) {
    if (getenv("LIBMMS_DEBUG"))
      fprintf(stderr,
              "mms: "
              "mms: Cannot convert string to utf16le: Buffer too small\n");
    return 0;
  }
  dest_size -= 2;
  while (*str) {
    slen = utf8_decode((const unsigned char *)str, &cp);
    if (slen == 0)
      return 0;
    olen = utf16le_encode(op, dest_size, cp, prev_op);
    if (!olen)
      return 0;
    str += slen;
    dest_size -= olen;
    op += olen;
    prev_op = op - 2;
  }
  *op++ = 0;
  *op++ = 0;
  return op - (unsigned char *)dest;
}


x86 assembly (clang, -O0):
<mms_utf8_to_utf16le>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x2,-0x18(%rbp)
jae    11a6 <mms_utf8_to_utf16le+0x66>
lea    0xe8a(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     119a <mms_utf8_to_utf16le+0x5a>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <mms_utf8_to_utf16le+0x140>
mov    -0x18(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     124e <mms_utf8_to_utf16le+0x10e>
mov    -0x20(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1060 <utf8_decode@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11e7 <mms_utf8_to_utf16le+0xa7>
movl   $0x0,-0x4(%rbp)
jmp    1280 <mms_utf8_to_utf16le+0x140>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rcx
call   1040 <utf16le_encode@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1216 <mms_utf8_to_utf16le+0xd6>
movl   $0x0,-0x4(%rbp)
jmp    1280 <mms_utf8_to_utf16le+0x140>
mov    -0x40(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x48(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x30(%rbp)
jmp    11b2 <mms_utf8_to_utf16le+0x72>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mms_utf8_to_utf16le>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%rsi
ja     1185 <mms_utf8_to_utf16le+0x45>
lea    0xea5(%rip),%rdi
call   1030 <getenv@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     120e <mms_utf8_to_utf16le+0xce>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
call   1050 <fprintf@plt>
jmp    120e <mms_utf8_to_utf16le+0xce>
mov    %rdx,%r12
cmpb   $0x0,(%rdx)
mov    %rdi,%rax
mov    %rdi,0x10(%rsp)
mov    %rdi,%rcx
je     11fc <mms_utf8_to_utf16le+0xbc>
mov    %rsi,%rbx
add    $0xfffffffffffffffe,%rbx
lea    0xc(%rsp),%r13
xor    %r14d,%r14d
mov    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <utf8_decode@plt>
test   %rax,%rax
je     120c <mms_utf8_to_utf16le+0xcc>
mov    %rax,%r15
mov    0xc(%rsp),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rcx
call   1040 <utf16le_encode@plt>
test   %rax,%rax
je     120c <mms_utf8_to_utf16le+0xcc>
mov    %r15,%rdx
add    %r12,%rdx
sub    %rax,%rbx
lea    (%rax,%rbp,1),%rcx
lea    (%rax,%rbp,1),%r14
add    $0xfffffffffffffffe,%r14
cmpb   $0x0,(%r12,%r15,1)
mov    %rcx,%rbp
mov    %rdx,%r12
jne    11b0 <mms_utf8_to_utf16le+0x70>
movw   $0x0,(%rcx)
sub    0x10(%rsp),%ecx
add    $0x2,%ecx
mov    %ecx,%ebx
jmp    120e <mms_utf8_to_utf16le+0xce>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"LIBMMS_DEBUG\00"
@stderr = global i32 0
@.str.1 = [62 x i8] c"mms: mms: Cannot convert string to utf16le: Buffer too small\0A\00"
define i32 @mms_utf8_to_utf16le(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
store i8* %13, i8** %8
store i8* null, i8** %9
%14 = load i64, i64* %6
%15 = icmp ult i64 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %3
%17 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
store i32 0, i32* %4
br label %68
23: ; preds = %3
%24 = load i64, i64* %6
%25 = sub i64 %24, 2
store i64 %25, i64* %6
br label %26
26: ; preds = %45, %23
%27 = load i8*, i8** %7
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %57
30: ; preds = %26
%31 = load i8*, i8** %7
%32 = call i64 @utf8_decode(i8* %31, i32* %10)
store i64 %32, i64* %11
%33 = load i64, i64* %11
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 0, i32* %4
br label %68
36: ; preds = %30
%37 = load i8*, i8** %8
%38 = load i64, i64* %6
%39 = load i32, i32* %10
%40 = load i8*, i8** %9
%41 = call i64 @utf16le_encode(i8* %37, i64 %38, i32 %39, i8* %40)
store i64 %41, i64* %12
%42 = load i64, i64* %12
%43 = icmp ne i64 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %36
store i32 0, i32* %4
br label %68
45: ; preds = %36
%46 = load i64, i64* %11
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 %46
store i8* %48, i8** %7
%49 = load i64, i64* %12
%50 = load i64, i64* %6
%51 = sub i64 %50, %49
store i64 %51, i64* %6
%52 = load i64, i64* %12
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 %52
store i8* %54, i8** %8
%55 = load i8*, i8** %8
%56 = getelementptr inbounds i8, i8* %55, i64 -2
store i8* %56, i8** %9
br label %26
57: ; preds = %26
%58 = load i8*, i8** %8
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %8
store i8 0, i8* %58
%60 = load i8*, i8** %8
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %8
store i8 0, i8* %60
%62 = load i8*, i8** %8
%63 = load i8*, i8** %5
%64 = ptrtoint i8* %62 to i64
%65 = ptrtoint i8* %63 to i64
%66 = sub i64 %64, %65
%67 = trunc i64 %66 to i32
store i32 %67, i32* %4
br label %68
68: ; preds = %57, %44, %35, %22
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@.str = [13 x i8] c"LIBMMS_DEBUG\00"
@stderr = global i32 0
@.str.1 = [62 x i8] c"mms: mms: Cannot convert string to utf16le: Buffer too small\0A\00"
define i32 @mms_utf8_to_utf16le(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp ult i64 %1, 2
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = tail call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %44, label %10
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
br label %44
13: ; preds = %3
%14 = load i8, i8* %2
%15 = icmp eq i8 %14, 0
br i1 %15, label %36, label %16
16: ; preds = %13
%17 = add i64 %1, -2
br label %18
18: ; preds = %16, %29
%19 = phi i64 [ %31, %29 ], [ %17, %16 ]
%20 = phi i8* [ %33, %29 ], [ null, %16 ]
%21 = phi i8* [ %32, %29 ], [ %0, %16 ]
%22 = phi i8* [ %30, %29 ], [ %2, %16 ]
%23 = call i64 @utf8_decode(i8* %22, i32* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %44, label %25
25: ; preds = %18
%26 = load i32, i32* %4
%27 = call i64 @utf16le_encode(i8* %21, i64 %19, i32 %26, i8* %20)
%28 = icmp eq i64 %27, 0
br i1 %28, label %44, label %29
29: ; preds = %25
%30 = getelementptr inbounds i8, i8* %22, i64 %23
%31 = sub i64 %19, %27
%32 = getelementptr inbounds i8, i8* %21, i64 %27
%33 = getelementptr inbounds i8, i8* %32, i64 -2
%34 = load i8, i8* %30
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %18
36: ; preds = %29, %13
%37 = phi i8* [ %0, %13 ], [ %32, %29 ]
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8 0, i8* %37
%39 = getelementptr inbounds i8, i8* %37, i64 2
store i8 0, i8* %38
%40 = ptrtoint i8* %39 to i64
%41 = ptrtoint i8* %0 to i64
%42 = sub i64 %40, %41
%43 = trunc i64 %42 to i32
br label %44
44: ; preds = %25, %18, %7, %10, %36
%45 = phi i32 [ %43, %36 ], [ 0, %10 ], [ 0, %7 ], [ 0, %18 ], [ 0, %25 ]
ret i32 %45
}