#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 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 = 0;
    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    1193 <ytcpsocket_pull+0x63>
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   1050 <FD_SET@plt>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     118e <ytcpsocket_pull+0x5e>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ab <ytcpsocket_pull+0x7b>
jmp    1193 <ytcpsocket_pull+0x63>
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   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
test   %ecx,%ecx
jle    1155 <ytcpsocket_pull+0x25>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
xor    %eax,%eax
jmp    115c <ytcpsocket_pull+0x2c>
mov    %ebx,%edi
call   1030 <read@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 %28
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)
store i32 0, i32* %12
%23 = load i32, i32* %12
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = load i32, i32* %12
store i32 %26, i32* %5
br label %35
27: ; preds = %16
br label %28
28: ; preds = %27, %4
%29 = load i32, i32* %6
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = call i64 @read(i32 %29, i8* %30, i32 %31)
%33 = trunc i64 %32 to i32
store i32 %33, i32* %13
%34 = load i32, i32* %13
store i32 %34, i32* %5
br label %35
35: ; preds = %28, %25
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @ytcpsocket_pull(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = bitcast i32* %5 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
%10 = call i32 @FD_SET(i32 %0, i32* %5)
br label %14
11: ; preds = %4
%12 = tail call i64 @read(i32 %0, i8* %1, i32 %2)
%13 = trunc i64 %12 to i32
br label %14
14: ; preds = %7, %11
%15 = phi i32 [ 0, %7 ], [ %13, %11 ]
ret i32 %15
}