#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 uint8_t;

/* Variables and functions */
scalar_t__ CheckEnd();
int ReadUInt8(const uint8_t *ptr, int ofs, uint8_t *val) {
  if (CheckEnd()) {
    *val = ptr[ofs];
    return ofs + 1;
  } else {
    int len = 1;
    uint8_t res = 0;
    for (int i = 0; i < len; i++) {
      res |= ptr[ofs + i] << (8 * i);
    }
    *val = res;
    return (ofs + len);
  }
}


x86 assembly (clang, -O0):
<ReadUInt8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1030 <CheckEnd@plt>
cmp    $0x0,%rax
je     1153 <ReadUInt8+0x43>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <ReadUInt8+0xa2>
movl   $0x1,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11a0 <ReadUInt8+0x90>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x2c(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1168 <ReadUInt8+0x58>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadUInt8>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <CheckEnd@plt>
movslq %ebp,%rax
mov    (%rbx,%rax,4),%ecx
mov    %ecx,(%r14)
add    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @ReadUInt8(i32* nocapture %0, i32 %1, i32* nocapture writeonly %2) {
%4 = tail call i64 (...) @CheckEnd()
%5 = sext i32 %1 to i64
%6 = getelementptr inbounds i32, i32* %0, i64 %5
%7 = load i32, i32* %6
store i32 %7, i32* %2
%8 = add nsw i32 %1, 1
ret i32 %8
}