#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 CheckMPEGPacket(unsigned char *, long);
int CheckTSPacket(unsigned char *, long);
int DetectInputStreamFormat(unsigned char *pData, long dwSize) {
  int proTS = 0;
  int proPES = 0;
  unsigned char uType = 0;
  proTS = CheckTSPacket(pData, dwSize);
  if (proTS) {
    uType = 1;
    return uType;
  }
  proPES = CheckMPEGPacket(pData, dwSize);
  if (proPES && proPES < 1024)
    return 2;
  if (proPES == 0 && proTS == 0)
    return 0;
  if (proPES == 0)
    uType = 1;
  else if (proTS == 0)
    uType = 2;
  else if (proTS > proPES)
    uType = 2;
  else
    uType = 1;
  return uType;
}


x86 assembly (clang, -O0):
<DetectInputStreamFormat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <CheckTSPacket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     116c <DetectInputStreamFormat+0x4c>
movb   $0x1,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <DetectInputStreamFormat+0xef>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <CheckMPEGPacket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     119f <DetectInputStreamFormat+0x7f>
cmpl   $0x400,-0x20(%rbp)
jge    119f <DetectInputStreamFormat+0x7f>
movl   $0x2,-0x4(%rbp)
jmp    120f <DetectInputStreamFormat+0xef>
cmpl   $0x0,-0x20(%rbp)
jne    11bf <DetectInputStreamFormat+0x9f>
cmpl   $0x0,-0x1c(%rbp)
jne    11bf <DetectInputStreamFormat+0x9f>
movl   $0x0,-0x4(%rbp)
jmp    120f <DetectInputStreamFormat+0xef>
cmpl   $0x0,-0x20(%rbp)
jne    11d2 <DetectInputStreamFormat+0xb2>
movb   $0x1,-0x21(%rbp)
jmp    1208 <DetectInputStreamFormat+0xe8>
cmpl   $0x0,-0x1c(%rbp)
jne    11e5 <DetectInputStreamFormat+0xc5>
movb   $0x2,-0x21(%rbp)
jmp    1203 <DetectInputStreamFormat+0xe3>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11fa <DetectInputStreamFormat+0xda>
movb   $0x2,-0x21(%rbp)
jmp    11fe <DetectInputStreamFormat+0xde>
movb   $0x1,-0x21(%rbp)
jmp    1203 <DetectInputStreamFormat+0xe3>
jmp    1208 <DetectInputStreamFormat+0xe8>
movzbl -0x21(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DetectInputStreamFormat>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <CheckTSPacket@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    1161 <DetectInputStreamFormat+0x41>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <CheckMPEGPacket@plt>
mov    %eax,%ecx
test   %eax,%eax
je     1158 <DetectInputStreamFormat+0x38>
mov    $0x2,%eax
cmp    $0x400,%ecx
jl     1161 <DetectInputStreamFormat+0x41>
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
add    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @DetectInputStreamFormat(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i8 0, i8* %8
%9 = load i8*, i8** %4
%10 = load i64, i64* %5
%11 = call i32 @CheckTSPacket(i8* %9, i64 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
store i8 1, i8* %8
%15 = load i8, i8* %8
%16 = zext i8 %15 to i32
store i32 %16, i32* %3
br label %53
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = call i32 @CheckMPEGPacket(i8* %18, i64 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 1024
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 2, i32* %3
br label %53
27: ; preds = %23, %17
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 0, i32* %3
br label %53
34: ; preds = %30, %27
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i8 1, i8* %8
br label %50
38: ; preds = %34
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i8 2, i8* %8
br label %49
42: ; preds = %38
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = icmp sgt i32 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
store i8 2, i8* %8
br label %48
47: ; preds = %42
store i8 1, i8* %8
br label %48
48: ; preds = %47, %46
br label %49
49: ; preds = %48, %41
br label %50
50: ; preds = %49, %37
%51 = load i8, i8* %8
%52 = zext i8 %51 to i32
store i32 %52, i32* %3
br label %53
53: ; preds = %50, %33, %26, %14
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
define i32 @DetectInputStreamFormat(i8* %0, i64 %1) {
%3 = tail call i32 @CheckTSPacket(i8* %0, i64 %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i32 @CheckMPEGPacket(i8* %0, i64 %1)
%7 = icmp ne i32 %6, 0
%8 = icmp slt i32 %6, 1024
%9 = and i1 %7, %8
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = icmp eq i32 %6, 0
%12 = select i1 %11, i32 0, i32 2
br label %13
13: ; preds = %10, %5, %2
%14 = phi i32 [ 1, %2 ], [ 2, %5 ], [ %12, %10 ]
ret i32 %14
}