#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
scalar_t__ unicode_is_high_surrogate_utf16(int);
int /*<<< orphan*/ unicode_is_low_surrogate_utf16(int);
int unicode_reverse_byte_order_utf16(int, int);
int unicode_read_utf16_with_endianness(char *string, uint32_t *code_point,
                                       bool system_endianness) {
  uint16_t first =
      unicode_reverse_byte_order_utf16(*(uint16_t *)string, !system_endianness);
  if (unicode_is_high_surrogate_utf16(first)) {
    uint16_t second = unicode_reverse_byte_order_utf16(*(uint16_t *)&string[2],
                                                       !system_endianness);
    if (!unicode_is_low_surrogate_utf16(second))
      return 0;
    *code_point =
        ((((uint32_t)first & 0x3FF) << 10) | ((uint32_t)second & 0x3FF)) +
        0x10000;
    return 4;
  } else {
    *code_point = first;
    return 2;
  }
}


x86 assembly (clang, -O0):
<unicode_read_utf16_with_endianness>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
call   1050 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <unicode_is_high_surrogate_utf16@plt>
cmp    $0x0,%rax
je     11d9 <unicode_read_utf16_with_endianness+0xa9>
mov    -0x10(%rbp),%rax
mov    0x2(%rax),%edi
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%esi
call   1050 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <unicode_is_low_surrogate_utf16@plt>
cmp    $0x0,%eax
jne    11ab <unicode_read_utf16_with_endianness+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    11e9 <unicode_read_utf16_with_endianness+0xb9>
mov    -0x20(%rbp),%ecx
and    $0x3ff,%ecx
shl    $0xa,%ecx
mov    -0x24(%rbp),%eax
and    $0x3ff,%eax
or     %eax,%ecx
add    $0x10000,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x4,-0x4(%rbp)
jmp    11e9 <unicode_read_utf16_with_endianness+0xb9>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_read_utf16_with_endianness>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%edi
xor    %ebp,%ebp
test   %edx,%edx
sete   %bpl
mov    %ebp,%esi
call   1050 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <unicode_is_high_surrogate_utf16@plt>
test   %rax,%rax
je     1192 <unicode_read_utf16_with_endianness+0x62>
mov    0x2(%r15),%edi
mov    %ebp,%esi
call   1050 <unicode_reverse_byte_order_utf16@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <unicode_is_low_surrogate_utf16@plt>
test   %eax,%eax
je     119c <unicode_read_utf16_with_endianness+0x6c>
shl    $0xa,%ebx
and    $0xffc00,%ebx
and    $0x3ff,%ebp
add    %ebp,%ebx
add    $0x10000,%ebx
mov    $0x4,%eax
jmp    1197 <unicode_read_utf16_with_endianness+0x67>
mov    $0x2,%eax
mov    %ebx,(%r14)
jmp    119e <unicode_read_utf16_with_endianness+0x6e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unicode_read_utf16_with_endianness(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
%16 = zext i1 %15 to i32
%17 = call i32 @unicode_reverse_byte_order_utf16(i32 %12, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = call i64 @unicode_is_high_surrogate_utf16(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %44
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 2
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %24
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
%28 = xor i1 %27, true
%29 = zext i1 %28 to i32
%30 = call i32 @unicode_reverse_byte_order_utf16(i32 %25, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = call i32 @unicode_is_low_surrogate_utf16(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %21
store i32 0, i32* %4
br label %47
35: ; preds = %21
%36 = load i32, i32* %8
%37 = and i32 %36, 1023
%38 = shl i32 %37, 10
%39 = load i32, i32* %9
%40 = and i32 %39, 1023
%41 = or i32 %38, %40
%42 = add nsw i32 %41, 65536
%43 = load i32*, i32** %6
store i32 %42, i32* %43
store i32 4, i32* %4
br label %47
44: ; preds = %3
%45 = load i32, i32* %8
%46 = load i32*, i32** %6
store i32 %45, i32* %46
store i32 2, i32* %4
br label %47
47: ; preds = %44, %35, %34
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
define i32 @unicode_read_utf16_with_endianness(i8* nocapture %0, i32* nocapture writeonly %1, i32 %2) {
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = icmp eq i32 %2, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @unicode_reverse_byte_order_utf16(i32 %5, i32 %7)
%9 = tail call i64 @unicode_is_high_surrogate_utf16(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %3
%12 = getelementptr inbounds i8, i8* %0, i64 2
%13 = bitcast i8* %12 to i32*
%14 = load i32, i32* %13
%15 = tail call i32 @unicode_reverse_byte_order_utf16(i32 %14, i32 %7)
%16 = tail call i32 @unicode_is_low_surrogate_utf16(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %11
%19 = shl i32 %8, 10
%20 = and i32 %19, 1047552
%21 = and i32 %15, 1023
%22 = add nuw nsw i32 %20, 65536
%23 = or i32 %22, %21
br label %24
24: ; preds = %3, %18
%25 = phi i32 [ %23, %18 ], [ %8, %3 ]
%26 = phi i32 [ 4, %18 ], [ 2, %3 ]
store i32 %25, i32* %1
br label %27
27: ; preds = %24, %11
%28 = phi i32 [ 0, %11 ], [ %26, %24 ]
ret i32 %28
}