#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 sockaddr_nl {
  int dummy;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  int msg_flags;
  scalar_t__ msg_controllen;
  int /*<<< orphan*/ *msg_control;
  struct iovec *msg_iov;
  void *msg_name;
};
struct iovec {
  void *member_0;
  size_t member_1;
};
typedef int /*<<< orphan*/ l_addr;

/* Variables and functions */
scalar_t__ EINTR;
int MSG_TRUNC;
scalar_t__ errno;
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int netlink_recv(int p_socket, void *p_buffer, size_t p_len) {
  struct msghdr l_msg;
  struct iovec l_iov = {p_buffer, p_len};
  struct sockaddr_nl l_addr;
  int l_result;
  for (;;) {
    l_msg.msg_name = (void *)&l_addr;
    l_msg.msg_namelen = sizeof(l_addr);
    l_msg.msg_iov = &l_iov;
    l_msg.msg_iovlen = 1;
    l_msg.msg_control = NULL;
    l_msg.msg_controllen = 0;
    l_msg.msg_flags = 0;
    int l_result = recvmsg(p_socket, &l_msg, 0);
    if (l_result < 0) {
      if (errno == EINTR) {
        continue;
      }
      return -2;
    }
    if (l_msg.msg_flags & MSG_TRUNC) {
      return -1;
    }
    return l_result;
  }
}


x86 assembly (clang, -O0):
<netlink_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x4,-0x48(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x44(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1030 <recvmsg@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jge    11ae <netlink_recv+0x9e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11a2 <netlink_recv+0x92>
jmp    1133 <netlink_recv+0x23>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d5 <netlink_recv+0xc5>
mov    -0x40(%rbp),%eax
mov    0x2e30(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11cf <netlink_recv+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <netlink_recv+0xc5>
mov    -0x68(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
mov    %rsi,0x8(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsp,%r12
lea    0x8(%rsp),%r13
lea    0x18(%rsp),%r14
mov    0x2e9a(%rip),%rbp
mov    0x2e8b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,0x40(%rsp)
movl   $0x4,0x18(%rsp)
mov    %r13,0x38(%rsp)
movq   $0x1,0x1c(%rsp)
lea    0x28(%rsp),%rax
xorps  %xmm0,%xmm0
movups %xmm0,(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <recvmsg@plt>
test   %eax,%eax
jns    1196 <netlink_recv+0x86>
mov    0x0(%rbp),%rax
cmp    (%r15),%rax
je     1150 <netlink_recv+0x40>
mov    $0xfffffffe,%ecx
jmp    11a9 <netlink_recv+0x99>
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%ecx
and    0x20(%rsp),%ecx
neg    %ecx
sbb    %ecx,%ecx
or     %eax,%ecx
mov    %ecx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i32, i32, i64, i32*, %struct.iovec*, i8* }
%struct.iovec = type { i8*, i64 }
%struct.sockaddr_nl = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@MSG_TRUNC = global i32 0
define i32 @netlink_recv(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.msghdr
%9 = alloca %struct.iovec
%10 = alloca %struct.sockaddr_nl
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
%14 = load i8*, i8** %6
store i8* %14, i8** %13
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
%16 = load i64, i64* %7
store i64 %16, i64* %15
br label %17
17: ; preds = %34, %3
%18 = bitcast %struct.sockaddr_nl* %10 to i8*
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 6
store i8* %18, i8** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 4, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 5
store %struct.iovec* %9, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
store i32* null, i32** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 0, i32* %25
%26 = load i32, i32* %5
%27 = call i32 @recvmsg(i32 %26, %struct.msghdr* %8, i32 0)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %17
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %17
35: ; preds = %30
store i32 -2, i32* %4
br label %45
36: ; preds = %17
%37 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
%38 = load i32, i32* %37
%39 = load i32, i32* @MSG_TRUNC
%40 = and i32 %38, %39
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %4
br label %45
43: ; preds = %36
%44 = load i32, i32* %12
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %42, %35
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i32, i32, i64, i32*, %struct.iovec*, i8* }
%struct.iovec = type { i8*, i64 }
%struct.sockaddr_nl = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@MSG_TRUNC = global i32 0
define i32 @netlink_recv(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = alloca %struct.sockaddr_nl
%7 = bitcast %struct.msghdr* %4 to i8*
%8 = bitcast %struct.iovec* %5 to i8*
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %1, i8** %9
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i64 %2, i64* %10
%11 = bitcast %struct.sockaddr_nl* %6 to i8*
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 6
%13 = bitcast i8** %12 to %struct.sockaddr_nl**
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 5
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
%19 = bitcast i64* %17 to i8*
br label %20
20: ; preds = %29, %3
store %struct.sockaddr_nl* %6, %struct.sockaddr_nl** %13
store i32 4, i32* %14
store %struct.iovec* %5, %struct.iovec** %15
store i32 1, i32* %16
store i32 0, i32* %18
%21 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = load i32, i32* %18
%25 = load i32, i32* @MSG_TRUNC
%26 = and i32 %25, %24
%27 = icmp eq i32 %26, 0
%28 = select i1 %27, i32 %21, i32 -1
br label %33
29: ; preds = %20
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %20, label %33
33: ; preds = %29, %23
%34 = phi i32 [ %28, %23 ], [ -2, %29 ]
ret i32 %34
}