#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 tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ recv(int, void *, size_t, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
ssize_t sock_read(int fd, void *buf, size_t len, int flags) {
  fd_set rfds;
  struct timeval tv;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 100;
  select(fd + 1, &rfds, NULL, NULL, &tv);
  return recv(fd, buf, len, flags);
}


x86 assembly (clang, -O0):
<sock_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x28(%rbp)
movl   $0x64,-0x30(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <recv@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
movl   $0x64,0x10(%rsp)
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <recv@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @sock_read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%11 = call i32 @FD_ZERO(i32* %9)
%12 = load i32, i32* %5
%13 = call i32 @FD_SET(i32 %12, i32* %9)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 100, i32* %15
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
%18 = call i32 @select(i32 %17, i32* %9, i32* null, i32* null, %struct.timeval* %10)
%19 = load i32, i32* %5
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = load i32, i32* %8
%23 = call i32 @recv(i32 %19, i8* %20, i64 %21, i32 %22)
ret i32 %23
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @sock_read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
%10 = call i32 @FD_SET(i32 %0, i32* %5)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 100, i32* %12
%13 = add nsw i32 %0, 1
%14 = call i32 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%15 = call i32 @recv(i32 %0, i8* %1, i64 %2, i32 %3)
ret i32 %15
}