#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int readData(int, char *, int, int);
int /*<<< orphan*/ stderr;
int readMpegHeader(int file, void *data, int size) {
  unsigned int flagmax = 0;
  unsigned long *syncword = (unsigned long *)data;
  int result;
  if (size < 4)
    return -2;
  result = readData(file, data, 4, 2);
  if (result == 0)
    return -3;
  if (result == -1) {
    fprintf(stderr, "header\n");
    return -4;
  }
  if (result < 4) {
    return -7;
  }
  while ((*syncword & 0xe0ff) != 0xe0ff) {
    *syncword >>= 8;
    result = readData(file, (char *)data + 3, 1, 2);
    if (result == 0) {
      return -3;
    } else if (result == -1) {
      return -4;
    }
    if (++flagmax > 1200) {
      return -5;
    }
  }
  return 4;
}


x86 assembly (clang, -O0):
<readMpegHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    1157 <readMpegHeader+0x37>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
mov    $0x2,%ecx
call   1030 <readData@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1186 <readMpegHeader+0x66>
movl   $0xfffffffd,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b1 <readMpegHeader+0x91>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
cmpl   $0x4,-0x24(%rbp)
jge    11c7 <readMpegHeader+0xa7>
movl   $0xfffffff9,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
jmp    11cc <readMpegHeader+0xac>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
and    $0xe0ff,%rax
cmp    $0xe0ff,%rax
je     1266 <readMpegHeader+0x146>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
shr    $0x8,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x3,%rsi
mov    $0x1,%edx
mov    $0x2,%ecx
call   1030 <readData@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1226 <readMpegHeader+0x106>
movl   $0xfffffffd,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
cmpl   $0xffffffff,-0x24(%rbp)
jne    123c <readMpegHeader+0x11c>
movl   $0xfffffffc,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
jmp    1241 <readMpegHeader+0x121>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmp    $0x4b0,%eax
jbe    1261 <readMpegHeader+0x141>
movl   $0xfffffffb,-0x4(%rbp)
jmp    126d <readMpegHeader+0x14d>
jmp    11cc <readMpegHeader+0xac>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMpegHeader>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xfffffffe,%eax
cmp    $0x4,%edx
jl     11d5 <readMpegHeader+0xb5>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x4,%edx
mov    $0x2,%ecx
call   1030 <readData@plt>
test   %eax,%eax
je     11d0 <readMpegHeader+0xb0>
mov    %eax,%ecx
cmp    $0xffffffff,%eax
jne    1175 <readMpegHeader+0x55>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xfffffffc,%eax
jmp    11d5 <readMpegHeader+0xb5>
mov    $0xfffffff9,%eax
cmp    $0x4,%ecx
jl     11d5 <readMpegHeader+0xb5>
lea    0x3(%rbx),%r14
mov    $0x4b1,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%rax
mov    %eax,%ecx
not    %ecx
test   $0xe0ff,%ecx
je     11e0 <readMpegHeader+0xc0>
shr    $0x8,%rax
mov    %rax,(%rbx)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
mov    $0x2,%ecx
call   1030 <readData@plt>
cmp    $0xffffffff,%eax
je     116e <readMpegHeader+0x4e>
test   %eax,%eax
je     11d0 <readMpegHeader+0xb0>
mov    $0xfffffffb,%eax
add    $0xffffffff,%ebp
jne    1190 <readMpegHeader+0x70>
jmp    11d5 <readMpegHeader+0xb5>
mov    $0xfffffffd,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x4,%eax
jmp    11d5 <readMpegHeader+0xb5>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [8 x i8] c"header\0A\00"
define i32 @readMpegHeader(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %6
%12 = bitcast i8* %11 to i64*
store i64* %12, i64** %9
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -2, i32* %4
br label %62
16: ; preds = %3
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = call i32 @readData(i32 %17, i8* %18, i32 4, i32 2)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -3, i32* %4
br label %62
23: ; preds = %16
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -4, i32* %4
br label %62
29: ; preds = %23
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 4
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -7, i32* %4
br label %62
33: ; preds = %29
br label %34
34: ; preds = %60, %33
%35 = load i64*, i64** %9
%36 = load i64, i64* %35
%37 = and i64 %36, 57599
%38 = icmp ne i64 %37, 57599
br i1 %38, label %39, label %61
39: ; preds = %34
%40 = load i64*, i64** %9
%41 = load i64, i64* %40
%42 = lshr i64 %41, 8
store i64 %42, i64* %40
%43 = load i32, i32* %5
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 3
%46 = call i32 @readData(i32 %43, i8* %45, i32 1, i32 2)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %39
store i32 -3, i32* %4
br label %62
50: ; preds = %39
%51 = load i32, i32* %10
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 -4, i32* %4
br label %62
54: ; preds = %50
br label %55
55: ; preds = %54
%56 = load i32, i32* %8
%57 = add i32 %56, 1
store i32 %57, i32* %8
%58 = icmp ugt i32 %57, 1200
br i1 %58, label %59, label %60
59: ; preds = %55
store i32 -5, i32* %4
br label %62
60: ; preds = %55
br label %34
61: ; preds = %34
store i32 4, i32* %4
br label %62
62: ; preds = %61, %59, %53, %49, %32, %26, %22, %15
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [8 x i8] c"header\0A\00"
define i32 @readMpegHeader(i32 %0, i8* %1, i32 %2) {
%4 = bitcast i8* %1 to i64*
%5 = icmp slt i32 %2, 4
br i1 %5, label %28, label %6
6: ; preds = %3
%7 = tail call i32 @readData(i32 %0, i8* %1, i32 4, i32 2)
switch i32 %7, label %11 [
i32 0, label %28
i32 -1, label %8
]
8: ; preds = %6
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %28
11: ; preds = %6
%12 = icmp slt i32 %7, 4
br i1 %12, label %28, label %13
13: ; preds = %11
%14 = getelementptr inbounds i8, i8* %1, i64 3
br label %15
15: ; preds = %13, %23
%16 = phi i32 [ %24, %23 ], [ 0, %13 ]
%17 = load i64, i64* %4
%18 = and i64 %17, 57599
%19 = icmp eq i64 %18, 57599
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = lshr i64 %17, 8
store i64 %21, i64* %4
%22 = tail call i32 @readData(i32 %0, i8* %14, i32 1, i32 2)
switch i32 %22, label %23 [
i32 0, label %26
i32 -1, label %28
]
23: ; preds = %20
%24 = add nuw nsw i32 %16, 1
%25 = icmp eq i32 %24, 1201
br i1 %25, label %26, label %15
26: ; preds = %20, %23, %15
%27 = phi i32 [ 4, %15 ], [ -5, %23 ], [ -3, %20 ]
br label %28
28: ; preds = %20, %26, %11, %6, %3, %8
%29 = phi i32 [ -4, %8 ], [ -2, %3 ], [ -3, %6 ], [ -7, %11 ], [ %27, %26 ], [ -4, %20 ]
ret i32 %29
}