#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 h264avcc_get_nb_pps(int const *);
int h264avcc_get_nb_sps(int const *);
int const *h264avcc_get_ppsh(int const *, int);
int const *h264avcc_get_spsh(int const *, int);
bool h264avcc_validate(const uint8_t *p, size_t size) {
  if (p[0] != 1)
    return false;
  if (size < 6 + 1)
    return false;
  uint8_t nb = h264avcc_get_nb_sps(p);
  uint8_t n = 0;
  while (n < nb)
    if (h264avcc_get_spsh(p, n++) + 2 > p + size)
      return false;
  if (h264avcc_get_spsh(p, n) + 1 > p + size)
    return false;
  nb = h264avcc_get_nb_pps(p);
  n = 0;
  while (n < nb)
    if (h264avcc_get_ppsh(p, n++) + 2 > p + size)
      return false;
  if (h264avcc_get_ppsh(p, n) > p + size)
    return false;
  return true;
}


x86 assembly (clang, -O0):
<h264avcc_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x1,(%rax)
je     1169 <h264avcc_validate+0x29>
movl   $0x0,-0x4(%rbp)
jmp    12ab <h264avcc_validate+0x16b>
cmpq   $0x7,-0x18(%rbp)
jae    1180 <h264avcc_validate+0x40>
movl   $0x0,-0x4(%rbp)
jmp    12ab <h264avcc_validate+0x16b>
mov    -0x10(%rbp),%rdi
call   1060 <h264avcc_get_nb_sps@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e0 <h264avcc_validate+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
call   1030 <h264avcc_get_spsh@plt>
add    $0x8,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    11db <h264avcc_validate+0x9b>
movl   $0x0,-0x4(%rbp)
jmp    12ab <h264avcc_validate+0x16b>
jmp    1193 <h264avcc_validate+0x53>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <h264avcc_get_spsh@plt>
add    $0x4,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    1214 <h264avcc_validate+0xd4>
movl   $0x0,-0x4(%rbp)
jmp    12ab <h264avcc_validate+0x16b>
mov    -0x10(%rbp),%rdi
call   1040 <h264avcc_get_nb_pps@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1274 <h264avcc_validate+0x134>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
call   1050 <h264avcc_get_ppsh@plt>
add    $0x8,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    126f <h264avcc_validate+0x12f>
movl   $0x0,-0x4(%rbp)
jmp    12ab <h264avcc_validate+0x16b>
jmp    1227 <h264avcc_validate+0xe7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1050 <h264avcc_get_ppsh@plt>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jbe    12a4 <h264avcc_validate+0x164>
movl   $0x0,-0x4(%rbp)
jmp    12ab <h264avcc_validate+0x16b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<h264avcc_validate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
cmp    $0x7,%rsi
jb     1201 <h264avcc_validate+0xc1>
mov    %rdi,%r15
cmpl   $0x1,(%rdi)
jne    1201 <h264avcc_validate+0xc1>
mov    %rsi,%r14
mov    %r15,%rdi
call   1060 <h264avcc_get_nb_sps@plt>
mov    %eax,%ebp
lea    (%r15,%r14,4),%r14
xor    %esi,%esi
test   %eax,%eax
cmovle %esi,%ebp
nopw   0x0(%rax,%rax,1)
cmp    %esi,%ebp
je     119e <h264avcc_validate+0x5e>
lea    0x1(%rsi),%ebx
mov    %r15,%rdi
call   1030 <h264avcc_get_spsh@plt>
add    $0x8,%rax
mov    %ebx,%esi
cmp    %r14,%rax
jbe    1180 <h264avcc_validate+0x40>
xor    %eax,%eax
jmp    1201 <h264avcc_validate+0xc1>
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <h264avcc_get_spsh@plt>
mov    %rax,%rcx
add    $0x4,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
ja     1201 <h264avcc_validate+0xc1>
mov    %r15,%rdi
call   1040 <h264avcc_get_nb_pps@plt>
mov    %eax,%ebp
xor    %esi,%esi
test   %eax,%eax
cmovle %esi,%ebp
nopw   0x0(%rax,%rax,1)
cmp    %esi,%ebp
je     11ec <h264avcc_validate+0xac>
lea    0x1(%rsi),%ebx
mov    %r15,%rdi
call   1050 <h264avcc_get_ppsh@plt>
add    $0x8,%rax
mov    %ebx,%esi
cmp    %r14,%rax
jbe    11d0 <h264avcc_validate+0x90>
jmp    119a <h264avcc_validate+0x5a>
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <h264avcc_get_ppsh@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
setbe  %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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