#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 */
int const *h265hvcc_array_get_nalu(int const *, int /*<<< orphan*/);
int h265hvcc_array_get_num_nalus(int const *);
int const *h265hvcc_get_array(int const *, int);
int h265hvcc_get_num_of_arrays(int const *);
bool h265hvcc_validate(const uint8_t *p, size_t size) {
  if (p[0] != 1 && p[0] != 0)
    return false;
  if (size < 23)
    return false;
  uint8_t num_arrays = h265hvcc_get_num_of_arrays(p);
  uint8_t array = 0;
  while (array < num_arrays) {
    const uint8_t *a = h265hvcc_get_array(p, array++);
    if (a + 3 > p + size)
      return false;
    uint8_t num_nalus = h265hvcc_array_get_num_nalus(a);
    uint8_t nalu = 0;
    while (nalu < num_nalus)
      if (h265hvcc_array_get_nalu(a, nalu++) + 2 > p + size)
        return false;
  }
  if (h265hvcc_get_array(p, num_arrays) > p + size)
    return false;
  return true;
}


x86 assembly (clang, -O0):
<h265hvcc_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x1,(%rax)
je     1176 <h265hvcc_validate+0x36>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     1176 <h265hvcc_validate+0x36>
movl   $0x0,-0x4(%rbp)
jmp    128c <h265hvcc_validate+0x14c>
cmpq   $0x17,-0x18(%rbp)
jae    118d <h265hvcc_validate+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    128c <h265hvcc_validate+0x14c>
mov    -0x10(%rbp),%rdi
call   1050 <h265hvcc_get_num_of_arrays@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1255 <h265hvcc_validate+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
call   1040 <h265hvcc_get_array@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xc,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    11f0 <h265hvcc_validate+0xb0>
movl   $0x0,-0x4(%rbp)
jmp    128c <h265hvcc_validate+0x14c>
mov    -0x28(%rbp),%rdi
call   1060 <h265hvcc_array_get_num_nalus@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1250 <h265hvcc_validate+0x110>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
call   1030 <h265hvcc_array_get_nalu@plt>
add    $0x8,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    124b <h265hvcc_validate+0x10b>
movl   $0x0,-0x4(%rbp)
jmp    128c <h265hvcc_validate+0x14c>
jmp    1203 <h265hvcc_validate+0xc3>
jmp    11a0 <h265hvcc_validate+0x60>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <h265hvcc_get_array@plt>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    1285 <h265hvcc_validate+0x145>
movl   $0x0,-0x4(%rbp)
jmp    128c <h265hvcc_validate+0x14c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<h265hvcc_validate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %edx,%edx
cmp    $0x17,%rsi
jb     1224 <h265hvcc_validate+0xe4>
mov    %rdi,%r13
cmpl   $0x1,(%rdi)
ja     1224 <h265hvcc_validate+0xe4>
mov    %rsi,%rbx
mov    %r13,%rdi
call   1050 <h265hvcc_get_num_of_arrays@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    1202 <h265hvcc_validate+0xc2>
lea    0x0(,%rbx,4),%r14
add    %r13,%r14
xor    %r12d,%r12d
mov    %r15d,0xc(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r13,%rdi
mov    %r12d,%esi
call   1040 <h265hvcc_get_array@plt>
mov    %rax,%rbp
add    $0xc,%rax
cmp    %r14,%rax
ja     1222 <h265hvcc_validate+0xe2>
add    $0x1,%r12d
mov    %r12d,%ebx
mov    %rbp,%rdi
call   1060 <h265hvcc_array_get_num_nalus@plt>
mov    %eax,%r15d
xor    %esi,%esi
test   %eax,%eax
cmovle %esi,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %esi,%r15d
je     11f0 <h265hvcc_validate+0xb0>
lea    0x1(%rsi),%r12d
mov    %rbp,%rdi
call   1030 <h265hvcc_array_get_nalu@plt>
add    $0x8,%rax
mov    %r12d,%esi
cmp    %r14,%rax
jbe    11d0 <h265hvcc_validate+0x90>
jmp    1222 <h265hvcc_validate+0xe2>
nop
mov    0xc(%rsp),%r15d
mov    %ebx,%r12d
cmp    %r15d,%ebx
mov    0x10(%rsp),%rbx
jne    1194 <h265hvcc_validate+0x54>
mov    %r13,%rdi
mov    %r15d,%esi
call   1040 <h265hvcc_get_array@plt>
lea    0x0(,%rbx,4),%rcx
add    %r13,%rcx
xor    %edx,%edx
cmp    %rcx,%rax
setbe  %dl
jmp    1224 <h265hvcc_validate+0xe4>
xor    %edx,%edx
mov    %edx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @h265hvcc_validate(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
%11 = load i32*, i32** %4
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i64 0
%18 = load i32, i32* %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %3
br label %74
21: ; preds = %15, %2
%22 = load i64, i64* %5
%23 = icmp ult i64 %22, 23
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 0, i32* %3
br label %74
25: ; preds = %21
%26 = load i32*, i32** %4
%27 = call i32 @h265hvcc_get_num_of_arrays(i32* %26)
store i32 %27, i32* %6
store i32 0, i32* %7
br label %28
28: ; preds = %63, %25
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %64
32: ; preds = %28
%33 = load i32*, i32** %4
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
%36 = call i32* @h265hvcc_get_array(i32* %33, i32 %34)
store i32* %36, i32** %8
%37 = load i32*, i32** %8
%38 = getelementptr inbounds i32, i32* %37, i64 3
%39 = load i32*, i32** %4
%40 = load i64, i64* %5
%41 = getelementptr inbounds i32, i32* %39, i64 %40
%42 = icmp ugt i32* %38, %41
br i1 %42, label %43, label %44
43: ; preds = %32
store i32 0, i32* %3
br label %74
44: ; preds = %32
%45 = load i32*, i32** %8
%46 = call i32 @h265hvcc_array_get_num_nalus(i32* %45)
store i32 %46, i32* %9
store i32 0, i32* %10
br label %47
47: ; preds = %62, %44
%48 = load i32, i32* %10
%49 = load i32, i32* %9
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %63
51: ; preds = %47
%52 = load i32*, i32** %8
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
%55 = call i32* @h265hvcc_array_get_nalu(i32* %52, i32 %53)
%56 = getelementptr inbounds i32, i32* %55, i64 2
%57 = load i32*, i32** %4
%58 = load i64, i64* %5
%59 = getelementptr inbounds i32, i32* %57, i64 %58
%60 = icmp ugt i32* %56, %59
br i1 %60, label %61, label %62
61: ; preds = %51
store i32 0, i32* %3
br label %74
62: ; preds = %51
br label %47
63: ; preds = %47
br label %28
64: ; preds = %28
%65 = load i32*, i32** %4
%66 = load i32, i32* %6
%67 = call i32* @h265hvcc_get_array(i32* %65, i32 %66)
%68 = load i32*, i32** %4
%69 = load i64, i64* %5
%70 = getelementptr inbounds i32, i32* %68, i64 %69
%71 = icmp ugt i32* %67, %70
br i1 %71, label %72, label %73
72: ; preds = %64
store i32 0, i32* %3
br label %74
73: ; preds = %64
store i32 1, i32* %3
br label %74
74: ; preds = %73, %72, %61, %43, %24, %20
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
define i32 @h265hvcc_validate(i32* %0, i64 %1) {
%3 = load i32, i32* %0
%4 = icmp ugt i32 %3, 1
%5 = icmp ult i64 %1, 23
%6 = or i1 %5, %4
br i1 %6, label %36, label %7
7: ; preds = %2
%8 = tail call i32 @h265hvcc_get_num_of_arrays(i32* %0)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %7
%11 = getelementptr inbounds i32, i32* %0, i64 %1
br label %14
12: ; preds = %23
%13 = icmp eq i32 %16, %8
br i1 %13, label %31, label %14
14: ; preds = %10, %12
%15 = phi i32 [ 0, %10 ], [ %16, %12 ]
%16 = add nuw nsw i32 %15, 1
%17 = tail call i32* @h265hvcc_get_array(i32* %0, i32 %15)
%18 = getelementptr inbounds i32, i32* %17, i64 3
%19 = icmp ugt i32* %18, %11
br i1 %19, label %36, label %20
20: ; preds = %14
%21 = tail call i32 @h265hvcc_array_get_num_nalus(i32* %17)
%22 = call i32 @llvm.smax.i32(i32 %21, i32 0)
br label %23
23: ; preds = %26, %20
%24 = phi i32 [ 0, %20 ], [ %27, %26 ]
%25 = icmp eq i32 %24, %22
br i1 %25, label %12, label %26
26: ; preds = %23
%27 = add nuw i32 %24, 1
%28 = tail call i32* @h265hvcc_array_get_nalu(i32* %17, i32 %24)
%29 = getelementptr inbounds i32, i32* %28, i64 2
%30 = icmp ugt i32* %29, %11
br i1 %30, label %36, label %23
31: ; preds = %12, %7
%32 = tail call i32* @h265hvcc_get_array(i32* %0, i32 %8)
%33 = getelementptr inbounds i32, i32* %0, i64 %1
%34 = icmp ule i32* %32, %33
%35 = zext i1 %34 to i32
br label %36
36: ; preds = %14, %26, %2, %31
%37 = phi i32 [ 0, %2 ], [ %35, %31 ], [ 0, %26 ], [ 0, %14 ]
ret i32 %37
}