#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 nl_groups;
  int msg_namelen;
  int msg_iovlen;
  int nlmsg_len;
  struct iovec *msg_iov;
  void *msg_name;
  int /*<<< orphan*/ nl_family;
};
struct nlmsghdr {
  int nl_groups;
  int msg_namelen;
  int msg_iovlen;
  int nlmsg_len;
  struct iovec *msg_iov;
  void *msg_name;
  int /*<<< orphan*/ nl_family;
};
struct msghdr {
  int nl_groups;
  int msg_namelen;
  int msg_iovlen;
  int nlmsg_len;
  struct iovec *msg_iov;
  void *msg_name;
  int /*<<< orphan*/ nl_family;
};
struct iovec {
  int iov_len;
  void *iov_base;
};
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ d_nladdr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NLMSG_DATA(struct sockaddr_nl *);
int NLMSG_HDRLEN;
int NLMSG_SPACE(int);
int /*<<< orphan*/ free(struct sockaddr_nl *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int recvmsg(int, struct sockaddr_nl *, int /*<<< orphan*/);
int netlink_recv(int fd, char *packetpayload) {
  struct sockaddr_nl d_nladdr;
  struct nlmsghdr *nlh = NULL;
  struct iovec iov;
  struct msghdr msg;
  int packetpayloadlen = 0;
  int ret;
  memset(&d_nladdr, 0, sizeof(d_nladdr));
  d_nladdr.nl_family = AF_NETLINK;
  d_nladdr.nl_groups = 1;
  nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(2000) + NLMSG_HDRLEN);
  if (nlh == NULL) {
    printf("netlink_recv(): netlink header memory not allocated\n");
    return -1;
  }
  memset(nlh, 0, NLMSG_SPACE(2000) + NLMSG_HDRLEN);
  iov.iov_base = (void *)nlh;
  iov.iov_len = NLMSG_SPACE(2000);
  memset(&msg, 0, sizeof(msg));
  msg.msg_name = (void *)&d_nladdr;
  msg.msg_namelen = sizeof(d_nladdr);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  ret = recvmsg(fd, &msg, 0);
  if (ret < 0) {
    printf("netlink_recv(): error in recvmsg\n");
    return -1;
  }
  packetpayloadlen = nlh->nlmsg_len - NLMSG_HDRLEN;
  if (packetpayloadlen < 0) {
    printf("netlink_recv(): error in recvmsg\n");
    return -1;
  }
  memcpy(packetpayload, NLMSG_DATA(nlh), packetpayloadlen);
  free(nlh);
  return packetpayloadlen;
}


x86 assembly (clang, -O0):
<netlink_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x7c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x38(%rbp)
mov    $0x7d0,%edi
call   1060 <NLMSG_SPACE@plt>
mov    %eax,%edi
mov    0x2e19(%rip),%rax
add    (%rax),%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1205 <netlink_recv+0x85>
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <netlink_recv+0x192>
mov    -0x40(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    $0x7d0,%edi
call   1060 <NLMSG_SPACE@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%edx
mov    0x2dc6(%rip),%rax
add    (%rax),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x7d0,%edi
call   1060 <NLMSG_SPACE@plt>
mov    %eax,-0x50(%rbp)
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x28,-0x74(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0x1,-0x70(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x78(%rbp),%rsi
xor    %edx,%edx
call   10a0 <recvmsg@plt>
mov    %eax,-0x80(%rbp)
cmpl   $0x0,-0x80(%rbp)
jge    12a9 <netlink_recv+0x129>
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <netlink_recv+0x192>
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%eax
mov    0x2d39(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jge    12de <netlink_recv+0x15e>
lea    0xd68(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <netlink_recv+0x192>
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <NLMSG_DATA@plt>
mov    -0x90(%rbp),%rdi
mov    %eax,%esi
mov    -0x7c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
lea    0x40(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x60(%rsp)
movl   $0x1,0x40(%rsp)
mov    $0x7d0,%edi
call   1060 <NLMSG_SPACE@plt>
mov    0x2e22(%rip),%r13
add    0x0(%r13),%eax
mov    %eax,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     127c <netlink_recv+0xfc>
mov    %rax,%rbx
mov    $0x7d0,%edi
call   1060 <NLMSG_SPACE@plt>
add    0x0(%r13),%eax
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1050 <memset@plt>
mov    %rbx,0x10(%rsp)
mov    $0x7d0,%edi
call   1060 <NLMSG_SPACE@plt>
mov    %eax,0x8(%rsp)
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    %r15,0x30(%rsp)
movabs $0x100000028,%rax
mov    %rax,0x1c(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x28(%rsp)
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   10a0 <recvmsg@plt>
test   %eax,%eax
js     1285 <netlink_recv+0x105>
mov    0xc(%rbx),%ebp
sub    0x0(%r13),%ebp
js     1285 <netlink_recv+0x105>
mov    %rbx,%rdi
call   1080 <NLMSG_DATA@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1296 <netlink_recv+0x116>
lea    0xd9e(%rip),%rdi
jmp    128c <netlink_recv+0x10c>
lea    0xd74(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32, i32, %struct.iovec*, i8*, i32 }
%struct.iovec = type { i32, i8* }
%struct.nlmsghdr = type { i32, i32, i32, i32, %struct.iovec*, i8*, i32 }
%struct.msghdr = type { i32, i32, i32, i32, %struct.iovec*, i8*, i32 }
@AF_NETLINK = global i32 0
@NLMSG_HDRLEN = global i32 0
@.str = [53 x i8] c"netlink_recv(): netlink header memory not allocated\0A\00"
@.str.1 = [34 x i8] c"netlink_recv(): error in recvmsg\0A\00"
define i32 @netlink_recv(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.sockaddr_nl
%7 = alloca %struct.nlmsghdr*
%8 = alloca %struct.iovec
%9 = alloca %struct.msghdr
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store %struct.nlmsghdr* null, %struct.nlmsghdr** %7
store i32 0, i32* %10
%12 = call i32 @memset(%struct.sockaddr_nl* %6, i32 0, i32 40)
%13 = load i32, i32* @AF_NETLINK
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 6
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 0
store i32 1, i32* %15
%16 = call i32 @NLMSG_SPACE(i32 2000)
%17 = load i32, i32* @NLMSG_HDRLEN
%18 = add nsw i32 %16, %17
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to %struct.nlmsghdr*
store %struct.nlmsghdr* %20, %struct.nlmsghdr** %7
%21 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%22 = icmp eq %struct.nlmsghdr* %21, null
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
25: ; preds = %2
%26 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%27 = bitcast %struct.nlmsghdr* %26 to %struct.sockaddr_nl*
%28 = call i32 @NLMSG_SPACE(i32 2000)
%29 = load i32, i32* @NLMSG_HDRLEN
%30 = add nsw i32 %28, %29
%31 = call i32 @memset(%struct.sockaddr_nl* %27, i32 0, i32 %30)
%32 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%33 = bitcast %struct.nlmsghdr* %32 to i8*
%34 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 1
store i8* %33, i8** %34
%35 = call i32 @NLMSG_SPACE(i32 2000)
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.msghdr* %9 to %struct.sockaddr_nl*
%38 = call i32 @memset(%struct.sockaddr_nl* %37, i32 0, i32 40)
%39 = bitcast %struct.sockaddr_nl* %6 to i8*
%40 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 5
store i8* %39, i8** %40
%41 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i32 40, i32* %41
%42 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 4
store %struct.iovec* %8, %struct.iovec** %42
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i32 1, i32* %43
%44 = load i32, i32* %4
%45 = bitcast %struct.msghdr* %9 to %struct.sockaddr_nl*
%46 = call i32 @recvmsg(i32 %44, %struct.sockaddr_nl* %45, i32 0)
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %25
%50 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
51: ; preds = %25
%52 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%53 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %52, i32 0, i32 3
%54 = load i32, i32* %53
%55 = load i32, i32* @NLMSG_HDRLEN
%56 = sub nsw i32 %54, %55
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %51
%60 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
61: ; preds = %51
%62 = load i8*, i8** %5
%63 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%64 = bitcast %struct.nlmsghdr* %63 to %struct.sockaddr_nl*
%65 = call i32 @NLMSG_DATA(%struct.sockaddr_nl* %64)
%66 = load i32, i32* %10
%67 = call i32 @memcpy(i8* %62, i32 %65, i32 %66)
%68 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%69 = bitcast %struct.nlmsghdr* %68 to %struct.sockaddr_nl*
%70 = call i32 @free(%struct.sockaddr_nl* %69)
%71 = load i32, i32* %10
store i32 %71, i32* %3
br label %72
72: ; preds = %61, %59, %49, %23
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32, i32, %struct.iovec*, i8*, i32 }
%struct.iovec = type { i32, i8* }
%struct.msghdr = type { i32, i32, i32, i32, %struct.iovec*, i8*, i32 }
%struct.nlmsghdr = type { i32, i32, i32, i32, %struct.iovec*, i8*, i32 }
@AF_NETLINK = global i32 0
@NLMSG_HDRLEN = global i32 0
@str.2 = [33 x i8] c"netlink_recv(): error in recvmsg\00"
@str.3 = [52 x i8] c"netlink_recv(): netlink header memory not allocated\00"
define i32 @netlink_recv(i32 %0, i8* %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 = bitcast %struct.msghdr* %5 to i8*
%9 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 40)
%10 = load i32, i32* @AF_NETLINK
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 6
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i32 1, i32* %12
%13 = call i32 @NLMSG_SPACE(i32 2000)
%14 = load i32, i32* @NLMSG_HDRLEN
%15 = add nsw i32 %14, %13
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to %struct.nlmsghdr*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.3, i64 0, i64 0))
br label %54
21: ; preds = %2
%22 = inttoptr i64 %16 to %struct.sockaddr_nl*
%23 = call i32 @NLMSG_SPACE(i32 2000)
%24 = load i32, i32* @NLMSG_HDRLEN
%25 = add nsw i32 %24, %23
%26 = call i32 @memset(%struct.sockaddr_nl* %22, i32 0, i32 %25)
%27 = inttoptr i64 %16 to i8*
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i8* %27, i8** %28
%29 = call i32 @NLMSG_SPACE(i32 2000)
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.msghdr* %5 to %struct.sockaddr_nl*
%32 = call i32 @memset(%struct.sockaddr_nl* %31, i32 0, i32 40)
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 5
%34 = bitcast i8** %33 to %struct.sockaddr_nl**
store %struct.sockaddr_nl* %3, %struct.sockaddr_nl** %34
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 40, i32* %35
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 4
store %struct.iovec* %4, %struct.iovec** %36
%37 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 1, i32* %37
%38 = call i32 @recvmsg(i32 %0, %struct.sockaddr_nl* %31, i32 0)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %21
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.2, i64 0, i64 0))
br label %54
42: ; preds = %21
%43 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %17, i64 0, i32 3
%44 = load i32, i32* %43
%45 = load i32, i32* @NLMSG_HDRLEN
%46 = sub nsw i32 %44, %45
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.2, i64 0, i64 0))
br label %54
50: ; preds = %42
%51 = call i32 @NLMSG_DATA(%struct.sockaddr_nl* %22)
%52 = call i32 @memcpy(i8* %1, i32 %51, i32 %46)
%53 = call i32 @free(%struct.sockaddr_nl* %22)
br label %54
54: ; preds = %50, %48, %40, %19
%55 = phi i32 [ -1, %19 ], [ -1, %40 ], [ -1, %48 ], [ %46, %50 ]
ret i32 %55
}