#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 errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int open_dvr(int dev_num) {
  int dvrfd = -1;
  char device[32] = {0};
  sprintf(device, "/dev/dvb/adapter%d/dvr0", dev_num);
  if ((dvrfd = open(device, O_RDONLY | O_NONBLOCK)) < 0) {
    fprintf(stderr, "Error: Cannot open dvr device. (errno=%d)\n", errno);
    return -1;
  }
  return dvrfd;
}


x86 assembly (clang, -O0):
<open_dvr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c6 <open_dvr+0x86>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
lea    0xe63(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <open_dvr+0x8c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_dvr>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%edx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xeb6(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
mov    0x2e84(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %eax,%eax
jns    1196 <open_dvr+0x66>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
lea    0xe8c(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/dev/dvb/adapter%d/dvr0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [43 x i8] c"Error: Cannot open dvr device. (errno=%d)\0A\00"
@errno = global i32 0
define i32 @open_dvr(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [32 x i8]
store i32 %0, i32* %3
store i32 -1, i32* %4
%6 = bitcast [32 x i8]* %5 to i8*
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 -1, i32* %2
br label %22
20: ; preds = %1
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@.str = [24 x i8] c"/dev/dvb/adapter%d/dvr0\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [43 x i8] c"Error: Cannot open dvr device. (errno=%d)\0A\00"
@errno = global i32 0
define i32 @open_dvr(i32 %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %1, %10
%15 = phi i32 [ -1, %10 ], [ %8, %1 ]
ret i32 %15
}