#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;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ MSG_WAITALL;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ fwprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ net_fail(int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
size_t recv(int, void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
extern size_t net_recv_timed(int fd, void *buf, size_t size, int timeout) {
  size_t brcv = 0;
  int pret = 0;
  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  pret = poll(fds, 1, timeout);
  if (pret > 0) {
    brcv = recv(fd, buf, size, MSG_WAITALL);
    if (brcv == 0) {
      fwprintf(stderr, L"net_recv_timed(): connection closed unexpectedly\n");
      net_fail(fd);
      return -3;
    } else if (brcv == -1) {
      perror("net_recv_timed(): recv failed");
      return -1;
    }
    return brcv;
  } else if (pret == 0)
    return -2;
  else {
    perror("net_recv_timed(): poll failed");
    return -1;
  }
}


x86 assembly (clang, -O0):
<net_recv_timed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    122d <net_recv_timed+0xdd>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f7 <net_recv_timed+0xa7>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1060 <fwprintf@plt>
mov    -0xc(%rbp),%edi
call   1040 <net_fail@plt>
movq   $0xfffffffffffffffd,-0x8(%rbp)
ff
jmp    1258 <net_recv_timed+0x108>
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    121b <net_recv_timed+0xcb>
lea    0xebf(%rip),%rdi
call   1070 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1258 <net_recv_timed+0x108>
jmp    1220 <net_recv_timed+0xd0>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1258 <net_recv_timed+0x108>
cmpl   $0x0,-0x34(%rbp)
jne    1244 <net_recv_timed+0xf4>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    1258 <net_recv_timed+0x108>
lea    0xe9b(%rip),%rdi
call   1070 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_recv_timed>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    %edi,0x8(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %ecx,%edx
call   1050 <poll@plt>
test   %eax,%eax
jle    11b1 <net_recv_timed+0x61>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <recv@plt>
test   %rax,%rax
je     11cf <net_recv_timed+0x7f>
cmp    $0xffffffffffffffff,%rax
jne    11f2 <net_recv_timed+0xa2>
lea    0xf19(%rip),%rdi
jmp    11c1 <net_recv_timed+0x71>
mov    $0xfffffffffffffffe,%rax
je     11f2 <net_recv_timed+0xa2>
lea    0xf25(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    11f2 <net_recv_timed+0xa2>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1060 <fwprintf@plt>
mov    %ebx,%edi
call   1040 <net_fail@plt>
mov    $0xfffffffffffffffd,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@MSG_WAITALL = global i32 0
@stderr = global i32 0
@.str = [50 x i32] [i32 110, i32 101, i32 116, i32 95, i32 114, i32 101, i32 99, i32 118, i32 95, i32 116, i32 105, i32 109, i32 101, i32 100, i32 40, i32 41, i32 58, i32 32, i32 99, i32 111, i32 110, i32 110, i32 101, i32 99, i32 116, i32 105, i32 111, i32 110, i32 32, i32 99, i32 108, i32 111, i32 115, i32 101, i32 100, i32 32, i32 117, i32 110, i32 101, i32 120, i32 112, i32 101, i32 99, i32 116, i32 101, i32 100, i32 108, i32 121, i32 10, i32 0]
@.str.1 = [30 x i8] c"net_recv_timed(): recv failed\00"
@.str.2 = [30 x i8] c"net_recv_timed(): poll failed\00"
define i64 @net_recv_timed(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca [1 x %struct.pollfd]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 0, i64* %10
store i32 0, i32* %11
%13 = load i32, i32* %6
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %12, i64 0, i64 0
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* @POLLIN
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %12, i64 0, i64 0
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 1
store i32 %16, i32* %18
%19 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %12, i64 0, i64 0
%20 = load i32, i32* %9
%21 = call i32 @poll(%struct.pollfd* %19, i32 1, i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %45
24: ; preds = %4
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = load i32, i32* @MSG_WAITALL
%29 = call i64 @recv(i32 %25, i8* %26, i64 %27, i32 %28)
store i64 %29, i64* %10
%30 = load i64, i64* %10
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load i32, i32* @stderr
%34 = call i32 @fwprintf(i32 %33, i8* bitcast ([50 x i32]* @.str to i8*))
%35 = load i32, i32* %6
%36 = call i32 @net_fail(i32 %35)
store i64 -3, i64* %5
br label %51
37: ; preds = %24
%38 = load i64, i64* %10
%39 = icmp eq i64 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i64 -1, i64* %5
br label %51
42: ; preds = %37
br label %43
43: ; preds = %42
%44 = load i64, i64* %10
store i64 %44, i64* %5
br label %51
45: ; preds = %4
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
store i64 -2, i64* %5
br label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i64 -1, i64* %5
br label %51
51: ; preds = %49, %48, %43, %40, %32
%52 = load i64, i64* %5
ret i64 %52
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@MSG_WAITALL = global i32 0
@stderr = global i32 0
@.str = [50 x i32] [i32 110, i32 101, i32 116, i32 95, i32 114, i32 101, i32 99, i32 118, i32 95, i32 116, i32 105, i32 109, i32 101, i32 100, i32 40, i32 41, i32 58, i32 32, i32 99, i32 111, i32 110, i32 110, i32 101, i32 99, i32 116, i32 105, i32 111, i32 110, i32 32, i32 99, i32 108, i32 111, i32 115, i32 101, i32 100, i32 32, i32 117, i32 110, i32 101, i32 120, i32 112, i32 101, i32 99, i32 116, i32 101, i32 100, i32 108, i32 121, i32 10, i32 0]
@.str.1 = [30 x i8] c"net_recv_timed(): recv failed\00"
@.str.2 = [30 x i8] c"net_recv_timed(): poll failed\00"
define i64 @net_recv_timed(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca [1 x %struct.pollfd]
%6 = bitcast [1 x %struct.pollfd]* %5 to i8*
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 0
store i32 %0, i32* %8
%9 = load i32, i32* @POLLIN
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 %3)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %4
%14 = load i32, i32* @MSG_WAITALL
%15 = call i64 @recv(i32 %0, i8* %1, i64 %2, i32 %14)
switch i64 %15, label %26 [
i64 0, label %16
i64 -1, label %20
]
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fwprintf(i32 %17, i8* bitcast ([50 x i32]* @.str to i8*))
%19 = call i32 @net_fail(i32 %0)
br label %26
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %26
22: ; preds = %4
%23 = icmp eq i32 %11, 0
br i1 %23, label %26, label %24
24: ; preds = %22
%25 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %22, %13, %24, %20, %16
%27 = phi i64 [ -3, %16 ], [ -1, %20 ], [ -1, %24 ], [ %15, %13 ], [ -2, %22 ]
ret i64 %27
}