#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) {
  int readlen = 0;
  int datalen = 0;
  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;
    }
  }
  do {
    readlen = (int)read(socketfd, data + datalen, len - datalen);
    if (readlen > 0) {
      datalen += readlen;
    }
  } while (readlen > 0);
  return datalen;
}


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)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11c7 <ytcpsocket_pull+0x87>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%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     11c2 <ytcpsocket_pull+0x82>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <ytcpsocket_pull+0xd0>
jmp    11c7 <ytcpsocket_pull+0x87>
jmp    11cc <ytcpsocket_pull+0x8c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11fb <ytcpsocket_pull+0xbb>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1200 <ytcpsocket_pull+0xc0>
cmpl   $0x0,-0x1c(%rbp)
jg     11cc <ytcpsocket_pull+0x8c>
mov    -0x20(%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   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
test   %ecx,%ecx
jle    1198 <ytcpsocket_pull+0x58>
movq   $0x0,0x18(%rsp)
mov    %ecx,0x10(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%r12),%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    11be <ytcpsocket_pull+0x7e>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ebx
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%rsi
mov    %r14d,%edx
sub    %ebp,%edx
mov    %r12d,%edi
call   1030 <read@plt>
add    %eax,%ebp
test   %eax,%eax
jg     11a0 <ytcpsocket_pull+0x60>
jmp    11c0 <ytcpsocket_pull+0x80>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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 i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%15 = load i32, i32* %9
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %4
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* %9
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %19, i32* %20
%21 = call i32 @FD_ZERO(i32* %12)
%22 = load i32, i32* %6
%23 = call i32 @FD_SET(i32 %22, i32* %12)
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %12, i32* null, i32* null, %struct.timeval* %13)
store i32 %26, i32* %14
%27 = load i32, i32* %14
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = load i32, i32* %14
store i32 %30, i32* %5
br label %56
31: ; preds = %17
br label %32
32: ; preds = %31, %4
br label %33
33: ; preds = %51, %32
%34 = load i32, i32* %6
%35 = load i8*, i8** %7
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i32, i32* %8
%40 = load i32, i32* %11
%41 = sub nsw i32 %39, %40
%42 = call i64 @read(i32 %34, i8* %38, i32 %41)
%43 = trunc i64 %42 to i32
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %33
%47 = load i32, i32* %10
%48 = load i32, i32* %11
%49 = add nsw i32 %48, %47
store i32 %49, i32* %11
br label %50
50: ; preds = %46, %33
br label %51
51: ; preds = %50
%52 = load i32, i32* %10
%53 = icmp sgt i32 %52, 0
br i1 %53, label %33, label %54
54: ; preds = %51
%55 = load i32, i32* %11
store i32 %55, i32* %5
br label %56
56: ; preds = %54, %29
%57 = load i32, i32* %5
ret i32 %57
}

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 slt i32 %16, 1
br i1 %17, label %28, label %18
18: ; preds = %8, %4
br label %19
19: ; preds = %18, %19
%20 = phi i32 [ %27, %19 ], [ 0, %18 ]
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = sub nsw i32 %2, %20
%24 = call i64 @read(i32 %0, i8* %22, i32 %23)
%25 = trunc i64 %24 to i32
%26 = icmp sgt i32 %25, 0
%27 = add nsw i32 %20, %25
br i1 %26, label %19, label %28
28: ; preds = %19, %8
%29 = phi i32 [ %16, %8 ], [ %20, %19 ]
ret i32 %29
}