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

/* Variables and functions */
int /*<<< orphan*/ DMX_IMMEDIATE_START;
int /*<<< orphan*/ DMX_IN_DVR;
int /*<<< orphan*/ DMX_IN_FRONTEND;
int /*<<< orphan*/ DMX_OUT_DECODER;
int /*<<< orphan*/ DMX_OUT_TAP;
int /*<<< orphan*/ DMX_OUT_TS_TAP;
int /*<<< orphan*/ DMX_PES_OTHER;
int /*<<< orphan*/ DMX_SET_PES_FILTER;
int EINVAL;
int ioctl(int, int /*<<< orphan*/, struct dmx_pes_filter_params *);
int /*<<< orphan*/ memset(struct dmx_pes_filter_params *, int /*<<< orphan*/,
                          int);
int dvbdemux_set_pid_filter(int fd, int pid, int input, int output, int start) {
  struct dmx_pes_filter_params filter;
  memset(&filter, 0, sizeof(filter));
  if (pid == -1)
    filter.pid = 0x2000;
  else
    filter.pid = pid;
  switch (input) {
  case 0:
    filter.input = DMX_IN_FRONTEND;
    break;
  case 1:
    filter.input = DMX_IN_DVR;
    break;
  default:
    return -EINVAL;
  }
  switch (output) {
  case 0:
    filter.output = DMX_OUT_DECODER;
    break;
  case 1:
    filter.output = DMX_OUT_TAP;
    break;
  case 2:
    filter.output = DMX_OUT_TS_TAP;
    break;
  default:
    return -EINVAL;
  }
  filter.pes_type = DMX_PES_OTHER;
  if (start)
    filter.flags |= DMX_IMMEDIATE_START;
  return ioctl(fd, DMX_SET_PES_FILTER, &filter);
}


x86 assembly (clang, -O0):
<dvbdemux_set_pid_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
cmpl   $0xffffffff,-0xc(%rbp)
jne    115e <dvbdemux_set_pid_filter+0x3e>
movl   $0x2000,-0x30(%rbp)
jmp    1164 <dvbdemux_set_pid_filter+0x44>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
test   %eax,%eax
je     1188 <dvbdemux_set_pid_filter+0x68>
jmp    1177 <dvbdemux_set_pid_filter+0x57>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
je     1199 <dvbdemux_set_pid_filter+0x79>
jmp    11aa <dvbdemux_set_pid_filter+0x8a>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11bd <dvbdemux_set_pid_filter+0x9d>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11bd <dvbdemux_set_pid_filter+0x9d>
xor    %eax,%eax
mov    0x2e15(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1275 <dvbdemux_set_pid_filter+0x155>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
test   %eax,%eax
je     11f2 <dvbdemux_set_pid_filter+0xd2>
jmp    11d0 <dvbdemux_set_pid_filter+0xb0>
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
je     1203 <dvbdemux_set_pid_filter+0xe3>
jmp    11e1 <dvbdemux_set_pid_filter+0xc1>
mov    -0x38(%rbp),%eax
sub    $0x2,%eax
je     1214 <dvbdemux_set_pid_filter+0xf4>
jmp    1225 <dvbdemux_set_pid_filter+0x105>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1238 <dvbdemux_set_pid_filter+0x118>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1238 <dvbdemux_set_pid_filter+0x118>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1238 <dvbdemux_set_pid_filter+0x118>
xor    %eax,%eax
mov    0x2d9a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1275 <dvbdemux_set_pid_filter+0x155>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     125d <dvbdemux_set_pid_filter+0x13d>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dvbdemux_set_pid_filter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%r15d
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
cmp    $0xffffffff,%ebx
mov    $0x2000,%eax
cmovne %ebx,%eax
mov    %eax,0x8(%rsp)
test   %ebp,%ebp
je     116b <dvbdemux_set_pid_filter+0x4b>
cmp    $0x1,%ebp
jne    11c9 <dvbdemux_set_pid_filter+0xa9>
mov    0x2e5f(%rip),%rax
jmp    1172 <dvbdemux_set_pid_filter+0x52>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
cmp    $0x3,%r12d
jae    11c9 <dvbdemux_set_pid_filter+0xa9>
movslq %r12d,%rax
lea    0x2c50(%rip),%rcx
mov    (%rcx,%rax,8),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
test   %r15d,%r15d
je     11b1 <dvbdemux_set_pid_filter+0x91>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
or     %eax,0xc(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1040 <ioctl@plt>
jmp    11d4 <dvbdemux_set_pid_filter+0xb4>
mov    0x2e08(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dmx_pes_filter_params = type { i32, i32, i32, i32, i32 }
@DMX_IN_FRONTEND = global i32 0
@DMX_IN_DVR = global i32 0
@EINVAL = global i32 0
@DMX_OUT_DECODER = global i32 0
@DMX_OUT_TAP = global i32 0
@DMX_OUT_TS_TAP = global i32 0
@DMX_PES_OTHER = global i32 0
@DMX_IMMEDIATE_START = global i32 0
@DMX_SET_PES_FILTER = global i32 0
define i32 @dvbdemux_set_pid_filter(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.dmx_pes_filter_params
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%13 = call i32 @memset(%struct.dmx_pes_filter_params* %12, i32 0, i32 20)
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 0
store i32 8192, i32* %17
br label %21
18: ; preds = %5
%19 = load i32, i32* %8
%20 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 0
store i32 %19, i32* %20
br label %21
21: ; preds = %18, %16
%22 = load i32, i32* %9
switch i32 %22, label %29 [
i32 0, label %23
i32 1, label %26
]
23: ; preds = %21
%24 = load i32, i32* @DMX_IN_FRONTEND
%25 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 4
store i32 %24, i32* %25
br label %32
26: ; preds = %21
%27 = load i32, i32* @DMX_IN_DVR
%28 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 4
store i32 %27, i32* %28
br label %32
29: ; preds = %21
%30 = load i32, i32* @EINVAL
%31 = sub nsw i32 0, %30
store i32 %31, i32* %6
br label %60
32: ; preds = %26, %23
%33 = load i32, i32* %10
switch i32 %33, label %43 [
i32 0, label %34
i32 1, label %37
i32 2, label %40
]
34: ; preds = %32
%35 = load i32, i32* @DMX_OUT_DECODER
%36 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 3
store i32 %35, i32* %36
br label %46
37: ; preds = %32
%38 = load i32, i32* @DMX_OUT_TAP
%39 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 3
store i32 %38, i32* %39
br label %46
40: ; preds = %32
%41 = load i32, i32* @DMX_OUT_TS_TAP
%42 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 3
store i32 %41, i32* %42
br label %46
43: ; preds = %32
%44 = load i32, i32* @EINVAL
%45 = sub nsw i32 0, %44
store i32 %45, i32* %6
br label %60
46: ; preds = %40, %37, %34
%47 = load i32, i32* @DMX_PES_OTHER
%48 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 2
store i32 %47, i32* %48
%49 = load i32, i32* %11
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = load i32, i32* @DMX_IMMEDIATE_START
%53 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %12, i32 0, i32 1
%54 = load i32, i32* %53
%55 = or i32 %54, %52
store i32 %55, i32* %53
br label %56
56: ; preds = %51, %46
%57 = load i32, i32* %7
%58 = load i32, i32* @DMX_SET_PES_FILTER
%59 = call i32 @ioctl(i32 %57, i32 %58, %struct.dmx_pes_filter_params* %12)
store i32 %59, i32* %6
br label %60
60: ; preds = %56, %43, %29
%61 = load i32, i32* %6
ret i32 %61
}

LLVM IR (-O2):
%struct.dmx_pes_filter_params = type { i32, i32, i32, i32, i32 }
@DMX_IN_FRONTEND = global i32 0
@DMX_IN_DVR = global i32 0
@EINVAL = global i32 0
@DMX_OUT_DECODER = global i32 0
@DMX_OUT_TAP = global i32 0
@DMX_OUT_TS_TAP = global i32 0
@DMX_PES_OTHER = global i32 0
@DMX_IMMEDIATE_START = global i32 0
@DMX_SET_PES_FILTER = global i32 0
@switch.table.dvbdemux_set_pid_filter = [3 x i32*] [i32* @DMX_OUT_DECODER, i32* @DMX_OUT_TAP, i32* @DMX_OUT_TS_TAP]
define i32 @dvbdemux_set_pid_filter(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.dmx_pes_filter_params
%7 = bitcast %struct.dmx_pes_filter_params* %6 to i8*
%8 = call i32 @memset(%struct.dmx_pes_filter_params* %6, i32 0, i32 20)
%9 = icmp eq i32 %1, -1
%10 = select i1 %9, i32 8192, i32 %1
%11 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %6, i64 0, i32 0
store i32 %10, i32* %11
switch i32 %2, label %13 [
i32 0, label %16
i32 1, label %12
]
12: ; preds = %5
br label %16
13: ; preds = %5
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
br label %41
16: ; preds = %5, %12
%17 = phi i32* [ @DMX_IN_DVR, %12 ], [ @DMX_IN_FRONTEND, %5 ]
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %6, i64 0, i32 4
store i32 %18, i32* %19
%20 = icmp ult i32 %3, 3
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @EINVAL
%23 = sub nsw i32 0, %22
br label %41
24: ; preds = %16
%25 = sext i32 %3 to i64
%26 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.dvbdemux_set_pid_filter, i64 0, i64 %25
%27 = load i32*, i32** %26
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %6, i64 0, i32 3
store i32 %28, i32* %29
%30 = load i32, i32* @DMX_PES_OTHER
%31 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %6, i64 0, i32 2
store i32 %30, i32* %31
%32 = icmp eq i32 %4, 0
br i1 %32, label %38, label %33
33: ; preds = %24
%34 = load i32, i32* @DMX_IMMEDIATE_START
%35 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %6, i64 0, i32 1
%36 = load i32, i32* %35
%37 = or i32 %36, %34
store i32 %37, i32* %35
br label %38
38: ; preds = %33, %24
%39 = load i32, i32* @DMX_SET_PES_FILTER
%40 = call i32 @ioctl(i32 %0, i32 %39, %struct.dmx_pes_filter_params* %6)
br label %41
41: ; preds = %38, %21, %13
%42 = phi i32 [ %15, %13 ], [ %23, %21 ], [ %40, %38 ]
ret i32 %42
}