#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 read(int, char *, int);
size_t u8_getc(int fd, char *buffer) {
  if (1 != read(fd, buffer, 1)) {
    return 0;
  }
  unsigned int num_ones = 0;
  for (num_ones = 0; 0x80u == (0x80u & (buffer[0] << num_ones)); ++num_ones)
    ;
  if (0 == num_ones) {
    return 1;
  } else if (1 == num_ones || num_ones > 6) {
    return 0;
  } else {
    size_t buffer_sz;
    for (buffer_sz = 1; buffer_sz < num_ones; ++buffer_sz) {
      if (1 != read(fd, buffer + buffer_sz, 1)) {
        return 0;
      }
    }
    return buffer_sz;
  }
}


x86 assembly (clang, -O0):
<u8_getc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     114c <u8_getc+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1232 <u8_getc+0x122>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    -0x1c(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%ecx
and    $0x80,%ecx
mov    $0x80,%eax
cmp    %ecx,%eax
jne    118e <u8_getc+0x7e>
jmp    1180 <u8_getc+0x70>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115a <u8_getc+0x4a>
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    11a6 <u8_getc+0x96>
movq   $0x1,-0x8(%rbp)
jmp    1232 <u8_getc+0x122>
mov    $0x1,%eax
cmp    -0x1c(%rbp),%eax
je     11be <u8_getc+0xae>
cmpl   $0x6,-0x1c(%rbp)
jbe    11cb <u8_getc+0xbb>
movq   $0x0,-0x8(%rbp)
jmp    1232 <u8_getc+0x122>
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
cmp    %rcx,%rax
jae    122a <u8_getc+0x11a>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1214 <u8_getc+0x104>
movq   $0x0,-0x8(%rbp)
jmp    1232 <u8_getc+0x122>
jmp    1219 <u8_getc+0x109>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d3 <u8_getc+0xc3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u8_getc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11a1 <u8_getc+0x91>
movzbl (%rbx),%eax
mov    $0xffffffff,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ecx
mov    %eax,%edx
shl    %cl,%edx
test   $0x80,%dl
jne    1140 <u8_getc+0x30>
mov    $0x1,%r14d
test   %ecx,%ecx
je     11a4 <u8_getc+0x94>
xor    %r14d,%r14d
cmp    $0x1,%ecx
je     11a4 <u8_getc+0x94>
cmp    $0x6,%ecx
ja     11a4 <u8_getc+0x94>
mov    $0x1,%r14d
cmp    $0x2,%ecx
jb     11a4 <u8_getc+0x94>
mov    %ecx,%r14d
mov    %r14,%rbp
add    $0xffffffffffffffff,%rbp
add    $0x1,%rbx
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11a1 <u8_getc+0x91>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    1180 <u8_getc+0x70>
jmp    11a4 <u8_getc+0x94>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @u8_getc(i32 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = call i32 @read(i32 %8, i8* %9, i32 1)
%11 = icmp ne i32 1, %10
br i1 %11, label %12, label %13
12: ; preds = %2
store i64 0, i64* %3
br label %58
13: ; preds = %2
store i32 0, i32* %6
store i32 0, i32* %6
br label %14
14: ; preds = %24, %13
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = load i32, i32* %6
%20 = shl i32 %18, %19
%21 = and i32 128, %20
%22 = icmp eq i32 128, %21
br i1 %22, label %23, label %27
23: ; preds = %14
br label %24
24: ; preds = %23
%25 = load i32, i32* %6
%26 = add i32 %25, 1
store i32 %26, i32* %6
br label %14
27: ; preds = %14
%28 = load i32, i32* %6
%29 = icmp eq i32 0, %28
br i1 %29, label %30, label %31
30: ; preds = %27
store i64 1, i64* %3
br label %58
31: ; preds = %27
%32 = load i32, i32* %6
%33 = icmp eq i32 1, %32
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i32, i32* %6
%36 = icmp ugt i32 %35, 6
br i1 %36, label %37, label %38
37: ; preds = %34, %31
store i64 0, i64* %3
br label %58
38: ; preds = %34
store i64 1, i64* %7
br label %39
39: ; preds = %53, %38
%40 = load i64, i64* %7
%41 = load i32, i32* %6
%42 = zext i32 %41 to i64
%43 = icmp ult i64 %40, %42
br i1 %43, label %44, label %56
44: ; preds = %39
%45 = load i32, i32* %4
%46 = load i8*, i8** %5
%47 = load i64, i64* %7
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = call i32 @read(i32 %45, i8* %48, i32 1)
%50 = icmp ne i32 1, %49
br i1 %50, label %51, label %52
51: ; preds = %44
store i64 0, i64* %3
br label %58
52: ; preds = %44
br label %53
53: ; preds = %52
%54 = load i64, i64* %7
%55 = add i64 %54, 1
store i64 %55, i64* %7
br label %39
56: ; preds = %39
%57 = load i64, i64* %7
store i64 %57, i64* %3
br label %58
58: ; preds = %56, %51, %37, %30, %12
%59 = load i64, i64* %3
ret i64 %59
}

LLVM IR (-O2):
define i64 @u8_getc(i32 %0, i8* nocapture %1) {
%3 = tail call i32 @read(i32 %0, i8* %1, i32 1)
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %31
5: ; preds = %2
%6 = load i8, i8* %1
%7 = zext i8 %6 to i32
br label %8
8: ; preds = %8, %5
%9 = phi i32 [ %13, %8 ], [ 0, %5 ]
%10 = lshr i32 128, %9
%11 = and i32 %10, %7
%12 = icmp eq i32 %11, 0
%13 = add i32 %9, 1
br i1 %12, label %14, label %8
14: ; preds = %8
%15 = icmp eq i32 %9, 0
br i1 %15, label %31, label %16
16: ; preds = %14
%17 = icmp eq i32 %9, 1
%18 = icmp ugt i32 %9, 6
%19 = or i1 %17, %18
br i1 %19, label %31, label %20
20: ; preds = %16
%21 = zext i32 %9 to i64
%22 = icmp ugt i32 %9, 1
br i1 %22, label %23, label %31
23: ; preds = %20, %28
%24 = phi i64 [ %29, %28 ], [ 1, %20 ]
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = tail call i32 @read(i32 %0, i8* %25, i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = add nuw nsw i64 %24, 1
%30 = icmp eq i64 %29, %21
br i1 %30, label %31, label %23
31: ; preds = %23, %28, %20, %14, %16, %2
%32 = phi i64 [ 0, %2 ], [ 1, %14 ], [ 0, %16 ], [ 1, %20 ], [ 0, %23 ], [ %21, %28 ]
ret i64 %32
}