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

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int poll(struct pollfd *, int, int);
int read(int, void *, size_t);
ssize_t net__read(int fd, void *dst, size_t size, int timeout) {
  struct pollfd pollfd = {.fd = fd, .events = POLLIN, .revents = 0};
  if (poll(&pollfd, 1, timeout) == 1)
    return read(fd, dst, size);
  return -1;
}


x86 assembly (clang, -O0):
<net__read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
cmp    $0x1,%eax
jne    1181 <net__read+0x61>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    1188 <net__read+0x68>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net__read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %edi,(%rsp)
movl   $0x0,0x4(%rsp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %ecx,%edx
call   1040 <poll@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    1170 <net__read+0x50>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
define i32 @net__read(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.pollfd
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
%12 = load i32, i32* %6
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 2
%15 = load i32, i32* @POLLIN
store i32 %15, i32* %14
%16 = load i32, i32* %9
%17 = call i32 @poll(%struct.pollfd* %10, i32 1, i32 %16)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %4
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = call i32 @read(i32 %20, i8* %21, i64 %22)
store i32 %23, i32* %5
br label %25
24: ; preds = %4
store i32 -1, i32* %5
br label %25
25: ; preds = %24, %19
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
define i32 @net__read(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %0, i32* %7
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
%10 = load i32, i32* @POLLIN
store i32 %10, i32* %9
%11 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %3)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = call i32 @read(i32 %0, i8* %1, i64 %2)
br label %15
15: ; preds = %4, %13
%16 = phi i32 [ %14, %13 ], [ -1, %4 ]
ret i32 %16
}