#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 *dest, size_t dest_size, unsigned int cp,
                      const unsigned char *prev) {
  unsigned short val;
  if (cp <= 0xffff) {
    if (dest_size < 2) {
      if (getenv("LIBMMS_DEBUG"))
        fprintf(stderr, "mms: "
                        "mms: Buffer too small to encode string\n");
      return 0;
    }
    if (cp >= 0xdc00 && cp <= 0xdfff && prev) {
      val = prev[0] | (prev[1] << 8);
      if (val >= 0xd800 && val <= 0xdbff) {
        if (getenv("LIBMMS_DEBUG"))
          fprintf(stderr, "mms: "
                          "mms: Cannot encode reserved character\n");
        return 0;
      }
    }
    val = cp & 0xffff;
    dest[0] = val & 0xff;
    dest[1] = (val >> 8) & 0xff;
    return 2;
  } else if (cp >= 0x10000 && cp <= 0x10ffff) {
    if (dest_size < 4) {
      if (getenv("LIBMMS_DEBUG"))
        fprintf(stderr, "mms: "
                        "mms: Buffer too small to encode string\n");
      return 0;
    }
    cp -= 0x10000;
    val = ((cp & 0xffc00) >> 10) + 0xd800;
    dest[0] = val & 0xff;
    dest[1] = (val >> 8) & 0xff;
    val = (cp & 0x3ff) + 0xdc00;
    dest[2] = val & 0xff;
    dest[3] = (val >> 8) & 0xff;
    return 4;
  }
  if (getenv("LIBMMS_DEBUG"))
    fprintf(stderr, "mms: "
                    "mms: Cannot encode character\n");
  return 0;
}


x86 assembly (clang, -O0):
<utf16le_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
cmpl   $0xffff,-0x1c(%rbp)
ja     125e <utf16le_encode+0x13e>
cmpq   $0x2,-0x18(%rbp)
jae    1187 <utf16le_encode+0x67>
lea    0xeaa(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     117a <utf16le_encode+0x5a>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    137d <utf16le_encode+0x25d>
cmpl   $0xdc00,-0x1c(%rbp)
jb     121f <utf16le_encode+0xff>
cmpl   $0xdfff,-0x1c(%rbp)
ja     121f <utf16le_encode+0xff>
cmpq   $0x0,-0x28(%rbp)
je     121f <utf16le_encode+0xff>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
mov    -0x28(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
cmp    $0xd800,%eax
jl     121a <utf16le_encode+0xfa>
movzwl -0x2a(%rbp),%eax
cmp    $0xdbff,%eax
jg     121a <utf16le_encode+0xfa>
lea    0xe17(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     120d <utf16le_encode+0xed>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    137d <utf16le_encode+0x25d>
jmp    121f <utf16le_encode+0xff>
mov    -0x1c(%rbp),%eax
and    $0xffff,%eax
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movzwl -0x2a(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
movq   $0x2,-0x8(%rbp)
jmp    137d <utf16le_encode+0x25d>
cmpl   $0x10000,-0x1c(%rbp)
jb     1345 <utf16le_encode+0x225>
cmpl   $0x10ffff,-0x1c(%rbp)
ja     1345 <utf16le_encode+0x225>
cmpq   $0x4,-0x18(%rbp)
jae    12bb <utf16le_encode+0x19b>
lea    0xd76(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     12ae <utf16le_encode+0x18e>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    137d <utf16le_encode+0x25d>
mov    -0x1c(%rbp),%eax
sub    $0x10000,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xffc00,%eax
shr    $0xa,%eax
add    $0xd800,%eax
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movzwl -0x2a(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x1c(%rbp),%eax
and    $0x3ff,%eax
add    $0xdc00,%eax
mov    %ax,-0x2a(%rbp)
movzwl -0x2a(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
movzwl -0x2a(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
movq   $0x4,-0x8(%rbp)
jmp    137d <utf16le_encode+0x25d>
jmp    134a <utf16le_encode+0x22a>
lea    0xcaf(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1375 <utf16le_encode+0x255>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf16le_encode>:
push   %rax
cmp    $0xffff,%edx
ja     1176 <utf16le_encode+0x56>
cmp    $0x1,%rsi
jbe    1184 <utf16le_encode+0x64>
mov    %edx,%eax
and    $0xfffffc00,%eax
cmp    $0xdc00,%eax
jne    11d1 <utf16le_encode+0xb1>
test   %rcx,%rcx
je     11d1 <utf16le_encode+0xb1>
mov    0x1(%rcx),%al
and    $0xfc,%al
cmp    $0xd8,%al
jne    11d1 <utf16le_encode+0xb1>
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11cd <utf16le_encode+0xad>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0xec6(%rip),%rsi
jmp    11c8 <utf16le_encode+0xa8>
cmp    $0x10ffff,%edx
ja     11a7 <utf16le_encode+0x87>
cmp    $0x3,%rsi
ja     11db <utf16le_encode+0xbb>
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11cd <utf16le_encode+0xad>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
jmp    11c8 <utf16le_encode+0xa8>
lea    0xe52(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11cd <utf16le_encode+0xad>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    %dx,(%rdi)
mov    $0x2,%eax
pop    %rcx
ret
lea    -0x10000(%rdx),%eax
mov    %eax,%ecx
shr    $0xa,%ecx
mov    %cl,(%rdi)
shr    $0x12,%eax
or     $0xd8,%al
mov    %al,0x1(%rdi)
mov    %dl,0x2(%rdi)
shr    $0x8,%edx
and    $0x3,%dl
or     $0xdc,%dl
mov    %dl,0x3(%rdi)
mov    $0x4,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"LIBMMS_DEBUG\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"mms: mms: Buffer too small to encode string\0A\00"
@.str.2 = [44 x i8] c"mms: mms: Cannot encode reserved character\0A\00"
@.str.3 = [35 x i8] c"mms: mms: Cannot encode character\0A\00"
define i64 @utf16le_encode(i8* %0, i64 %1, i32 %2, i8* %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i16
store i8* %0, i8** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i32, i32* %8
%12 = icmp ule i32 %11, 65535
br i1 %12, label %13, label %76
13: ; preds = %4
%14 = load i64, i64* %7
%15 = icmp ult i64 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %13
%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 ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
store i64 0, i64* %5
br label %138
23: ; preds = %13
%24 = load i32, i32* %8
%25 = icmp uge i32 %24, 56320
br i1 %25, label %26, label %59
26: ; preds = %23
%27 = load i32, i32* %8
%28 = icmp ule i32 %27, 57343
br i1 %28, label %29, label %59
29: ; preds = %26
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %59
32: ; preds = %29
%33 = load i8*, i8** %9
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = load i8*, i8** %9
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = shl i32 %40, 8
%42 = or i32 %36, %41
%43 = trunc i32 %42 to i16
store i16 %43, i16* %10
%44 = load i16, i16* %10
%45 = zext i16 %44 to i32
%46 = icmp sge i32 %45, 55296
br i1 %46, label %47, label %58
47: ; preds = %32
%48 = load i16, i16* %10
%49 = zext i16 %48 to i32
%50 = icmp sle i32 %49, 56319
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %54, %51
store i64 0, i64* %5
br label %138
58: ; preds = %47, %32
br label %59
59: ; preds = %58, %29, %26, %23
%60 = load i32, i32* %8
%61 = and i32 %60, 65535
%62 = trunc i32 %61 to i16
store i16 %62, i16* %10
%63 = load i16, i16* %10
%64 = zext i16 %63 to i32
%65 = and i32 %64, 255
%66 = trunc i32 %65 to i8
%67 = load i8*, i8** %6
%68 = getelementptr inbounds i8, i8* %67, i64 0
store i8 %66, i8* %68
%69 = load i16, i16* %10
%70 = zext i16 %69 to i32
%71 = ashr i32 %70, 8
%72 = and i32 %71, 255
%73 = trunc i32 %72 to i8
%74 = load i8*, i8** %6
%75 = getelementptr inbounds i8, i8* %74, i64 1
store i8 %73, i8* %75
store i64 2, i64* %5
br label %138
76: ; preds = %4
%77 = load i32, i32* %8
%78 = icmp uge i32 %77, 65536
br i1 %78, label %79, label %130
79: ; preds = %76
%80 = load i32, i32* %8
%81 = icmp ule i32 %80, 1114111
br i1 %81, label %82, label %130
82: ; preds = %79
%83 = load i64, i64* %7
%84 = icmp ult i64 %83, 4
br i1 %84, label %85, label %92
85: ; preds = %82
%86 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%87 = icmp ne i64 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = load i32, i32* @stderr
%90 = call i32 @fprintf(i32 %89, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %91
91: ; preds = %88, %85
store i64 0, i64* %5
br label %138
92: ; preds = %82
%93 = load i32, i32* %8
%94 = sub i32 %93, 65536
store i32 %94, i32* %8
%95 = load i32, i32* %8
%96 = and i32 %95, 1047552
%97 = lshr i32 %96, 10
%98 = add i32 %97, 55296
%99 = trunc i32 %98 to i16
store i16 %99, i16* %10
%100 = load i16, i16* %10
%101 = zext i16 %100 to i32
%102 = and i32 %101, 255
%103 = trunc i32 %102 to i8
%104 = load i8*, i8** %6
%105 = getelementptr inbounds i8, i8* %104, i64 0
store i8 %103, i8* %105
%106 = load i16, i16* %10
%107 = zext i16 %106 to i32
%108 = ashr i32 %107, 8
%109 = and i32 %108, 255
%110 = trunc i32 %109 to i8
%111 = load i8*, i8** %6
%112 = getelementptr inbounds i8, i8* %111, i64 1
store i8 %110, i8* %112
%113 = load i32, i32* %8
%114 = and i32 %113, 1023
%115 = add i32 %114, 56320
%116 = trunc i32 %115 to i16
store i16 %116, i16* %10
%117 = load i16, i16* %10
%118 = zext i16 %117 to i32
%119 = and i32 %118, 255
%120 = trunc i32 %119 to i8
%121 = load i8*, i8** %6
%122 = getelementptr inbounds i8, i8* %121, i64 2
store i8 %120, i8* %122
%123 = load i16, i16* %10
%124 = zext i16 %123 to i32
%125 = ashr i32 %124, 8
%126 = and i32 %125, 255
%127 = trunc i32 %126 to i8
%128 = load i8*, i8** %6
%129 = getelementptr inbounds i8, i8* %128, i64 3
store i8 %127, i8* %129
store i64 4, i64* %5
br label %138
130: ; preds = %79, %76
br label %131
131: ; preds = %130
%132 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%133 = icmp ne i64 %132, 0
br i1 %133, label %134, label %137
134: ; preds = %131
%135 = load i32, i32* @stderr
%136 = call i32 @fprintf(i32 %135, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %137
137: ; preds = %134, %131
store i64 0, i64* %5
br label %138
138: ; preds = %137, %92, %91, %59, %57, %22
%139 = load i64, i64* %5
ret i64 %139
}

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