#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 /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ task_yield();
int sys_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
               struct timeval *timeout) {
  int ret;
  struct timeval nonblock = {0, 0};
RETRY:
  if ((ret = select(nfds, readfds, writefds, exceptfds, &nonblock)) == 0) {
    task_yield();
    goto RETRY;
  }
  return ret;
}


x86 assembly (clang, -O0):
<sys_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    118b <sys_select+0x5b>
mov    $0x0,%al
call   1050 <task_yield@plt>
jmp    115b <sys_select+0x2b>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_select>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%r8
call   1030 <select@plt>
test   %eax,%eax
jne    117e <sys_select+0x5e>
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1040 <task_yield@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1030 <select@plt>
test   %eax,%eax
je     1160 <sys_select+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @sys_select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.timeval*
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store %struct.timeval* %4, %struct.timeval** %10
%13 = bitcast %struct.timeval* %12 to i8*
br label %14
14: ; preds = %21, %5
%15 = load i32, i32* %6
%16 = load i32*, i32** %7
%17 = load i32*, i32** %8
%18 = load i32*, i32** %9
%19 = call i32 @select(i32 %15, i32* %16, i32* %17, i32* %18, %struct.timeval* %12)
store i32 %19, i32* %11
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (...) @task_yield()
br label %14
23: ; preds = %14
%24 = load i32, i32* %11
ret i32 %24
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @sys_select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* nocapture readnone %4) {
%6 = alloca i64
%7 = bitcast i64* %6 to %struct.timeval*
%8 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%9 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %5, %11
%12 = call i32 (...) @task_yield()
%13 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %11, label %15
15: ; preds = %11, %5
%16 = phi i32 [ %9, %5 ], [ %13, %11 ]
ret i32 %16
}