#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/);
int htonl(unsigned long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int byte_read = 0;
  FILE *file_i1394 = 0;
  int data_length = 0;
  unsigned char fmt = 0;
  unsigned short dbs_fn_qpc_sph = 0;
  unsigned char CIP_header[8];
  unsigned char SP_header[4];
  unsigned char ts_packet[188];
  unsigned char i1394_header[4];
  file_i1394 = 0;
  if (argc > 1) {
    file_i1394 = fopen(argv[1], "rb");
  }
  if (file_i1394 == 0) {
    fprintf(stderr,
            "Usage: 'i13942ts file.iso', where .iso is a ieee1394-6 dump\n");
    return 2;
  }
  byte_read = fread(i1394_header, 1, 4, file_i1394);
  while (byte_read) {
    byte_read = fread(CIP_header, 1, 8, file_i1394);
    data_length = (htonl(*(unsigned long *)i1394_header) >> 16);
    dbs_fn_qpc_sph = (htonl(*(unsigned long *)CIP_header) >> 10) & 0x3fff;
    fmt = (htonl(*(unsigned long *)(CIP_header + 4)) >> 24) & 0x3f;
    if (fmt == 0x20 && dbs_fn_qpc_sph == 0x01b1) {
      data_length -= 8;
      for (; data_length > 188; data_length -= 188 + 4) {
        fread(SP_header, 1, 4, file_i1394);
        fread(ts_packet, 1, 188, file_i1394);
        fwrite(ts_packet, 1, 188, stdout);
      }
    }
    byte_read = fread(i1394_header, 1, 4, file_i1394);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x0,-0x25(%rbp)
movw   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b3 <main+0x63>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <main+0x8f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1319 <main+0x1c9>
lea    -0xf4(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1312 <main+0x1c2>
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1030 <fread@plt>
mov    %eax,-0x14(%rbp)
mov    -0xf4(%rbp),%rdi
call   1040 <htonl@plt>
sar    $0x10,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <htonl@plt>
sar    $0xa,%eax
and    $0x3fff,%eax
mov    %ax,-0x28(%rbp)
mov    -0x2c(%rbp),%rdi
call   1040 <htonl@plt>
sar    $0x18,%eax
and    $0x3f,%eax
mov    %al,-0x25(%rbp)
movzbl -0x25(%rbp),%eax
cmp    $0x20,%eax
jne    12f0 <main+0x1a0>
movzwl -0x28(%rbp),%eax
cmp    $0x1b1,%eax
jne    12f0 <main+0x1a0>
mov    -0x24(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xbc,-0x24(%rbp)
jle    12eb <main+0x19b>
lea    -0x34(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
lea    -0xf0(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xbc,%edx
call   1030 <fread@plt>
lea    -0xf0(%rbp),%rdi
mov    0x2d0e(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0xbc,%edx
call   1070 <fwrite@plt>
mov    -0x24(%rbp),%eax
sub    $0xc0,%eax
mov    %eax,-0x24(%rbp)
jmp    127e <main+0x12e>
jmp    12f0 <main+0x1a0>
lea    -0xf4(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    %eax,-0x14(%rbp)
jmp    11fc <main+0xac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
cmp    $0x2,%edi
jl     12a5 <main+0x155>
mov    0x8(%rsi),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12a5 <main+0x155>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %ecx,%ecx
test   %eax,%eax
je     12c3 <main+0x173>
lea    0x1c(%rsp),%r12
lea    0x20(%rsp),%rbp
mov    0x2e20(%rip),%r13
jmp    11df <main+0x8f>
nopw   0x0(%rax,%rax,1)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     12c1 <main+0x171>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    0x8(%rsp),%rdi
call   1040 <htonl@plt>
mov    %eax,%r14d
mov    0x10(%rsp),%rdi
call   1040 <htonl@plt>
mov    %eax,%r15d
mov    0x14(%rsp),%rdi
call   1040 <htonl@plt>
and    $0x3f000000,%eax
cmp    $0x20000000,%eax
jne    11c0 <main+0x70>
and    $0xfffc00,%r15d
cmp    $0x6c400,%r15d
jne    11c0 <main+0x70>
cmp    $0xc50000,%r14d
jl     11c0 <main+0x70>
shr    $0x10,%r14d
add    $0xb8,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0xbc,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    0x0(%r13),%ecx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0xbc,%edx
call   1070 <fwrite@plt>
add    $0xffffff40,%r14d
cmp    $0x17c,%r14d
ja     1250 <main+0x100>
jmp    11c0 <main+0x70>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%ecx
jmp    12c3 <main+0x173>
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [61 x i8] c"Usage: 'i13942ts file.iso', where .iso is a ieee1394-6 dump\0A\00"
@stdout = 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 i8
%10 = alloca i16
%11 = alloca [8 x i8]
%12 = alloca [4 x i8]
%13 = alloca [188 x i8]
%14 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32* null, i32** %7
store i32 0, i32* %8
store i8 0, i8* %9
store i16 0, i16* %10
store i32* null, i32** %7
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %7
br label %22
22: ; preds = %17, %2
%23 = load i32*, i32** %7
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %91
28: ; preds = %22
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%30 = load i32*, i32** %7
%31 = call i32 @fread(i8* %29, i32 1, i32 4, i32* %30)
store i32 %31, i32* %6
br label %32
32: ; preds = %86, %28
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %90
35: ; preds = %32
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%37 = load i32*, i32** %7
%38 = call i32 @fread(i8* %36, i32 1, i32 8, i32* %37)
store i32 %38, i32* %6
%39 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%40 = bitcast i8* %39 to i64*
%41 = load i64, i64* %40
%42 = call i32 @htonl(i64 %41)
%43 = ashr i32 %42, 16
store i32 %43, i32* %8
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%45 = bitcast i8* %44 to i64*
%46 = load i64, i64* %45
%47 = call i32 @htonl(i64 %46)
%48 = ashr i32 %47, 10
%49 = and i32 %48, 16383
%50 = trunc i32 %49 to i16
store i16 %50, i16* %10
%51 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%52 = getelementptr inbounds i8, i8* %51, i64 4
%53 = bitcast i8* %52 to i64*
%54 = load i64, i64* %53
%55 = call i32 @htonl(i64 %54)
%56 = ashr i32 %55, 24
%57 = and i32 %56, 63
%58 = trunc i32 %57 to i8
store i8 %58, i8* %9
%59 = load i8, i8* %9
%60 = zext i8 %59 to i32
%61 = icmp eq i32 %60, 32
br i1 %61, label %62, label %86
62: ; preds = %35
%63 = load i16, i16* %10
%64 = zext i16 %63 to i32
%65 = icmp eq i32 %64, 433
br i1 %65, label %66, label %86
66: ; preds = %62
%67 = load i32, i32* %8
%68 = sub nsw i32 %67, 8
store i32 %68, i32* %8
br label %69
69: ; preds = %82, %66
%70 = load i32, i32* %8
%71 = icmp sgt i32 %70, 188
br i1 %71, label %72, label %85
72: ; preds = %69
%73 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%74 = load i32*, i32** %7
%75 = call i32 @fread(i8* %73, i32 1, i32 4, i32* %74)
%76 = getelementptr inbounds [188 x i8], [188 x i8]* %13, i64 0, i64 0
%77 = load i32*, i32** %7
%78 = call i32 @fread(i8* %76, i32 1, i32 188, i32* %77)
%79 = getelementptr inbounds [188 x i8], [188 x i8]* %13, i64 0, i64 0
%80 = load i32, i32* @stdout
%81 = call i32 @fwrite(i8* %79, i32 1, i32 188, i32 %80)
br label %82
82: ; preds = %72
%83 = load i32, i32* %8
%84 = sub nsw i32 %83, 192
store i32 %84, i32* %8
br label %69
85: ; preds = %69
br label %86
86: ; preds = %85, %62, %35
%87 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%88 = load i32*, i32** %7
%89 = call i32 @fread(i8* %87, i32 1, i32 4, i32* %88)
store i32 %89, i32* %6
br label %32
90: ; preds = %32
store i32 0, i32* %3
br label %91
91: ; preds = %90, %25
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [61 x i8] c"Usage: 'i13942ts file.iso', where .iso is a ieee1394-6 dump\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = alloca [4 x i8]
%5 = alloca [188 x i8]
%6 = alloca [4 x i8]
%7 = bitcast i64* %3 to i8*
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [188 x i8], [188 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%11 = icmp sgt i32 %0, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2, %12
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
br label %56
20: ; preds = %12
%21 = call i32 @fread(i8* %10, i32 1, i32 4, i32* %15)
%22 = icmp eq i32 %21, 0
br i1 %22, label %56, label %23
23: ; preds = %20
%24 = bitcast [4 x i8]* %6 to i64*
%25 = getelementptr inbounds i8, i8* %7, i64 4
%26 = bitcast i8* %25 to i64*
br label %27
27: ; preds = %23, %53
%28 = call i32 @fread(i8* %7, i32 1, i32 8, i32* %15)
%29 = load i64, i64* %24
%30 = call i32 @htonl(i64 %29)
%31 = load i64, i64* %3
%32 = call i32 @htonl(i64 %31)
%33 = load i64, i64* %26
%34 = call i32 @htonl(i64 %33)
%35 = and i32 %34, 1056964608
%36 = icmp eq i32 %35, 536870912
%37 = and i32 %32, 16776192
%38 = icmp eq i32 %37, 443392
%39 = select i1 %36, i1 %38, i1 false
%40 = icmp sgt i32 %30, 12910591
%41 = select i1 %39, i1 %40, i1 false
br i1 %41, label %42, label %53
42: ; preds = %27
%43 = lshr i32 %30, 16
%44 = add nsw i32 %43, -8
br label %45
45: ; preds = %42, %45
%46 = phi i32 [ %51, %45 ], [ %44, %42 ]
%47 = call i32 @fread(i8* %8, i32 1, i32 4, i32* %15)
%48 = call i32 @fread(i8* %9, i32 1, i32 188, i32* %15)
%49 = load i32, i32* @stdout
%50 = call i32 @fwrite(i8* %9, i32 1, i32 188, i32 %49)
%51 = add nsw i32 %46, -192
%52 = icmp ugt i32 %46, 380
br i1 %52, label %45, label %53
53: ; preds = %45, %27
%54 = call i32 @fread(i8* %10, i32 1, i32 4, i32* %15)
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %27
56: ; preds = %53, %20, %17
%57 = phi i32 [ 2, %17 ], [ 0, %20 ], [ 0, %53 ]
ret i32 %57
}