#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 uint16_t;

/* Variables and functions */
char *strdup(char *);
char *to_ascii(unsigned char const *, int);
char *convert_from_utf16(const unsigned char *utf16) {
  int le = 0;
  uint16_t bom = *utf16 << 8 | *(utf16 + 1);
  if (bom == 0xfffe)
    le = 1;
  else if (bom != 0xfeff)
    return strdup("");
  return to_ascii(utf16, le);
}


x86 assembly (clang, -O0):
<convert_from_utf16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xfffe,-0x18(%rbp)
jne    1163 <convert_from_utf16+0x43>
movl   $0x1,-0x14(%rbp)
jmp    118a <convert_from_utf16+0x6a>
cmpl   $0xfeff,-0x18(%rbp)
je     1185 <convert_from_utf16+0x65>
lea    0xe89(%rip),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    119a <convert_from_utf16+0x7a>
jmp    118a <convert_from_utf16+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <to_ascii@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_from_utf16>:
movzwl (%rdi),%eax
rol    $0x8,%ax
movzwl %ax,%ecx
cmp    $0xfeff,%ecx
je     1142 <convert_from_utf16+0x22>
mov    $0x1,%esi
cmp    $0xfffe,%ax
jne    1149 <convert_from_utf16+0x29>
jmp    1030 <to_ascii@plt>
xor    %esi,%esi
jmp    1030 <to_ascii@plt>
lea    0xeb0(%rip),%rdi
jmp    1040 <strdup@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @convert_from_utf16(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = zext i8 %7 to i32
%9 = shl i32 %8, 8
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = or i32 %9, %13
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 65534
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 1, i32* %4
br label %24
18: ; preds = %1
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 65279
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %22, i8** %2
br label %28
23: ; preds = %18
br label %24
24: ; preds = %23, %17
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = call i8* @to_ascii(i8* %25, i32 %26)
store i8* %27, i8** %2
br label %28
28: ; preds = %24, %21
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @convert_from_utf16(i8* %0) {
%2 = load i8, i8* %0
%3 = zext i8 %2 to i16
%4 = shl nuw i16 %3, 8
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = zext i8 %6 to i16
%8 = or i16 %4, %7
switch i16 %8, label %9 [
i16 -2, label %12
i16 -257, label %11
]
9: ; preds = %1
%10 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %15
11: ; preds = %1
br label %12
12: ; preds = %1, %11
%13 = phi i32 [ 1, %1 ], [ 0, %11 ]
%14 = tail call i8* @to_ascii(i8* %0, i32 %13)
br label %15
15: ; preds = %12, %9
%16 = phi i8* [ %14, %12 ], [ %10, %9 ]
ret i8* %16
}