#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;
  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,-0x64(%rbp)
cmpl   $0x0,-0x64(%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    -0x64(%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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
%13 = load i8*, i8** %6
store i8* %13, i8** %12
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
%15 = load i64, i64* %7
store i64 %15, i64* %14
br label %16
16: ; preds = %33, %3
%17 = bitcast %struct.sockaddr_nl* %10 to i8*
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 6
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 4, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 5
store %struct.iovec* %9, %struct.iovec** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 1, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
store i32* null, i32** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 0, i32* %24
%25 = load i32, i32* %5
%26 = call i32 @recvmsg(i32 %25, %struct.msghdr* %8, i32 0)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %16
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %16
34: ; preds = %29
store i32 -2, i32* %4
br label %44
35: ; preds = %16
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
%37 = load i32, i32* %36
%38 = load i32, i32* @MSG_TRUNC
%39 = and i32 %37, %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
store i32 -1, i32* %4
br label %44
42: ; preds = %35
%43 = load i32, i32* %11
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %41, %34
%45 = load i32, i32* %4
ret i32 %45
}

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
}