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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int ytcpsocket_pull(int socketfd, char *data, int len, double timeout_sec) {
  if (timeout_sec > 0) {
    fd_set fdset;
    struct timeval timeout;
    timeout.tv_usec = timeout_sec * 1000000;
    timeout.tv_sec = 0;
    FD_ZERO(&fdset);
    FD_SET(socketfd, &fdset);
    int ret = select(socketfd + 1, &fdset, NULL, NULL, &timeout);
    if (ret <= 0) {
      return ret;
    }
  }
  int readlen = (int)read(socketfd, data, len);
  return readlen;
}


x86 assembly (clang, -O0):
<ytcpsocket_pull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11cf <ytcpsocket_pull+0x8f>
movsd  0xe8f(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     11ca <ytcpsocket_pull+0x8a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <ytcpsocket_pull+0xa7>
jmp    11cf <ytcpsocket_pull+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ytcpsocket_pull>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11a2 <ytcpsocket_pull+0x62>
mulsd  0xe9c(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbp),%edi
lea    0x8(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    11af <ytcpsocket_pull+0x6f>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { double, i64 }
define i32 @ytcpsocket_pull(i32 %0, i8* %1, i32 %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store double %3, double* %9
%14 = load double, double* %9
%15 = fcmp ogt double %14, 0.000000e+00
br i1 %15, label %16, label %32
16: ; preds = %4
%17 = load double, double* %9
%18 = fmul double %17, 1.000000e+06
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store double %18, double* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %20
%21 = call i32 @FD_ZERO(i32* %10)
%22 = load i32, i32* %6
%23 = call i32 @FD_SET(i32 %22, i32* %10)
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = load i32, i32* %12
store i32 %30, i32* %5
br label %39
31: ; preds = %16
br label %32
32: ; preds = %31, %4
%33 = load i32, i32* %6
%34 = load i8*, i8** %7
%35 = load i32, i32* %8
%36 = call i64 @read(i32 %33, i8* %34, i32 %35)
%37 = trunc i64 %36 to i32
store i32 %37, i32* %13
%38 = load i32, i32* %13
store i32 %38, i32* %5
br label %39
39: ; preds = %32, %29
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
%struct.timeval = type { double, i64 }
define i32 @ytcpsocket_pull(i32 %0, i8* nocapture %1, i32 %2, double %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = fcmp ogt double %3, 0.000000e+00
br i1 %7, label %8, label %19
8: ; preds = %4
%9 = bitcast i32* %5 to i8*
%10 = bitcast %struct.timeval* %6 to i8*
%11 = fmul double %3, 1.000000e+06
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store double %11, double* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %13
%14 = call i32 @FD_ZERO(i32* %5)
%15 = call i32 @FD_SET(i32 %0, i32* %5)
%16 = add nsw i32 %0, 1
%17 = call i32 @select(i32 %16, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %8, %4
%20 = call i64 @read(i32 %0, i8* %1, i32 %2)
%21 = trunc i64 %20 to i32
br label %22
22: ; preds = %8, %19
%23 = phi i32 [ %17, %8 ], [ %21, %19 ]
ret i32 %23
}