#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*/ PATH_MAX;
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int dvbdemux_open_dvr(int adapter, int dvrdevice, int readonly,
                      int nonblocking) {
  char filename[PATH_MAX + 1];
  int flags = O_RDWR;
  int fd;
  if (readonly)
    flags = O_RDONLY;
  if (nonblocking)
    flags |= O_NONBLOCK;
  sprintf(filename, "/dev/dvb/adapter%i/dvr%i", adapter, dvrdevice);
  if ((fd = open(filename, flags)) < 0) {
    sprintf(filename, "/dev/dvb%i.dvr%i", adapter, dvrdevice);
    fd = open(filename, flags);
  }
  return fd;
}


x86 assembly (clang, -O0):
<dvbdemux_open_dvr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1185 <dvbdemux_open_dvr+0x65>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     119e <dvbdemux_open_dvr+0x7e>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe51(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11f1 <dvbdemux_open_dvr+0xd1>
mov    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe3c(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dvbdemux_open_dvr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %edx,%edx
jne    1163 <dvbdemux_open_dvr+0x43>
mov    0x2e84(%rip),%rax
test   %r12d,%r12d
jne    116f <dvbdemux_open_dvr+0x4f>
jmp    1179 <dvbdemux_open_dvr+0x59>
mov    0x2e66(%rip),%rax
test   %r12d,%r12d
je     1179 <dvbdemux_open_dvr+0x59>
mov    0x2e62(%rip),%rcx
mov    (%rcx),%r12d
or     (%rax),%r12d
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <open@plt>
test   %eax,%eax
jns    11c0 <dvbdemux_open_dvr+0xa0>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <open@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [25 x i8] c"/dev/dvb/adapter%i/dvr%i\00"
@.str.1 = [17 x i8] c"/dev/dvb%i.dvr%i\00"
define i32 @dvbdemux_open_dvr(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i32, i32* @PATH_MAX
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
%18 = load i32, i32* @O_RDWR
store i32 %18, i32* %11
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = load i32, i32* @O_RDONLY
store i32 %22, i32* %11
br label %23
23: ; preds = %21, %4
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @O_NONBLOCK
%28 = load i32, i32* %11
%29 = or i32 %28, %27
store i32 %29, i32* %11
br label %30
30: ; preds = %26, %23
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i32, i32* %11
%35 = call i32 @open(i8* %17, i32 %34)
store i32 %35, i32* %12
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %30
%38 = load i32, i32* %5
%39 = load i32, i32* %6
%40 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39)
%41 = load i32, i32* %11
%42 = call i32 @open(i8* %17, i32 %41)
store i32 %42, i32* %12
br label %43
43: ; preds = %37, %30
%44 = load i32, i32* %12
%45 = load i8*, i8** %9
ret i32 %44
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [25 x i8] c"/dev/dvb/adapter%i/dvr%i\00"
@.str.1 = [17 x i8] c"/dev/dvb%i.dvr%i\00"
define i32 @dvbdemux_open_dvr(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = icmp eq i32 %2, 0
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_RDONLY
%12 = select i1 %9, i32 %10, i32 %11
%13 = icmp eq i32 %3, 0
%14 = load i32, i32* @O_NONBLOCK
%15 = select i1 %13, i32 0, i32 %14
%16 = or i32 %15, %12
%17 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%18 = call i32 @open(i8* %8, i32 %16)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %1)
%22 = call i32 @open(i8* %8, i32 %16)
br label %23
23: ; preds = %20, %4
%24 = phi i32 [ %22, %20 ], [ %18, %4 ]
ret i32 %24
}