#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 /*<<< orphan*/ printf(char *, int);
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);
    printf("%d\n", readlen);
    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    11d7 <ytcpsocket_pull+0x87>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <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   1060 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     11d2 <ytcpsocket_pull+0x82>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <ytcpsocket_pull+0xdf>
jmp    11d7 <ytcpsocket_pull+0x87>
jmp    11dc <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   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdfe(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    121a <ytcpsocket_pull+0xca>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    121f <ytcpsocket_pull+0xcf>
cmpl   $0x0,-0x1c(%rbp)
jg     11dc <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   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r13d
test   %ecx,%ecx
jle    11a9 <ytcpsocket_pull+0x59>
movq   $0x0,0x10(%rsp)
mov    %ecx,0x8(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
lea    0x1(%r13),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jle    11e2 <ytcpsocket_pull+0x92>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %r12d,%ebx
movslq %r12d,%r12
lea    (%r15,%r12,1),%rsi
mov    %r14d,%edx
sub    %r12d,%edx
mov    %r13d,%edi
call   1040 <read@plt>
mov    %rax,%rbp
lea    0xe2e(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
add    %ebp,%r12d
test   %ebp,%ebp
jg     11b0 <ytcpsocket_pull+0x60>
jmp    11e4 <ytcpsocket_pull+0x94>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [4 x i8] c"%d\0A\00"
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 %58
31: ; preds = %17
br label %32
32: ; preds = %31, %4
br label %33
33: ; preds = %53, %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 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %10
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %33
%49 = load i32, i32* %10
%50 = load i32, i32* %11
%51 = add nsw i32 %50, %49
store i32 %51, i32* %11
br label %52
52: ; preds = %48, %33
br label %53
53: ; preds = %52
%54 = load i32, i32* %10
%55 = icmp sgt i32 %54, 0
br i1 %55, label %33, label %56
56: ; preds = %53
%57 = load i32, i32* %11
store i32 %57, i32* %5
br label %58
58: ; preds = %56, %29
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [4 x i8] c"%d\0A\00"
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 %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 = call i64 @read(i32 %0, i8* %22, i32 %23)
%25 = trunc i64 %24 to i32
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = icmp sgt i32 %25, 0
%28 = add nsw i32 %20, %25
br i1 %27, label %19, label %29
29: ; preds = %19, %8
%30 = phi i32 [ %16, %8 ], [ %20, %19 ]
ret i32 %30
}