#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*/ STDOUT_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcpy(unsigned short *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int, int);
unsigned short ntohs(unsigned short);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int /*<<< orphan*/, unsigned char *, int);
int main(int argc, char *argv[]) {
  int open_file;
  int byte_read;
  int fd_ts;
  unsigned short pid;
  unsigned char packet[188];
  unsigned char pid_cc_table[8192];
  if (argc >= 2) {
    open_file = 1;
    fd_ts = open(argv[open_file], O_RDONLY);
    if (fd_ts < 0) {
      fprintf(stderr, "Can't find file %s\n", argv[open_file]);
      return 2;
    }
  } else {
    fprintf(stderr, "Usage: 'tsfixcc filename.ts > filenamefixed.ts'\n");
    fprintf(stderr,
            "Tsfixcc increases countinuity counter of every packet following "
            "each other without adapatation field parsing'\n");
    fprintf(stderr, "Useful to link ts of sections in a single ts'\n");
    return 2;
  }
  memset(pid_cc_table, 0x10, 8192);
  while (1) {
    byte_read = 0;
    byte_read = read(fd_ts, packet, 188);
    if (byte_read < 188) {
      return 0;
    }
    memcpy(&pid, packet + 1, 2);
    pid = ntohs(pid);
    pid = pid & 0x1fff;
    if (pid < 8192) {
      if (pid_cc_table[pid] == 0x10) {
        fprintf(stderr, "new pid entry %d\n", pid);
        pid_cc_table[pid] = 0;
      } else {
        pid_cc_table[pid] = (pid_cc_table[pid] + 1) % 0x10;
      }
      packet[3] = (pid_cc_table[pid] | (packet[3] & 0xf0));
    }
    write(STDOUT_FILENO, packet, 188);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20e0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11f5 <main+0x85>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f0 <main+0x80>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    137a <main+0x20a>
jmp    1246 <main+0xd6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    137a <main+0x20a>
lea    -0x20e0(%rbp),%rdi
mov    $0x10,%esi
mov    $0x2000,%edx
call   1040 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0xbc,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xbc,-0x18(%rbp)
jge    1293 <main+0x123>
movl   $0x0,-0x4(%rbp)
jmp    137a <main+0x20a>
lea    -0xe0(%rbp),%rsi
add    $0x1,%rsi
lea    -0x1e(%rbp),%rdi
mov    $0x2,%edx
call   1070 <memcpy@plt>
movzwl -0x1e(%rbp),%edi
call   1080 <ntohs@plt>
mov    %ax,-0x1e(%rbp)
movzwl -0x1e(%rbp),%eax
and    $0x1fff,%eax
mov    %ax,-0x1e(%rbp)
movzwl -0x1e(%rbp),%eax
cmp    $0x2000,%eax
jge    135b <main+0x1eb>
movzwl -0x1e(%rbp),%eax
movzbl -0x20e0(%rbp,%rax,1),%eax
ff
cmp    $0x10,%eax
jne    1316 <main+0x1a6>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
movzwl -0x1e(%rbp),%edx
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movzwl -0x1e(%rbp),%eax
movb   $0x0,-0x20e0(%rbp,%rax,1)
jmp    133a <main+0x1ca>
movzwl -0x1e(%rbp),%eax
movzbl -0x20e0(%rbp,%rax,1),%eax
ff
add    $0x1,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %dl,%cl
movzwl -0x1e(%rbp),%eax
mov    %cl,-0x20e0(%rbp,%rax,1)
movzwl -0x1e(%rbp),%eax
movzbl -0x20e0(%rbp,%rax,1),%eax
ff
movzbl -0xdd(%rbp),%ecx
and    $0xf0,%ecx
or     %ecx,%eax
mov    %al,-0xdd(%rbp)
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
lea    -0xe0(%rbp),%rsi
mov    $0xbc,%edx
call   1030 <write@plt>
jmp    125c <main+0xec>
mov    -0x4(%rbp),%eax
add    $0x20e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20d8,%rsp
cmp    $0x2,%edi
jl     12ae <main+0x13e>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     12ec <main+0x17c>
mov    %eax,%ebx
lea    0xd0(%rsp),%rdi
mov    $0x10,%esi
mov    $0x2000,%edx
call   1040 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xbc,%edx
call   1050 <read@plt>
xor    %ecx,%ecx
cmp    $0xbc,%eax
jl     1310 <main+0x1a0>
lea    0x11(%rsp),%r15
lea    0xe(%rsp),%r13
mov    0x2e01(%rip),%r12
lea    0x10(%rsp),%rbp
jmp    1249 <main+0xd9>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r14b
and    $0xf,%r14b
mov    %r14b,0xd0(%rsp,%rax,1)
movzbl 0x13(%rsp),%eax
and    $0xf0,%al
or     %r14b,%al
mov    %al,0x13(%rsp)
mov    (%r12),%edi
mov    %rbp,%rsi
mov    $0xbc,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xbc,%edx
call   1050 <read@plt>
cmp    $0xbb,%eax
jle    130e <main+0x19e>
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
movzwl 0xe(%rsp),%edi
call   1080 <ntohs@plt>
movzwl %ax,%eax
and    $0x1fff,%eax
mov    %ax,0xe(%rsp)
movzbl 0xd0(%rsp,%rax,1),%r14d
cmp    $0x10,%r14b
jne    1200 <main+0x90>
movzwl %ax,%edx
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
xor    %r14d,%r14d
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
movzwl 0xe(%rsp),%eax
movb   $0x0,0xd0(%rsp,%rax,1)
jmp    1210 <main+0xa0>
mov    0x2d1b(%rip),%rbx
mov    (%rbx),%edi
lea    0xd56(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd77(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdd6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%ecx
jmp    1310 <main+0x1a0>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xd00(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%ecx
jmp    1310 <main+0x1a0>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x20d8,%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 = [20 x i8] c"Can't find file %s\0A\00"
@.str.1 = [49 x i8] c"Usage: 'tsfixcc filename.ts > filenamefixed.ts'\0A\00"
@.str.2 = [111 x i8] c"Tsfixcc increases countinuity counter of every packet following each other without adapatation field parsing'\0A\00"
@.str.3 = [47 x i8] c"Useful to link ts of sections in a single ts'\0A\00"
@.str.4 = [18 x i8] c"new pid entry %d\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 i16
%10 = alloca [188 x i8]
%11 = alloca [8192 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 2
br i1 %13, label %14, label %33
14: ; preds = %2
store i32 1, i32* %6
%15 = load i8**, i8*** %5
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %15, i64 %17
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %30)
store i32 2, i32* %3
br label %107
32: ; preds = %14
br label %40
33: ; preds = %2
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([111 x i8], [111 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
br label %107
40: ; preds = %32
%41 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i32 16, i32 8192)
br label %43
43: ; preds = %40, %103
store i32 0, i32* %7
%44 = load i32, i32* %8
%45 = getelementptr inbounds [188 x i8], [188 x i8]* %10, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 188)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 188
br i1 %48, label %49, label %50
49: ; preds = %43
store i32 0, i32* %3
br label %107
50: ; preds = %43
%51 = getelementptr inbounds [188 x i8], [188 x i8]* %10, i64 0, i64 0
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = call i32 @memcpy(i16* %9, i8* %52, i32 2)
%54 = load i16, i16* %9
%55 = call zeroext i16 @ntohs(i16 zeroext %54)
store i16 %55, i16* %9
%56 = load i16, i16* %9
%57 = zext i16 %56 to i32
%58 = and i32 %57, 8191
%59 = trunc i32 %58 to i16
store i16 %59, i16* %9
%60 = load i16, i16* %9
%61 = zext i16 %60 to i32
%62 = icmp slt i32 %61, 8192
br i1 %62, label %63, label %103
63: ; preds = %50
%64 = load i16, i16* %9
%65 = zext i16 %64 to i64
%66 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 %65
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = icmp eq i32 %68, 16
br i1 %69, label %70, label %78
70: ; preds = %63
%71 = load i32, i32* @stderr
%72 = load i16, i16* %9
%73 = zext i16 %72 to i32
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %73)
%75 = load i16, i16* %9
%76 = zext i16 %75 to i64
%77 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 %76
store i8 0, i8* %77
br label %90
78: ; preds = %63
%79 = load i16, i16* %9
%80 = zext i16 %79 to i64
%81 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 %80
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = add nsw i32 %83, 1
%85 = srem i32 %84, 16
%86 = trunc i32 %85 to i8
%87 = load i16, i16* %9
%88 = zext i16 %87 to i64
%89 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 %88
store i8 %86, i8* %89
br label %90
90: ; preds = %78, %70
%91 = load i16, i16* %9
%92 = zext i16 %91 to i64
%93 = getelementptr inbounds [8192 x i8], [8192 x i8]* %11, i64 0, i64 %92
%94 = load i8, i8* %93
%95 = zext i8 %94 to i32
%96 = getelementptr inbounds [188 x i8], [188 x i8]* %10, i64 0, i64 3
%97 = load i8, i8* %96
%98 = zext i8 %97 to i32
%99 = and i32 %98, 240
%100 = or i32 %95, %99
%101 = trunc i32 %100 to i8
%102 = getelementptr inbounds [188 x i8], [188 x i8]* %10, i64 0, i64 3
store i8 %101, i8* %102
br label %103
103: ; preds = %90, %50
%104 = load i32, i32* @STDOUT_FILENO
%105 = getelementptr inbounds [188 x i8], [188 x i8]* %10, i64 0, i64 0
%106 = call i32 @write(i32 %104, i8* %105, i32 188)
br label %43
107: ; preds = %49, %33, %24
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Can't find file %s\0A\00"
@.str.1 = [49 x i8] c"Usage: 'tsfixcc filename.ts > filenamefixed.ts'\0A\00"
@.str.2 = [111 x i8] c"Tsfixcc increases countinuity counter of every packet following each other without adapatation field parsing'\0A\00"
@.str.3 = [47 x i8] c"Useful to link ts of sections in a single ts'\0A\00"
@.str.4 = [18 x i8] c"new pid entry %d\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca [188 x i8]
%5 = alloca [8192 x i8]
%6 = bitcast i16* %3 to i8*
%7 = getelementptr inbounds [188 x i8], [188 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %20
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %11
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %62
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([111 x i8], [111 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %62
27: ; preds = %10
%28 = call i32 @memset(i8* %8, i32 16, i32 8192)
%29 = call i32 @read(i32 %14, i8* %7, i32 188)
%30 = icmp slt i32 %29, 188
br i1 %30, label %62, label %31
31: ; preds = %27
%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
br label %34
34: ; preds = %31, %53
%35 = call i32 @memcpy(i16* %3, i8* %32, i32 2)
%36 = load i16, i16* %3
%37 = call zeroext i16 @ntohs(i16 zeroext %36)
%38 = and i16 %37, 8191
store i16 %38, i16* %3
%39 = zext i16 %38 to i64
%40 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 16
br i1 %42, label %43, label %50
43: ; preds = %34
%44 = zext i16 %38 to i32
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%47 = load i16, i16* %3
%48 = zext i16 %47 to i64
%49 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 %48
store i8 0, i8* %49
br label %53
50: ; preds = %34
%51 = add i8 %41, 1
%52 = and i8 %51, 15
store i8 %52, i8* %40
br label %53
53: ; preds = %50, %43
%54 = phi i8 [ %52, %50 ], [ 0, %43 ]
%55 = load i8, i8* %33
%56 = and i8 %55, -16
%57 = or i8 %56, %54
store i8 %57, i8* %33
%58 = load i32, i32* @STDOUT_FILENO
%59 = call i32 @write(i32 %58, i8* %7, i32 188)
%60 = call i32 @read(i32 %14, i8* %7, i32 188)
%61 = icmp slt i32 %60, 188
br i1 %61, label %62, label %34
62: ; preds = %53, %27, %20, %16
%63 = phi i32 [ 2, %16 ], [ 2, %20 ], [ 0, %27 ], [ 0, %53 ]
ret i32 %63
}