#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 pes_type;
  int /*<<< orphan*/ flags;
  int /*<<< orphan*/ output;
  int /*<<< orphan*/ input;
};

/* Variables and functions */
int /*<<< orphan*/ DMX_IMMEDIATE_START;
int /*<<< orphan*/ DMX_IN_FRONTEND;
int /*<<< orphan*/ DMX_OUT_DECODER;
int /*<<< orphan*/ DMX_OUT_TS_TAP;
int DMX_PES_OTHER;
int /*<<< orphan*/ DMX_SET_BUFFER_SIZE;
int /*<<< orphan*/ DMX_SET_PES_FILTER;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int set_pesfilter(int dmxfd, int pid, int pes_type, int dvr) {
  struct dmx_pes_filter_params pesfilter;
  if (pid < 0 || pid >= 0x1fff || (pid == 0 && pes_type != DMX_PES_OTHER))
    return 0;
  if (dvr) {
    int buffersize = 64 * 1024;
    if (ioctl(dmxfd, DMX_SET_BUFFER_SIZE, buffersize) == -1)
      perror("DMX_SET_BUFFER_SIZE failed");
  }
  pesfilter.pid = pid;
  pesfilter.input = DMX_IN_FRONTEND;
  pesfilter.output = dvr ? DMX_OUT_TS_TAP : DMX_OUT_DECODER;
  pesfilter.pes_type = pes_type;
  pesfilter.flags = DMX_IMMEDIATE_START;
  if (ioctl(dmxfd, DMX_SET_PES_FILTER, &pesfilter) == -1) {
    fprintf(stderr,
            "DMX_SET_PES_FILTER failed "
            "(PID = 0x%04x): %d %m\n",
            pid, errno);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_pesfilter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1177 <set_pesfilter+0x47>
cmpl   $0x1fff,-0xc(%rbp)
jge    1177 <set_pesfilter+0x47>
cmpl   $0x0,-0xc(%rbp)
jne    1183 <set_pesfilter+0x53>
mov    -0x10(%rbp),%eax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%eax
je     1183 <set_pesfilter+0x53>
movl   $0x0,-0x4(%rbp)
jmp    1269 <set_pesfilter+0x139>
cmpl   $0x0,-0x14(%rbp)
je     11c4 <set_pesfilter+0x94>
movl   $0x10000,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    $0x0,%al
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11bf <set_pesfilter+0x8f>
lea    0xe46(%rip),%rdi
call   1050 <perror@plt>
jmp    11c4 <set_pesfilter+0x94>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f1 <set_pesfilter+0xc1>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    11fd <set_pesfilter+0xcd>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1262 <set_pesfilter+0x132>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    0x2d80(%rip),%rax
mov    (%rax),%ecx
lea    0xdca(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <set_pesfilter+0x139>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pesfilter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
xor    %ebp,%ebp
cmp    $0x1ffe,%esi
ja     1211 <set_pesfilter+0xe1>
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%r14d
test   %esi,%esi
jne    1169 <set_pesfilter+0x39>
mov    0x2e48(%rip),%rax
cmp    %r15d,(%rax)
jne    1211 <set_pesfilter+0xe1>
test   %r12d,%r12d
je     118f <set_pesfilter+0x5f>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    $0x10000,%edx
xor    %eax,%eax
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1220 <set_pesfilter+0xf0>
mov    %ebx,0x8(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
test   %r12d,%r12d
jne    11ae <set_pesfilter+0x7e>
mov    0x2e34(%rip),%rax
jmp    11b5 <set_pesfilter+0x85>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,0xc(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1211 <set_pesfilter+0xe1>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
lea    0xe16(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
call   1050 <perror@plt>
jmp    118f <set_pesfilter+0x5f>

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

LLVM IR (-O2):
%struct.dmx_pes_filter_params = type { i32, i32, i32, i32, i32 }
@DMX_PES_OTHER = global i32 0
@DMX_SET_BUFFER_SIZE = global i32 0
@.str = [27 x i8] c"DMX_SET_BUFFER_SIZE failed\00"
@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.1 = [49 x i8] c"DMX_SET_PES_FILTER failed (PID = 0x%04x): %d %m\0A\00"
@errno = global i32 0
define i32 @set_pesfilter(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.dmx_pes_filter_params
%6 = bitcast %struct.dmx_pes_filter_params* %5 to i8*
%7 = icmp ugt i32 %1, 8190
br i1 %7, label %39, label %8
8: ; preds = %4
%9 = icmp ne i32 %1, 0
%10 = load i32, i32* @DMX_PES_OTHER
%11 = icmp eq i32 %10, %2
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %39
13: ; preds = %8
%14 = icmp eq i32 %3, 0
br i1 %14, label %21, label %15
15: ; preds = %13
%16 = load i32, i32* @DMX_SET_BUFFER_SIZE
%17 = tail call i32 (i32, i32, ...) @ioctl(i32 %0, i32 %16, i32 65536)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %15, %19, %13
%22 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 0
store i32 %1, i32* %22
%23 = load i32, i32* @DMX_IN_FRONTEND
%24 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 4
store i32 %23, i32* %24
%25 = load i32, i32* @DMX_OUT_TS_TAP
%26 = load i32, i32* @DMX_OUT_DECODER
%27 = select i1 %14, i32 %26, i32 %25
%28 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 3
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 1
store i32 %2, i32* %29
%30 = load i32, i32* @DMX_IMMEDIATE_START
%31 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %5, i64 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @DMX_SET_PES_FILTER
%33 = call i32 (i32, i32, ...) @ioctl(i32 %0, i32 %32, %struct.dmx_pes_filter_params* %5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %21
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %37)
br label %39
39: ; preds = %21, %4, %8, %35
%40 = phi i32 [ -1, %35 ], [ 0, %8 ], [ 0, %4 ], [ 0, %21 ]
ret i32 %40
}