#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 u_short;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ memcpy(int *, unsigned char *, int);
int ntohs(int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int /*<<< orphan*/, unsigned char *, unsigned int);
int main(int argc, char *argv[]) {
  int fd_ts;
  u_short pid;
  int byte_read;
  int tsdiscont = 0;
  int haspcr = 0;
  int firstpcr = 0;
  int ts_header_size = 0;
  unsigned int i;
  unsigned int buffer_size;
  unsigned char adapt = 0;
  unsigned char *packet_buffer;
  unsigned char *current_packet;
  unsigned long long int ts_packet_count;
  buffer_size = 0;
  if (argc >= 2)
    fd_ts = open(argv[1], O_RDONLY);
  else {
    fprintf(stderr, "Usage: tsdiscont inputfile.ts > outputfile.ts\n");
    fprintf(stderr,
            "N.B: this tool will check for and fix discontinuity problems\n");
    fprintf(stderr,
            "If discontinuity is found on packet without pcr it is set to 0\n");
    fprintf(
        stderr,
        "If discontinuity is not found on first pcr packet it is set to 1\n");
    return 2;
  }
  if (fd_ts < 0) {
    fprintf(stderr, "Can't find file %s\n", argv[1]);
    return 2;
  }
  buffer_size = 1;
  buffer_size *= 188;
  packet_buffer = malloc(buffer_size);
  if (packet_buffer == NULL) {
    fprintf(stderr, "out of memory\n");
    return 2;
  }
  ts_packet_count = 0;
  byte_read = 1;
  while (byte_read) {
    byte_read = read(fd_ts, packet_buffer, buffer_size);
    for (i = 0; i < buffer_size; i += 188) {
      current_packet = packet_buffer + i;
      memcpy(&pid, current_packet + 1, 2);
      pid = ntohs(pid);
      pid = pid & 0x1fff;
      if ((pid < 8192) && (current_packet[3] & 0x20) &&
          (current_packet[4] != 0) && (current_packet[5] & 0x10)) {
        haspcr = 1;
        if (!firstpcr) {
          tsdiscont = (current_packet[5] & 0x80) >> 7;
          fprintf(stderr, "PID: %d, Pos: %lld  - First Pcr Discont : %X\n", pid,
                  ts_packet_count, tsdiscont);
          if (!tsdiscont) {
            current_packet[5] = current_packet[5] | 0x80;
            fprintf(
                stderr,
                "PID: %d, Pos: %lld  - Fixed first pcr discontinuity flag\n",
                pid, ts_packet_count);
          }
          firstpcr = 1;
        }
      } else {
        haspcr = 0;
      }
      tsdiscont = 0;
      adapt = (current_packet[3] >> 4) & 0x03;
      if (adapt == 0) {
        ts_header_size = 188;
        fprintf(stderr, "PID: %d, Pos: %lld  - Invalid packet found\n", pid,
                ts_packet_count);
      } else if (adapt == 1) {
        ts_header_size = 4;
      } else if (adapt == 2) {
        tsdiscont = (current_packet[5] & 0x80) >> 7;
        ts_header_size = 188;
      } else if (adapt == 3) {
        tsdiscont = (current_packet[5] & 0x80) >> 7;
        ts_header_size = 4 + current_packet[4] + 1;
      } else {
        ts_header_size = 188;
        fprintf(
            stderr,
            "PID: %d, Pos: %lld  - Invalid adaptation field flag found : %X\n",
            pid, ts_packet_count, adapt);
      }
      if (tsdiscont) {
        if (haspcr) {
          fprintf(stderr,
                  "PID: %d, Pos: %lld  - discontinuity found with pcr : %X\n",
                  pid, ts_packet_count, adapt);
        } else {
          fprintf(stderr,
                  "PID: %d, Pos: %lld  - Fixing discontinuity found WITHOUT "
                  "pcr : %X\n",
                  pid, ts_packet_count, adapt);
          current_packet[5] = current_packet[5] & 0x7F;
        }
      }
      ts_packet_count++;
    }
    write(STDOUT_FILENO, packet_buffer, buffer_size);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movb   $0x0,-0x35(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11d5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    123d <main+0xcd>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    15ca <main+0x45a>
cmpl   $0x0,-0x14(%rbp)
jge    1272 <main+0x102>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    15ca <main+0x45a>
movl   $0x1,-0x34(%rbp)
imul   $0xbc,-0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12bd <main+0x14d>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    15ca <main+0x45a>
movq   $0x0,-0x50(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     15c3 <main+0x453>
mov    -0x14(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x34(%rbp),%eax
jae    15a9 <main+0x439>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
add    $0x1,%rsi
lea    -0x18(%rbp),%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%edi
call   1080 <ntohs@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x1fff,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x2000,-0x18(%rbp)
jge    140b <main+0x29b>
mov    -0x48(%rbp),%rax
movzbl 0x3(%rax),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     140b <main+0x29b>
mov    -0x48(%rbp),%rax
movzbl 0x4(%rax),%eax
cmp    $0x0,%eax
je     140b <main+0x29b>
mov    -0x48(%rbp),%rax
movzbl 0x5(%rax),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     140b <main+0x29b>
movl   $0x1,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1406 <main+0x296>
mov    -0x48(%rbp),%rax
movzbl 0x5(%rax),%eax
and    $0x80,%eax
sar    $0x7,%eax
mov    %eax,-0x20(%rbp)
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%r8d
lea    0xd58(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x20(%rbp)
jne    13ff <main+0x28f>
mov    -0x48(%rbp),%rax
movzbl 0x5(%rax),%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x48(%rbp),%rax
mov    %cl,0x5(%rax)
mov    0x2be8(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x50(%rbp),%rcx
lea    0xd48(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x28(%rbp)
jmp    1412 <main+0x2a2>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x48(%rbp),%rax
movzbl 0x3(%rax),%eax
sar    $0x4,%eax
and    $0x3,%eax
mov    %al,-0x35(%rbp)
movzbl -0x35(%rbp),%eax
cmp    $0x0,%eax
jne    1461 <main+0x2f1>
movl   $0xbc,-0x2c(%rbp)
mov    0x2b8b(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x50(%rbp),%rcx
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1515 <main+0x3a5>
movzbl -0x35(%rbp),%eax
cmp    $0x1,%eax
jne    147a <main+0x30a>
movl   $0x4,-0x2c(%rbp)
jmp    1510 <main+0x3a0>
movzbl -0x35(%rbp),%eax
cmp    $0x2,%eax
jne    14a6 <main+0x336>
mov    -0x48(%rbp),%rax
movzbl 0x5(%rax),%eax
and    $0x80,%eax
sar    $0x7,%eax
mov    %eax,-0x20(%rbp)
movl   $0xbc,-0x2c(%rbp)
jmp    150b <main+0x39b>
movzbl -0x35(%rbp),%eax
cmp    $0x3,%eax
jne    14dc <main+0x36c>
mov    -0x48(%rbp),%rax
movzbl 0x5(%rax),%eax
and    $0x80,%eax
sar    $0x7,%eax
mov    %eax,-0x20(%rbp)
mov    -0x48(%rbp),%rax
movzbl 0x4(%rax),%eax
add    $0x4,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1506 <main+0x396>
movl   $0xbc,-0x2c(%rbp)
mov    0x2ae6(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x50(%rbp),%rcx
movzbl -0x35(%rbp),%r8d
lea    0xca7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    150b <main+0x39b>
jmp    1510 <main+0x3a0>
jmp    1515 <main+0x3a5>
cmpl   $0x0,-0x20(%rbp)
je     158d <main+0x41d>
cmpl   $0x0,-0x24(%rbp)
je     1551 <main+0x3e1>
mov    0x2aa0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x50(%rbp),%rcx
movzbl -0x35(%rbp),%r8d
lea    0xca1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1588 <main+0x418>
mov    0x2a78(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x50(%rbp),%rcx
movzbl -0x35(%rbp),%r8d
lea    0xcb2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rax
movzbl 0x5(%rax),%eax
and    $0x7f,%eax
mov    %al,%cl
mov    -0x48(%rbp),%rax
mov    %cl,0x5(%rax)
jmp    158d <main+0x41d>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%eax
add    $0xbc,%eax
mov    %eax,-0x30(%rbp)
jmp    12ef <main+0x17f>
mov    0x2a40(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
jmp    12cc <main+0x15c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     1351 <main+0x1e1>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1397 <main+0x227>
mov    %eax,%r14d
mov    $0xbc,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     13b4 <main+0x244>
mov    %rax,%rbx
mov    %rax,%rbp
add    $0x1,%rbp
xor    %r13d,%r13d
movl   $0x0,0xc(%rsp)
mov    %rbp,0x10(%rsp)
jmp    1221 <main+0xb1>
cs nopw 0x0(%rax,%rax,1)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%edx
lea    0xf86(%rip),%rsi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r13
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0xbc,%edx
call   1030 <write@plt>
test   %r12d,%r12d
je     1393 <main+0x223>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xbc,%edx
call   1040 <read@plt>
mov    %eax,%r12d
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1060 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1080 <ntohs@plt>
and    $0x1fff,%eax
mov    %eax,0x8(%rsp)
mov    $0x1,%r15b
testb  $0x20,0x3(%rbx)
je     12e5 <main+0x175>
cmpb   $0x0,0x4(%rbx)
je     12e5 <main+0x175>
mov    %r14d,%ebp
movzbl 0x5(%rbx),%r14d
test   $0x10,%r14b
je     12dd <main+0x16d>
cmpl   $0x0,0xc(%rsp)
je     1285 <main+0x115>
xor    %r15d,%r15d
jmp    12d5 <main+0x165>
mov    %r14d,%r8d
shr    $0x7,%r8d
mov    0x2d3d(%rip),%rcx
mov    (%rcx),%edi
xor    %r15d,%r15d
lea    0xe73(%rip),%rsi
mov    %eax,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r14b,%r14b
js     12d5 <main+0x165>
orb    $0x80,0x5(%rbx)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%edx
xor    %r15d,%r15d
lea    0xe75(%rip),%rsi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
movl   $0x1,0xc(%rsp)
mov    %ebp,%r14d
mov    0x10(%rsp),%rbp
movzbl 0x3(%rbx),%r8d
shr    $0x4,%r8d
and    $0x3,%r8d
cmp    $0x1,%r8b
je     11fe <main+0x8e>
test   %r8b,%r8b
je     11e0 <main+0x70>
cmpb   $0x0,0x5(%rbx)
jns    11fe <main+0x8e>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%edx
test   %r15b,%r15b
je     133b <main+0x1cb>
lea    0xeb7(%rip),%rsi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
andb   $0x7f,0x5(%rbx)
jmp    11fe <main+0x8e>
lea    0xe64(%rip),%rsi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11fe <main+0x8e>
mov    0x2c78(%rip),%rbx
mov    (%rbx),%edi
lea    0xc9f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xcbe(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xcec(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd1c(%rip),%rsi
jmp    13c4 <main+0x254>
xor    %eax,%eax
jmp    13d0 <main+0x260>
mov    0x2c32(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd44(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    13cb <main+0x25b>
mov    0x2c15(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [47 x i8] c"Usage: tsdiscont inputfile.ts > outputfile.ts\0A\00"
@.str.1 = [62 x i8] c"N.B: this tool will check for and fix discontinuity problems\0A\00"
@.str.2 = [64 x i8] c"If discontinuity is found on packet without pcr it is set to 0\0A\00"
@.str.3 = [66 x i8] c"If discontinuity is not found on first pcr packet it is set to 1\0A\00"
@.str.4 = [20 x i8] c"Can't find file %s\0A\00"
@.str.5 = [15 x i8] c"out of memory\0A\00"
@.str.6 = [46 x i8] c"PID: %d, Pos: %lld - First Pcr Discont : %X\0A\00"
@.str.7 = [58 x i8] c"PID: %d, Pos: %lld - Fixed first pcr discontinuity flag\0A\00"
@.str.8 = [44 x i8] c"PID: %d, Pos: %lld - Invalid packet found\0A\00"
@.str.9 = [64 x i8] c"PID: %d, Pos: %lld - Invalid adaptation field flag found : %X\0A\00"
@.str.10 = [57 x i8] c"PID: %d, Pos: %lld - discontinuity found with pcr : %X\0A\00"
@.str.11 = [67 x i8] c"PID: %d, Pos: %lld - Fixing discontinuity found WITHOUT pcr : %X\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i8 0, i8* %15
store i32 0, i32* %14
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 2
br i1 %20, label %21, label %27
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
br label %36
27: ; preds = %2
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
br label %235
36: ; preds = %21
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %43)
store i32 2, i32* %3
br label %235
45: ; preds = %36
store i32 1, i32* %14
%46 = load i32, i32* %14
%47 = mul i32 %46, 188
store i32 %47, i32* %14
%48 = load i32, i32* %14
%49 = call i8* @malloc(i32 %48)
store i8* %49, i8** %16
%50 = load i8*, i8** %16
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %45
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %3
br label %235
55: ; preds = %45
store i64 0, i64* %18
store i32 1, i32* %8
br label %56
56: ; preds = %229, %55
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %234
59: ; preds = %56
%60 = load i32, i32* %6
%61 = load i8*, i8** %16
%62 = load i32, i32* %14
%63 = call i32 @read(i32 %60, i8* %61, i32 %62)
store i32 %63, i32* %8
store i32 0, i32* %13
br label %64
64: ; preds = %226, %59
%65 = load i32, i32* %13
%66 = load i32, i32* %14
%67 = icmp ult i32 %65, %66
br i1 %67, label %68, label %229
68: ; preds = %64
%69 = load i8*, i8** %16
%70 = load i32, i32* %13
%71 = zext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8* %72, i8** %17
%73 = load i8*, i8** %17
%74 = getelementptr inbounds i8, i8* %73, i64 1
%75 = call i32 @memcpy(i32* %7, i8* %74, i32 2)
%76 = load i32, i32* %7
%77 = call i32 @ntohs(i32 %76)
store i32 %77, i32* %7
%78 = load i32, i32* %7
%79 = and i32 %78, 8191
store i32 %79, i32* %7
%80 = load i32, i32* %7
%81 = icmp slt i32 %80, 8192
br i1 %81, label %82, label %134
82: ; preds = %68
%83 = load i8*, i8** %17
%84 = getelementptr inbounds i8, i8* %83, i64 3
%85 = load i8, i8* %84
%86 = zext i8 %85 to i32
%87 = and i32 %86, 32
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %134
89: ; preds = %82
%90 = load i8*, i8** %17
%91 = getelementptr inbounds i8, i8* %90, i64 4
%92 = load i8, i8* %91
%93 = zext i8 %92 to i32
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %134
95: ; preds = %89
%96 = load i8*, i8** %17
%97 = getelementptr inbounds i8, i8* %96, i64 5
%98 = load i8, i8* %97
%99 = zext i8 %98 to i32
%100 = and i32 %99, 16
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %134
102: ; preds = %95
store i32 1, i32* %10
%103 = load i32, i32* %11
%104 = icmp ne i32 %103, 0
br i1 %104, label %133, label %105
105: ; preds = %102
%106 = load i8*, i8** %17
%107 = getelementptr inbounds i8, i8* %106, i64 5
%108 = load i8, i8* %107
%109 = zext i8 %108 to i32
%110 = and i32 %109, 128
%111 = ashr i32 %110, 7
store i32 %111, i32* %9
%112 = load i32, i32* @stderr
%113 = load i32, i32* %7
%114 = load i64, i64* %18
%115 = load i32, i32* %9
%116 = call i32 (i32, i8*, ...) @fprintf(i32 %112, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i32 %113, i64 %114, i32 %115)
%117 = load i32, i32* %9
%118 = icmp ne i32 %117, 0
br i1 %118, label %132, label %119
119: ; preds = %105
%120 = load i8*, i8** %17
%121 = getelementptr inbounds i8, i8* %120, i64 5
%122 = load i8, i8* %121
%123 = zext i8 %122 to i32
%124 = or i32 %123, 128
%125 = trunc i32 %124 to i8
%126 = load i8*, i8** %17
%127 = getelementptr inbounds i8, i8* %126, i64 5
store i8 %125, i8* %127
%128 = load i32, i32* @stderr
%129 = load i32, i32* %7
%130 = load i64, i64* %18
%131 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0), i32 %129, i64 %130)
br label %132
132: ; preds = %119, %105
store i32 1, i32* %11
br label %133
133: ; preds = %132, %102
br label %135
134: ; preds = %95, %89, %82, %68
store i32 0, i32* %10
br label %135
135: ; preds = %134, %133
store i32 0, i32* %9
%136 = load i8*, i8** %17
%137 = getelementptr inbounds i8, i8* %136, i64 3
%138 = load i8, i8* %137
%139 = zext i8 %138 to i32
%140 = ashr i32 %139, 4
%141 = and i32 %140, 3
%142 = trunc i32 %141 to i8
store i8 %142, i8* %15
%143 = load i8, i8* %15
%144 = zext i8 %143 to i32
%145 = icmp eq i32 %144, 0
br i1 %145, label %146, label %151
146: ; preds = %135
store i32 188, i32* %12
%147 = load i32, i32* @stderr
%148 = load i32, i32* %7
%149 = load i64, i64* %18
%150 = call i32 (i32, i8*, ...) @fprintf(i32 %147, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i32 %148, i64 %149)
br label %194
151: ; preds = %135
%152 = load i8, i8* %15
%153 = zext i8 %152 to i32
%154 = icmp eq i32 %153, 1
br i1 %154, label %155, label %156
155: ; preds = %151
store i32 4, i32* %12
br label %193
156: ; preds = %151
%157 = load i8, i8* %15
%158 = zext i8 %157 to i32
%159 = icmp eq i32 %158, 2
br i1 %159, label %160, label %167
160: ; preds = %156
%161 = load i8*, i8** %17
%162 = getelementptr inbounds i8, i8* %161, i64 5
%163 = load i8, i8* %162
%164 = zext i8 %163 to i32
%165 = and i32 %164, 128
%166 = ashr i32 %165, 7
store i32 %166, i32* %9
store i32 188, i32* %12
br label %192
167: ; preds = %156
%168 = load i8, i8* %15
%169 = zext i8 %168 to i32
%170 = icmp eq i32 %169, 3
br i1 %170, label %171, label %184
171: ; preds = %167
%172 = load i8*, i8** %17
%173 = getelementptr inbounds i8, i8* %172, i64 5
%174 = load i8, i8* %173
%175 = zext i8 %174 to i32
%176 = and i32 %175, 128
%177 = ashr i32 %176, 7
store i32 %177, i32* %9
%178 = load i8*, i8** %17
%179 = getelementptr inbounds i8, i8* %178, i64 4
%180 = load i8, i8* %179
%181 = zext i8 %180 to i32
%182 = add nsw i32 4, %181
%183 = add nsw i32 %182, 1
store i32 %183, i32* %12
br label %191
184: ; preds = %167
store i32 188, i32* %12
%185 = load i32, i32* @stderr
%186 = load i32, i32* %7
%187 = load i64, i64* %18
%188 = load i8, i8* %15
%189 = zext i8 %188 to i32
%190 = call i32 (i32, i8*, ...) @fprintf(i32 %185, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.9, i64 0, i64 0), i32 %186, i64 %187, i32 %189)
br label %191
191: ; preds = %184, %171
br label %192
192: ; preds = %191, %160
br label %193
193: ; preds = %192, %155
br label %194
194: ; preds = %193, %146
%195 = load i32, i32* %9
%196 = icmp ne i32 %195, 0
br i1 %196, label %197, label %223
197: ; preds = %194
%198 = load i32, i32* %10
%199 = icmp ne i32 %198, 0
br i1 %199, label %200, label %207
200: ; preds = %197
%201 = load i32, i32* @stderr
%202 = load i32, i32* %7
%203 = load i64, i64* %18
%204 = load i8, i8* %15
%205 = zext i8 %204 to i32
%206 = call i32 (i32, i8*, ...) @fprintf(i32 %201, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.10, i64 0, i64 0), i32 %202, i64 %203, i32 %205)
br label %222
207: ; preds = %197
%208 = load i32, i32* @stderr
%209 = load i32, i32* %7
%210 = load i64, i64* %18
%211 = load i8, i8* %15
%212 = zext i8 %211 to i32
%213 = call i32 (i32, i8*, ...) @fprintf(i32 %208, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.11, i64 0, i64 0), i32 %209, i64 %210, i32 %212)
%214 = load i8*, i8** %17
%215 = getelementptr inbounds i8, i8* %214, i64 5
%216 = load i8, i8* %215
%217 = zext i8 %216 to i32
%218 = and i32 %217, 127
%219 = trunc i32 %218 to i8
%220 = load i8*, i8** %17
%221 = getelementptr inbounds i8, i8* %220, i64 5
store i8 %219, i8* %221
br label %222
222: ; preds = %207, %200
br label %223
223: ; preds = %222, %194
%224 = load i64, i64* %18
%225 = add i64 %224, 1
store i64 %225, i64* %18
br label %226
226: ; preds = %223
%227 = load i32, i32* %13
%228 = add i32 %227, 188
store i32 %228, i32* %13
br label %64
229: ; preds = %64
%230 = load i32, i32* @STDOUT_FILENO
%231 = load i8*, i8** %16
%232 = load i32, i32* %14
%233 = call i32 @write(i32 %230, i8* %231, i32 %232)
br label %56
234: ; preds = %56
store i32 0, i32* %3
br label %235
235: ; preds = %234, %52, %39, %27
%236 = load i32, i32* %3
ret i32 %236
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [47 x i8] c"Usage: tsdiscont inputfile.ts > outputfile.ts\0A\00"
@.str.1 = [62 x i8] c"N.B: this tool will check for and fix discontinuity problems\0A\00"
@.str.2 = [64 x i8] c"If discontinuity is found on packet without pcr it is set to 0\0A\00"
@.str.3 = [66 x i8] c"If discontinuity is not found on first pcr packet it is set to 1\0A\00"
@.str.4 = [20 x i8] c"Can't find file %s\0A\00"
@.str.5 = [15 x i8] c"out of memory\0A\00"
@.str.6 = [46 x i8] c"PID: %d, Pos: %lld - First Pcr Discont : %X\0A\00"
@.str.7 = [58 x i8] c"PID: %d, Pos: %lld - Fixed first pcr discontinuity flag\0A\00"
@.str.8 = [44 x i8] c"PID: %d, Pos: %lld - Invalid packet found\0A\00"
@.str.10 = [57 x i8] c"PID: %d, Pos: %lld - discontinuity found with pcr : %X\0A\00"
@.str.11 = [67 x i8] c"PID: %d, Pos: %lld - Fixing discontinuity found WITHOUT pcr : %X\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %21, label %25
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0))
br label %97
21: ; preds = %6
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %7
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %97
25: ; preds = %6
%26 = tail call dereferenceable_or_null(188) i8* @malloc(i32 188)
%27 = icmp eq i8* %26, null
br i1 %27, label %34, label %28
28: ; preds = %25
%29 = getelementptr inbounds i8, i8* %26, i64 1
%30 = getelementptr inbounds i8, i8* %26, i64 3
%31 = getelementptr inbounds i8, i8* %26, i64 4
%32 = getelementptr inbounds i8, i8* %26, i64 5
%33 = getelementptr inbounds i8, i8* %26, i64 5
br label %37
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %97
37: ; preds = %28, %92
%38 = phi i64 [ %93, %92 ], [ 0, %28 ]
%39 = phi i32 [ %71, %92 ], [ 0, %28 ]
%40 = call i32 @read(i32 %10, i8* %26, i32 188)
%41 = call i32 @memcpy(i32* %3, i8* %29, i32 2)
%42 = load i32, i32* %3
%43 = call i32 @ntohs(i32 %42)
%44 = and i32 %43, 8191
store i32 %44, i32* %3
%45 = load i8, i8* %30
%46 = and i8 %45, 32
%47 = icmp eq i8 %46, 0
br i1 %47, label %69, label %48
48: ; preds = %37
%49 = load i8, i8* %31
%50 = icmp eq i8 %49, 0
br i1 %50, label %69, label %51
51: ; preds = %48
%52 = load i8, i8* %32
%53 = zext i8 %52 to i32
%54 = and i32 %53, 16
%55 = icmp eq i32 %54, 0
br i1 %55, label %69, label %56
56: ; preds = %51
%57 = icmp eq i32 %39, 0
br i1 %57, label %58, label %69
58: ; preds = %56
%59 = lshr i32 %53, 7
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i32 %44, i64 %38, i32 %59)
%62 = icmp sgt i8 %52, -1
br i1 %62, label %63, label %69
63: ; preds = %58
%64 = load i8, i8* %32
%65 = or i8 %64, -128
store i8 %65, i8* %32
%66 = load i32, i32* @stderr
%67 = load i32, i32* %3
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0), i32 %67, i64 %38)
br label %69
69: ; preds = %37, %48, %51, %58, %63, %56
%70 = phi i1 [ false, %56 ], [ false, %63 ], [ false, %58 ], [ true, %51 ], [ true, %48 ], [ true, %37 ]
%71 = phi i32 [ 1, %56 ], [ 1, %63 ], [ 1, %58 ], [ %39, %51 ], [ %39, %48 ], [ %39, %37 ]
%72 = load i8, i8* %30
%73 = lshr i8 %72, 4
%74 = and i8 %73, 3
%75 = zext i8 %74 to i32
switch i8 %74, label %80 [
i8 0, label %76
i8 1, label %92
]
76: ; preds = %69
%77 = load i32, i32* @stderr
%78 = load i32, i32* %3
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i32 %78, i64 %38)
br label %92
80: ; preds = %69
%81 = load i8, i8* %33
%82 = icmp sgt i8 %81, -1
br i1 %82, label %92, label %83
83: ; preds = %80
%84 = load i32, i32* @stderr
%85 = load i32, i32* %3
br i1 %70, label %88, label %86
86: ; preds = %83
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.10, i64 0, i64 0), i32 %85, i64 %38, i32 %75)
br label %92
88: ; preds = %83
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.11, i64 0, i64 0), i32 %85, i64 %38, i32 %75)
%90 = load i8, i8* %33
%91 = and i8 %90, 127
store i8 %91, i8* %33
br label %92
92: ; preds = %69, %76, %86, %88, %80
%93 = add i64 %38, 1
%94 = load i32, i32* @STDOUT_FILENO
%95 = call i32 @write(i32 %94, i8* %26, i32 188)
%96 = icmp eq i32 %40, 0
br i1 %96, label %97, label %37
97: ; preds = %92, %34, %21, %12
%98 = phi i32 [ 2, %21 ], [ 2, %34 ], [ 2, %12 ], [ 0, %92 ]
ret i32 %98
}