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

/* Variables and functions */
int ECONNRESET;
int ETIMEDOUT;
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ assert(int);
int errno;
int poll(struct pollfd *, int, int);
size_t recv(int, void *, int, int /*<<< orphan*/);
int udp_read_timeout(int fd, void *buf, size_t *len, int timeout_ms) {
  int x;
  struct pollfd fds;
  assert(timeout_ms > 0);
  fds.fd = fd;
  fds.events = POLLIN;
  fds.revents = 0;
  x = poll(&fds, 1, timeout_ms);
  if (x == 0) {
    *len = 0;
    return -ETIMEDOUT;
  }
  *len = recv(fd, buf, 8192, MSG_DONTWAIT);
  if (*len == -1)
    return errno;
  if (*len == 0)
    return -ECONNRESET;
  return 0;
}


x86 assembly (clang, -O0):
<udp_read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%edx
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11ad <udp_read_timeout+0x7d>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
mov    0x2e1d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <udp_read_timeout+0xe8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    $0x2000,%edx
call   1030 <recv@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0xffffffffffffffff,(%rax)
jne    11f0 <udp_read_timeout+0xc0>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <udp_read_timeout+0xe8>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1211 <udp_read_timeout+0xe1>
xor    %eax,%eax
mov    0x2dd1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1218 <udp_read_timeout+0xe8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_read_timeout>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
xor    %edi,%edi
test   %ecx,%ecx
setg   %dil
call   1040 <assert@plt>
mov    %ebp,(%rsp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     11b3 <udp_read_timeout+0x83>
mov    0x2e44(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x2000,%edx
call   1030 <recv@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11c3 <udp_read_timeout+0x93>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0xffffffffffffffff,%rcx
jne    11ce <udp_read_timeout+0x9e>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
jmp    11ce <udp_read_timeout+0x9e>
movq   $0x0,(%r15)
mov    0x2dff(%rip),%rcx
jmp    11ca <udp_read_timeout+0x9a>
mov    0x2e0e(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32 }
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
@MSG_DONTWAIT = global i32 0
@errno = global i32 0
@ECONNRESET = global i32 0
define i32 @udp_read_timeout(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 i32
%11 = alloca %struct.pollfd
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* %6
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @POLLIN
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %9
%22 = call i32 @poll(%struct.pollfd* %11, i32 1, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %4
%26 = load i64*, i64** %8
store i64 0, i64* %26
%27 = load i32, i32* @ETIMEDOUT
%28 = sub nsw i32 0, %27
store i32 %28, i32* %5
br label %48
29: ; preds = %4
%30 = load i32, i32* %6
%31 = load i8*, i8** %7
%32 = load i32, i32* @MSG_DONTWAIT
%33 = call i64 @recv(i32 %30, i8* %31, i32 8192, i32 %32)
%34 = load i64*, i64** %8
store i64 %33, i64* %34
%35 = load i64*, i64** %8
%36 = load i64, i64* %35
%37 = icmp eq i64 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %29
%39 = load i32, i32* @errno
store i32 %39, i32* %5
br label %48
40: ; preds = %29
%41 = load i64*, i64** %8
%42 = load i64, i64* %41
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @ECONNRESET
%46 = sub nsw i32 0, %45
store i32 %46, i32* %5
br label %48
47: ; preds = %40
store i32 0, i32* %5
br label %48
48: ; preds = %47, %44, %38, %25
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i32 }
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
@MSG_DONTWAIT = global i32 0
@errno = global i32 0
@ECONNRESET = global i32 0
define i32 @udp_read_timeout(i32 %0, i8* %1, i64* nocapture writeonly %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = icmp sgt i32 %3, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %0, i32* %10
%11 = load i32, i32* @POLLIN
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i64 0, i64* %13
%14 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %4
store i64 0, i64* %2
%17 = load i32, i32* @ETIMEDOUT
%18 = sub nsw i32 0, %17
br label %27
19: ; preds = %4
%20 = load i32, i32* @MSG_DONTWAIT
%21 = call i64 @recv(i32 %0, i8* %1, i32 8192, i32 %20)
store i64 %21, i64* %2
switch i64 %21, label %27 [
i64 -1, label %22
i64 0, label %24
]
22: ; preds = %19
%23 = load i32, i32* @errno
br label %27
24: ; preds = %19
%25 = load i32, i32* @ECONNRESET
%26 = sub nsw i32 0, %25
br label %27
27: ; preds = %19, %24, %22, %16
%28 = phi i32 [ %18, %16 ], [ %23, %22 ], [ %26, %24 ], [ 0, %19 ]
ret i32 %28
}