#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 *);
int /*<<< orphan*/ load_iframe(int, int, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int strtol(char *, char **, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  int filefd;
  int ti = 3;
  if (argc < 2) {
    fprintf(stderr, "usage: %s <filename> [secs]\n", argv[0]);
    return -1;
  }
  if (argc == 3)
    ti = strtol(argv[2], (char **)NULL, 0);
  if ((filefd = open(argv[1], O_RDONLY)) < 0) {
    perror("File open:");
    return -1;
  }
  if ((fd = open("/dev/dvb/adapter0/video0", O_RDWR | O_NONBLOCK)) < 0) {
    perror("VIDEO DEVICE: ");
    return -1;
  }
  load_iframe(filefd, fd, ti);
  close(fd);
  close(filefd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11af <main+0x4f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe62(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <main+0x110>
cmpl   $0x3,-0x8(%rbp)
jne    11cf <main+0x6f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1209 <main+0xa9>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <main+0x110>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
or     (%rax),%esi
lea    0xe06(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124b <main+0xeb>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <main+0x110>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1080 <load_iframe@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <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
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     118c <main+0x2c>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11ff <main+0x9f>
mov    $0x3,%r14d
cmp    $0x3,%edi
jne    11a7 <main+0x47>
mov    0x10(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,%r14d
mov    0x8(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1204 <main+0xa4>
mov    %eax,%ebp
mov    0x2e22(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe50(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     120d <main+0xad>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %eax,%esi
mov    %r14d,%edx
call   1080 <load_iframe@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe12(%rip),%rdi
jmp    1214 <main+0xb4>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ff <main+0x9f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s <filename> [secs]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"File open:\00"
@.str.2 = [25 x i8] c"/dev/dvb/adapter0/video0\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [15 x i8] c"VIDEO 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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 3, i32* %8
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %51
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 3
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @strtol(i8* %23, i8** null, i32 0)
store i32 %24, i32* %8
br label %25
25: ; preds = %20, %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
34: ; preds = %25
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i32 %38, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
42: ; preds = %34
%43 = load i32, i32* %7
%44 = load i32, i32* %6
%45 = load i32, i32* %8
%46 = call i32 @load_iframe(i32 %43, i32 %44, i32 %45)
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
br label %51
51: ; preds = %42, %40, %32, %11
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s <filename> [secs]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"File open:\00"
@.str.2 = [25 x i8] c"/dev/dvb/adapter0/video0\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [15 x i8] c"VIDEO DEVICE: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %35
8: ; preds = %2
%9 = icmp eq i32 %0, 3
br i1 %9, label %10, label %14
10: ; preds = %8
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i8** null, i32 0)
br label %14
14: ; preds = %10, %8
%15 = phi i32 [ %13, %10 ], [ 3, %8 ]
%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 = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %35
23: ; preds = %14
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), 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.3, i64 0, i64 0))
br label %35
31: ; preds = %23
%32 = tail call i32 @load_iframe(i32 %19, i32 %27, i32 %15)
%33 = tail call i32 @close(i32 %27)
%34 = tail call i32 @close(i32 %19)
br label %35
35: ; preds = %31, %29, %21, %4
%36 = phi i32 [ -1, %4 ], [ -1, %21 ], [ -1, %29 ], [ 0, %31 ]
ret i32 %36
}