#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 unsigned int uint32_t;

/* Variables and functions */
unsigned int readUInt32(void const *);
int bsonCheckValidity(const void *document, unsigned int fileSize) {
  const char *docBytes = (const char *)document;
  uint32_t docLen = readUInt32(document);
  int offset;
  if (fileSize < 4 + 1 + 2 + 1) {
    return 0;
  }
  if (docLen > fileSize) {
    return 0;
  }
  offset = 4;
  switch (docBytes[offset]) {
  case 0x02:
    break;
  default:
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<bsonCheckValidity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <readUInt32@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x8,-0x14(%rbp)
jae    1149 <bsonCheckValidity+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1198 <bsonCheckValidity+0x88>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jbe    1161 <bsonCheckValidity+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1198 <bsonCheckValidity+0x88>
movl   $0x4,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%al
sub    $0x2,%al
jne    1185 <bsonCheckValidity+0x75>
jmp    1180 <bsonCheckValidity+0x70>
jmp    1191 <bsonCheckValidity+0x81>
movl   $0x0,-0x4(%rbp)
jmp    1198 <bsonCheckValidity+0x88>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsonCheckValidity>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <readUInt32@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x8,%ebp
jb     1133 <bsonCheckValidity+0x23>
cmp    %ebp,%ecx
ja     1133 <bsonCheckValidity+0x23>
xor    %eax,%eax
cmpb   $0x2,0x4(%rbx)
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bsonCheckValidity(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
store i8* %9, i8** %6
%10 = load i8*, i8** %4
%11 = call i32 @readUInt32(i8* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %5
%13 = icmp ult i32 %12, 8
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %30
15: ; preds = %2
%16 = load i32, i32* %7
%17 = load i32, i32* %5
%18 = icmp ugt i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %3
br label %30
20: ; preds = %15
store i32 4, i32* %8
%21 = load i8*, i8** %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
switch i32 %26, label %28 [
i32 2, label %27
]
27: ; preds = %20
br label %29
28: ; preds = %20
store i32 0, i32* %3
br label %30
29: ; preds = %27
store i32 1, i32* %3
br label %30
30: ; preds = %29, %28, %19, %14
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
define i32 @bsonCheckValidity(i8* %0, i32 %1) {
%3 = tail call i32 @readUInt32(i8* %0)
%4 = icmp ult i32 %1, 8
%5 = icmp ugt i32 %3, %1
%6 = select i1 %4, i1 true, i1 %5
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = getelementptr inbounds i8, i8* %0, i64 4
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 2
%11 = zext i1 %10 to i32
br label %12
12: ; preds = %7, %2
%13 = phi i32 [ 0, %2 ], [ %11, %7 ]
ret i32 %13
}