#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int fgetc(int /*<<< orphan*/ *);
int read_utf16(FILE *f, char buf[4]) {
  int c, d;
  int r;
  c = fgetc(f);
  if (c < 0)
    return -1;
  d = fgetc(f);
  if (d < 0)
    return -1;
  c = c + (d << 8);
  if (c < 128) {
    buf[0] = (char)c;
    r = 1;
  } else if (c < 0x800) {
    buf[0] = 0xC0 + ((c >> 6) & 0x1F);
    buf[1] = 0x80 + (c & 0x3F);
    r = 2;
  } else if (c < 0x10000) {
    buf[0] = 0xE0 + ((c >> 12) & 0x0F);
    buf[1] = 0x80 + ((c >> 6) & 0x3F);
    buf[2] = 0x80 + (c & 0x3F);
    r = 3;
  } else {
    buf[0] = 0xE0 + ((c >> 18) & 0x07);
    buf[1] = 0x80 + ((c >> 12) & 0x3F);
    buf[2] = 0x80 + ((c >> 6) & 0x3F);
    buf[3] = 0x80 + (c & 0x3F);
    r = 4;
  }
  return r;
}


x86 assembly (clang, -O0):
<read_utf16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1142 <read_utf16+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <read_utf16+0x192>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1164 <read_utf16+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <read_utf16+0x192>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x80,-0x1c(%rbp)
jge    1196 <read_utf16+0x86>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x24(%rbp)
jmp    129c <read_utf16+0x18c>
cmpl   $0x800,-0x1c(%rbp)
jge    11d9 <read_utf16+0xc9>
mov    -0x1c(%rbp),%eax
sar    $0x6,%eax
and    $0x1f,%eax
add    $0xc0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
and    $0x3f,%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
movl   $0x2,-0x24(%rbp)
jmp    1297 <read_utf16+0x187>
cmpl   $0x10000,-0x1c(%rbp)
jge    1233 <read_utf16+0x123>
mov    -0x1c(%rbp),%eax
sar    $0xc,%eax
and    $0xf,%eax
add    $0xe0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x1c(%rbp),%eax
and    $0x3f,%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x2(%rax)
movl   $0x3,-0x24(%rbp)
jmp    1292 <read_utf16+0x182>
mov    -0x1c(%rbp),%eax
sar    $0x12,%eax
and    $0x7,%eax
add    $0xe0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
sar    $0xc,%eax
and    $0x3f,%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x1c(%rbp),%eax
and    $0x3f,%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x3(%rax)
movl   $0x4,-0x24(%rbp)
jmp    1297 <read_utf16+0x187>
jmp    129c <read_utf16+0x18c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_utf16>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
call   1030 <fgetc@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11e3 <read_utf16+0xd3>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fgetc@plt>
test   %eax,%eax
js     11e3 <read_utf16+0xd3>
shl    $0x8,%eax
add    %ebx,%eax
cmp    $0x7f,%eax
jg     115a <read_utf16+0x4a>
mov    %al,(%r14)
mov    $0x1,%r15d
jmp    11e3 <read_utf16+0xd3>
mov    %eax,%ecx
cmp    $0x7ff,%eax
ja     117c <read_utf16+0x6c>
shr    $0x6,%ecx
or     $0xc0,%cl
mov    %cl,(%r14)
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x1(%r14)
mov    $0x2,%r15d
jmp    11e3 <read_utf16+0xd3>
cmp    $0xffff,%eax
ja     11ab <read_utf16+0x9b>
shr    $0xc,%ecx
or     $0xe0,%cl
mov    %cl,(%r14)
mov    %eax,%ecx
shr    $0x6,%ecx
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x1(%r14)
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x2(%r14)
mov    $0x3,%r15d
jmp    11e3 <read_utf16+0xd3>
shr    $0x12,%ecx
and    $0x7,%cl
or     $0xe0,%cl
mov    %cl,(%r14)
mov    %eax,%ecx
shr    $0xc,%ecx
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x1(%r14)
mov    %eax,%ecx
shr    $0x6,%ecx
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x2(%r14)
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x3(%r14)
mov    $0x4,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_utf16(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32*, i32** %4
%10 = call i32 @fgetc(i32* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %105
14: ; preds = %2
%15 = load i32*, i32** %4
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %3
br label %105
20: ; preds = %14
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = shl i32 %22, 8
%24 = add nsw i32 %21, %23
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 128
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* %6
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 0
store i8 %29, i8* %31
store i32 1, i32* %8
br label %103
32: ; preds = %20
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 2048
br i1 %34, label %35, label %49
35: ; preds = %32
%36 = load i32, i32* %6
%37 = ashr i32 %36, 6
%38 = and i32 %37, 31
%39 = add nsw i32 192, %38
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 0
store i8 %40, i8* %42
%43 = load i32, i32* %6
%44 = and i32 %43, 63
%45 = add nsw i32 128, %44
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i64 1
store i8 %46, i8* %48
store i32 2, i32* %8
br label %102
49: ; preds = %32
%50 = load i32, i32* %6
%51 = icmp slt i32 %50, 65536
br i1 %51, label %52, label %73
52: ; preds = %49
%53 = load i32, i32* %6
%54 = ashr i32 %53, 12
%55 = and i32 %54, 15
%56 = add nsw i32 224, %55
%57 = trunc i32 %56 to i8
%58 = load i8*, i8** %5
%59 = getelementptr inbounds i8, i8* %58, i64 0
store i8 %57, i8* %59
%60 = load i32, i32* %6
%61 = ashr i32 %60, 6
%62 = and i32 %61, 63
%63 = add nsw i32 128, %62
%64 = trunc i32 %63 to i8
%65 = load i8*, i8** %5
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8 %64, i8* %66
%67 = load i32, i32* %6
%68 = and i32 %67, 63
%69 = add nsw i32 128, %68
%70 = trunc i32 %69 to i8
%71 = load i8*, i8** %5
%72 = getelementptr inbounds i8, i8* %71, i64 2
store i8 %70, i8* %72
store i32 3, i32* %8
br label %101
73: ; preds = %49
%74 = load i32, i32* %6
%75 = ashr i32 %74, 18
%76 = and i32 %75, 7
%77 = add nsw i32 224, %76
%78 = trunc i32 %77 to i8
%79 = load i8*, i8** %5
%80 = getelementptr inbounds i8, i8* %79, i64 0
store i8 %78, i8* %80
%81 = load i32, i32* %6
%82 = ashr i32 %81, 12
%83 = and i32 %82, 63
%84 = add nsw i32 128, %83
%85 = trunc i32 %84 to i8
%86 = load i8*, i8** %5
%87 = getelementptr inbounds i8, i8* %86, i64 1
store i8 %85, i8* %87
%88 = load i32, i32* %6
%89 = ashr i32 %88, 6
%90 = and i32 %89, 63
%91 = add nsw i32 128, %90
%92 = trunc i32 %91 to i8
%93 = load i8*, i8** %5
%94 = getelementptr inbounds i8, i8* %93, i64 2
store i8 %92, i8* %94
%95 = load i32, i32* %6
%96 = and i32 %95, 63
%97 = add nsw i32 128, %96
%98 = trunc i32 %97 to i8
%99 = load i8*, i8** %5
%100 = getelementptr inbounds i8, i8* %99, i64 3
store i8 %98, i8* %100
store i32 4, i32* %8
br label %101
101: ; preds = %73, %52
br label %102
102: ; preds = %101, %35
br label %103
103: ; preds = %102, %27
%104 = load i32, i32* %8
store i32 %104, i32* %3
br label %105
105: ; preds = %103, %19, %13
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
define i32 @read_utf16(i32* nocapture %0, i8* nocapture writeonly %1) {
%3 = tail call i32 @fgetc(i32* %0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %58, label %5
5: ; preds = %2
%6 = tail call i32 @fgetc(i32* %0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %58, label %8
8: ; preds = %5
%9 = shl i32 %6, 8
%10 = add nsw i32 %9, %3
%11 = icmp slt i32 %10, 128
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = trunc i32 %10 to i8
store i8 %13, i8* %1
br label %58
14: ; preds = %8
%15 = icmp ult i32 %10, 2048
br i1 %15, label %16, label %24
16: ; preds = %14
%17 = lshr i32 %10, 6
%18 = trunc i32 %17 to i8
%19 = or i8 %18, -64
store i8 %19, i8* %1
%20 = trunc i32 %10 to i8
%21 = and i8 %20, 63
%22 = or i8 %21, -128
%23 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %22, i8* %23
br label %58
24: ; preds = %14
%25 = icmp ult i32 %10, 65536
br i1 %25, label %26, label %39
26: ; preds = %24
%27 = lshr i32 %10, 12
%28 = trunc i32 %27 to i8
%29 = or i8 %28, -32
store i8 %29, i8* %1
%30 = lshr i32 %10, 6
%31 = trunc i32 %30 to i8
%32 = and i8 %31, 63
%33 = or i8 %32, -128
%34 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %33, i8* %34
%35 = trunc i32 %10 to i8
%36 = and i8 %35, 63
%37 = or i8 %36, -128
%38 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %37, i8* %38
br label %58
39: ; preds = %24
%40 = lshr i32 %10, 18
%41 = trunc i32 %40 to i8
%42 = and i8 %41, 7
%43 = or i8 %42, -32
store i8 %43, i8* %1
%44 = lshr i32 %10, 12
%45 = trunc i32 %44 to i8
%46 = and i8 %45, 63
%47 = or i8 %46, -128
%48 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %47, i8* %48
%49 = lshr i32 %10, 6
%50 = trunc i32 %49 to i8
%51 = and i8 %50, 63
%52 = or i8 %51, -128
%53 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %52, i8* %53
%54 = trunc i32 %10 to i8
%55 = and i8 %54, 63
%56 = or i8 %55, -128
%57 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %56, i8* %57
br label %58
58: ; preds = %12, %26, %39, %16, %5, %2
%59 = phi i32 [ -1, %2 ], [ -1, %5 ], [ 1, %12 ], [ 2, %16 ], [ 3, %26 ], [ 4, %39 ]
ret i32 %59
}