#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_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int sys_select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
               struct timeval *tval) {
  int ret;
  fd_set *readfds2, readfds_buf;
  if (readfds) {
    readfds2 = readfds;
  } else {
    readfds2 = &readfds_buf;
    FD_ZERO(readfds2);
  }
  errno = 0;
  ret = select(maxfd, readfds2, writefds, errorfds, tval);
  if (ret <= 0) {
    FD_ZERO(readfds2);
    if (writefds)
      FD_ZERO(writefds);
    if (errorfds)
      FD_ZERO(errorfds);
  }
  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)
cmpq   $0x0,-0x10(%rbp)
je     1153 <sys_select+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1164 <sys_select+0x44>
lea    -0x3c(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    0x2e75(%rip),%rax
movq   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     11cd <sys_select+0xad>
mov    -0x38(%rbp),%rdi
call   1030 <FD_ZERO@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b4 <sys_select+0x94>
mov    -0x18(%rbp),%rdi
call   1030 <FD_ZERO@plt>
cmpq   $0x0,-0x20(%rbp)
je     11c8 <sys_select+0xa8>
mov    -0x20(%rbp),%rdi
call   1030 <FD_ZERO@plt>
jmp    11cd <sys_select+0xad>
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    %r8,%r12
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
test   %rsi,%rsi
jne    114c <sys_select+0x2c>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    0x2e8d(%rip),%rax
movq   $0x0,(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1040 <select@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1195 <sys_select+0x75>
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
test   %r15,%r15
je     1188 <sys_select+0x68>
mov    %r15,%rdi
call   1030 <FD_ZERO@plt>
test   %r14,%r14
je     1195 <sys_select+0x75>
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@errno = global i64 0
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 i32*
%13 = alloca i32
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
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = load i32*, i32** %7
store i32* %17, i32** %12
br label %21
18: ; preds = %5
store i32* %13, i32** %12
%19 = load i32*, i32** %12
%20 = call i32 @FD_ZERO(i32* %19)
br label %21
21: ; preds = %18, %16
store i64 0, i64* @errno
%22 = load i32, i32* %6
%23 = load i32*, i32** %12
%24 = load i32*, i32** %8
%25 = load i32*, i32** %9
%26 = load %struct.timeval*, %struct.timeval** %10
%27 = call i32 @select(i32 %22, i32* %23, i32* %24, i32* %25, %struct.timeval* %26)
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %45
30: ; preds = %21
%31 = load i32*, i32** %12
%32 = call i32 @FD_ZERO(i32* %31)
%33 = load i32*, i32** %8
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32*, i32** %8
%37 = call i32 @FD_ZERO(i32* %36)
br label %38
38: ; preds = %35, %30
%39 = load i32*, i32** %9
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32*, i32** %9
%43 = call i32 @FD_ZERO(i32* %42)
br label %44
44: ; preds = %41, %38
br label %45
45: ; preds = %44, %21
%46 = load i32, i32* %11
ret i32 %46
}

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