#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*/ perror(char *);
int recv(int, char *, size_t, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int readwrapper(int sock, char *buf, size_t size) {
  int ret = 0;
  int curread = 0;
  fd_set rfds;
  struct timeval tv;
  int retval;
  while (curread < size) {
    FD_ZERO(&rfds);
    FD_SET(sock, &rfds);
    tv.tv_sec = 300;
    tv.tv_usec = 0;
    retval = select(sock + 1, &rfds, NULL, NULL, &tv);
    if (retval == -1) {
      perror("error reading");
      return -1;
    } else if (retval == 0) {
      return -1;
    }
    ret = recv(sock, buf + curread, size - curread, 0);
    if (ret == -1)
      return ret;
    if (ret == 0)
      return ret;
    curread += ret;
  }
  return curread;
}


x86 assembly (clang, -O0):
<readwrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1258 <readwrapper+0x108>
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x12c,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
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   $0xffffffff,-0x3c(%rbp)
jne    11e2 <readwrapper+0x92>
lea    0xe2f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <readwrapper+0x10e>
cmpl   $0x0,-0x3c(%rbp)
jne    11f8 <readwrapper+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <readwrapper+0x10e>
jmp    11fd <readwrapper+0xad>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x20(%rbp),%rax
sub    %rax,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1235 <readwrapper+0xe5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125e <readwrapper+0x10e>
cmpl   $0x0,-0x1c(%rbp)
jne    124a <readwrapper+0xfa>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125e <readwrapper+0x10e>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1171 <readwrapper+0x21>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readwrapper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x8(%rsp)
test   %rdx,%rdx
je     11ed <readwrapper+0x9d>
mov    %rdx,%r15
mov    %edi,%ebx
lea    0x1(%rbx),%r12d
xor    %r14d,%r14d
lea    0x14(%rsp),%r13
xor    %ebp,%ebp
nop
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
movl   $0x12c,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %r12d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1050 <select@plt>
test   %eax,%eax
je     11f1 <readwrapper+0xa1>
cmp    $0xffffffff,%eax
je     120b <readwrapper+0xbb>
mov    0x8(%rsp),%rax
lea    (%rax,%r14,1),%rsi
mov    %r15,%rdx
sub    %r14,%rdx
mov    %ebx,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     11f8 <readwrapper+0xa8>
test   %eax,%eax
je     11ed <readwrapper+0x9d>
add    %eax,%ebp
movslq %ebp,%r14
cmp    %r15,%r14
jb     1180 <readwrapper+0x30>
jmp    11fa <readwrapper+0xaa>
xor    %ebp,%ebp
jmp    11fa <readwrapper+0xaa>
mov    $0xffffffff,%ebp
jmp    11fa <readwrapper+0xaa>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11fa <readwrapper+0xaa>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [14 x i8] c"error reading\00"
define i32 @readwrapper(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %13
13: ; preds = %56, %3
%14 = load i32, i32* %9
%15 = sext i32 %14 to i64
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %60
18: ; preds = %13
%19 = call i32 @FD_ZERO(i32* %10)
%20 = load i32, i32* %5
%21 = call i32 @FD_SET(i32 %20, i32* %10)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 300, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
31: ; preds = %18
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 -1, i32* %4
br label %62
35: ; preds = %31
br label %36
36: ; preds = %35
%37 = load i32, i32* %5
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i64, i64* %7
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = sub i64 %42, %44
%46 = call i32 @recv(i32 %37, i8* %41, i64 %45, i32 0)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %36
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %62
51: ; preds = %36
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* %8
store i32 %55, i32* %4
br label %62
56: ; preds = %51
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = add nsw i32 %58, %57
store i32 %59, i32* %9
br label %13
60: ; preds = %13
%61 = load i32, i32* %9
store i32 %61, i32* %4
br label %62
62: ; preds = %60, %54, %49, %34, %29
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [14 x i8] c"error reading\00"
define i32 @readwrapper(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = icmp eq i64 %2, 0
br i1 %8, label %31, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%12 = add nsw i32 %0, 1
br label %13
13: ; preds = %9, %25
%14 = phi i64 [ 0, %9 ], [ %27, %25 ]
%15 = phi i32 [ 0, %9 ], [ %26, %25 ]
%16 = call i32 @FD_ZERO(i32* %4)
%17 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 300, i32* %10
store i64 0, i64* %11
%18 = call i32 @select(i32 %12, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %18, label %21 [
i32 -1, label %19
i32 0, label %29
]
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %31
21: ; preds = %13
%22 = getelementptr inbounds i8, i8* %1, i64 %14
%23 = sub i64 %2, %14
%24 = call i32 @recv(i32 %0, i8* %22, i64 %23, i32 0)
switch i32 %24, label %25 [
i32 -1, label %29
i32 0, label %31
]
25: ; preds = %21
%26 = add nsw i32 %24, %15
%27 = sext i32 %26 to i64
%28 = icmp ult i64 %27, %2
br i1 %28, label %13, label %29
29: ; preds = %25, %21, %13
%30 = phi i32 [ %26, %25 ], [ %24, %21 ], [ -1, %13 ]
br label %31
31: ; preds = %21, %29, %3, %19
%32 = phi i32 [ -1, %19 ], [ 0, %3 ], [ %30, %29 ], [ 0, %21 ]
ret i32 %32
}