#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ process_pes(int, int /*<<< orphan*/ *);
scalar_t__ set_filter(int, unsigned long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
unsigned long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  int dmxfd;
  unsigned long pid;
  char *dmxdev = "/dev/dvb/adapter0/demux0";
  FILE *out = stdout;
  if (argc != 2 && argc != 3)
    usage();
  pid = strtoul(argv[1], NULL, 0);
  if (pid > 0x1fff)
    usage();
  if (getenv("DEMUX"))
    dmxdev = getenv("DEMUX");
  fprintf(stderr, "test_pes: using '%s'\n", dmxdev);
  fprintf(stderr, "          PID 0x%04lx\n", pid);
  if (argc == 3) {
    out = fopen(argv[2], "wb");
    if (!out) {
      perror("open output file");
      exit(1);
    }
    fprintf(stderr, "          output to '%s'\n", argv[2]);
  }
  if ((dmxfd = open(dmxdev, O_RDWR)) < 0) {
    perror("open");
    return 1;
  }
  if (set_filter(dmxfd, pid) != 0)
    return 1;
  for (;;) {
    process_pes(dmxfd, out);
  }
  close(dmxfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe43(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ea <main+0x4a>
cmpl   $0x3,-0x8(%rbp)
je     11ea <main+0x4a>
mov    $0x0,%al
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1fff,-0x20(%rbp)
jbe    1216 <main+0x76>
mov    $0x0,%al
call   1070 <usage@plt>
lea    0xdfc(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     123c <main+0x9c>
lea    0xde6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x3,-0x8(%rbp)
jne    12d4 <main+0x134>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdc1(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b5 <main+0x115>
lea    0xda9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    130a <main+0x16a>
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <main+0x1a2>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <set_filter@plt>
cmp    $0x0,%rax
je     132c <main+0x18c>
movl   $0x1,-0x4(%rbp)
jmp    1342 <main+0x1a2>
jmp    1331 <main+0x191>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1050 <process_pes@plt>
jmp    1331 <main+0x191>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x2e18(%rip),%rax
mov    (%rax),%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
je     11cc <main+0x2c>
xor    %eax,%eax
call   1070 <usage@plt>
mov    0x8(%r15),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %rax,%r14
cmp    $0x2000,%rax
jb     11eb <main+0x4b>
xor    %eax,%eax
call   1070 <usage@plt>
lea    0xe27(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xdff(%rip),%rbp
cmovne %rax,%rbp
mov    0x2dcc(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe08(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xe09(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
cmp    $0x3,%r12d
jne    126a <main+0xca>
mov    0x10(%r15),%rdi
lea    0xe05(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ca <main+0x12a>
mov    %rax,%rbx
mov    0x0(%r13),%edi
mov    0x10(%r15),%rdx
lea    0xdfd(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12bc <main+0x11c>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <set_filter@plt>
test   %rax,%rax
je     12b0 <main+0x110>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <process_pes@plt>
jmp    12b0 <main+0x110>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
jmp    1290 <main+0xf0>
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"/dev/dvb/adapter0/demux0\00"
@stdout = global i32* null
@.str.1 = [6 x i8] c"DEMUX\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"test_pes: using '%s'\0A\00"
@.str.3 = [23 x i8] c" PID 0x%04lx\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [17 x i8] c"open output file\00"
@.str.6 = [26 x i8] c" output to '%s'\0A\00"
@O_RDWR = global i32 0
@.str.7 = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %8
%10 = load i32*, i32** @stdout
store i32* %10, i32** %9
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (...) @usage()
br label %18
18: ; preds = %16, %13, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strtoul(i8* %21, i32* null, i32 0)
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp ugt i64 %23, 8191
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (...) @usage()
br label %27
27: ; preds = %25, %18
%28 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %31, i8** %8
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %8
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @stderr
%37 = load i64, i64* %7
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %37)
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 3
br i1 %40, label %41, label %57
41: ; preds = %32
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %45, i32** %9
%46 = load i32*, i32** %9
%47 = icmp ne i32* %46, null
br i1 %47, label %51, label %48
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %41
%52 = load i32, i32* @stderr
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %51, %32
%58 = load i8*, i8** %8
%59 = load i32, i32* @O_RDWR
%60 = call i32 @open(i8* %58, i32 %59)
store i32 %60, i32* %6
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %75
64: ; preds = %57
%65 = load i32, i32* %6
%66 = load i64, i64* %7
%67 = call i64 @set_filter(i32 %65, i64 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %64
store i32 1, i32* %3
br label %75
70: ; preds = %64
br label %71
71: ; preds = %71, %70
%72 = load i32, i32* %6
%73 = load i32*, i32** %9
%74 = call i32 @process_pes(i32 %72, i32* %73)
br label %71
75: ; preds = %69, %62
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str = [25 x i8] c"/dev/dvb/adapter0/demux0\00"
@stdout = global i32* null
@.str.1 = [6 x i8] c"DEMUX\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"test_pes: using '%s'\0A\00"
@.str.3 = [23 x i8] c" PID 0x%04lx\0A\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [17 x i8] c"open output file\00"
@.str.6 = [26 x i8] c" output to '%s'\0A\00"
@O_RDWR = global i32 0
@.str.7 = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32*, i32** @stdout
%4 = and i32 %0, -2
%5 = icmp eq i32 %4, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (...) @usage()
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strtoul(i8* nocapture %10, i32* null, i32 0)
%12 = icmp ugt i64 %11, 8191
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 (...) @usage()
br label %15
15: ; preds = %13, %8
%16 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
%18 = select i1 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %16
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %11)
%23 = icmp eq i32 %0, 3
br i1 %23, label %24, label %36
24: ; preds = %15
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %25
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %32, %15
%37 = phi i32* [ %27, %32 ], [ %3, %15 ]
%38 = load i32, i32* @O_RDWR
%39 = tail call i32 @open(i8* %18, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %48
43: ; preds = %36
%44 = tail call i64 @set_filter(i32 %39, i64 %11)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43, %46
%47 = tail call i32 @process_pes(i32 %39, i32* %37)
br label %46
48: ; preds = %43, %41
ret i32 1
}