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

/* Variables and functions */
scalar_t__ ntohl(scalar_t__);
int find_mpeg_header(const uint8_t *buf, const unsigned int bs,
                     const uint8_t type) {
  unsigned int offset = 0;
  uint32_t header = 0x00000100 + type;
  header = ntohl(header);
  while (offset <= bs - sizeof(header)) {
    if (*(uint32_t *)(buf + offset) == header)
      return offset;
    offset++;
  }
  return -1;
}


x86 assembly (clang, -O0):
<find_mpeg_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <ntohl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x8,%rcx
cmp    %rcx,%rax
ja     1186 <find_mpeg_header+0x76>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
cmp    -0x30(%rbp),%rax
jne    1178 <find_mpeg_header+0x68>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118d <find_mpeg_header+0x7d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1145 <find_mpeg_header+0x35>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_mpeg_header>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x100(%rdx),%rdi
call   1030 <ntohl@plt>
mov    %rax,%rcx
mov    %ebp,%edx
add    $0xfffffffffffffff8,%rdx
xor    %eax,%eax
nop
cmp    %rcx,(%rbx,%rax,8)
je     1143 <find_mpeg_header+0x33>
add    $0x1,%eax
cmp    %rax,%rdx
jae    1130 <find_mpeg_header+0x20>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @find_mpeg_header(i64* nocapture %0, i32 %1, i64 %2) {
%4 = add nsw i64 %2, 256
%5 = tail call i64 @ntohl(i64 %4)
%6 = zext i32 %1 to i64
%7 = add nsw i64 %6, -8
br label %8
8: ; preds = %3, %13
%9 = phi i64 [ 0, %3 ], [ %15, %13 ]
%10 = getelementptr inbounds i64, i64* %0, i64 %9
%11 = load i64, i64* %10
%12 = icmp eq i64 %11, %5
br i1 %12, label %17, label %13
13: ; preds = %8
%14 = add nuw nsw i64 %9, 1
%15 = and i64 %14, 4294967295
%16 = icmp ult i64 %7, %15
br i1 %16, label %19, label %8
17: ; preds = %8
%18 = trunc i64 %9 to i32
br label %19
19: ; preds = %13, %17
%20 = phi i32 [ %18, %17 ], [ -1, %13 ]
ret i32 %20
}