#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 size_t uint8_t;
typedef size_t uint32_t;
typedef int /*<<< orphan*/ objsize;

/* Variables and functions */
size_t be32toh(size_t);
size_t le32toh(size_t);
int /*<<< orphan*/ memcpy(size_t *, size_t *, int);
size_t cso_validate_bin(int isconvert, uint8_t *start, size_t size) {
  uint32_t objsize;
  if (size < 1 + sizeof(objsize))
    return 0;
  do {
    memcpy(&objsize, (start + 1), sizeof(objsize));
    if (isconvert) {
      if (1)
        objsize = be32toh(objsize);
      else
        objsize = le32toh(objsize);
      memcpy((start + 1), &objsize, sizeof(objsize));
    }
  } while (0);
  if (objsize > size - sizeof(objsize) - 1)
    return 0;
  return 1 + sizeof(objsize) + objsize;
}


x86 assembly (clang, -O0):
<cso_validate_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x9,-0x20(%rbp)
jae    114b <cso_validate_bin+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    11ca <cso_validate_bin+0xaa>
jmp    1150 <cso_validate_bin+0x30>
mov    -0x18(%rbp),%rsi
add    $0x8,%rsi
lea    -0x28(%rbp),%rdi
mov    $0x8,%edx
call   1030 <memcpy@plt>
cmpl   $0x0,-0xc(%rbp)
je     1193 <cso_validate_bin+0x73>
mov    -0x28(%rbp),%rdi
call   1040 <be32toh@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x8,%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
jmp    1198 <cso_validate_bin+0x78>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x8,%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jbe    11be <cso_validate_bin+0x9e>
movq   $0x0,-0x8(%rbp)
jmp    11ca <cso_validate_bin+0xaa>
mov    -0x28(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cso_validate_bin>:
cmp    $0x9,%rdx
jae    1129 <cso_validate_bin+0x9>
xor    %eax,%eax
ret
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
add    $0x8,%r14
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
test   %ebp,%ebp
je     1174 <cso_validate_bin+0x54>
mov    0x8(%rsp),%rdi
call   1040 <be32toh@plt>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    0x8(%rsp),%rcx
add    $0xfffffffffffffff7,%rbx
lea    0x9(%rcx),%rdx
xor    %eax,%eax
cmp    %rbx,%rcx
cmovbe %rdx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @cso_validate_bin(i32 %0, i64* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = icmp ult i64 %9, 9
br i1 %10, label %11, label %12
11: ; preds = %3
store i64 0, i64* %4
br label %36
12: ; preds = %3
br label %13
13: ; preds = %12
%14 = load i64*, i64** %6
%15 = getelementptr inbounds i64, i64* %14, i64 1
%16 = call i32 @memcpy(i64* %8, i64* %15, i32 8)
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i64, i64* %8
%21 = call i64 @be32toh(i64 %20)
store i64 %21, i64* %8
%22 = load i64*, i64** %6
%23 = getelementptr inbounds i64, i64* %22, i64 1
%24 = call i32 @memcpy(i64* %23, i64* %8, i32 8)
br label %25
25: ; preds = %19, %13
br label %26
26: ; preds = %25
%27 = load i64, i64* %8
%28 = load i64, i64* %7
%29 = sub i64 %28, 8
%30 = sub i64 %29, 1
%31 = icmp ugt i64 %27, %30
br i1 %31, label %32, label %33
32: ; preds = %26
store i64 0, i64* %4
br label %36
33: ; preds = %26
%34 = load i64, i64* %8
%35 = add i64 9, %34
store i64 %35, i64* %4
br label %36
36: ; preds = %33, %32, %11
%37 = load i64, i64* %4
ret i64 %37
}

LLVM IR (-O2):
define i64 @cso_validate_bin(i32 %0, i64* %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = icmp ult i64 %2, 9
br i1 %6, label %21, label %7
7: ; preds = %3
%8 = getelementptr inbounds i64, i64* %1, i64 1
%9 = call i32 @memcpy(i64* %4, i64* %8, i32 8)
%10 = icmp eq i32 %0, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i64, i64* %4
%13 = call i64 @be32toh(i64 %12)
store i64 %13, i64* %4
%14 = call i32 @memcpy(i64* %8, i64* %4, i32 8)
br label %15
15: ; preds = %11, %7
%16 = load i64, i64* %4
%17 = add i64 %2, -9
%18 = icmp ugt i64 %16, %17
%19 = add i64 %16, 9
%20 = select i1 %18, i64 0, i64 %19
br label %21
21: ; preds = %15, %3
%22 = phi i64 [ 0, %3 ], [ %20, %15 ]
ret i64 %22
}