#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int EPOLLET;
int EPOLLIN;
int /*<<< orphan*/ EPOLL_CTL_ADD;
scalar_t__ EWOULDBLOCK;
int epoll_create(int);
int /*<<< orphan*/ epoll_ctl(int, int /*<<< orphan*/, int,
                             struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
scalar_t__ errno;
int netlink_create();
int recv(int, char *, size_t, int /*<<< orphan*/);
int recv_msg(char *buffer, size_t size, int timeout) {
  static int first_init = 0;
  static int nlfd = -1;
  static int epollfd = -1;
  int retval;
  int len;
  int recv_size = 0;
  struct epoll_event event;
  if (!first_init) {
    nlfd = netlink_create();
    epollfd = epoll_create(1);
    if (epollfd < 0 || nlfd < 0)
      return -1;
    event.events = EPOLLIN | EPOLLET;
    event.data.fd = nlfd;
    epoll_ctl(epollfd, EPOLL_CTL_ADD, nlfd, &event);
    first_init = 1;
  }
retry_again:
  errno = 0;
  retval =
      epoll_wait(epollfd, &event, 1, timeout < 0 ? -1 : timeout * 1000 / 4);
  if (retval == 0)
    return 0;
  if (retval < 0) {
    if (errno == EINTR)
      goto retry_again;
    return -1;
  }
  while (size && (len = recv(event.data.fd, buffer, size, 0)) > 0) {
    buffer += len;
    size -= len;
    recv_size += len;
  }
  if (len < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
    return -1;
  return recv_size;
}


x86 assembly (clang, -O0):
<recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,0x2ee7(%rip)
jne    1200 <recv_msg+0xb0>
mov    $0x0,%al
call   1060 <netlink_create@plt>
mov    %eax,0x2ec4(%rip)
mov    $0x1,%edi
call   1070 <epoll_create@plt>
mov    %eax,0x2eb8(%rip)
cmpl   $0x0,0x2eb1(%rip)
jl     11ae <recv_msg+0x5e>
cmpl   $0x0,0x2ea0(%rip)
jge    11ba <recv_msg+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <recv_msg+0x218>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    0x2dee(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e73(%rip),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e6e(%rip),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%edx
lea    -0x30(%rbp),%rcx
call   1040 <epoll_ctl@plt>
movl   $0x1,0x2e58(%rip)
jmp    1205 <recv_msg+0xb5>
mov    0x2dc4(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e33(%rip),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1233 <recv_msg+0xe3>
mov    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
jmp    1245 <recv_msg+0xf5>
imul   $0x3e8,-0x1c(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%ecx
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1050 <epoll_wait@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1272 <recv_msg+0x122>
movl   $0x0,-0x4(%rbp)
jmp    1368 <recv_msg+0x218>
cmpl   $0x0,-0x20(%rbp)
jge    12a7 <recv_msg+0x157>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <recv_msg+0x14b>
jmp    1205 <recv_msg+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <recv_msg+0x218>
jmp    12ac <recv_msg+0x15c>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x39(%rbp)
je     12da <recv_msg+0x18a>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    12ea <recv_msg+0x19a>
jmp    1318 <recv_msg+0x1c8>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12ac <recv_msg+0x15c>
cmpl   $0x0,-0x24(%rbp)
jge    1362 <recv_msg+0x212>
mov    0x2ca7(%rip),%rax
mov    (%rax),%rax
mov    0x2c95(%rip),%rcx
cmp    (%rcx),%rax
je     1362 <recv_msg+0x212>
mov    0x2c8d(%rip),%rax
mov    (%rax),%rax
mov    0x2c5b(%rip),%rcx
cmp    (%rcx),%rax
je     1362 <recv_msg+0x212>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <recv_msg+0x218>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
cmpb   $0x0,0x2eee(%rip)
jne    11d7 <recv_msg+0x87>
xor    %eax,%eax
call   1060 <netlink_create@plt>
mov    %eax,0x2ecf(%rip)
mov    $0x1,%edi
call   1070 <epoll_create@plt>
mov    %eax,%edi
mov    %eax,0x2ec1(%rip)
mov    $0xffffffff,%eax
test   %edi,%edi
js     1267 <recv_msg+0x117>
mov    0x2eaa(%rip),%edx
test   %edx,%edx
js     1267 <recv_msg+0x117>
mov    0x2e3b(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    %edx,0x4(%rsp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
call   1040 <epoll_ctl@plt>
movb   $0x1,0x2e81(%rip)
imul   $0xfa,%ebp,%eax
test   %ebp,%ebp
mov    $0xffffffff,%ebp
cmovns %eax,%ebp
mov    0x2de2(%rip),%r12
mov    %rsp,%r15
mov    0x2dc8(%rip),%r13
nopl   0x0(%rax,%rax,1)
movq   $0x0,(%r12)
mov    0x2e3e(%rip),%edi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %ebp,%ecx
call   1050 <epoll_wait@plt>
test   %eax,%eax
je     1234 <recv_msg+0xe4>
jns    1238 <recv_msg+0xe8>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1200 <recv_msg+0xb0>
mov    $0xffffffff,%eax
jmp    1267 <recv_msg+0x117>
xor    %eax,%eax
jmp    1267 <recv_msg+0x117>
xor    %ebp,%ebp
test   %rbx,%rbx
je     1265 <recv_msg+0x115>
nop
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1263 <recv_msg+0x113>
mov    %eax,%ecx
add    %rcx,%r14
add    %eax,%ebp
sub    %rcx,%rbx
jne    1240 <recv_msg+0xf0>
jmp    1265 <recv_msg+0x115>
js     1276 <recv_msg+0x126>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%rcx
mov    0x2d47(%rip),%rax
cmp    (%rax),%rcx
je     1265 <recv_msg+0x115>
mov    0x2d1b(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    1267 <recv_msg+0x117>
jmp    1265 <recv_msg+0x115>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@recv_msg.first_init = internal global i32 0
@recv_msg.nlfd = internal global i32 -1
@recv_msg.epollfd = internal global i32 -1
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
define i32 @recv_msg(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.epoll_event
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 0, i32* %10
%12 = load i32, i32* @recv_msg.first_init
%13 = icmp ne i32 %12, 0
br i1 %13, label %35, label %14
14: ; preds = %3
%15 = call i32 (...) @netlink_create()
store i32 %15, i32* @recv_msg.nlfd
%16 = call i32 @epoll_create(i32 1)
store i32 %16, i32* @recv_msg.epollfd
%17 = load i32, i32* @recv_msg.epollfd
%18 = icmp slt i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i32, i32* @recv_msg.nlfd
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19, %14
store i32 -1, i32* %4
br label %100
23: ; preds = %19
%24 = load i32, i32* @EPOLLIN
%25 = load i32, i32* @EPOLLET
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @recv_msg.nlfd
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* @recv_msg.epollfd
%32 = load i32, i32* @EPOLL_CTL_ADD
%33 = load i32, i32* @recv_msg.nlfd
%34 = call i32 @epoll_ctl(i32 %31, i32 %32, i32 %33, %struct.epoll_event* %11)
store i32 1, i32* @recv_msg.first_init
br label %35
35: ; preds = %23, %3
br label %36
36: ; preds = %58, %35
store i64 0, i64* @errno
%37 = load i32, i32* @recv_msg.epollfd
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %45
41: ; preds = %36
%42 = load i32, i32* %7
%43 = mul nsw i32 %42, 1000
%44 = sdiv i32 %43, 4
br label %45
45: ; preds = %41, %40
%46 = phi i32 [ -1, %40 ], [ %44, %41 ]
%47 = call i32 @epoll_wait(i32 %37, %struct.epoll_event* %11, i32 1, i32 %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
store i32 0, i32* %4
br label %100
51: ; preds = %45
%52 = load i32, i32* %8
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i64, i64* @errno
%56 = load i64, i64* @EINTR
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %54
br label %36
59: ; preds = %54
store i32 -1, i32* %4
br label %100
60: ; preds = %51
br label %61
61: ; preds = %74, %60
%62 = load i64, i64* %6
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %72
64: ; preds = %61
%65 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 1
%66 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i8*, i8** %5
%69 = load i64, i64* %6
%70 = call i32 @recv(i32 %67, i8* %68, i64 %69, i32 0)
store i32 %70, i32* %9
%71 = icmp sgt i32 %70, 0
br label %72
72: ; preds = %64, %61
%73 = phi i1 [ false, %61 ], [ %71, %64 ]
br i1 %73, label %74, label %86
74: ; preds = %72
%75 = load i32, i32* %9
%76 = load i8*, i8** %5
%77 = sext i32 %75 to i64
%78 = getelementptr inbounds i8, i8* %76, i64 %77
store i8* %78, i8** %5
%79 = load i32, i32* %9
%80 = sext i32 %79 to i64
%81 = load i64, i64* %6
%82 = sub i64 %81, %80
store i64 %82, i64* %6
%83 = load i32, i32* %9
%84 = load i32, i32* %10
%85 = add nsw i32 %84, %83
store i32 %85, i32* %10
br label %61
86: ; preds = %72
%87 = load i32, i32* %9
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %98
89: ; preds = %86
%90 = load i64, i64* @errno
%91 = load i64, i64* @EWOULDBLOCK
%92 = icmp ne i64 %90, %91
br i1 %92, label %93, label %98
93: ; preds = %89
%94 = load i64, i64* @errno
%95 = load i64, i64* @EAGAIN
%96 = icmp ne i64 %94, %95
br i1 %96, label %97, label %98
97: ; preds = %93
store i32 -1, i32* %4
br label %100
98: ; preds = %93, %89, %86
%99 = load i32, i32* %10
store i32 %99, i32* %4
br label %100
100: ; preds = %98, %97, %59, %50, %22
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@recv_msg.first_init = internal unnamed_addr global i1 false
@recv_msg.nlfd = internal unnamed_addr global i32 -1
@recv_msg.epollfd = internal unnamed_addr global i32 -1
@EPOLLIN = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
define i32 @recv_msg(i8* %0, i64 %1, i32 %2) {
%4 = alloca %struct.epoll_event
%5 = bitcast %struct.epoll_event* %4 to i8*
%6 = load i1, i1* @recv_msg.first_init
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = tail call i32 (...) @netlink_create()
store i32 %8, i32* @recv_msg.nlfd
%9 = tail call i32 @epoll_create(i32 1)
store i32 %9, i32* @recv_msg.epollfd
%10 = icmp slt i32 %9, 0
%11 = load i32, i32* @recv_msg.nlfd
%12 = icmp slt i32 %11, 0
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %64, label %14
14: ; preds = %7
%15 = load i32, i32* @EPOLLIN
%16 = load i32, i32* @EPOLLET
%17 = or i32 %16, %15
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 1, i32 0
store i32 %11, i32* %19
%20 = load i32, i32* @EPOLL_CTL_ADD
%21 = call i32 @epoll_ctl(i32 %9, i32 %20, i32 %11, %struct.epoll_event* %4)
store i1 true, i1* @recv_msg.first_init
br label %22
22: ; preds = %14, %3
%23 = mul nsw i32 %2, 250
%24 = icmp sgt i32 %2, -1
%25 = select i1 %24, i32 %23, i32 -1
br label %26
26: ; preds = %36, %22
store i64 0, i64* @errno
%27 = load i32, i32* @recv_msg.epollfd
%28 = call i32 @epoll_wait(i32 %27, %struct.epoll_event* %4, i32 1, i32 %25)
%29 = icmp eq i32 %28, 0
br i1 %29, label %64, label %30
30: ; preds = %26
%31 = icmp slt i32 %28, 0
br i1 %31, label %36, label %32
32: ; preds = %30
%33 = icmp eq i64 %1, 0
br i1 %33, label %62, label %34
34: ; preds = %32
%35 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 1, i32 0
br label %40
36: ; preds = %30
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
br i1 %39, label %26, label %64
40: ; preds = %34, %47
%41 = phi i32 [ 0, %34 ], [ %51, %47 ]
%42 = phi i8* [ %0, %34 ], [ %49, %47 ]
%43 = phi i64 [ %1, %34 ], [ %50, %47 ]
%44 = load i32, i32* %35
%45 = call i32 @recv(i32 %44, i8* %42, i64 %43, i32 0)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %40
%48 = zext i32 %45 to i64
%49 = getelementptr inbounds i8, i8* %42, i64 %48
%50 = sub i64 %43, %48
%51 = add nsw i32 %45, %41
%52 = icmp eq i64 %50, 0
br i1 %52, label %62, label %40
53: ; preds = %40
%54 = icmp slt i32 %45, 0
br i1 %54, label %55, label %62
55: ; preds = %53
%56 = load i64, i64* @errno
%57 = load i64, i64* @EWOULDBLOCK
%58 = icmp eq i64 %56, %57
%59 = load i64, i64* @EAGAIN
%60 = icmp eq i64 %56, %59
%61 = select i1 %58, i1 true, i1 %60
br i1 %61, label %62, label %64
62: ; preds = %47, %32, %55, %53
%63 = phi i32 [ %41, %55 ], [ %41, %53 ], [ 0, %32 ], [ %51, %47 ]
br label %64
64: ; preds = %36, %26, %55, %7, %62
%65 = phi i32 [ %63, %62 ], [ -1, %7 ], [ -1, %55 ], [ -1, %36 ], [ 0, %26 ]
ret i32 %65
}