#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 pollfd {
  int fd;
  void *events;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
void *POLLIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int nbrecv(int, char *, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ stderr;
int brecv_timeout(int s, void *vbuf, int len, int timeout) {
  char *buf = (char *)vbuf;
  int recv_size = len;
  int recv_total = 0;
  char *recv_ptr = buf;
  int initial_tries = 8;
  int i = 0;
  int ret = -1;
  struct pollfd poll_conn;
  int initial_backoff = timeout / initial_tries;
  int final_backoff = timeout - (timeout / initial_tries);
  if (timeout < 0) {
    errno = EINVAL;
    return (-1);
  }
  do {
    if ((ret = nbrecv(s, recv_ptr, recv_size)) < 0) {
      return (ret);
    }
    recv_size -= ret;
    recv_total += ret;
    recv_ptr += ret;
    if (recv_total < len) {
      poll_conn.fd = s;
      poll_conn.events = POLLIN;
    brecv_timeout_poll1_restart:
      ret = poll(&poll_conn, 1, (initial_backoff * 1000));
      if (ret < 0) {
        if (errno == EINTR)
          goto brecv_timeout_poll1_restart;
        return (ret);
      }
    }
    i++;
  } while ((i < initial_tries) && (recv_total < len));
  if (recv_total == len) {
    return (len);
  }
  poll_conn.fd = s;
  poll_conn.events = POLLIN;
brecv_timeout_poll2_restart:
  ret = poll(&poll_conn, 1, (final_backoff * 1000));
  if (ret < 0) {
    if (errno == EINTR)
      goto brecv_timeout_poll2_restart;
    return (ret);
  }
  if ((ret = nbrecv(s, recv_ptr, recv_size)) < 0) {
    fprintf(stderr, "brecv_timeout: giving up");
    return (ret);
  }
  recv_size -= ret;
  recv_total += ret;
  return (recv_total);
}


x86 assembly (clang, -O0):
<brecv_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x8,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x34(%rbp)
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x34(%rbp)
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c2 <brecv_timeout+0x92>
mov    0x2e17(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1d(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <brecv_timeout+0x250>
jmp    11c7 <brecv_timeout+0x97>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <nbrecv@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    11ed <brecv_timeout+0xbd>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <brecv_timeout+0x250>
mov    -0x3c(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1283 <brecv_timeout+0x153>
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
imul   $0x3e8,-0x54(%rbp),%edx
lea    -0x50(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    127e <brecv_timeout+0x14e>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <brecv_timeout+0x143>
jmp    1232 <brecv_timeout+0x102>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <brecv_timeout+0x250>
jmp    1283 <brecv_timeout+0x153>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
xor    %eax,%eax
cmp    -0x34(%rbp),%ecx
mov    %al,-0x5d(%rbp)
jge    12a9 <brecv_timeout+0x179>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x5d(%rbp)
mov    -0x5d(%rbp),%al
test   $0x1,%al
jne    11c7 <brecv_timeout+0x97>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    12cb <brecv_timeout+0x19b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <brecv_timeout+0x250>
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
imul   $0x3e8,-0x58(%rbp),%edx
lea    -0x50(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    132b <brecv_timeout+0x1fb>
mov    0x2cc8(%rip),%rax
mov    (%rax),%rax
mov    0x2cb6(%rip),%rcx
cmp    (%rcx),%rax
jne    1320 <brecv_timeout+0x1f0>
jmp    12df <brecv_timeout+0x1af>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <brecv_timeout+0x250>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <nbrecv@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    1366 <brecv_timeout+0x236>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
lea    0xcaa(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <brecv_timeout+0x250>
mov    -0x3c(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brecv_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,0x10(%rsp)
lea    0x7(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
js     124e <brecv_timeout+0x11e>
mov    %rsi,%r13
sar    $0x3,%eax
sub    %eax,%ecx
mov    %rcx,0x30(%rsp)
imul   $0x3e8,%eax,%ebx
movl   $0x0,0xc(%rsp)
lea    0x20(%rsp),%r14
mov    0x2e59(%rip),%r12
mov    0x2e4a(%rip),%r15
xor    %eax,%eax
mov    %rax,0x18(%rsp)
mov    %edx,0x14(%rsp)
mov    %edx,%ebp
mov    0x10(%rsp),%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <nbrecv@plt>
test   %eax,%eax
js     1267 <brecv_timeout+0x137>
sub    %eax,%ebp
mov    0xc(%rsp),%ecx
add    %eax,%ecx
mov    %eax,%eax
add    %rax,%r13
mov    %ecx,0xc(%rsp)
cmp    0x14(%rsp),%ecx
jge    1202 <brecv_timeout+0xd2>
mov    0x10(%rsp),%eax
mov    %eax,0x20(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x28(%rsp)
nop
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jns    11f0 <brecv_timeout+0xc0>
mov    (%r12),%rcx
cmp    (%r15),%rcx
je     11d0 <brecv_timeout+0xa0>
jmp    1267 <brecv_timeout+0x137>
xchg   %ax,%ax
mov    0x18(%rsp),%rcx
lea    0x1(%rcx),%eax
cmp    $0x7,%ecx
mov    %rax,0x18(%rsp)
jb     118b <brecv_timeout+0x5b>
mov    0x14(%rsp),%eax
cmp    %eax,0xc(%rsp)
je     1267 <brecv_timeout+0x137>
mov    0x10(%rsp),%eax
mov    %eax,0x20(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x28(%rsp)
imul   $0x3e8,0x30(%rsp),%ebx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jns    1276 <brecv_timeout+0x146>
mov    (%r12),%rcx
cmp    (%r15),%rcx
je     1230 <brecv_timeout+0x100>
jmp    1267 <brecv_timeout+0x137>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rax
mov    0x2d71(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <nbrecv@plt>
test   %eax,%eax
js     128e <brecv_timeout+0x15e>
add    0xc(%rsp),%eax
jmp    1267 <brecv_timeout+0x137>
mov    0x2d43(%rip),%rcx
mov    (%rcx),%edi
lea    0xd62(%rip),%rsi
mov    %eax,%ebx
call   1030 <fprintf@plt>
mov    %ebx,%eax
jmp    1267 <brecv_timeout+0x137>

LLVM IR (-O0):
%struct.pollfd = type { i32, i8* }
@EINVAL = global i64 0
@errno = global i64 0
@POLLIN = global i8* null
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"brecv_timeout: giving up\00"
define i32 @brecv_timeout(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 i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca %struct.pollfd
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%20 = load i8*, i8** %7
store i8* %20, i8** %10
%21 = load i32, i32* %8
store i32 %21, i32* %11
store i32 0, i32* %12
%22 = load i8*, i8** %10
store i8* %22, i8** %13
store i32 8, i32* %14
store i32 0, i32* %15
store i32 -1, i32* %16
%23 = load i32, i32* %9
%24 = load i32, i32* %14
%25 = sdiv i32 %23, %24
store i32 %25, i32* %18
%26 = load i32, i32* %9
%27 = load i32, i32* %9
%28 = load i32, i32* %14
%29 = sdiv i32 %27, %28
%30 = sub nsw i32 %26, %29
store i32 %30, i32* %19
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %4
%34 = load i64, i64* @EINVAL
store i64 %34, i64* @errno
store i32 -1, i32* %5
br label %132
35: ; preds = %4
br label %36
36: ; preds = %88, %35
%37 = load i32, i32* %6
%38 = load i8*, i8** %13
%39 = load i32, i32* %11
%40 = call i32 @nbrecv(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %16
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = load i32, i32* %16
store i32 %43, i32* %5
br label %132
44: ; preds = %36
%45 = load i32, i32* %16
%46 = load i32, i32* %11
%47 = sub nsw i32 %46, %45
store i32 %47, i32* %11
%48 = load i32, i32* %16
%49 = load i32, i32* %12
%50 = add nsw i32 %49, %48
store i32 %50, i32* %12
%51 = load i32, i32* %16
%52 = load i8*, i8** %13
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %13
%55 = load i32, i32* %12
%56 = load i32, i32* %8
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %77
58: ; preds = %44
%59 = load i32, i32* %6
%60 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
store i32 %59, i32* %60
%61 = load i8*, i8** @POLLIN
%62 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 1
store i8* %61, i8** %62
br label %63
63: ; preds = %73, %58
%64 = load i32, i32* %18
%65 = mul nsw i32 %64, 1000
%66 = call i32 @poll(%struct.pollfd* %17, i32 1, i32 %65)
store i32 %66, i32* %16
%67 = load i32, i32* %16
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %76
69: ; preds = %63
%70 = load i64, i64* @errno
%71 = load i64, i64* @EINTR
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %74
73: ; preds = %69
br label %63
74: ; preds = %69
%75 = load i32, i32* %16
store i32 %75, i32* %5
br label %132
76: ; preds = %63
br label %77
77: ; preds = %76, %44
%78 = load i32, i32* %15
%79 = add nsw i32 %78, 1
store i32 %79, i32* %15
br label %80
80: ; preds = %77
%81 = load i32, i32* %15
%82 = load i32, i32* %14
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %88
84: ; preds = %80
%85 = load i32, i32* %12
%86 = load i32, i32* %8
%87 = icmp slt i32 %85, %86
br label %88
88: ; preds = %84, %80
%89 = phi i1 [ false, %80 ], [ %87, %84 ]
br i1 %89, label %36, label %90
90: ; preds = %88
%91 = load i32, i32* %12
%92 = load i32, i32* %8
%93 = icmp eq i32 %91, %92
br i1 %93, label %94, label %96
94: ; preds = %90
%95 = load i32, i32* %8
store i32 %95, i32* %5
br label %132
96: ; preds = %90
%97 = load i32, i32* %6
%98 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
store i32 %97, i32* %98
%99 = load i8*, i8** @POLLIN
%100 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 1
store i8* %99, i8** %100
br label %101
101: ; preds = %111, %96
%102 = load i32, i32* %19
%103 = mul nsw i32 %102, 1000
%104 = call i32 @poll(%struct.pollfd* %17, i32 1, i32 %103)
store i32 %104, i32* %16
%105 = load i32, i32* %16
%106 = icmp slt i32 %105, 0
br i1 %106, label %107, label %114
107: ; preds = %101
%108 = load i64, i64* @errno
%109 = load i64, i64* @EINTR
%110 = icmp eq i64 %108, %109
br i1 %110, label %111, label %112
111: ; preds = %107
br label %101
112: ; preds = %107
%113 = load i32, i32* %16
store i32 %113, i32* %5
br label %132
114: ; preds = %101
%115 = load i32, i32* %6
%116 = load i8*, i8** %13
%117 = load i32, i32* %11
%118 = call i32 @nbrecv(i32 %115, i8* %116, i32 %117)
store i32 %118, i32* %16
%119 = icmp slt i32 %118, 0
br i1 %119, label %120, label %124
120: ; preds = %114
%121 = load i32, i32* @stderr
%122 = call i32 @fprintf(i32 %121, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%123 = load i32, i32* %16
store i32 %123, i32* %5
br label %132
124: ; preds = %114
%125 = load i32, i32* %16
%126 = load i32, i32* %11
%127 = sub nsw i32 %126, %125
store i32 %127, i32* %11
%128 = load i32, i32* %16
%129 = load i32, i32* %12
%130 = add nsw i32 %129, %128
store i32 %130, i32* %12
%131 = load i32, i32* %12
store i32 %131, i32* %5
br label %132
132: ; preds = %124, %120, %112, %94, %74, %42, %33
%133 = load i32, i32* %5
ret i32 %133
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i8* }
@EINVAL = global i64 0
@errno = global i64 0
@POLLIN = global i8* null
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"brecv_timeout: giving up\00"
define i32 @brecv_timeout(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = sdiv i32 %3, 8
%8 = sub nsw i32 %3, %7
%9 = icmp slt i32 %3, 0
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
%13 = mul nsw i32 %7, 1000
br label %16
14: ; preds = %4
%15 = load i64, i64* @EINVAL
store i64 %15, i64* @errno
br label %61
16: ; preds = %10, %38
%17 = phi i8* [ %27, %38 ], [ %1, %10 ]
%18 = phi i32 [ %25, %38 ], [ 0, %10 ]
%19 = phi i32 [ %39, %38 ], [ 0, %10 ]
%20 = phi i32 [ %24, %38 ], [ %2, %10 ]
%21 = call i32 @nbrecv(i32 %0, i8* %17, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %61, label %23
23: ; preds = %16
%24 = sub nsw i32 %20, %21
%25 = add nsw i32 %21, %18
%26 = zext i32 %21 to i64
%27 = getelementptr inbounds i8, i8* %17, i64 %26
%28 = icmp slt i32 %25, %2
br i1 %28, label %29, label %41
29: ; preds = %23
store i32 %0, i32* %11
%30 = load i8*, i8** @POLLIN
store i8* %30, i8** %12
br label %31
31: ; preds = %34, %29
%32 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %13)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %31, label %61
38: ; preds = %31
%39 = add nuw nsw i32 %19, 1
%40 = icmp ult i32 %19, 7
br i1 %40, label %16, label %41
41: ; preds = %23, %38
%42 = icmp eq i32 %25, %2
br i1 %42, label %61, label %43
43: ; preds = %41
store i32 %0, i32* %11
%44 = load i8*, i8** @POLLIN
store i8* %44, i8** %12
%45 = mul nsw i32 %8, 1000
br label %46
46: ; preds = %49, %43
%47 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %45)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINTR
%52 = icmp eq i64 %50, %51
br i1 %52, label %46, label %61
53: ; preds = %46
%54 = call i32 @nbrecv(i32 %0, i8* %27, i32 %24)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %61
59: ; preds = %53
%60 = add nsw i32 %54, %25
br label %61
61: ; preds = %16, %34, %49, %41, %59, %56, %14
%62 = phi i32 [ -1, %14 ], [ %54, %56 ], [ %60, %59 ], [ %2, %41 ], [ %47, %49 ], [ %32, %34 ], [ %21, %16 ]
ret i32 %62
}