#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*/ *);
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, int timeout_sec) {
  if (timeout_sec > 0) {
    fd_set fdset;
    struct timeval timeout;
    timeout.tv_usec = 0;
    timeout.tv_sec = timeout_sec;
    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)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11b9 <ytcpsocket_pull+0x79>
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     11b4 <ytcpsocket_pull+0x74>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <ytcpsocket_pull+0x91>
jmp    11b9 <ytcpsocket_pull+0x79>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%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,%r15
mov    %edi,%ebp
test   %ecx,%ecx
jle    1192 <ytcpsocket_pull+0x52>
movq   $0x0,0x10(%rsp)
mov    %ecx,0x8(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbp),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    119f <ytcpsocket_pull+0x5f>
mov    %ebp,%edi
mov    %r15,%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 { i32, i64 }
define i32 @ytcpsocket_pull(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%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 i32 %3, i32* %9
%14 = load i32, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %4
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %9
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %18, i32* %19
%20 = call i32 @FD_ZERO(i32* %10)
%21 = load i32, i32* %6
%22 = call i32 @FD_SET(i32 %21, i32* %10)
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = load i32, i32* %12
store i32 %29, i32* %5
br label %38
30: ; preds = %16
br label %31
31: ; preds = %30, %4
%32 = load i32, i32* %6
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i64 @read(i32 %32, i8* %33, i32 %34)
%36 = trunc i64 %35 to i32
store i32 %36, i32* %13
%37 = load i32, i32* %13
store i32 %37, i32* %5
br label %38
38: ; preds = %31, %28
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @ytcpsocket_pull(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = icmp sgt i32 %3, 0
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = bitcast i32* %5 to i8*
%10 = bitcast %struct.timeval* %6 to i8*
%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 %3, i32* %12
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = add nsw i32 %0, 1
%16 = call i32 @select(i32 %15, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %8, %4
%19 = call i64 @read(i32 %0, i8* %1, i32 %2)
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %8, %18
%22 = phi i32 [ %16, %8 ], [ %20, %18 ]
ret i32 %22
}