#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_RDWR;
int /*<<< orphan*/ close(int);
char *getenv(char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ process_kbd_input(int, int);
int main(void) {
  int vfd, afd;
  char *videodev = "/dev/dvb/adapter0/video0";
  char *audiodev = "/dev/dvb/adapter0/audio0";
  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);
  printf("enter command or h + enter for help\n");
  if ((vfd = open(videodev, O_RDWR | O_NONBLOCK)) < 0) {
    perror("open video device");
    return 1;
  }
  if ((afd = open(audiodev, O_RDWR | O_NONBLOCK)) < 0) {
    perror("open audio device");
    return 1;
  }
  while (process_kbd_input(vfd, afd) == 0)
    ;
  close(vfd);
  close(afd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe98(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11ab <main+0x4b>
lea    0xe90(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    0xe86(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11d1 <main+0x71>
lea    0xe70(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1242 <main+0xe2>
lea    0xe64(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b7 <main+0x157>
mov    -0x20(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1281 <main+0x121>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b7 <main+0x157>
jmp    1286 <main+0x126>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <process_kbd_input@plt>
cmp    $0x0,%rax
jne    12a0 <main+0x140>
jmp    1286 <main+0x126>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xeb7(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe76(%rip),%rbx
cmovne %rax,%rbx
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
cmovne %rax,%rbx
lea    0xe96(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe9e(%rip),%rdi
lea    0xe59(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xec6(%rip),%rdi
call   1050 <puts@plt>
mov    0x2e0e(%rip),%r14
mov    0x2dff(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1233 <main+0xc3>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
or     (%r14),%esi
lea    0xe17(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     123c <main+0xcc>
mov    %eax,%ebp
nopl   (%rax)
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <process_kbd_input@plt>
test   %rax,%rax
je     1210 <main+0xa0>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe36(%rip),%rdi
jmp    1243 <main+0xd3>
lea    0xe3f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    122e <main+0xbe>

LLVM IR (-O0):
@.str = [25 x i8] c"/dev/dvb/adapter0/video0\00"
@.str.1 = [25 x i8] c"/dev/dvb/adapter0/audio0\00"
@.str.2 = [6 x i8] c"VIDEO\00"
@.str.3 = [6 x i8] c"AUDIO\00"
@.str.4 = [25 x i8] c"using video device '%s'\0A\00"
@.str.5 = [25 x i8] c"using audio device '%s'\0A\00"
@.str.6 = [37 x i8] c"enter command or h + enter for help\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.7 = [18 x i8] c"open video device\00"
@.str.8 = [18 x i8] c"open audio device\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8** %5
%6 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %9, i8** %4
br label %10
10: ; preds = %8, %0
%11 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i8* %14, i8** %4
br label %15
15: ; preds = %13, %10
%16 = load i8*, i8** %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %21, i32 %24)
store i32 %25, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %50
29: ; preds = %15
%30 = load i8*, i8** %5
%31 = load i32, i32* @O_RDWR
%32 = load i32, i32* @O_NONBLOCK
%33 = or i32 %31, %32
%34 = call i32 @open(i8* %30, i32 %33)
store i32 %34, i32* %3
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %50
38: ; preds = %29
br label %39
39: ; preds = %44, %38
%40 = load i32, i32* %2
%41 = load i32, i32* %3
%42 = call i64 @process_kbd_input(i32 %40, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
br label %39
45: ; preds = %39
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
store i32 0, i32* %1
br label %50
50: ; preds = %45, %36, %27
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [25 x i8] c"/dev/dvb/adapter0/video0\00"
@.str.1 = [25 x i8] c"/dev/dvb/adapter0/audio0\00"
@.str.2 = [6 x i8] c"VIDEO\00"
@.str.3 = [6 x i8] c"AUDIO\00"
@.str.4 = [25 x i8] c"using video device '%s'\0A\00"
@.str.5 = [25 x i8] c"using audio device '%s'\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.7 = [18 x i8] c"open video device\00"
@.str.8 = [18 x i8] c"open audio device\00"
@str = [36 x i8] c"enter command or h + enter for help\00"
define i32 @main() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%5 = icmp eq i8* %4, null
%6 = select i1 %5, i8* %3, i8* %4
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %6, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %31
17: ; preds = %0
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %19, %18
%21 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %31
25: ; preds = %17, %25
%26 = tail call i64 @process_kbd_input(i32 %13, i32 %21)
%27 = icmp eq i64 %26, 0
br i1 %27, label %25, label %28
28: ; preds = %25
%29 = tail call i32 @close(i32 %13)
%30 = tail call i32 @close(i32 %21)
br label %31
31: ; preds = %28, %23, %15
%32 = phi i32 [ 1, %15 ], [ 1, %23 ], [ 0, %28 ]
ret i32 %32
}