#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 {
  scalar_t__ nl_groups;
  scalar_t__ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct nlmsghdr {
  scalar_t__ nlmsg_len;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  int msg_flags;
  struct iovec *msg_iov;
  struct sockaddr_nl *msg_name;
};
struct iovec {
  scalar_t__ iov_len;
  struct nlmsghdr *iov_base;
};
typedef int /*<<< orphan*/ nladdr;
typedef scalar_t__ __u32;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ EINTR;
scalar_t__ EMSGSIZE;
int MSG_TRUNC;
scalar_t__ errno;
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int netlink_recv(int fd, struct nlmsghdr *nlmsghdr) {
  int ret;
  struct sockaddr_nl nladdr;
  struct iovec iov = {
      .iov_base = nlmsghdr,
      .iov_len = nlmsghdr->nlmsg_len,
  };
  struct msghdr msg = {
      .msg_name = &nladdr,
      .msg_namelen = sizeof(nladdr),
      .msg_iov = &iov,
      .msg_iovlen = 1,
  };
  memset(&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  nladdr.nl_pid = 0;
  nladdr.nl_groups = 0;
again:
  ret = recvmsg(fd, &msg, 0);
  if (ret < 0) {
    if (errno == EINTR)
      goto again;
    return -1;
  }
  if (!ret)
    return 0;
  if (msg.msg_flags & MSG_TRUNC && ((__u32)ret == nlmsghdr->nlmsg_len)) {
    errno = EMSGSIZE;
    ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<netlink_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x18,-0x60(%rbp)
movl   $0x1,-0x5c(%rbp)
movl   $0x0,-0x58(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
xor    %edx,%edx
call   1040 <recvmsg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d9 <netlink_recv+0xb9>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
jne    11cd <netlink_recv+0xad>
jmp    1193 <netlink_recv+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <netlink_recv+0x116>
cmpl   $0x0,-0x14(%rbp)
jne    11ef <netlink_recv+0xcf>
movl   $0x0,-0x4(%rbp)
jmp    1236 <netlink_recv+0x116>
mov    -0x58(%rbp),%eax
mov    0x2de7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1230 <netlink_recv+0x110>
movslq -0x14(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jne    1230 <netlink_recv+0x110>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rcx
mov    0x2da2(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_recv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    (%rsi),%rax
mov    %rax,0x20(%rsp)
mov    %rsi,0x28(%rsp)
movabs $0x100000018,%rax
mov    %rax,(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x30(%rsp),%rdi
mov    %rdi,0x18(%rsp)
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
mov    %rsp,%r12
mov    0x2e35(%rip),%r15
mov    0x2e26(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1040 <recvmsg@plt>
test   %eax,%eax
jns    11bf <netlink_recv+0x9f>
mov    (%r15),%rax
cmp    (%rbx),%rax
je     11a0 <netlink_recv+0x80>
mov    $0xffffffff,%eax
jmp    11e8 <netlink_recv+0xc8>
je     11e6 <netlink_recv+0xc6>
mov    0x2e18(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,0x8(%rsp)
je     11e8 <netlink_recv+0xc8>
mov    %eax,%ecx
cmp    %rcx,(%r14)
jne    11e8 <netlink_recv+0xc8>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
jmp    11b8 <netlink_recv+0x98>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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