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

/* Variables and functions */
int /*<<< orphan*/ DMX_IMMEDIATE_START;
int /*<<< orphan*/ DMX_IN_FRONTEND;
int /*<<< orphan*/ DMX_OUT_TS_TAP;
int /*<<< orphan*/ DMX_PES_OTHER;
int /*<<< orphan*/ DMX_SET_PES_FILTER;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int, int);
int ioctl(int, int /*<<< orphan*/, struct dmx_pes_filter_params *);
int /*<<< orphan*/ memset(struct dmx_pes_filter_params *, int /*<<< orphan*/,
                          int);
int open(unsigned char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int add_filter(unsigned int pid, const unsigned char *dmxdev) {
  int fd;
  struct dmx_pes_filter_params f;
  fd = open(dmxdev, O_RDONLY);
  if (fd == -1) {
    perror("cannot open demux device");
    return 1;
  }
  memset(&f, 0, sizeof(f));
  f.pid = (uint16_t)pid;
  f.input = DMX_IN_FRONTEND;
  f.output = DMX_OUT_TS_TAP;
  f.pes_type = DMX_PES_OTHER;
  f.flags = DMX_IMMEDIATE_START;
  if (ioctl(fd, DMX_SET_PES_FILTER, &f) == -1) {
    perror("DMX_SET_PES_FILTER");
    return 1;
  }
  fprintf(stderr, "set filter for PID 0x%04x on fd %d\n", pid, fd);
  return 0;
}


x86 assembly (clang, -O0):
<add_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1196 <add_filter+0x46>
lea    0xe7b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1235 <add_filter+0xe5>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %rax,-0x20(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1213 <add_filter+0xc3>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1235 <add_filter+0xe5>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_filter>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %edi,%ebx
mov    0x2e82(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1209 <add_filter+0xb9>
mov    %eax,%ebp
mov    %rsp,%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    %ebx,%eax
mov    %rax,0x10(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1212 <add_filter+0xc2>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1050 <fprintf@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
jmp    1219 <add_filter+0xc9>
lea    0xe00(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%r14d
jmp    11fb <add_filter+0xab>

LLVM IR (-O0):
%struct.dmx_pes_filter_params = type { i32, i32, i32, i32, i64 }
@O_RDONLY = global i32 0
@.str = [25 x i8] c"cannot open demux device\00"
@DMX_IN_FRONTEND = 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
@.str.1 = [19 x i8] c"DMX_SET_PES_FILTER\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"set filter for PID 0x%04x on fd %d\0A\00"
define i32 @add_filter(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.dmx_pes_filter_params
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %39
15: ; preds = %2
%16 = call i32 @memset(%struct.dmx_pes_filter_params* %7, i32 0, i32 24)
%17 = load i32, i32* %4
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %7, i32 0, i32 4
store i64 %18, i64* %19
%20 = load i32, i32* @DMX_IN_FRONTEND
%21 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %7, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i32, i32* @DMX_OUT_TS_TAP
%23 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @DMX_PES_OTHER
%25 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @DMX_IMMEDIATE_START
%27 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %7, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = load i32, i32* @DMX_SET_PES_FILTER
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.dmx_pes_filter_params* %7)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %15
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %39
34: ; preds = %15
%35 = load i32, i32* @stderr
%36 = load i32, i32* %4
%37 = load i32, i32* %6
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %37)
store i32 0, i32* %3
br label %39
39: ; preds = %34, %32, %13
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.dmx_pes_filter_params = type { i32, i32, i32, i32, i64 }
@O_RDONLY = global i32 0
@.str = [25 x i8] c"cannot open demux device\00"
@DMX_IN_FRONTEND = 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
@.str.1 = [19 x i8] c"DMX_SET_PES_FILTER\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"set filter for PID 0x%04x on fd %d\0A\00"
define i32 @add_filter(i32 %0, i8* nocapture %1) {
%3 = alloca %struct.dmx_pes_filter_params
%4 = bitcast %struct.dmx_pes_filter_params* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %30
10: ; preds = %2
%11 = call i32 @memset(%struct.dmx_pes_filter_params* %3, i32 0, i32 24)
%12 = zext i32 %0 to i64
%13 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %3, i64 0, i32 4
store i64 %12, i64* %13
%14 = load i32, i32* @DMX_IN_FRONTEND
%15 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %3, i64 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @DMX_OUT_TS_TAP
%17 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @DMX_PES_OTHER
%19 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @DMX_IMMEDIATE_START
%21 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @DMX_SET_PES_FILTER
%23 = call i32 @ioctl(i32 %6, i32 %22, %struct.dmx_pes_filter_params* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %10
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %30
27: ; preds = %10
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %6)
br label %30
30: ; preds = %27, %25, %8
%31 = phi i32 [ 1, %8 ], [ 1, %25 ], [ 0, %27 ]
ret i32 %31
}