#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 frequency;
};

/* Variables and functions */
int /*<<< orphan*/ FE_ATSC;
int /*<<< orphan*/ FE_SET_FRONTEND;
scalar_t__ check_frontend(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct dvb_frontend_parameters *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int setup_frontend(int fe_fd, struct dvb_frontend_parameters *frontend) {
  uint32_t mstd;
  if (check_frontend(fe_fd, FE_ATSC, &mstd) < 0) {
    close(fe_fd);
    return -1;
  }
  printf("tuning to %i Hz\n", frontend->frequency);
  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    -0x14(%rbp),%rdx
call   1070 <check_frontend@plt>
cmp    $0x0,%rax
jge    11a2 <setup_frontend+0x42>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <setup_frontend+0xe7>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1240 <setup_frontend+0xe0>
jmp    11d8 <setup_frontend+0x78>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <setup_frontend+0xe7>
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     11f7 <setup_frontend+0x97>
mov    (%rbx),%esi
lea    0xe77(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1203 <setup_frontend+0xa3>
mov    0x2e26(%rip),%rbx
mov    (%rbx),%edi
lea    0xe56(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%ebx
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe4d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11fe <setup_frontend+0x9e>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.dvb_frontend_parameters = type { i32 }
@FE_ATSC = global i32 0
@.str = [17 x i8] c"tuning to %i Hz\0A\00"
@FE_SET_FRONTEND = global i32 0
@stderr = global i32 0
@.str.1 = [8 x i8] c"ERROR: \00"
@.str.2 = [29 x i8] c"ioctl FE_SET_FRONTEND failed\00"
@.str.3 = [7 x i8] c" (%s)\0A\00"
@errno = global i32 0
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
store i32 %0, i32* %4
store %struct.dvb_frontend_parameters* %1, %struct.dvb_frontend_parameters** %5
%7 = load i32, i32* %4
%8 = load i32, i32* @FE_ATSC
%9 = call i64 @check_frontend(i32 %7, i32 %8, i32* %6)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* %4
%13 = call i32 @close(i32 %12)
store i32 -1, i32* %3
br label %36
14: ; preds = %2
%15 = load %struct.dvb_frontend_parameters*, %struct.dvb_frontend_parameters** %5
%16 = getelementptr inbounds %struct.dvb_frontend_parameters, %struct.dvb_frontend_parameters* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %4
%20 = load i32, i32* @FE_SET_FRONTEND
%21 = load %struct.dvb_frontend_parameters*, %struct.dvb_frontend_parameters** %5
%22 = call i64 @ioctl(i32 %19, i32 %20, %struct.dvb_frontend_parameters* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %14
br label %25
25: ; preds = %24
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %25
store i32 -1, i32* %3
br label %36
35: ; preds = %14
store i32 0, i32* %3
br label %36
36: ; preds = %35, %34, %11
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.dvb_frontend_parameters = type { i32 }
@FE_ATSC = global i32 0
@.str = [17 x i8] c"tuning to %i Hz\0A\00"
@FE_SET_FRONTEND = global i32 0
@stderr = global i32 0
@.str.1 = [8 x i8] c"ERROR: \00"
@.str.2 = [29 x i8] c"ioctl FE_SET_FRONTEND failed\00"
@.str.3 = [7 x i8] c" (%s)\0A\00"
@errno = global i32 0
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_ATSC
%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 %26
10: ; preds = %2
%11 = getelementptr inbounds %struct.dvb_frontend_parameters, %struct.dvb_frontend_parameters* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @FE_SET_FRONTEND
%15 = call i64 @ioctl(i32 %0, i32 %14, %struct.dvb_frontend_parameters* %1)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %10, %17, %8
%27 = phi i32 [ -1, %8 ], [ -1, %17 ], [ 0, %10 ]
ret i32 %27
}