#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned int data;
};
struct dtv_property {
  TYPE_1__ u;
  int /*<<< orphan*/ cmd;
};
struct dtv_properties {
  int num;
  struct dtv_property *props;
};

/* Variables and functions */
int /*<<< orphan*/ DTV_DELIVERY_SYSTEM;
#define FE_ATSC 131
#define FE_OFDM 130
#define FE_QAM 129
#define FE_QPSK 128
int /*<<< orphan*/ FE_SET_PROPERTY;
unsigned int SYS_ATSC;
unsigned int SYS_DVBC_ANNEX_AC;
unsigned int SYS_DVBS;
unsigned int SYS_DVBT;
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct dtv_properties *);
int set_delivery_system(int fd, unsigned type) {
  struct dtv_properties props;
  struct dtv_property dvb_prop[1];
  unsigned delsys;
  switch (type) {
  case FE_QPSK:
    delsys = SYS_DVBS;
    break;
  case FE_QAM:
    delsys = SYS_DVBC_ANNEX_AC;
    break;
  case FE_OFDM:
    delsys = SYS_DVBT;
    break;
  case FE_ATSC:
    delsys = SYS_ATSC;
    break;
  default:
    return -1;
  }
  dvb_prop[0].cmd = DTV_DELIVERY_SYSTEM;
  dvb_prop[0].u.data = delsys;
  props.num = 1;
  props.props = dvb_prop;
  if (ioctl(fd, FE_SET_PROPERTY, &props) >= 0)
    return 0;
  return errno;
}


x86 assembly (clang, -O0):
<set_delivery_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     118b <set_delivery_system+0x7b>
mov    -0x38(%rbp),%rax
lea    0xec2(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1197 <set_delivery_system+0x87>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1197 <set_delivery_system+0x87>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1197 <set_delivery_system+0x87>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1197 <set_delivery_system+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <set_delivery_system+0xdf>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jl     11e3 <set_delivery_system+0xd3>
movl   $0x0,-0x4(%rbp)
jmp    11ef <set_delivery_system+0xdf>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_delivery_system>:
add    $0xffffff80,%esi
mov    $0xffffffff,%eax
cmp    $0x3,%esi
ja     117b <set_delivery_system+0x6b>
sub    $0x18,%rsp
movslq %esi,%rax
lea    0x2cbd(%rip),%rcx
mov    (%rcx,%rax,8),%rax
mov    (%rax),%eax
mov    0x2eb0(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rsp)
mov    %eax,(%rsp)
movl   $0x1,0x8(%rsp)
mov    %rsp,%rax
mov    %rax,0x10(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1177 <set_delivery_system+0x67>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.dtv_properties = type { i32, %struct.dtv_property* }
%struct.dtv_property = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@SYS_DVBS = global i32 0
@SYS_DVBC_ANNEX_AC = global i32 0
@SYS_DVBT = global i32 0
@SYS_ATSC = global i32 0
@DTV_DELIVERY_SYSTEM = global i32 0
@FE_SET_PROPERTY = global i32 0
@errno = global i32 0
define i32 @set_delivery_system(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.dtv_properties
%7 = alloca [1 x %struct.dtv_property]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
switch i32 %9, label %18 [
i32 128, label %10
i32 129, label %12
i32 130, label %14
i32 131, label %16
]
10: ; preds = %2
%11 = load i32, i32* @SYS_DVBS
store i32 %11, i32* %8
br label %19
12: ; preds = %2
%13 = load i32, i32* @SYS_DVBC_ANNEX_AC
store i32 %13, i32* %8
br label %19
14: ; preds = %2
%15 = load i32, i32* @SYS_DVBT
store i32 %15, i32* %8
br label %19
16: ; preds = %2
%17 = load i32, i32* @SYS_ATSC
store i32 %17, i32* %8
br label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %37
19: ; preds = %16, %14, %12, %10
%20 = load i32, i32* @DTV_DELIVERY_SYSTEM
%21 = getelementptr inbounds [1 x %struct.dtv_property], [1 x %struct.dtv_property]* %7, i64 0, i64 0
%22 = getelementptr inbounds %struct.dtv_property, %struct.dtv_property* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = load i32, i32* %8
%24 = getelementptr inbounds [1 x %struct.dtv_property], [1 x %struct.dtv_property]* %7, i64 0, i64 0
%25 = getelementptr inbounds %struct.dtv_property, %struct.dtv_property* %24, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %23, i32* %26
%27 = getelementptr inbounds %struct.dtv_properties, %struct.dtv_properties* %6, i32 0, i32 0
store i32 1, i32* %27
%28 = getelementptr inbounds [1 x %struct.dtv_property], [1 x %struct.dtv_property]* %7, i64 0, i64 0
%29 = getelementptr inbounds %struct.dtv_properties, %struct.dtv_properties* %6, i32 0, i32 1
store %struct.dtv_property* %28, %struct.dtv_property** %29
%30 = load i32, i32* %4
%31 = load i32, i32* @FE_SET_PROPERTY
%32 = call i64 @ioctl(i32 %30, i32 %31, %struct.dtv_properties* %6)
%33 = icmp sge i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %19
store i32 0, i32* %3
br label %37
35: ; preds = %19
%36 = load i32, i32* @errno
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %34, %18
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.dtv_properties = type { i32, %struct.dtv_property* }
%struct.dtv_property = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@SYS_DVBS = global i32 0
@SYS_DVBC_ANNEX_AC = global i32 0
@SYS_DVBT = global i32 0
@SYS_ATSC = global i32 0
@DTV_DELIVERY_SYSTEM = global i32 0
@FE_SET_PROPERTY = global i32 0
@errno = global i32 0
@switch.table.set_delivery_system = [4 x i32*] [i32* @SYS_DVBS, i32* @SYS_DVBC_ANNEX_AC, i32* @SYS_DVBT, i32* @SYS_ATSC]
define i32 @set_delivery_system(i32 %0, i32 %1) {
%3 = alloca %struct.dtv_properties
%4 = alloca [1 x %struct.dtv_property]
%5 = bitcast %struct.dtv_properties* %3 to i8*
%6 = bitcast [1 x %struct.dtv_property]* %4 to i8*
%7 = add i32 %1, -128
%8 = icmp ult i32 %7, 4
br i1 %8, label %9, label %25
9: ; preds = %2
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds [4 x i32*], [4 x i32*]* @switch.table.set_delivery_system, i64 0, i64 %10
%12 = load i32*, i32** %11
%13 = load i32, i32* %12
%14 = load i32, i32* @DTV_DELIVERY_SYSTEM
%15 = getelementptr inbounds [1 x %struct.dtv_property], [1 x %struct.dtv_property]* %4, i64 0, i64 0
%16 = getelementptr inbounds [1 x %struct.dtv_property], [1 x %struct.dtv_property]* %4, i64 0, i64 0, i32 1
store i32 %14, i32* %16
%17 = getelementptr inbounds [1 x %struct.dtv_property], [1 x %struct.dtv_property]* %4, i64 0, i64 0, i32 0, i32 0
store i32 %13, i32* %17
%18 = getelementptr inbounds %struct.dtv_properties, %struct.dtv_properties* %3, i64 0, i32 0
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.dtv_properties, %struct.dtv_properties* %3, i64 0, i32 1
store %struct.dtv_property* %15, %struct.dtv_property** %19
%20 = load i32, i32* @FE_SET_PROPERTY
%21 = call i64 @ioctl(i32 %0, i32 %20, %struct.dtv_properties* %3)
%22 = icmp sgt i64 %21, -1
%23 = load i32, i32* @errno
%24 = select i1 %22, i32 0, i32 %23
br label %25
25: ; preds = %2, %9
%26 = phi i32 [ -1, %2 ], [ %24, %9 ]
ret i32 %26
}