#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 */
size_t read(int, char *, int);
size_t utf8BytesToCodePoint(char *, size_t, int *);
size_t linenoiseUtf8ReadCode(int fd, char *buf, size_t buf_len, int *cp) {
  if (buf_len < 1)
    return -1;
  size_t nread = read(fd, &buf[0], 1);
  if (nread <= 0)
    return nread;
  unsigned char byte = buf[0];
  if ((byte & 0x80) == 0) {
    ;
  } else if ((byte & 0xE0) == 0xC0) {
    if (buf_len < 2)
      return -1;
    nread = read(fd, &buf[1], 1);
    if (nread <= 0)
      return nread;
  } else if ((byte & 0xF0) == 0xE0) {
    if (buf_len < 3)
      return -1;
    nread = read(fd, &buf[1], 2);
    if (nread <= 0)
      return nread;
  } else if ((byte & 0xF8) == 0xF0) {
    if (buf_len < 3)
      return -1;
    nread = read(fd, &buf[1], 3);
    if (nread <= 0)
      return nread;
  } else {
    return -1;
  }
  return utf8BytesToCodePoint(buf, buf_len, cp);
}


x86 assembly (clang, -O0):
<linenoiseUtf8ReadCode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x1,-0x20(%rbp)
jae    114f <linenoiseUtf8ReadCode+0x2f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     117c <linenoiseUtf8ReadCode+0x5c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
movzbl -0x31(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    119c <linenoiseUtf8ReadCode+0x7c>
jmp    12de <linenoiseUtf8ReadCode+0x1be>
movzbl -0x31(%rbp),%eax
and    $0xe0,%eax
cmp    $0xc0,%eax
jne    11fe <linenoiseUtf8ReadCode+0xde>
cmpq   $0x2,-0x20(%rbp)
jae    11c8 <linenoiseUtf8ReadCode+0xa8>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     11f9 <linenoiseUtf8ReadCode+0xd9>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
jmp    12d9 <linenoiseUtf8ReadCode+0x1b9>
movzbl -0x31(%rbp),%eax
and    $0xf0,%eax
cmp    $0xe0,%eax
jne    1260 <linenoiseUtf8ReadCode+0x140>
cmpq   $0x3,-0x20(%rbp)
jae    122a <linenoiseUtf8ReadCode+0x10a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x2,%edx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     125b <linenoiseUtf8ReadCode+0x13b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
jmp    12d4 <linenoiseUtf8ReadCode+0x1b4>
movzbl -0x31(%rbp),%eax
and    $0xf8,%eax
cmp    $0xf0,%eax
jne    12c2 <linenoiseUtf8ReadCode+0x1a2>
cmpq   $0x3,-0x20(%rbp)
jae    128c <linenoiseUtf8ReadCode+0x16c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x3,%edx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     12bd <linenoiseUtf8ReadCode+0x19d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
jmp    12cf <linenoiseUtf8ReadCode+0x1af>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f3 <linenoiseUtf8ReadCode+0x1d3>
jmp    12d4 <linenoiseUtf8ReadCode+0x1b4>
jmp    12d9 <linenoiseUtf8ReadCode+0x1b9>
jmp    12de <linenoiseUtf8ReadCode+0x1be>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <utf8BytesToCodePoint@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoiseUtf8ReadCode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffffffffffff,%r12
test   %rdx,%rdx
je     11dc <linenoiseUtf8ReadCode+0xbc>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
je     11d9 <linenoiseUtf8ReadCode+0xb9>
movzbl (%r15),%eax
test   $0x80,%al
je     11c3 <linenoiseUtf8ReadCode+0xa3>
mov    %eax,%ecx
and    $0xffffffe0,%ecx
cmp    $0xc0,%ecx
jne    117e <linenoiseUtf8ReadCode+0x5e>
cmp    $0x2,%rbx
jb     11dc <linenoiseUtf8ReadCode+0xbc>
lea    0x1(%r15),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
jmp    11b9 <linenoiseUtf8ReadCode+0x99>
mov    %eax,%ecx
and    $0xfffffff0,%ecx
cmp    $0xe0,%ecx
jne    119e <linenoiseUtf8ReadCode+0x7e>
cmp    $0x3,%rbx
jb     11dc <linenoiseUtf8ReadCode+0xbc>
lea    0x1(%r15),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
jmp    11b9 <linenoiseUtf8ReadCode+0x99>
cmp    $0x3,%rbx
jb     11dc <linenoiseUtf8ReadCode+0xbc>
and    $0xfffffff8,%eax
cmp    $0xf0,%eax
jne    11dc <linenoiseUtf8ReadCode+0xbc>
lea    0x1(%r15),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1040 <read@plt>
test   %rax,%rax
je     11d9 <linenoiseUtf8ReadCode+0xb9>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <utf8BytesToCodePoint@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @linenoiseUtf8ReadCode(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %43, label %6
6: ; preds = %4
%7 = tail call i64 @read(i32 %0, i8* %1, i32 1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %43, label %9
9: ; preds = %6
%10 = load i8, i8* %1
%11 = zext i8 %10 to i32
%12 = and i32 %11, 128
%13 = icmp eq i32 %12, 0
br i1 %13, label %41, label %14
14: ; preds = %9
%15 = and i32 %11, 224
%16 = icmp eq i32 %15, 192
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = icmp ult i64 %2, 2
br i1 %18, label %43, label %19
19: ; preds = %17
%20 = getelementptr inbounds i8, i8* %1, i64 1
%21 = tail call i64 @read(i32 %0, i8* %20, i32 1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %43, label %41
23: ; preds = %14
%24 = and i32 %11, 240
%25 = icmp eq i32 %24, 224
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = icmp ult i64 %2, 3
br i1 %27, label %43, label %28
28: ; preds = %26
%29 = getelementptr inbounds i8, i8* %1, i64 1
%30 = tail call i64 @read(i32 %0, i8* %29, i32 2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %43, label %41
32: ; preds = %23
%33 = and i32 %11, 248
%34 = icmp ne i32 %33, 240
%35 = icmp ult i64 %2, 3
%36 = or i1 %35, %34
br i1 %36, label %43, label %37
37: ; preds = %32
%38 = getelementptr inbounds i8, i8* %1, i64 1
%39 = tail call i64 @read(i32 %0, i8* %38, i32 3)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %19, %37, %28, %9
%42 = tail call i64 @utf8BytesToCodePoint(i8* %1, i64 %2, i32* %3)
br label %43
43: ; preds = %6, %32, %37, %28, %26, %19, %17, %41, %4
%44 = phi i64 [ -1, %4 ], [ 0, %6 ], [ %42, %41 ], [ -1, %17 ], [ 0, %19 ], [ -1, %26 ], [ 0, %28 ], [ 0, %37 ], [ -1, %32 ]
ret i64 %44
}