#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 timeval {
  long tv_sec;
  long tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int sbus_listen(int n_sbus_handle, float f_timeout) {
  fd_set fdset;
  FD_ZERO(&fdset);
  FD_SET(n_sbus_handle, &fdset);
  struct timeval tv;
  struct timeval *tvp;
  if (f_timeout > 0.0) {
    tv.tv_sec = (long)f_timeout;
    tv.tv_usec = (long)((f_timeout - (long)f_timeout) * 1000 * 1000);
    tvp = &tv;
  } else {
    tvp = NULL;
  }
  int n_status = select(n_sbus_handle + 1, &fdset, (fd_set *)0, (fd_set *)0,
                        (struct timeval *)tvp);
  if (n_status == 0) {
    syslog(LOG_DEBUG, "sbus_listen: Timed out");
  } else if (n_status == 1) {
    if (!FD_ISSET(n_sbus_handle, &fdset)) {
      syslog(LOG_ERR, "sbus_listen: Select returned on a different fs. %s",
             strerror(errno));
      n_status = -1;
    } else {
      syslog(LOG_DEBUG, "sbus_listen: SBus listened successfully");
    }
  } else {
    syslog(LOG_ERR, "sbus_listen: Select returned unexpectedly. %s",
           strerror(errno));
  }
  return n_status;
}


x86 assembly (clang, -O0):
<sbus_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movss  %xmm0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11e7 <sbus_listen+0x87>
cvttss2si -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movss  -0x8(%rbp),%xmm0
cvttss2si -0x8(%rbp),%rax
cvtsi2ss %rax,%xmm1
subss  %xmm1,%xmm0
movss  0xe3f(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  0xe33(%rip),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11ef <sbus_listen+0x8f>
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1232 <sbus_listen+0xd2>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    12d8 <sbus_listen+0x178>
cmpl   $0x1,-0x2c(%rbp)
jne    12a6 <sbus_listen+0x146>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    128a <sbus_listen+0x12a>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x2c(%rbp)
jmp    12a1 <sbus_listen+0x141>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    12d3 <sbus_listen+0x173>
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    12d8 <sbus_listen+0x178>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbus_listen>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movss  %xmm0,0x4(%rsp)
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
movss  0x4(%rsp),%xmm1
xorps  %xmm0,%xmm0
ucomiss %xmm0,%xmm1
jbe    11c8 <sbus_listen+0x68>
cvttss2si %xmm1,%rax
mov    %rax,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
subss  %xmm0,%xmm1
movss  0xe51(%rip),%xmm0
mulss  %xmm0,%xmm1
mulss  %xmm0,%xmm1
cvttss2si %xmm1,%rax
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%r8
jmp    11cb <sbus_listen+0x6b>
xor    %r8d,%r8d
lea    0x1(%rbx),%edi
mov    %rsp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0x1,%eax
je     1203 <sbus_listen+0xa3>
mov    %eax,%ebp
test   %eax,%eax
jne    122f <sbus_listen+0xcf>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    1288 <sbus_listen+0x128>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     125a <sbus_listen+0xfa>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    $0x1,%ebp
jmp    1288 <sbus_listen+0x128>
mov    0x2d92(%rip),%rax
mov    (%rax),%ebx
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    1288 <sbus_listen+0x128>
mov    0x2d67(%rip),%rax
mov    (%rax),%ebx
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xda3(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@LOG_DEBUG = global i32 0
@.str = [23 x i8] c"sbus_listen: Timed out\00"
@LOG_ERR = global i32 0
@.str.1 = [51 x i8] c"sbus_listen: Select returned on a different fs. %s\00"
@errno = global i32 0
@.str.2 = [40 x i8] c"sbus_listen: SBus listened successfully\00"
@.str.3 = [46 x i8] c"sbus_listen: Select returned unexpectedly. %s\00"
define i32 @sbus_listen(i32 %0, float %1) {
%3 = alloca i32
%4 = alloca float
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval*
%8 = alloca i32
store i32 %0, i32* %3
store float %1, float* %4
%9 = call i32 @FD_ZERO(i32* %5)
%10 = load i32, i32* %3
%11 = call i32 @FD_SET(i32 %10, i32* %5)
%12 = load float, float* %4
%13 = fpext float %12 to double
%14 = fcmp ogt double %13, 0.000000e+00
br i1 %14, label %15, label %28
15: ; preds = %2
%16 = load float, float* %4
%17 = fptosi float %16 to i64
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i64 %17, i64* %18
%19 = load float, float* %4
%20 = load float, float* %4
%21 = fptosi float %20 to i64
%22 = sitofp i64 %21 to float
%23 = fsub float %19, %22
%24 = fmul float %23, 1.000000e+03
%25 = fmul float %24, 1.000000e+03
%26 = fptosi float %25 to i64
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 %26, i64* %27
store %struct.timeval* %6, %struct.timeval** %7
br label %29
28: ; preds = %2
store %struct.timeval* null, %struct.timeval** %7
br label %29
29: ; preds = %28, %15
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
%32 = load %struct.timeval*, %struct.timeval** %7
%33 = call i32 @select(i32 %31, i32* %5, i32* null, i32* null, %struct.timeval* %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* @LOG_DEBUG
%38 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %61
39: ; preds = %29
%40 = load i32, i32* %8
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = load i32, i32* %3
%44 = call i32 @FD_ISSET(i32 %43, i32* %5)
%45 = icmp ne i32 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %42
%47 = load i32, i32* @LOG_ERR
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %49)
store i32 -1, i32* %8
br label %54
51: ; preds = %42
%52 = load i32, i32* @LOG_DEBUG
%53 = call i32 (i32, i8*, ...) @syslog(i32 %52, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %51, %46
br label %60
55: ; preds = %39
%56 = load i32, i32* @LOG_ERR
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @syslog(i32 %56, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %55, %54
br label %61
61: ; preds = %60, %36
%62 = load i32, i32* %8
ret i32 %62
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@LOG_DEBUG = global i32 0
@.str = [23 x i8] c"sbus_listen: Timed out\00"
@LOG_ERR = global i32 0
@.str.1 = [51 x i8] c"sbus_listen: Select returned on a different fs. %s\00"
@errno = global i32 0
@.str.2 = [40 x i8] c"sbus_listen: SBus listened successfully\00"
@.str.3 = [46 x i8] c"sbus_listen: Select returned unexpectedly. %s\00"
define i32 @sbus_listen(i32 %0, float %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 %0, i32* %3)
%8 = bitcast %struct.timeval* %4 to i8*
%9 = fcmp ogt float %1, 0.000000e+00
br i1 %9, label %10, label %19
10: ; preds = %2
%11 = fptosi float %1 to i64
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i64 %11, i64* %12
%13 = sitofp i64 %11 to float
%14 = fsub float %1, %13
%15 = fmul float %14, 1.000000e+03
%16 = fmul float %15, 1.000000e+03
%17 = fptosi float %16 to i64
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 %17, i64* %18
br label %19
19: ; preds = %2, %10
%20 = phi %struct.timeval* [ %4, %10 ], [ null, %2 ]
%21 = add nsw i32 %0, 1
%22 = call i32 @select(i32 %21, i32* %3, i32* null, i32* null, %struct.timeval* %20)
switch i32 %22, label %37 [
i32 0, label %23
i32 1, label %26
]
23: ; preds = %19
%24 = load i32, i32* @LOG_DEBUG
%25 = call i32 (i32, i8*, ...) @syslog(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %42
26: ; preds = %19
%27 = call i32 @FD_ISSET(i32 %0, i32* %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @LOG_ERR
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @syslog(i32 %30, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %42
34: ; preds = %26
%35 = load i32, i32* @LOG_DEBUG
%36 = call i32 (i32, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %42
37: ; preds = %19
%38 = load i32, i32* @LOG_ERR
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @syslog(i32 %38, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %37, %34, %29, %23
%43 = phi i32 [ 0, %23 ], [ 1, %34 ], [ -1, %29 ], [ %22, %37 ]
ret i32 %43
}