#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 */
typedef int /*<<< orphan*/ uint32_t;
struct dvb_frontend_parameters {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ FE_QAM;
int /*<<< orphan*/ FE_SET_FRONTEND;
int /*<<< orphan*/ SYS_DVBC_ANNEX_AC;
scalar_t__ check_frontend(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int dvbfe_set_delsys(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct dvb_frontend_parameters *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int setup_frontend(int fe_fd, struct dvb_frontend_parameters *frontend) {
  int ret;
  uint32_t mstd;
  if (check_frontend(fe_fd, FE_QAM, &mstd) < 0) {
    close(fe_fd);
    return -1;
  }
  ret = dvbfe_set_delsys(fe_fd, SYS_DVBC_ANNEX_AC);
  if (ret) {
    do {
      fprintf(stderr, "ERROR: ");
      fprintf(stderr, "SET Delsys failed");
      fprintf(stderr, " (%s)\n", strerror(errno));
    } while (0);
    return -1;
  }
  if (ioctl(fe_fd, FE_SET_FRONTEND, frontend) < 0) {
    do {
      fprintf(stderr, "ERROR: ");
      fprintf(stderr, "ioctl FE_SET_FRONTEND failed");
      fprintf(stderr, " (%s)\n", strerror(errno));
    } while (0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_frontend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <check_frontend@plt>
cmp    $0x0,%rax
jge    11a2 <setup_frontend+0x42>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <setup_frontend+0x160>
mov    -0x8(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
call   1050 <dvbfe_set_delsys@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     122d <setup_frontend+0xcd>
jmp    11c5 <setup_frontend+0x65>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <setup_frontend+0x160>
mov    -0x8(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    12b9 <setup_frontend+0x159>
jmp    1251 <setup_frontend+0xf1>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <setup_frontend+0x160>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_frontend>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1070 <check_frontend@plt>
test   %rax,%rax
js     11e0 <setup_frontend+0x80>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dvbfe_set_delsys@plt>
test   %eax,%eax
je     11f3 <setup_frontend+0x93>
mov    0x2e3d(%rip),%rbx
mov    (%rbx),%edi
lea    0xe5c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe48(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11e7 <setup_frontend+0x87>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    11ec <setup_frontend+0x8c>
mov    0x2dc1(%rip),%rbx
mov    (%rbx),%edi
lea    0xde0(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdf1(%rip),%rsi
jmp    11b4 <setup_frontend+0x54>

LLVM IR (-O0):
%struct.dvb_frontend_parameters = type { i32 }
@FE_QAM = global i32 0
@SYS_DVBC_ANNEX_AC = global i32 0
@stderr = global i32 0
@.str = [8 x i8] c"ERROR: \00"
@.str.1 = [18 x i8] c"SET Delsys failed\00"
@.str.2 = [7 x i8] c" (%s)\0A\00"
@errno = global i32 0
@FE_SET_FRONTEND = global i32 0
@.str.3 = [29 x i8] c"ioctl FE_SET_FRONTEND failed\00"
define i32 @setup_frontend(i32 %0, %struct.dvb_frontend_parameters* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.dvb_frontend_parameters*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.dvb_frontend_parameters* %1, %struct.dvb_frontend_parameters** %5
%8 = load i32, i32* %4
%9 = load i32, i32* @FE_QAM
%10 = call i64 @check_frontend(i32 %8, i32 %9, i32* %7)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 @close(i32 %13)
store i32 -1, i32* %3
br label %50
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* @SYS_DVBC_ANNEX_AC
%18 = call i32 @dvbfe_set_delsys(i32 %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %15
br label %22
22: ; preds = %21
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %22
store i32 -1, i32* %3
br label %50
32: ; preds = %15
%33 = load i32, i32* %4
%34 = load i32, i32* @FE_SET_FRONTEND
%35 = load %struct.dvb_frontend_parameters*, %struct.dvb_frontend_parameters** %5
%36 = call i64 @ioctl(i32 %33, i32 %34, %struct.dvb_frontend_parameters* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %32
br label %39
39: ; preds = %38
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %39
store i32 -1, i32* %3
br label %50
49: ; preds = %32
store i32 0, i32* %3
br label %50
50: ; preds = %49, %48, %31, %12
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.dvb_frontend_parameters = type { i32 }
@FE_QAM = global i32 0
@SYS_DVBC_ANNEX_AC = global i32 0
@stderr = global i32 0
@.str = [8 x i8] c"ERROR: \00"
@.str.1 = [18 x i8] c"SET Delsys failed\00"
@.str.2 = [7 x i8] c" (%s)\0A\00"
@errno = global i32 0
@FE_SET_FRONTEND = global i32 0
@.str.3 = [29 x i8] c"ioctl FE_SET_FRONTEND failed\00"
define i32 @setup_frontend(i32 %0, %struct.dvb_frontend_parameters* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @FE_QAM
%6 = call i64 @check_frontend(i32 %0, i32 %5, i32* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @close(i32 %0)
br label %36
10: ; preds = %2
%11 = load i32, i32* @SYS_DVBC_ANNEX_AC
%12 = call i32 @dvbfe_set_delsys(i32 %0, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %36
23: ; preds = %10
%24 = load i32, i32* @FE_SET_FRONTEND
%25 = call i64 @ioctl(i32 %0, i32 %24, %struct.dvb_frontend_parameters* %1)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %23, %27, %14, %8
%37 = phi i32 [ -1, %8 ], [ -1, %14 ], [ -1, %27 ], [ 0, %23 ]
ret i32 %37
}