#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 */

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ play_file_av(int, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int vfd, afd;
  int filefd;
  char *videodev = "/dev/dvb/adapter0/video0";
  char *audiodev = "/dev/dvb/adapter0/audio0";
  if (argc < 2) {
    fprintf(stderr, "usage: test_av_play mpeg_A+V_PES_file\n");
    return 1;
  }
  if (getenv("VIDEO"))
    videodev = getenv("VIDEO");
  if (getenv("AUDIO"))
    videodev = getenv("AUDIO");
  printf("using video device '%s'\n", videodev);
  printf("using audio device '%s'\n", audiodev);
  if ((filefd = open(argv[1], O_RDONLY)) < 0) {
    perror("File open:");
    return -1;
  }
  if ((vfd = open(videodev, O_RDWR | O_NONBLOCK)) < 0) {
    perror("VIDEO DEVICE: ");
    return -1;
  }
  if ((afd = open(audiodev, O_RDWR | O_NONBLOCK)) < 0) {
    perror("AUDIO DEVICE: ");
    return -1;
  }
  play_file_av(filefd, vfd, afd);
  close(vfd);
  close(afd);
  close(filefd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe81(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c7 <main+0x57>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1318 <main+0x1a8>
lea    0xe8b(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11ed <main+0x7d>
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1213 <main+0xa3>
lea    0xe55(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rsi
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    126d <main+0xfd>
lea    0xe3b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <main+0x1a8>
mov    -0x28(%rbp),%rdi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12ac <main+0x13c>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <main+0x1a8>
mov    -0x30(%rbp),%rdi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
mov    0x2d20(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12eb <main+0x17b>
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <main+0x1a8>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1070 <play_file_av@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     119b <main+0x2b>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xea6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1264 <main+0xf4>
mov    %rsi,%rbp
lea    0xeb4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe4c(%rip),%rbx
cmovne %rax,%rbx
lea    0xea0(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
cmovne %rax,%rbx
lea    0xe93(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe9d(%rip),%rdi
lea    0xe31(%rip),%rsi
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     126f <main+0xff>
mov    %eax,%r14d
mov    0x2ddb(%rip),%r15
mov    0x2dcc(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r15),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1278 <main+0x108>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
or     (%r15),%esi
lea    0xde4(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1281 <main+0x111>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1070 <play_file_av@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe21(%rip),%rdi
jmp    1288 <main+0x118>
lea    0xe23(%rip),%rdi
jmp    1288 <main+0x118>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1264 <main+0xf4>

LLVM IR (-O0):
@.str = [25 x i8] c"/dev/dvb/adapter0/video0\00"
@.str.1 = [25 x i8] c"/dev/dvb/adapter0/audio0\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"usage: test_av_play mpeg_A+V_PES_file\0A\00"
@.str.3 = [6 x i8] c"VIDEO\00"
@.str.4 = [6 x i8] c"AUDIO\00"
@.str.5 = [25 x i8] c"using video device '%s'\0A\00"
@.str.6 = [25 x i8] c"using audio device '%s'\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [11 x i8] c"File open:\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.8 = [15 x i8] c"VIDEO DEVICE: \00"
@.str.9 = [15 x i8] c"AUDIO DEVICE: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %9
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %68
16: ; preds = %2
%17 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i8* %20, i8** %9
br label %21
21: ; preds = %19, %16
%22 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i8* %25, i8** %9
br label %26
26: ; preds = %24, %21
%27 = load i8*, i8** %9
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %10
%30 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
39: ; preds = %26
%40 = load i8*, i8** %9
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_NONBLOCK
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %40, i32 %43)
store i32 %44, i32* %6
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
48: ; preds = %39
%49 = load i8*, i8** %10
%50 = load i32, i32* @O_RDWR
%51 = load i32, i32* @O_NONBLOCK
%52 = or i32 %50, %51
%53 = call i32 @open(i8* %49, i32 %52)
store i32 %53, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %3
br label %68
57: ; preds = %48
%58 = load i32, i32* %8
%59 = load i32, i32* %6
%60 = load i32, i32* %7
%61 = call i32 @play_file_av(i32 %58, i32 %59, i32 %60)
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 0, i32* %3
br label %68
68: ; preds = %57, %55, %46, %37, %13
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [25 x i8] c"/dev/dvb/adapter0/video0\00"
@.str.1 = [25 x i8] c"/dev/dvb/adapter0/audio0\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"usage: test_av_play mpeg_A+V_PES_file\0A\00"
@.str.3 = [6 x i8] c"VIDEO\00"
@.str.4 = [6 x i8] c"AUDIO\00"
@.str.5 = [25 x i8] c"using video device '%s'\0A\00"
@.str.6 = [25 x i8] c"using audio device '%s'\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [11 x i8] c"File open:\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.8 = [15 x i8] c"VIDEO DEVICE: \00"
@.str.9 = [15 x i8] c"AUDIO DEVICE: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %44
7: ; preds = %2
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %8
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* %10, i8* %11
%14 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %7
%22 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %44
23: ; preds = %7
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* %13, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
br label %44
31: ; preds = %23
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %33, %32
%35 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %44
39: ; preds = %31
%40 = tail call i32 @play_file_av(i32 %19, i32 %27, i32 %35)
%41 = tail call i32 @close(i32 %27)
%42 = tail call i32 @close(i32 %35)
%43 = tail call i32 @close(i32 %19)
br label %44
44: ; preds = %39, %37, %29, %21, %4
%45 = phi i32 [ 1, %4 ], [ -1, %21 ], [ -1, %29 ], [ -1, %37 ], [ 0, %39 ]
ret i32 %45
}