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

/* Variables and functions */
int todword(int /*<<< orphan*/ const *);
int count_array(const uint8_t *stream) {
  uint32_t length = todword(stream);
  stream += 4;
  int n = 0;
  while (length > 0) {
    if (length < 4)
      return -1;
    uint32_t nsz = todword(stream);
    nsz += 4;
    if (nsz > length)
      return -1;
    ++n;
    stream += nsz;
    length -= nsz;
  }
  return n;
}


x86 assembly (clang, -O0):
<count_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <todword@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b6 <count_array+0xa6>
cmpl   $0x4,-0x14(%rbp)
jge    115b <count_array+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <count_array+0xac>
mov    -0x10(%rbp),%rdi
call   1030 <todword@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1188 <count_array+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <count_array+0xac>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    113b <count_array+0x2b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_array>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <todword@plt>
mov    %eax,%ebp
add    $0x10,%rbx
xor    %r15d,%r15d
mov    $0xffffffff,%r14d
xchg   %ax,%ax
test   %ebp,%ebp
jle    1158 <count_array+0x48>
cmp    $0x4,%ebp
jb     115b <count_array+0x4b>
mov    %rbx,%rdi
call   1030 <todword@plt>
lea    0x4(%rax),%ecx
add    $0x1,%r15d
cltq
lea    (%rbx,%rax,4),%rbx
add    $0x10,%rbx
sub    %ecx,%ebp
jge    1130 <count_array+0x20>
jmp    115b <count_array+0x4b>
mov    %r15d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @count_array(i32* %0) {
%2 = tail call i32 @todword(i32* %0)
%3 = getelementptr inbounds i32, i32* %0, i64 4
br label %4
4: ; preds = %11, %1
%5 = phi i32* [ %3, %1 ], [ %17, %11 ]
%6 = phi i32 [ %2, %1 ], [ %18, %11 ]
%7 = phi i32 [ 0, %1 ], [ %15, %11 ]
%8 = icmp sgt i32 %6, 0
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = icmp ult i32 %6, 4
br i1 %10, label %19, label %11
11: ; preds = %9
%12 = tail call i32 @todword(i32* %5)
%13 = add nsw i32 %12, 4
%14 = icmp slt i32 %6, %13
%15 = add nuw nsw i32 %7, 1
%16 = sext i32 %13 to i64
%17 = getelementptr inbounds i32, i32* %5, i64 %16
%18 = sub nsw i32 %6, %13
br i1 %14, label %19, label %4
19: ; preds = %4, %9, %11
%20 = phi i32 [ -1, %11 ], [ -1, %9 ], [ %7, %4 ]
ret i32 %20
}