#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 */
unsigned char *findPcrPacket(unsigned char *, int, int *, int, int);
unsigned long long getPcrValue(unsigned char *);
int calculateAvgBitrate(unsigned char *pucMemBuff, int iBuffSize, int iPcrPid) {
  int iBitrate, iOffset;
  unsigned long long startIdx, endIdx;
  unsigned long long llStartPcr, llEndPcr;
  unsigned char *pucBuff = 0;
  startIdx = 0, endIdx = 0;
  llStartPcr = 0, llEndPcr = 0;
  pucBuff = 0, iOffset = 0;
  if ((pucBuff = findPcrPacket(pucMemBuff, iBuffSize, &iOffset, 1, iPcrPid)) !=
      0) {
    llStartPcr = getPcrValue(pucBuff);
    startIdx = (long)(pucBuff + 10);
  }
  pucBuff = 0, iOffset = 0;
  if ((pucBuff = findPcrPacket(pucMemBuff, iBuffSize, &iOffset, 0, iPcrPid)) !=
      0) {
    llEndPcr = getPcrValue(pucBuff);
    endIdx = (long)(pucBuff + 10);
  }
  iBitrate = (int)(((long long)(endIdx - startIdx) * 27000000 /
                    (llEndPcr - llStartPcr) * 8) &
                   0xFFFFFFFF);
  return iBitrate;
}


x86 assembly (clang, -O0):
<calculateAvgBitrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%r8d
lea    -0x18(%rbp),%rdx
mov    $0x1,%ecx
call   1030 <findPcrPacket@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     11a9 <calculateAvgBitrate+0x89>
mov    -0x40(%rbp),%rdi
call   1040 <getPcrValue@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%r8d
lea    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <findPcrPacket@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     11f5 <calculateAvgBitrate+0xd5>
mov    -0x40(%rbp),%rdi
call   1040 <getPcrValue@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
imul   $0x19bfcc0,%rax,%rax
mov    -0x38(%rbp),%rcx
sub    -0x30(%rbp),%rcx
xor    %edx,%edx
div    %rcx
shl    $0x3,%rax
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculateAvgBitrate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%rbp
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    $0x1,%ecx
mov    %r15d,%r8d
call   1030 <findPcrPacket@plt>
test   %rax,%rax
je     1167 <calculateAvgBitrate+0x47>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <getPcrValue@plt>
mov    %rax,%r14
add    $0xa,%r13
jmp    116d <calculateAvgBitrate+0x4d>
xor    %r13d,%r13d
xor    %r14d,%r14d
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdx
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    %r12d,%esi
xor    %ecx,%ecx
mov    %r15d,%r8d
call   1030 <findPcrPacket@plt>
test   %rax,%rax
je     11a8 <calculateAvgBitrate+0x88>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <getPcrValue@plt>
mov    %rax,%rcx
add    $0xa,%rbp
mov    %rbp,%rbx
jmp    11aa <calculateAvgBitrate+0x8a>
xor    %ecx,%ecx
sub    %r14,%rcx
sub    %r13,%rbx
imul   $0x19bfcc0,%rbx,%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     11ca <calculateAvgBitrate+0xaa>
xor    %edx,%edx
div    %rcx
jmp    11ce <calculateAvgBitrate+0xae>
xor    %edx,%edx
div    %ecx
shl    $0x3,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calculateAvgBitrate(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* null, i8** %13
store i64 0, i64* %9
store i64 0, i64* %10
store i64 0, i64* %11
store i64 0, i64* %12
store i8* null, i8** %13
store i32 0, i32* %8
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = call i8* @findPcrPacket(i8* %14, i32 %15, i32* %8, i32 1, i32 %16)
store i8* %17, i8** %13
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i8*, i8** %13
%21 = call i64 @getPcrValue(i8* %20)
store i64 %21, i64* %11
%22 = load i8*, i8** %13
%23 = getelementptr inbounds i8, i8* %22, i64 10
%24 = ptrtoint i8* %23 to i64
store i64 %24, i64* %9
br label %25
25: ; preds = %19, %3
store i8* null, i8** %13
store i32 0, i32* %8
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = load i32, i32* %6
%29 = call i8* @findPcrPacket(i8* %26, i32 %27, i32* %8, i32 0, i32 %28)
store i8* %29, i8** %13
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load i8*, i8** %13
%33 = call i64 @getPcrValue(i8* %32)
store i64 %33, i64* %12
%34 = load i8*, i8** %13
%35 = getelementptr inbounds i8, i8* %34, i64 10
%36 = ptrtoint i8* %35 to i64
store i64 %36, i64* %10
br label %37
37: ; preds = %31, %25
%38 = load i64, i64* %10
%39 = load i64, i64* %9
%40 = sub i64 %38, %39
%41 = mul nsw i64 %40, 27000000
%42 = load i64, i64* %12
%43 = load i64, i64* %11
%44 = sub i64 %42, %43
%45 = udiv i64 %41, %44
%46 = mul i64 %45, 8
%47 = and i64 %46, 4294967295
%48 = trunc i64 %47 to i32
store i32 %48, i32* %7
%49 = load i32, i32* %7
ret i32 %49
}

LLVM IR (-O2):
define i32 @calculateAvgBitrate(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = call i8* @findPcrPacket(i8* %0, i32 %1, i32* %4, i32 1, i32 %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = call i64 @getPcrValue(i8* %6)
%10 = getelementptr inbounds i8, i8* %6, i64 10
%11 = ptrtoint i8* %10 to i64
br label %12
12: ; preds = %8, %3
%13 = phi i64 [ %11, %8 ], [ 0, %3 ]
%14 = phi i64 [ %9, %8 ], [ 0, %3 ]
store i32 0, i32* %4
%15 = call i8* @findPcrPacket(i8* %0, i32 %1, i32* %4, i32 0, i32 %2)
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = call i64 @getPcrValue(i8* %15)
%19 = getelementptr inbounds i8, i8* %15, i64 10
%20 = ptrtoint i8* %19 to i64
br label %21
21: ; preds = %17, %12
%22 = phi i64 [ %20, %17 ], [ 0, %12 ]
%23 = phi i64 [ %18, %17 ], [ 0, %12 ]
%24 = sub i64 %22, %13
%25 = mul nsw i64 %24, 27000000
%26 = sub i64 %23, %14
%27 = udiv i64 %25, %26
%28 = trunc i64 %27 to i32
%29 = shl i32 %28, 3
ret i32 %29
}