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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int errno;
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int WaitForBlocks(int sock_fd) {
  struct timeval tv;
  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(sock_fd, &fds);
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  if (select(sock_fd + 1, &fds, NULL, NULL, &tv) < 0) {
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<WaitForBlocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1050 <FD_SET@plt>
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x0,%rax
jge    1194 <WaitForBlocks+0x64>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <WaitForBlocks+0x6b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WaitForBlocks>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
add    $0x1,%ebx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
mov    0x2e62(%rip),%rcx
sar    $0x3f,%rax
and    (%rcx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i32 0
define i32 @WaitForBlocks(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i32 @FD_ZERO(i32* %5)
%7 = load i32, i32* %3
%8 = call i32 @FD_SET(i32 %7, i32* %5)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 1
%13 = call i64 @select(i32 %12, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* @errno
store i32 %16, i32* %2
br label %18
17: ; preds = %1
store i32 0, i32* %2
br label %18
18: ; preds = %17, %15
%19 = load i32, i32* %2
ret i32 %19
}

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