#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 *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int srcTcpSocket_pull(int socketFD, char *data, int len, int timeout_sec) {
  int readLen = 0;
  int dataLen = 0;
  if (timeout_sec > 0) {
    fd_set FD_set;
    struct timeval timeout;
    timeout.tv_usec = 0;
    timeout.tv_sec = timeout_sec;
    FD_ZERO(&FD_set);
    FD_SET(socketFD, &FD_set);
    int ret = select(socketFD + 1, &FD_set, NULL, NULL, &timeout);
    if (ret <= 0) {
      return ret;
    }
  }
  do {
    readLen = (int)read(socketFD, data + dataLen, (size_t)(len - dataLen));
    if (readLen > 0) {
      dataLen += readLen;
    }
  } while (readLen > 0);
  return dataLen;
}


x86 assembly (clang, -O0):
<srcTcpSocket_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 <srcTcpSocket_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 <srcTcpSocket_pull+0x82>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <srcTcpSocket_pull+0xd3>
jmp    11c7 <srcTcpSocket_pull+0x87>
jmp    11cc <srcTcpSocket_pull+0x8c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%eax
sub    -0x20(%rbp),%eax
movslq %eax,%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11fe <srcTcpSocket_pull+0xbe>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1203 <srcTcpSocket_pull+0xc3>
cmpl   $0x0,-0x1c(%rbp)
jg     11cc <srcTcpSocket_pull+0x8c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<srcTcpSocket_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 <srcTcpSocket_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    11c1 <srcTcpSocket_pull+0x81>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ebx
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%rsi
mov    %r14d,%eax
sub    %ebp,%eax
movslq %eax,%rdx
mov    %r12d,%edi
call   1030 <read@plt>
add    %eax,%ebp
test   %eax,%eax
jg     11a0 <srcTcpSocket_pull+0x60>
jmp    11c3 <srcTcpSocket_pull+0x83>
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 @srcTcpSocket_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 %57
31: ; preds = %17
br label %32
32: ; preds = %31, %4
br label %33
33: ; preds = %52, %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 = sext i32 %41 to i64
%43 = call i64 @read(i32 %34, i8* %38, i64 %42)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %33
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = add nsw i32 %49, %48
store i32 %50, i32* %11
br label %51
51: ; preds = %47, %33
br label %52
52: ; preds = %51
%53 = load i32, i32* %10
%54 = icmp sgt i32 %53, 0
br i1 %54, label %33, label %55
55: ; preds = %52
%56 = load i32, i32* %11
store i32 %56, i32* %5
br label %57
57: ; preds = %55, %29
%58 = load i32, i32* %5
ret i32 %58
}

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