#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*/ O_RDONLY;
int /*<<< orphan*/ add_payload(unsigned char *, int);
unsigned short atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcpy(unsigned short *, unsigned char *, int);
unsigned short ntohs(unsigned short);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int byte_read;
  int fd_ts;
  int ts_header_size;
  unsigned char adapt_field;
  unsigned short pid;
  unsigned short payload_pid;
  unsigned char current_packet[188];
  int pusi = 0;
  if (argc >= 3) {
    fd_ts = open(argv[1], O_RDONLY);
    payload_pid = atoi(argv[2]);
  } else {
    fprintf(stderr, "Usage: 'ts2sec filename.ts payload_pid > sections'\n");
    fprintf(
        stderr,
        "the tool manages corrupted ts, only complete sections are output\n");
    return 2;
  }
  if (fd_ts < 0) {
    fprintf(stderr, "Can't find file %s\n", argv[1]);
    return 2;
  }
  if (payload_pid > 8192 - 2) {
    fprintf(stderr, "Invalid PID, range is [0..8190]\n");
  }
  byte_read = 1;
  while (byte_read) {
    byte_read = read(fd_ts, current_packet, 188);
    memcpy(&pid, current_packet + 1, 2);
    pid = ntohs(pid);
    pid = pid & 0x1fff;
    if (pid == payload_pid && byte_read > 0) {
      if (current_packet[0] != 0x47) {
        fprintf(stderr, "missing sync byte, quitting\n");
        return 0;
      }
      pusi = (current_packet[1] & 0x40);
      ts_header_size = 4;
      adapt_field = (current_packet[3] >> 4) & 0x03;
      if (adapt_field == 0) {
        ts_header_size = 188;
        fprintf(stderr, "invalid packet!\n");
      } else if (adapt_field == 1) {
        ;
      } else if (adapt_field == 2) {
        ts_header_size = 188;
      } else if (adapt_field == 3) {
        ts_header_size += current_packet[ts_header_size] + 1;
      }
      if (ts_header_size < 188) {
        if (pusi) {
          if (current_packet[ts_header_size] > 0)
            add_payload(current_packet + ts_header_size + 1,
                        current_packet[ts_header_size]);
          add_payload(current_packet + ts_header_size + 1 +
                          current_packet[ts_header_size],
                      188 - ts_header_size - 1 -
                          current_packet[ts_header_size]);
        } else {
          add_payload(current_packet + ts_header_size, 188 - ts_header_size);
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0xe4(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11cc <main+0x5c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %ax,-0x22(%rbp)
jmp    1206 <main+0x96>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1485 <main+0x315>
cmpl   $0x0,-0x18(%rbp)
jge    123b <main+0xcb>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1485 <main+0x315>
movzwl -0x22(%rbp),%eax
cmp    $0x1ffe,%eax
jle    1261 <main+0xf1>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     147e <main+0x30e>
mov    -0x18(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0xbc,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
lea    -0xe0(%rbp),%rsi
add    $0x1,%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1050 <memcpy@plt>
movzwl -0x20(%rbp),%edi
call   1060 <ntohs@plt>
mov    %ax,-0x20(%rbp)
movzwl -0x20(%rbp),%eax
and    $0x1fff,%eax
mov    %ax,-0x20(%rbp)
movzwl -0x20(%rbp),%eax
movzwl -0x22(%rbp),%ecx
cmp    %ecx,%eax
jne    1479 <main+0x309>
cmpl   $0x0,-0x14(%rbp)
jle    1479 <main+0x309>
movzbl -0xe0(%rbp),%eax
cmp    $0x47,%eax
je     1309 <main+0x199>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1485 <main+0x315>
movzbl -0xdf(%rbp),%eax
and    $0x40,%eax
mov    %eax,-0xe4(%rbp)
movl   $0x4,-0x1c(%rbp)
movzbl -0xdd(%rbp),%eax
sar    $0x4,%eax
and    $0x3,%eax
mov    %al,-0x1d(%rbp)
movzbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
jne    1360 <main+0x1f0>
movl   $0xbc,-0x1c(%rbp)
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    13bc <main+0x24c>
movzbl -0x1d(%rbp),%eax
cmp    $0x1,%eax
jne    1372 <main+0x202>
jmp    13b7 <main+0x247>
movzbl -0x1d(%rbp),%eax
cmp    $0x2,%eax
jne    138b <main+0x21b>
movl   $0xbc,-0x1c(%rbp)
jmp    13b2 <main+0x242>
movzbl -0x1d(%rbp),%eax
cmp    $0x3,%eax
jne    13ad <main+0x23d>
movslq -0x1c(%rbp),%rax
movzbl -0xe0(%rbp,%rax,1),%eax
ff
add    $0x1,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    13b2 <main+0x242>
jmp    13b7 <main+0x247>
jmp    13bc <main+0x24c>
cmpl   $0xbc,-0x1c(%rbp)
jge    1474 <main+0x304>
cmpl   $0x0,-0xe4(%rbp)
je     1454 <main+0x2e4>
movslq -0x1c(%rbp),%rax
movzbl -0xe0(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
jle    140e <main+0x29e>
lea    -0xe0(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
movslq -0x1c(%rbp),%rax
movzbl -0xe0(%rbp,%rax,1),%esi
ff
call   1090 <add_payload@plt>
lea    -0xe0(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
movslq -0x1c(%rbp),%rax
movzbl -0xe0(%rbp,%rax,1),%eax
ff
cltq
add    %rax,%rdi
mov    $0xbc,%esi
sub    -0x1c(%rbp),%esi
sub    $0x1,%esi
movslq -0x1c(%rbp),%rax
movzbl -0xe0(%rbp,%rax,1),%eax
ff
sub    %eax,%esi
call   1090 <add_payload@plt>
jmp    146f <main+0x2ff>
lea    -0xe0(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    $0xbc,%esi
sub    -0x1c(%rbp),%esi
call   1090 <add_payload@plt>
jmp    1474 <main+0x304>
jmp    1479 <main+0x309>
jmp    1268 <main+0xf8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
cmp    $0x3,%edi
jl     12fe <main+0x18e>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1330 <main+0x1c0>
mov    %eax,%r12d
mov    0x10(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
movzwl %ax,%eax
cmp    $0x1fff,%eax
jb     11d7 <main+0x67>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xeba(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0x11(%rsp),%rbp
lea    0x10(%rsp),%rbx
mov    %r14w,0xc(%rsp)
jmp    1209 <main+0x99>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xecf(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r13d,%r13d
je     132c <main+0x1bc>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xbc,%edx
call   1030 <read@plt>
mov    %eax,%r13d
lea    0xe(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
movzwl 0xe(%rsp),%edi
call   1060 <ntohs@plt>
and    $0x1fff,%eax
mov    %ax,0xe(%rsp)
test   %r13d,%r13d
jle    1200 <main+0x90>
cmp    %r14w,%ax
jne    1200 <main+0x90>
cmpb   $0x47,0x10(%rsp)
jne    1352 <main+0x1e2>
movzbl 0x13(%rsp),%ecx
shr    $0x4,%cl
and    $0x3,%cl
cmp    $0x2,%cl
je     1200 <main+0x90>
movzbl 0x11(%rsp),%eax
cmp    $0x1,%cl
je     1293 <main+0x123>
test   %cl,%cl
je     11e9 <main+0x79>
movzbl 0x14(%rsp),%r15d
cmp    $0xb6,%r15d
ja     1200 <main+0x90>
add    $0x5,%r15d
jmp    1299 <main+0x129>
mov    $0x4,%r15d
mov    %r15d,%ecx
lea    (%rsp,%rcx,1),%r14
add    $0x10,%r14
test   $0x40,%al
jne    12b5 <main+0x145>
mov    $0xbc,%esi
sub    %r15d,%esi
mov    %r14,%rdi
jmp    12e4 <main+0x174>
movzbl (%r14),%esi
test   %esi,%esi
je     12cc <main+0x15c>
lea    0x1(%r14),%rdi
call   1090 <add_payload@plt>
movzbl (%r14),%eax
jmp    12ce <main+0x15e>
xor    %eax,%eax
movzbl %al,%eax
lea    (%r14,%rax,1),%rdi
add    $0x1,%rdi
add    %eax,%r15d
mov    $0xbb,%esi
sub    %r15d,%esi
call   1090 <add_payload@plt>
lea    0x11(%rsp),%rbp
lea    0x10(%rsp),%rbx
movzwl 0xc(%rsp),%r14d
jmp    1200 <main+0x90>
mov    0x2cd3(%rip),%rbx
mov    (%rbx),%edi
lea    0xcf2(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd16(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%ebx
jmp    136b <main+0x1fb>
xor    %ebx,%ebx
jmp    136b <main+0x1fb>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xd32(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%ebx
jmp    136b <main+0x1fb>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0xd8,%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 = [52 x i8] c"Usage: 'ts2sec filename.ts payload_pid > sections'\0A\00"
@.str.1 = [66 x i8] c"the tool manages corrupted ts, only complete sections are output\0A\00"
@.str.2 = [20 x i8] c"Can't find file %s\0A\00"
@.str.3 = [33 x i8] c"Invalid PID, range is [0..8190]\0A\00"
@.str.4 = [29 x i8] c"missing sync byte, quitting\0A\00"
@.str.5 = [17 x i8] c"invalid packet!\0A\00"
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 i8
%10 = alloca i16
%11 = alloca i16
%12 = alloca [188 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %13
%14 = load i32, i32* %4
%15 = icmp sge i32 %14, 3
br i1 %15, label %16, label %26
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %7
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call zeroext i16 @atoi(i8* %24)
store i16 %25, i16* %11
br label %31
26: ; preds = %2
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %183
31: ; preds = %16
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 2, i32* %3
br label %183
40: ; preds = %31
%41 = load i16, i16* %11
%42 = zext i16 %41 to i32
%43 = icmp sgt i32 %42, 8190
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %44, %40
store i32 1, i32* %6
br label %48
48: ; preds = %181, %47
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %182
51: ; preds = %48
%52 = load i32, i32* %7
%53 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 188)
store i32 %54, i32* %6
%55 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 0
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = call i32 @memcpy(i16* %10, i8* %56, i32 2)
%58 = load i16, i16* %10
%59 = call zeroext i16 @ntohs(i16 zeroext %58)
store i16 %59, i16* %10
%60 = load i16, i16* %10
%61 = zext i16 %60 to i32
%62 = and i32 %61, 8191
%63 = trunc i32 %62 to i16
store i16 %63, i16* %10
%64 = load i16, i16* %10
%65 = zext i16 %64 to i32
%66 = load i16, i16* %11
%67 = zext i16 %66 to i32
%68 = icmp eq i32 %65, %67
br i1 %68, label %69, label %181
69: ; preds = %51
%70 = load i32, i32* %6
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %181
72: ; preds = %69
%73 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 0
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = icmp ne i32 %75, 71
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %183
80: ; preds = %72
%81 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 1
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = and i32 %83, 64
store i32 %84, i32* %13
store i32 4, i32* %8
%85 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 3
%86 = load i8, i8* %85
%87 = zext i8 %86 to i32
%88 = ashr i32 %87, 4
%89 = and i32 %88, 3
%90 = trunc i32 %89 to i8
store i8 %90, i8* %9
%91 = load i8, i8* %9
%92 = zext i8 %91 to i32
%93 = icmp eq i32 %92, 0
br i1 %93, label %94, label %97
94: ; preds = %80
store i32 188, i32* %8
%95 = load i32, i32* @stderr
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %123
97: ; preds = %80
%98 = load i8, i8* %9
%99 = zext i8 %98 to i32
%100 = icmp eq i32 %99, 1
br i1 %100, label %101, label %102
101: ; preds = %97
br label %122
102: ; preds = %97
%103 = load i8, i8* %9
%104 = zext i8 %103 to i32
%105 = icmp eq i32 %104, 2
br i1 %105, label %106, label %107
106: ; preds = %102
store i32 188, i32* %8
br label %121
107: ; preds = %102
%108 = load i8, i8* %9
%109 = zext i8 %108 to i32
%110 = icmp eq i32 %109, 3
br i1 %110, label %111, label %120
111: ; preds = %107
%112 = load i32, i32* %8
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 %113
%115 = load i8, i8* %114
%116 = zext i8 %115 to i32
%117 = add nsw i32 %116, 1
%118 = load i32, i32* %8
%119 = add nsw i32 %118, %117
store i32 %119, i32* %8
br label %120
120: ; preds = %111, %107
br label %121
121: ; preds = %120, %106
br label %122
122: ; preds = %121, %101
br label %123
123: ; preds = %122, %94
%124 = load i32, i32* %8
%125 = icmp slt i32 %124, 188
br i1 %125, label %126, label %180
126: ; preds = %123
%127 = load i32, i32* %13
%128 = icmp ne i32 %127, 0
br i1 %128, label %129, label %171
129: ; preds = %126
%130 = load i32, i32* %8
%131 = sext i32 %130 to i64
%132 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 %131
%133 = load i8, i8* %132
%134 = zext i8 %133 to i32
%135 = icmp sgt i32 %134, 0
br i1 %135, label %136, label %148
136: ; preds = %129
%137 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 0
%138 = load i32, i32* %8
%139 = sext i32 %138 to i64
%140 = getelementptr inbounds i8, i8* %137, i64 %139
%141 = getelementptr inbounds i8, i8* %140, i64 1
%142 = load i32, i32* %8
%143 = sext i32 %142 to i64
%144 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 %143
%145 = load i8, i8* %144
%146 = zext i8 %145 to i32
%147 = call i32 @add_payload(i8* %141, i32 %146)
br label %148
148: ; preds = %136, %129
%149 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 0
%150 = load i32, i32* %8
%151 = sext i32 %150 to i64
%152 = getelementptr inbounds i8, i8* %149, i64 %151
%153 = getelementptr inbounds i8, i8* %152, i64 1
%154 = load i32, i32* %8
%155 = sext i32 %154 to i64
%156 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 %155
%157 = load i8, i8* %156
%158 = zext i8 %157 to i32
%159 = sext i32 %158 to i64
%160 = getelementptr inbounds i8, i8* %153, i64 %159
%161 = load i32, i32* %8
%162 = sub nsw i32 188, %161
%163 = sub nsw i32 %162, 1
%164 = load i32, i32* %8
%165 = sext i32 %164 to i64
%166 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 %165
%167 = load i8, i8* %166
%168 = zext i8 %167 to i32
%169 = sub nsw i32 %163, %168
%170 = call i32 @add_payload(i8* %160, i32 %169)
br label %179
171: ; preds = %126
%172 = getelementptr inbounds [188 x i8], [188 x i8]* %12, i64 0, i64 0
%173 = load i32, i32* %8
%174 = sext i32 %173 to i64
%175 = getelementptr inbounds i8, i8* %172, i64 %174
%176 = load i32, i32* %8
%177 = sub nsw i32 188, %176
%178 = call i32 @add_payload(i8* %175, i32 %177)
br label %179
179: ; preds = %171, %148
br label %180
180: ; preds = %179, %123
br label %181
181: ; preds = %180, %69, %51
br label %48
182: ; preds = %48
store i32 0, i32* %3
br label %183
183: ; preds = %182, %77, %34, %26
%184 = load i32, i32* %3
ret i32 %184
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [52 x i8] c"Usage: 'ts2sec filename.ts payload_pid > sections'\0A\00"
@.str.1 = [66 x i8] c"the tool manages corrupted ts, only complete sections are output\0A\00"
@.str.2 = [20 x i8] c"Can't find file %s\0A\00"
@.str.3 = [33 x i8] c"Invalid PID, range is [0..8190]\0A\00"
@.str.4 = [29 x i8] c"missing sync byte, quitting\0A\00"
@.str.5 = [17 x i8] c"invalid packet!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca [188 x i8]
%5 = bitcast i16* %3 to i8*
%6 = getelementptr inbounds [188 x i8], [188 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 2
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %19, label %23
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %91
19: ; preds = %8
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %9
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %91
23: ; preds = %8
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call zeroext i16 @atoi(i8* %25)
%27 = icmp ugt i16 %26, 8190
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %28, %23
%32 = getelementptr inbounds [188 x i8], [188 x i8]* %4, i64 0, i64 1
%33 = getelementptr inbounds [188 x i8], [188 x i8]* %4, i64 0, i64 3
%34 = getelementptr inbounds [188 x i8], [188 x i8]* %4, i64 0, i64 4
br label %35
35: ; preds = %31, %89
%36 = call i32 @read(i32 %12, i8* %6, i32 188)
%37 = call i32 @memcpy(i16* %3, i8* %32, i32 2)
%38 = load i16, i16* %3
%39 = call zeroext i16 @ntohs(i16 zeroext %38)
%40 = and i16 %39, 8191
store i16 %40, i16* %3
%41 = icmp eq i16 %40, %26
%42 = icmp sgt i32 %36, 0
%43 = and i1 %42, %41
br i1 %43, label %44, label %89
44: ; preds = %35
%45 = load i8, i8* %6
%46 = icmp eq i8 %45, 71
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %91
50: ; preds = %44
%51 = load i8, i8* %32
%52 = and i8 %51, 64
%53 = load i8, i8* %33
%54 = lshr i8 %53, 4
%55 = and i8 %54, 3
switch i8 %55, label %59 [
i8 0, label %56
i8 1, label %64
i8 2, label %89
]
56: ; preds = %50
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %89
59: ; preds = %50
%60 = load i8, i8* %34
%61 = zext i8 %60 to i32
%62 = add nuw nsw i32 %61, 5
%63 = icmp ult i8 %60, -73
br i1 %63, label %64, label %89
64: ; preds = %50, %59
%65 = phi i32 [ %62, %59 ], [ 4, %50 ]
%66 = icmp eq i8 %52, 0
%67 = zext i32 %65 to i64
%68 = getelementptr inbounds [188 x i8], [188 x i8]* %4, i64 0, i64 %67
br i1 %66, label %86, label %69
69: ; preds = %64
%70 = load i8, i8* %68
%71 = icmp eq i8 %70, 0
br i1 %71, label %77, label %72
72: ; preds = %69
%73 = zext i8 %70 to i32
%74 = getelementptr inbounds i8, i8* %68, i64 1
%75 = call i32 @add_payload(i8* %74, i32 %73)
%76 = load i8, i8* %68
br label %77
77: ; preds = %72, %69
%78 = phi i8 [ %76, %72 ], [ 0, %69 ]
%79 = getelementptr inbounds i8, i8* %68, i64 1
%80 = zext i8 %78 to i32
%81 = zext i8 %78 to i64
%82 = getelementptr inbounds i8, i8* %79, i64 %81
%83 = add nuw nsw i32 %65, %80
%84 = sub nsw i32 187, %83
%85 = call i32 @add_payload(i8* %82, i32 %84)
br label %89
86: ; preds = %64
%87 = sub nuw nsw i32 188, %65
%88 = call i32 @add_payload(i8* %68, i32 %87)
br label %89
89: ; preds = %50, %56, %59, %86, %77, %35
%90 = icmp eq i32 %36, 0
br i1 %90, label %91, label %35
91: ; preds = %89, %47, %19, %14
%92 = phi i32 [ 2, %19 ], [ 0, %47 ], [ 2, %14 ], [ 0, %89 ]
ret i32 %92
}