#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;
typedef int uint32_t;

/* Variables and functions */
int mpeg2_get_picture_type(const uint8_t *buf, int len) {
  int i;
  for (i = 0; i < len - 5; i++)
    if ((*(const uint32_t *)(buf + i) == 0x00010000U))
      return (buf[i + 5] >> 3) & 0x07;
  return 0;
}


x86 assembly (clang, -O0):
<mpeg2_get_picture_type>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x5,%ecx
cmp    %ecx,%eax
jge    1169 <mpeg2_get_picture_type+0x69>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0x10000,(%rax,%rcx,4)
jne    1156 <mpeg2_get_picture_type+0x56>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
sar    $0x3,%eax
and    $0x7,%eax
mov    %eax,-0x4(%rbp)
jmp    1170 <mpeg2_get_picture_type+0x70>
jmp    115b <mpeg2_get_picture_type+0x5b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1112 <mpeg2_get_picture_type+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpeg2_get_picture_type>:
xor    %eax,%eax
cmp    $0x6,%esi
jl     1124 <mpeg2_get_picture_type+0x24>
add    $0xfffffffb,%esi
xor    %eax,%eax
nopl   0x0(%rax)
cmpl   $0x10000,(%rdi,%rax,4)
je     1125 <mpeg2_get_picture_type+0x25>
add    $0x1,%rax
cmp    %rax,%rsi
jne    1110 <mpeg2_get_picture_type+0x10>
xor    %eax,%eax
ret
mov    0x14(%rdi,%rax,4),%eax
shr    $0x3,%eax
and    $0x7,%eax
ret

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

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