#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 EOF;
int read(int, unsigned char *, int);
int readc_utf8(int fd, int *res) {
  unsigned int n = 0;
  int expect = 1;
  while (expect--) {
    unsigned char ch;
    int len;
    if ((len = read(fd, &ch, 1)) == 0) {
      *res = EOF;
      return 1;
    }
    if (len == -1)
      return 0;
    if ((ch & 0b11111100) == 0b11111100) {
      n = ch & 0b00000001;
      expect = 5;
    } else if ((ch & 0b11111000) == 0b11111000) {
      n = ch & 0b00000011;
      expect = 4;
    } else if ((ch & 0b11110000) == 0b11110000) {
      n = ch & 0b00000111;
      expect = 3;
    } else if ((ch & 0b11100000) == 0b11100000) {
      n = ch & 0b00001111;
      expect = 2;
    } else if ((ch & 0b11000000) == 0b11000000) {
      n = ch & 0b00011111;
      expect = 1;
    } else if ((ch & 0b10000000) == 0b10000000) {
      n <<= 6;
      n |= ch & 0b00111111;
    } else {
      n = ch;
    }
  }
  *res = (int)n;
  return 1;
}


x86 assembly (clang, -O0):
<readc_utf8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x0,%eax
je     12b5 <readc_utf8+0x1a5>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1179 <readc_utf8+0x69>
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12c5 <readc_utf8+0x1b5>
cmpl   $0xffffffff,-0x20(%rbp)
jne    118f <readc_utf8+0x7f>
movl   $0x0,-0x4(%rbp)
jmp    12c5 <readc_utf8+0x1b5>
movzbl -0x19(%rbp),%eax
and    $0xfc,%eax
cmp    $0xfc,%eax
jne    11b9 <readc_utf8+0xa9>
movzbl -0x19(%rbp),%eax
and    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x5,-0x18(%rbp)
jmp    12b0 <readc_utf8+0x1a0>
movzbl -0x19(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf8,%eax
jne    11e3 <readc_utf8+0xd3>
movzbl -0x19(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
jmp    12ab <readc_utf8+0x19b>
movzbl -0x19(%rbp),%eax
and    $0xf0,%eax
cmp    $0xf0,%eax
jne    120d <readc_utf8+0xfd>
movzbl -0x19(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x14(%rbp)
movl   $0x3,-0x18(%rbp)
jmp    12a6 <readc_utf8+0x196>
movzbl -0x19(%rbp),%eax
and    $0xe0,%eax
cmp    $0xe0,%eax
jne    1237 <readc_utf8+0x127>
movzbl -0x19(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x14(%rbp)
movl   $0x2,-0x18(%rbp)
jmp    12a1 <readc_utf8+0x191>
movzbl -0x19(%rbp),%eax
and    $0xc0,%eax
cmp    $0xc0,%eax
jne    1261 <readc_utf8+0x151>
movzbl -0x19(%rbp),%eax
and    $0x1f,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    129c <readc_utf8+0x18c>
movzbl -0x19(%rbp),%eax
and    $0x80,%eax
cmp    $0x80,%eax
jne    1290 <readc_utf8+0x180>
mov    -0x14(%rbp),%eax
shl    $0x6,%eax
mov    %eax,-0x14(%rbp)
movzbl -0x19(%rbp),%eax
and    $0x3f,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1297 <readc_utf8+0x187>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    129c <readc_utf8+0x18c>
jmp    12a1 <readc_utf8+0x191>
jmp    12a6 <readc_utf8+0x196>
jmp    12ab <readc_utf8+0x19b>
jmp    12b0 <readc_utf8+0x1a0>
jmp    112d <readc_utf8+0x1d>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readc_utf8>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     11e3 <readc_utf8+0xd3>
test   %eax,%eax
je     11e7 <readc_utf8+0xd7>
movzbl 0xf(%rsp),%eax
mov    %eax,%ecx
not    %ecx
test   $0xfc,%cl
jne    1170 <readc_utf8+0x60>
and    $0x1,%eax
mov    $0x5,%ebx
jmp    11c0 <readc_utf8+0xb0>
nopl   0x0(%rax)
test   $0xf8,%cl
jne    1180 <readc_utf8+0x70>
and    $0x3,%eax
mov    $0x4,%ebx
jmp    11c0 <readc_utf8+0xb0>
nop
test   $0xf0,%cl
jne    118f <readc_utf8+0x7f>
and    $0x7,%eax
mov    $0x3,%ebx
jmp    11c0 <readc_utf8+0xb0>
test   $0xe0,%cl
jne    119e <readc_utf8+0x8e>
and    $0xf,%eax
mov    $0x2,%ebx
jmp    11c0 <readc_utf8+0xb0>
test   $0xc0,%cl
jne    11ad <readc_utf8+0x9d>
and    $0x1f,%eax
mov    $0x1,%ebx
jmp    11c0 <readc_utf8+0xb0>
test   $0x80,%al
jne    11cd <readc_utf8+0xbd>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebp
sub    $0x1,%ebx
jae    1130 <readc_utf8+0x20>
jmp    11de <readc_utf8+0xce>
shl    $0x6,%ebp
and    $0x3f,%eax
or     %eax,%ebp
sub    $0x1,%ebx
jae    1130 <readc_utf8+0x20>
mov    %ebp,(%r14)
jmp    11f3 <readc_utf8+0xe3>
xor    %eax,%eax
jmp    11f8 <readc_utf8+0xe8>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@EOF = global i32 0
define i32 @readc_utf8(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i8
br label %4
4: ; preds = %2, %46
%5 = phi i32 [ 0, %2 ], [ %49, %46 ]
%6 = phi i32 [ 0, %2 ], [ %47, %46 ]
%7 = call i32 @read(i32 %0, i8* %3, i32 1)
switch i32 %7, label %10 [
i32 0, label %8
i32 -1, label %44
]
8: ; preds = %4
%9 = load i32, i32* @EOF
store i32 %9, i32* %1
br label %44
10: ; preds = %4
%11 = load i8, i8* %3
%12 = zext i8 %11 to i32
%13 = and i32 %12, 252
%14 = icmp eq i32 %13, 252
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = and i32 %12, 1
br label %46
17: ; preds = %10
%18 = and i32 %12, 248
%19 = icmp eq i32 %18, 248
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = and i32 %12, 3
br label %46
22: ; preds = %17
%23 = and i32 %12, 240
%24 = icmp eq i32 %23, 240
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = and i32 %12, 7
br label %46
27: ; preds = %22
%28 = and i32 %12, 224
%29 = icmp eq i32 %28, 224
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = and i32 %12, 15
br label %46
32: ; preds = %27
%33 = and i32 %12, 192
%34 = icmp eq i32 %33, 192
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = and i32 %12, 31
br label %46
37: ; preds = %32
%38 = and i32 %12, 128
%39 = icmp eq i32 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %37
%41 = shl i32 %6, 6
%42 = and i32 %12, 63
%43 = or i32 %42, %41
br label %46
44: ; preds = %4, %8
%45 = phi i32 [ 1, %8 ], [ 0, %4 ]
br label %52
46: ; preds = %15, %25, %35, %40, %30, %20, %37
%47 = phi i32 [ %16, %15 ], [ %21, %20 ], [ %26, %25 ], [ %31, %30 ], [ %36, %35 ], [ %43, %40 ], [ %12, %37 ]
%48 = phi i32 [ 5, %15 ], [ 4, %20 ], [ 3, %25 ], [ 2, %30 ], [ 1, %35 ], [ %5, %40 ], [ %5, %37 ]
%49 = add nsw i32 %48, -1
%50 = icmp eq i32 %48, 0
br i1 %50, label %51, label %4
51: ; preds = %46
store i32 %47, i32* %1
br label %52
52: ; preds = %44, %51
%53 = phi i32 [ 1, %51 ], [ %45, %44 ]
ret i32 %53
}