#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 */
struct dvb_frontend_info {
  scalar_t__ type;
};
typedef int /*<<< orphan*/ fedev;

/* Variables and functions */
int ENODEV;
int /*<<< orphan*/ FE_GET_INFO;
scalar_t__ FE_QPSK;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, struct dvb_frontend_info *);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int /*<<< orphan*/ stderr;
int frontend_open(int *fd, int adapter, int frontend) {
  static struct dvb_frontend_info info;
  char fedev[128];
  int ret = 0;
  snprintf(fedev, sizeof(fedev), "/dev/dvb/adapter%d/frontend%d", adapter,
           frontend);
  *fd = open(fedev, O_RDWR | O_NONBLOCK);
  if (*fd < 0) {
    fprintf(stderr, "Frontend %d open failed\n", frontend);
    ret = -1;
    goto err;
  }
  ret = ioctl(*fd, FE_GET_INFO, &info);
  if (ret < 0) {
    fprintf(stderr, "ioctl FE_GET_INFO failed\n");
    goto err;
  }
  if (info.type != FE_QPSK) {
    fprintf(stderr, "frontend device is not a QPSK (DVB-S) device!\n");
    ret = -ENODEV;
    goto err;
  }
  return 0;
err:
  fprintf(stderr, "Closing Adapter:%d Frontend:%d\n", adapter, frontend);
  close(*fd);
  return ret;
}


x86 assembly (clang, -O0):
<frontend_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    $0x80,%esi
lea    0xe77(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11ea <frontend_open+0x9a>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0xa4(%rbp)
ff ff ff
jmp    1284 <frontend_open+0x134>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0x2e50(%rip),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jge    1234 <frontend_open+0xe4>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1284 <frontend_open+0x134>
mov    0x2e15(%rip),%rax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
je     1278 <frontend_open+0x128>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d4d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xa4(%rbp)
jmp    1284 <frontend_open+0x134>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <frontend_open+0x165>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0xa4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<frontend_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe94(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15d,%ecx
mov    %ebp,%r8d
call   1030 <snprintf@plt>
mov    0x2e5f(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     11fb <frontend_open+0xab>
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
lea    0x2e9d(%rip),%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
js     121b <frontend_open+0xcb>
mov    0x2e8b(%rip),%rax
mov    0x2df4(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     1258 <frontend_open+0x108>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2dc1(%rip),%rax
sub    (%rax),%ebx
jmp    1234 <frontend_open+0xe4>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1234 <frontend_open+0xe4>
mov    %eax,%ebx
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r14),%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dvb_frontend_info = type { i64 }
@frontend_open.info = internal global %struct.dvb_frontend_info zeroinitializer
@.str = [30 x i8] c"/dev/dvb/adapter%d/frontend%d\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Frontend %d open failed\0A\00"
@FE_GET_INFO = global i32 0
@.str.2 = [26 x i8] c"ioctl FE_GET_INFO failed\0A\00"
@FE_QPSK = global i64 0
@.str.3 = [47 x i8] c"frontend device is not a QPSK (DVB-S) device!\0A\00"
@ENODEV = global i32 0
@.str.4 = [32 x i8] c"Closing Adapter:%d Frontend:%d\0A\00"
define i32 @frontend_open(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i32 @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
%19 = load i32*, i32** %5
store i32 %18, i32* %19
%20 = load i32*, i32** %5
%21 = load i32, i32* %20
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %3
%24 = load i32, i32* @stderr
%25 = load i32, i32* %7
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 -1, i32* %9
br label %47
27: ; preds = %3
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = load i32, i32* @FE_GET_INFO
%31 = call i32 @ioctl(i32 %29, i32 %30, %struct.dvb_frontend_info* @frontend_open.info)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %47
37: ; preds = %27
%38 = load i64, i64* getelementptr inbounds (%struct.dvb_frontend_info, %struct.dvb_frontend_info* @frontend_open.info, i32 0, i32 0)
%39 = load i64, i64* @FE_QPSK
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @ENODEV
%45 = sub nsw i32 0, %44
store i32 %45, i32* %9
br label %47
46: ; preds = %37
store i32 0, i32* %4
br label %56
47: ; preds = %41, %34, %23
%48 = load i32, i32* @stderr
%49 = load i32, i32* %6
%50 = load i32, i32* %7
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %49, i32 %50)
%52 = load i32*, i32** %5
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %9
store i32 %55, i32* %4
br label %56
56: ; preds = %47, %46
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.dvb_frontend_info = type { i64 }
@frontend_open.info = internal global %struct.dvb_frontend_info zeroinitializer
@.str = [30 x i8] c"/dev/dvb/adapter%d/frontend%d\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Frontend %d open failed\0A\00"
@FE_GET_INFO = global i32 0
@.str.2 = [26 x i8] c"ioctl FE_GET_INFO failed\0A\00"
@FE_QPSK = global i64 0
@.str.3 = [47 x i8] c"frontend device is not a QPSK (DVB-S) device!\0A\00"
@ENODEV = global i32 0
@.str.4 = [32 x i8] c"Closing Adapter:%d Frontend:%d\0A\00"
define i32 @frontend_open(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = call i32 @open(i8* %5, i32 %9)
store i32 %10, i32* %0
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %2)
br label %31
15: ; preds = %3
%16 = load i32, i32* @FE_GET_INFO
%17 = call i32 @ioctl(i32 %10, i32 %16, %struct.dvb_frontend_info* @frontend_open.info)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %31
22: ; preds = %15
%23 = load i64, i64* getelementptr inbounds (%struct.dvb_frontend_info, %struct.dvb_frontend_info* @frontend_open.info, i64 0, i32 0)
%24 = load i64, i64* @FE_QPSK
%25 = icmp eq i64 %23, %24
br i1 %25, label %37, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @ENODEV
%30 = sub nsw i32 0, %29
br label %31
31: ; preds = %26, %19, %12
%32 = phi i32 [ -1, %12 ], [ %17, %19 ], [ %30, %26 ]
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %1, i32 %2)
%35 = load i32, i32* %0
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %22, %31
%38 = phi i32 [ %32, %31 ], [ 0, %22 ]
ret i32 %38
}