#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 {
  int dummy;
};
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) {
  fd_set fdset;
  FD_ZERO(&fdset);
  FD_SET(n_sbus_handle, &fdset);
  int n_status = select(n_sbus_handle + 1, &fdset, (fd_set *)0, (fd_set *)0,
                        (struct timeval *)0);
  if (0 > n_status)
    syslog(LOG_ERR, "sbus_listen: Select returned unexpectedly. %s",
           strerror(errno));
  else {
    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
      n_status = 0;
  }
  syslog(LOG_DEBUG, "sbus_listen: SBus listened successfully");
  return n_status;
}


x86 assembly (clang, -O0):
<sbus_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
jle    11da <sbus_listen+0x7a>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    1234 <sbus_listen+0xd4>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1228 <sbus_listen+0xc8>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0x1,-0xc(%rbp)
jmp    122f <sbus_listen+0xcf>
movl   $0x0,-0xc(%rbp)
jmp    1234 <sbus_listen+0xd4>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbus_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %r14d,%r14d
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
test   %eax,%eax
js     11b8 <sbus_listen+0x58>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
jne    11e7 <sbus_listen+0x87>
mov    $0x1,%r14d
lea    0xe78(%rip),%rbx
jmp    11c2 <sbus_listen+0x62>
lea    0xe41(%rip),%rbx
mov    %eax,%r14d
mov    0x2dff(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@LOG_ERR = global i32 0
@.str = [46 x i8] c"sbus_listen: Select returned unexpectedly. %s\00"
@errno = global i32 0
@.str.1 = [51 x i8] c"sbus_listen: Select returned on a different fs. %s\00"
@LOG_DEBUG = global i32 0
@.str.2 = [40 x i8] c"sbus_listen: SBus listened successfully\00"
define i32 @sbus_listen(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @FD_ZERO(i32* %3)
%6 = load i32, i32* %2
%7 = call i32 @FD_SET(i32 %6, i32* %3)
%8 = load i32, i32* %2
%9 = add nsw i32 %8, 1
%10 = call i32 @select(i32 %9, i32* %3, i32* null, i32* null, %struct.timeval* null)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp sgt i32 0, %11
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @LOG_ERR
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @syslog(i32 %14, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %29
18: ; preds = %1
%19 = load i32, i32* %2
%20 = call i32 @FD_ISSET(i32 %19, i32* %3)
%21 = icmp ne i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = load i32, i32* @LOG_ERR
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @syslog(i32 %23, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 1, i32* %4
br label %28
27: ; preds = %18
store i32 0, i32* %4
br label %28
28: ; preds = %27, %22
br label %29
29: ; preds = %28, %13
%30 = load i32, i32* @LOG_DEBUG
%31 = call i32 (i32, i8*, ...) @syslog(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@LOG_ERR = global i32 0
@.str = [46 x i8] c"sbus_listen: Select returned unexpectedly. %s\00"
@errno = global i32 0
@.str.1 = [51 x i8] c"sbus_listen: Select returned on a different fs. %s\00"
@LOG_DEBUG = global i32 0
@.str.2 = [40 x i8] c"sbus_listen: SBus listened successfully\00"
define i32 @sbus_listen(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @FD_ZERO(i32* %2)
%5 = call i32 @FD_SET(i32 %0, i32* %2)
%6 = add nsw i32 %0, 1
%7 = call i32 @select(i32 %6, i32* %2, i32* null, i32* null, %struct.timeval* null)
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i32 @FD_ISSET(i32 %0, i32* %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9, %1
%13 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), %9 ]
%14 = phi i32 [ %7, %1 ], [ 1, %9 ]
%15 = load i32, i32* @LOG_ERR
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @syslog(i32 %15, i8* %13, i32 %17)
br label %19
19: ; preds = %12, %9
%20 = phi i32 [ 0, %9 ], [ %14, %12 ]
%21 = load i32, i32* @LOG_DEBUG
%22 = call i32 (i32, i8*, ...) @syslog(i32 %21, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
ret i32 %20
}