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

/* Variables and functions */
void *DMX_IMMEDIATE_START;
void *DMX_IN_FRONTEND;
void *DMX_OUT_DECODER;
int /*<<< orphan*/ DMX_PES_AUDIO;
int /*<<< orphan*/ DMX_PES_VIDEO;
int /*<<< orphan*/ DMX_SET_PES_FILTER;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct dmx_pes_filter_params *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int setup_demux(char *dmxdev, int video_pid, int audio_pid) {
  int vfd, afd;
  struct dmx_pes_filter_params pesfilter;
  printf("video_pid == 0x%04x\n", video_pid);
  printf("audio_pid == 0x%04x\n", audio_pid);
  if ((vfd = open(dmxdev, O_RDWR)) < 0) {
    perror("open 1");
    return -1;
  }
  pesfilter.pid = video_pid;
  pesfilter.input = DMX_IN_FRONTEND;
  pesfilter.output = DMX_OUT_DECODER;
  pesfilter.pes_type = DMX_PES_VIDEO;
  pesfilter.flags = DMX_IMMEDIATE_START;
  if (ioctl(vfd, DMX_SET_PES_FILTER, &pesfilter) < 0) {
    perror("ioctl DMX_SET_PES_FILTER (video)");
    return -1;
  }
  close(vfd);
  if ((afd = open(dmxdev, O_RDWR)) < 0) {
    perror("open 1");
    return -1;
  }
  pesfilter.pid = audio_pid;
  pesfilter.input = DMX_IN_FRONTEND;
  pesfilter.output = DMX_OUT_DECODER;
  pesfilter.pes_type = DMX_PES_AUDIO;
  pesfilter.flags = DMX_IMMEDIATE_START;
  if (ioctl(afd, DMX_SET_PES_FILTER, &pesfilter) < 0) {
    perror("ioctl DMX_SET_PES_FILTER (audio)");
    return -1;
  }
  close(afd);
  return 0;
}


x86 assembly (clang, -O0):
<setup_demux>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe94(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b6 <setup_demux+0x66>
lea    0xe85(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <setup_demux+0x199>
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1229 <setup_demux+0xd9>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <setup_demux+0x199>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1267 <setup_demux+0x117>
lea    0xdd4(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <setup_demux+0x199>
mov    -0x18(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d56(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d1c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    12da <setup_demux+0x18a>
lea    0xd89(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <setup_demux+0x199>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_demux>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r15
lea    0xe93(%rip),%rdi
call   1030 <printf@plt>
lea    0xe9c(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
mov    0x2e58(%rip),%r12
mov    (%r12),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     126a <setup_demux+0x11a>
mov    %eax,%ebp
mov    %ebx,(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2dde(%rip),%r13
mov    0x0(%r13),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1273 <setup_demux+0x123>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%r12),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     126a <setup_demux+0x11a>
mov    %eax,%ebx
mov    %r14d,(%rsp)
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2d74(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x0(%r13),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     127c <setup_demux+0x12c>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    128d <setup_demux+0x13d>
lea    0xdb9(%rip),%rdi
jmp    1283 <setup_demux+0x133>
lea    0xdb7(%rip),%rdi
jmp    1283 <setup_demux+0x133>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dmx_pes_filter_params = type { i32, i8*, i32, i8*, i8* }
@.str = [21 x i8] c"video_pid == 0x%04x\0A\00"
@.str.1 = [21 x i8] c"audio_pid == 0x%04x\0A\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"open 1\00"
@DMX_IN_FRONTEND = global i8* null
@DMX_OUT_DECODER = global i8* null
@DMX_PES_VIDEO = global i32 0
@DMX_IMMEDIATE_START = global i8* null
@DMX_SET_PES_FILTER = global i32 0
@.str.3 = [33 x i8] c"ioctl DMX_SET_PES_FILTER (video)\00"
@DMX_PES_AUDIO = global i32 0
@.str.4 = [33 x i8] c"ioctl DMX_SET_PES_FILTER (audio)\00"
define i32 @setup_demux(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.dmx_pes_filter_params
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* %6
%12 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %7
%14 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i8*, i8** %5
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %67
21: ; preds = %3
%22 = load i32, i32* %6
%23 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** @DMX_IN_FRONTEND
%25 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 4
store i8* %24, i8** %25
%26 = load i8*, i8** @DMX_OUT_DECODER
%27 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 3
store i8* %26, i8** %27
%28 = load i32, i32* @DMX_PES_VIDEO
%29 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i8*, i8** @DMX_IMMEDIATE_START
%31 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 1
store i8* %30, i8** %31
%32 = load i32, i32* %8
%33 = load i32, i32* @DMX_SET_PES_FILTER
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.dmx_pes_filter_params* %10)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %21
%37 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %67
38: ; preds = %21
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %5
%42 = load i32, i32* @O_RDWR
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %9
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %67
47: ; preds = %38
%48 = load i32, i32* %7
%49 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 0
store i32 %48, i32* %49
%50 = load i8*, i8** @DMX_IN_FRONTEND
%51 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 4
store i8* %50, i8** %51
%52 = load i8*, i8** @DMX_OUT_DECODER
%53 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 3
store i8* %52, i8** %53
%54 = load i32, i32* @DMX_PES_AUDIO
%55 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 2
store i32 %54, i32* %55
%56 = load i8*, i8** @DMX_IMMEDIATE_START
%57 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %10, i32 0, i32 1
store i8* %56, i8** %57
%58 = load i32, i32* %9
%59 = load i32, i32* @DMX_SET_PES_FILTER
%60 = call i64 @ioctl(i32 %58, i32 %59, %struct.dmx_pes_filter_params* %10)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %47
%63 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %67
64: ; preds = %47
%65 = load i32, i32* %9
%66 = call i32 @close(i32 %65)
store i32 0, i32* %4
br label %67
67: ; preds = %64, %62, %45, %36, %19
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.dmx_pes_filter_params = type { i32, i8*, i32, i8*, i8* }
@.str = [21 x i8] c"video_pid == 0x%04x\0A\00"
@.str.1 = [21 x i8] c"audio_pid == 0x%04x\0A\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"open 1\00"
@DMX_IN_FRONTEND = global i8* null
@DMX_OUT_DECODER = global i8* null
@DMX_PES_VIDEO = global i32 0
@DMX_IMMEDIATE_START = global i8* null
@DMX_SET_PES_FILTER = global i32 0
@.str.3 = [33 x i8] c"ioctl DMX_SET_PES_FILTER (video)\00"
@DMX_PES_AUDIO = global i32 0
@.str.4 = [33 x i8] c"ioctl DMX_SET_PES_FILTER (audio)\00"
define i32 @setup_demux(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.dmx_pes_filter_params
%5 = bitcast %struct.dmx_pes_filter_params* %4 to i8*
%6 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %1)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %47
13: ; preds = %3
%14 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %4, i64 0, i32 0
store i32 %1, i32* %14
%15 = load i8*, i8** @DMX_IN_FRONTEND
%16 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %4, i64 0, i32 4
store i8* %15, i8** %16
%17 = load i8*, i8** @DMX_OUT_DECODER
%18 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %4, i64 0, i32 3
store i8* %17, i8** %18
%19 = load i32, i32* @DMX_PES_VIDEO
%20 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i8*, i8** @DMX_IMMEDIATE_START
%22 = getelementptr inbounds %struct.dmx_pes_filter_params, %struct.dmx_pes_filter_params* %4, i64 0, i32 1
store i8* %21, i8** %22
%23 = load i32, i32* @DMX_SET_PES_FILTER
%24 = call i64 @ioctl(i32 %9, i32 %23, %struct.dmx_pes_filter_params* %4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %47
28: ; preds = %13
%29 = call i32 @close(i32 %9)
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %0, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %47
35: ; preds = %28
store i32 %2, i32* %14
%36 = load i8*, i8** @DMX_IN_FRONTEND
store i8* %36, i8** %16
%37 = load i8*, i8** @DMX_OUT_DECODER
store i8* %37, i8** %18
%38 = load i32, i32* @DMX_PES_AUDIO
store i32 %38, i32* %20
%39 = load i8*, i8** @DMX_IMMEDIATE_START
store i8* %39, i8** %22
%40 = load i32, i32* @DMX_SET_PES_FILTER
%41 = call i64 @ioctl(i32 %31, i32 %40, %struct.dmx_pes_filter_params* %4)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %47
45: ; preds = %35
%46 = call i32 @close(i32 %31)
br label %47
47: ; preds = %45, %43, %33, %26, %11
%48 = phi i32 [ -1, %11 ], [ -1, %26 ], [ -1, %33 ], [ -1, %43 ], [ 0, %45 ]
ret i32 %48
}