#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ frontend_show_info(int);
int get_isdbtype(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int open_frontend(int dev_num) {
  int fefd;
  int isdbtype;
  char device[32] = {0};
  sprintf(device, "/dev/dvb/adapter%d/frontend0", dev_num);
  fefd = open(device, O_RDWR);
  if (fefd < 0) {
    fprintf(stderr, "Error: Cannot open dvb frontend. (errno=%d)\n", errno);
    return -1;
  }
  fprintf(stderr, "Info: DVB frontend = %s\n", device);
  isdbtype = get_isdbtype(fefd);
  if ((isdbtype != 0) && (isdbtype != 1)) {
    fprintf(stderr, "Error: tuner type is not ISDB-T/ISDB-S.\n");
    close(fefd);
    return -1;
  }
  fprintf(stderr, "Info: Tuner type is %s\n",
          isdbtype == 0 ? "ISDB-T" : "ISDB-S");
  if (frontend_show_info(fefd) != 0) {
    close(fefd);
    return -1;
  }
  return fefd;
}


x86 assembly (clang, -O0):
<open_frontend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe67(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e9 <open_frontend+0x79>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <open_frontend+0x139>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1070 <get_isdbtype@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     124e <open_frontend+0xde>
cmpl   $0x1,-0x10(%rbp)
je     124e <open_frontend+0xde>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <open_frontend+0x139>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%ecx
lea    0xe4a(%rip),%rdx
lea    0xe3c(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1030 <frontend_show_info@plt>
cmp    $0x0,%rax
je     12a3 <open_frontend+0x133>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <open_frontend+0x139>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_frontend>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%edx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe84(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    0x2e39(%rip),%r14
mov    (%r14),%edi
test   %eax,%eax
js     11f8 <open_frontend+0x98>
mov    %eax,%ebx
lea    0xe9b(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1060 <get_isdbtype@plt>
mov    (%r14),%edi
cmp    $0x2,%eax
jae    1211 <open_frontend+0xb1>
test   %eax,%eax
lea    0xed3(%rip),%rax
lea    0xed3(%rip),%rdx
cmove  %rax,%rdx
lea    0xea9(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1030 <frontend_show_info@plt>
test   %rax,%rax
jne    121f <open_frontend+0xbf>
jmp    122b <open_frontend+0xcb>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1226 <open_frontend+0xc6>
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/dev/dvb/adapter%d/frontend0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"Error: Cannot open dvb frontend. (errno=%d)\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Info: DVB frontend = %s\0A\00"
@.str.3 = [41 x i8] c"Error: tuner type is not ISDB-T/ISDB-S.\0A\00"
@.str.4 = [24 x i8] c"Info: Tuner type is %s\0A\00"
@.str.5 = [7 x i8] c"ISDB-T\00"
@.str.6 = [7 x i8] c"ISDB-S\00"
define i32 @open_frontend(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
store i32 %0, i32* %3
%7 = bitcast [32 x i8]* %6 to i8*
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, 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 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 -1, i32* %2
br label %51
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %4
%25 = call i32 @get_isdbtype(i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %20
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %51
36: ; preds = %28, %20
%37 = load i32, i32* @stderr
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, 0
%40 = zext i1 %39 to i64
%41 = select i1 %39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %4
%44 = call i64 @frontend_show_info(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %36
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %2
br label %51
49: ; preds = %36
%50 = load i32, i32* %4
store i32 %50, i32* %2
br label %51
51: ; preds = %49, %46, %31, %16
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@.str = [29 x i8] c"/dev/dvb/adapter%d/frontend0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"Error: Cannot open dvb frontend. (errno=%d)\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Info: DVB frontend = %s\0A\00"
@.str.3 = [41 x i8] c"Error: tuner type is not ISDB-T/ISDB-S.\0A\00"
@.str.4 = [24 x i8] c"Info: Tuner type is %s\0A\00"
@.str.5 = [7 x i8] c"ISDB-T\00"
@.str.6 = [7 x i8] c"ISDB-S\00"
define i32 @open_frontend(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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
%8 = load i32, i32* @stderr
br i1 %7, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %28
12: ; preds = %1
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%14 = call i32 @get_isdbtype(i32 %6)
%15 = icmp ult i32 %14, 2
%16 = load i32, i32* @stderr
br i1 %15, label %20, label %17
17: ; preds = %12
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @close(i32 %6)
br label %28
20: ; preds = %12
%21 = icmp eq i32 %14, 0
%22 = select i1 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = call i64 @frontend_show_info(i32 %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = call i32 @close(i32 %6)
br label %28
28: ; preds = %20, %26, %17, %9
%29 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %26 ], [ %6, %20 ]
ret i32 %29
}