#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 */
struct dmx_pes_filter_params {
  int pid;
  int /*<<< orphan*/ flags;
  scalar_t__ pes_type;
  int /*<<< orphan*/ output;
  int /*<<< orphan*/ input;
};
typedef scalar_t__ dmx_pes_type_t;

/* Variables and functions */
int /*<<< orphan*/ DMX_IMMEDIATE_START;
int /*<<< orphan*/ DMX_IN_FRONTEND;
int /*<<< orphan*/ DMX_OUT_DECODER;
int /*<<< orphan*/ DMX_OUT_TS_TAP;
scalar_t__ DMX_PES_AUDIO;
scalar_t__ DMX_PES_VIDEO;
int /*<<< orphan*/ DMX_SET_PES_FILTER;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct dmx_pes_filter_params *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int set_pesfilter(int fd, int pid, dmx_pes_type_t type, int dvr) {
  struct dmx_pes_filter_params pesfilter;
  if (pid <= 0 || pid >= 0x1fff)
    return 0;
  pesfilter.pid = pid;
  pesfilter.input = DMX_IN_FRONTEND;
  pesfilter.output = dvr ? DMX_OUT_TS_TAP : DMX_OUT_DECODER;
  pesfilter.pes_type = type;
  pesfilter.flags = DMX_IMMEDIATE_START;
  if (ioctl(fd, DMX_SET_PES_FILTER, &pesfilter) < 0) {
    do {
      fprintf(stderr, "ERROR: ");
      fprintf(stderr, "ioctl(DMX_SET_PES_FILTER) for %s PID failed",
              type == DMX_PES_AUDIO   ? "Audio"
              : type == DMX_PES_VIDEO ? "Video"
                                      : "??");
      fprintf(stderr, " (%s)\n", strerror(errno));
    } while (0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_pesfilter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    115c <set_pesfilter+0x2c>
cmpl   $0x1fff,-0xc(%rbp)
jl     1168 <set_pesfilter+0x38>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <set_pesfilter+0x173>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1195 <set_pesfilter+0x65>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    11a1 <set_pesfilter+0x71>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    129c <set_pesfilter+0x16c>
jmp    11df <set_pesfilter+0xaf>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <set_pesfilter+0xf6>
lea    0xe17(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    124d <set_pesfilter+0x11d>
mov    -0x18(%rbp),%rdx
mov    0x2dbf(%rip),%rax
mov    (%rax),%rsi
lea    0xe05(%rip),%rax
lea    0xdf8(%rip),%rcx
cmp    %rsi,%rdx
cmove  %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x48(%rbp),%rdx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <set_pesfilter+0x173>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pesfilter>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    -0x1fff(%rsi),%eax
xor    %ebp,%ebp
cmp    $0xffffe002,%eax
jb     1225 <set_pesfilter+0xf5>
mov    %rdx,%rbx
mov    %esi,(%rsp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
test   %ecx,%ecx
jne    1169 <set_pesfilter+0x39>
mov    0x2e71(%rip),%rax
jmp    1170 <set_pesfilter+0x40>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,0x8(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
jns    1225 <set_pesfilter+0xf5>
mov    0x2e1f(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe4d(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2dd7(%rip),%rax
mov    0x2e28(%rip),%rcx
cmp    %rbx,(%rcx)
lea    0xe68(%rip),%rcx
lea    0xe67(%rip),%rsi
cmove  %rcx,%rsi
cmp    %rbx,(%rax)
mov    0x0(%rbp),%edi
lea    0xe4a(%rip),%rdx
cmovne %rsi,%rdx
lea    0xe13(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%rbp),%ebx
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe2f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dmx_pes_filter_params = type { i32, i32, i64, i32, i32 }
@DMX_IN_FRONTEND = global i32 0
@DMX_OUT_TS_TAP = global i32 0
@DMX_OUT_DECODER = global i32 0
@DMX_IMMEDIATE_START = global i32 0
@DMX_SET_PES_FILTER = global i32 0
@stderr = global i32 0
@.str = [8 x i8] c"ERROR: \00"
@.str.1 = [44 x i8] c"ioctl(DMX_SET_PES_FILTER) for %s PID failed\00"
@DMX_PES_AUDIO = global i64 0
@.str.2 = [6 x i8] c"Audio\00"
@DMX_PES_VIDEO = global i64 0
@.str.3 = [6 x i8] c"Video\00"
@.str.4 = [3 x i8] c"??\00"
@.str.5 = [7 x i8] c" (%s)\0A\00"
@errno = global i32 0
define i32 @set_pesfilter(i32 %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.dmx_pes_filter_params
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%11 = load i32, i32* %7
%12 = icmp sle i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i32, i32* %7
%15 = icmp sge i32 %14, 8191
br i1 %15, label %16, label %17
16: ; preds = %13, %4
store i32 0, i32* %5
br label %63
17: ; preds = %13
%18 = load i32, i32* %7
%19 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @DMX_IN_FRONTEND
%21 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 4
store i32 %20, i32* %21
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = load i32, i32* @DMX_OUT_TS_TAP
br label %28
26: ; preds = %17
%27 = load i32, i32* @DMX_OUT_DECODER
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ %25, %24 ], [ %27, %26 ]
%30 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 3
store i32 %29, i32* %30
%31 = load i64, i64* %8
%32 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 2
store i64 %31, i64* %32
%33 = load i32, i32* @DMX_IMMEDIATE_START
%34 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* %6
%36 = load i32, i32* @DMX_SET_PES_FILTER
%37 = call i64 @ioctl(i32 %35, i32 %36, %struct.dmx_pes_filter_params* %10)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %62
39: ; preds = %28
br label %40
40: ; preds = %39
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* @stderr
%44 = load i64, i64* %8
%45 = load i64, i64* @DMX_PES_AUDIO
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %40
br label %54
48: ; preds = %40
%49 = load i64, i64* %8
%50 = load i64, i64* @DMX_PES_VIDEO
%51 = icmp eq i64 %49, %50
%52 = zext i1 %51 to i64
%53 = select i1 %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0)
br label %54
54: ; preds = %48, %47
%55 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %47 ], [ %53, %48 ]
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %54
store i32 -1, i32* %5
br label %63
62: ; preds = %28
store i32 0, i32* %5
br label %63
63: ; preds = %62, %61, %16
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
%struct.dmx_pes_filter_params = type { i32, i32, i64, i32, i32 }
@DMX_IN_FRONTEND = global i32 0
@DMX_OUT_TS_TAP = global i32 0
@DMX_OUT_DECODER = global i32 0
@DMX_IMMEDIATE_START = global i32 0
@DMX_SET_PES_FILTER = global i32 0
@stderr = global i32 0
@.str = [8 x i8] c"ERROR: \00"
@.str.1 = [44 x i8] c"ioctl(DMX_SET_PES_FILTER) for %s PID failed\00"
@DMX_PES_AUDIO = global i64 0
@.str.2 = [6 x i8] c"Audio\00"
@DMX_PES_VIDEO = global i64 0
@.str.3 = [6 x i8] c"Video\00"
@.str.4 = [3 x i8] c"??\00"
@.str.5 = [7 x i8] c" (%s)\0A\00"
@errno = global i32 0
define i32 @set_pesfilter(i32 %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca %struct.dmx_pes_filter_params
%6 = bitcast %struct.dmx_pes_filter_params* %5 to i8*
%7 = add i32 %1, -8191
%8 = icmp ult i32 %7, -8190
br i1 %8, label %39, label %9
9: ; preds = %4
%10 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 0
store i32 %1, i32* %10
%11 = load i32, i32* @DMX_IN_FRONTEND
%12 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 4
store i32 %11, i32* %12
%13 = icmp eq i32 %3, 0
%14 = load i32, i32* @DMX_OUT_TS_TAP
%15 = load i32, i32* @DMX_OUT_DECODER
%16 = select i1 %13, i32 %15, i32 %14
%17 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 3
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 2
store i64 %2, i64* %18
%19 = load i32, i32* @DMX_IMMEDIATE_START
%20 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @DMX_SET_PES_FILTER
%22 = call i64 @ioctl(i32 %0, i32 %21, %struct.dmx_pes_filter_params* %5)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %9
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = load i64, i64* @DMX_PES_AUDIO
%29 = icmp eq i64 %28, %2
%30 = load i64, i64* @DMX_PES_VIDEO
%31 = icmp eq i64 %30, %2
%32 = select i1 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0)
%33 = select i1 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %32
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %9, %4, %24
%40 = phi i32 [ -1, %24 ], [ 0, %4 ], [ 0, %9 ]
ret i32 %40
}