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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ NLMSG_DATA(struct sockaddr_nl *);
int NLMSG_SPACE(int);
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recvmsg(int, struct sockaddr_nl *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int main(int argc, char **argv) {
  int sk;
  struct sockaddr_nl src_addr, dest_addr;
  struct nlmsghdr *nlh = NULL;
  struct iovec iov;
  struct msghdr msg;
  sk = socket(AF_NETLINK, SOCK_RAW, 23);
  if (sk < 0) {
    perror("socket failed");
    return -1;
  }
  memset(&msg, 0, sizeof(msg));
  memset(&src_addr, 0, sizeof(src_addr));
  memset(&dest_addr, 0, sizeof(dest_addr));
  src_addr.nl_family = AF_NETLINK;
  src_addr.nl_pid = getpid();
  src_addr.nl_groups = 1;
  if (bind(sk, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) {
    perror("bind failed");
    close(sk);
    return -2;
  }
  nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(1024));
  memset(nlh, 0, NLMSG_SPACE(1024));
  iov.iov_base = (void *)nlh;
  iov.iov_len = NLMSG_SPACE(1024);
  msg.msg_name = (void *)&dest_addr;
  msg.msg_namelen = sizeof(dest_addr);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  while (1) {
    printf("waiting ...\n");
    recvmsg(sk, &msg, 0);
    printf("--> [%s]\n", (char *)NLMSG_DATA(nlh));
  }
  close(sk);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x70(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    $0x17,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1212 <main+0x62>
lea    0xdff(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1354 <main+0x1a4>
lea    -0xa8(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x28,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    129d <main+0xed>
lea    0xd8a(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1354 <main+0x1a4>
mov    $0x400,%edi
call   1070 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    $0x400,%edi
call   1070 <NLMSG_SPACE@plt>
mov    -0xb0(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    $0x400,%edi
call   1070 <NLMSG_SPACE@plt>
mov    %eax,-0x80(%rbp)
lea    -0x68(%rbp),%rax
mov    %rax,-0x90(%rbp)
movl   $0x28,-0xa4(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x98(%rbp)
movl   $0x1,-0xa0(%rbp)
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0xa8(%rbp),%rsi
xor    %edx,%edx
call   10a0 <recvmsg@plt>
mov    -0x70(%rbp),%rdi
call   1080 <NLMSG_DATA@plt>
mov    %rax,%rsi
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1316 <main+0x166>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    0x2e0a(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    $0x17,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     130c <main+0x14c>
mov    %eax,%r15d
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
lea    0x68(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x64(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x60(%rsp)
movl   $0x1,0x40(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x28,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     131f <main+0x15f>
mov    $0x400,%edi
call   1080 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    $0x400,%edi
call   1080 <NLMSG_SPACE@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1060 <memset@plt>
mov    %rbx,0x10(%rsp)
mov    $0x400,%edi
call   1080 <NLMSG_SPACE@plt>
mov    %eax,0x8(%rsp)
mov    %r14,0x30(%rsp)
movabs $0x100000028,%rax
mov    %rax,0x1c(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x28(%rsp)
lea    0xd5f(%rip),%r14
lea    0x18(%rsp),%r12
lea    0xd49(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   10b0 <recvmsg@plt>
mov    %rbx,%rdi
call   1090 <NLMSG_DATA@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12e0 <main+0x120>
lea    0xced(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1338 <main+0x178>
lea    0xce8(%rip),%rdi
call   10d0 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    $0xfffffffe,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32, %struct.iovec*, i8*, i32, i32 }
%struct.iovec = type { i32, i8* }
%struct.nlmsghdr = type { i32, i32, i32, %struct.iovec*, i8*, i32, i32 }
%struct.msghdr = type { i32, i32, i32, %struct.iovec*, i8*, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@.str = [14 x i8] c"socket failed\00"
@.str.1 = [12 x i8] c"bind failed\00"
@.str.2 = [13 x i8] c"waiting ...\0A\00"
@.str.3 = [10 x i8] c"--> [%s]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_nl
%8 = alloca %struct.sockaddr_nl
%9 = alloca %struct.nlmsghdr*
%10 = alloca %struct.iovec
%11 = alloca %struct.msghdr
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.nlmsghdr* null, %struct.nlmsghdr** %9
%12 = load i32, i32* @AF_NETLINK
%13 = load i32, i32* @SOCK_RAW
%14 = call i32 @socket(i32 %12, i32 %13, i32 23)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
19: ; preds = %2
%20 = bitcast %struct.msghdr* %11 to %struct.sockaddr_nl*
%21 = call i32 @memset(%struct.sockaddr_nl* %20, i32 0, i32 40)
%22 = call i32 @memset(%struct.sockaddr_nl* %7, i32 0, i32 40)
%23 = call i32 @memset(%struct.sockaddr_nl* %8, i32 0, i32 40)
%24 = load i32, i32* @AF_NETLINK
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 6
store i32 %24, i32* %25
%26 = call i32 (...) @getpid()
%27 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 5
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 0
store i32 1, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_nl* %7 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 40)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 -2, i32* %3
br label %65
37: ; preds = %19
%38 = call i32 @NLMSG_SPACE(i32 1024)
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to %struct.nlmsghdr*
store %struct.nlmsghdr* %40, %struct.nlmsghdr** %9
%41 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%42 = bitcast %struct.nlmsghdr* %41 to %struct.sockaddr_nl*
%43 = call i32 @NLMSG_SPACE(i32 1024)
%44 = call i32 @memset(%struct.sockaddr_nl* %42, i32 0, i32 %43)
%45 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%46 = bitcast %struct.nlmsghdr* %45 to i8*
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i8* %46, i8** %47
%48 = call i32 @NLMSG_SPACE(i32 1024)
%49 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i32 %48, i32* %49
%50 = bitcast %struct.sockaddr_nl* %8 to i8*
%51 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 4
store i8* %50, i8** %51
%52 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 1
store i32 40, i32* %52
%53 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 3
store %struct.iovec* %10, %struct.iovec** %53
%54 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 2
store i32 1, i32* %54
br label %55
55: ; preds = %37, %55
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = bitcast %struct.msghdr* %11 to %struct.sockaddr_nl*
%59 = call i32 @recvmsg(i32 %57, %struct.sockaddr_nl* %58, i32 0)
%60 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%61 = bitcast %struct.nlmsghdr* %60 to %struct.sockaddr_nl*
%62 = call i64 @NLMSG_DATA(%struct.sockaddr_nl* %61)
%63 = inttoptr i64 %62 to i8*
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %63)
br label %55
65: ; preds = %33, %17
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32, %struct.iovec*, i8*, i32, i32 }
%struct.iovec = type { i32, i8* }
%struct.msghdr = type { i32, i32, i32, %struct.iovec*, i8*, i32, i32 }
%struct.nlmsghdr = type { i32, i32, i32, %struct.iovec*, i8*, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@.str = [14 x i8] c"socket failed\00"
@.str.1 = [12 x i8] c"bind failed\00"
@.str.3 = [10 x i8] c"--> [%s]\0A\00"
@str = [12 x i8] c"waiting ...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_nl
%4 = alloca %struct.sockaddr_nl
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = bitcast %struct.sockaddr_nl* %3 to i8*
%8 = bitcast %struct.sockaddr_nl* %4 to i8*
%9 = bitcast %struct.iovec* %5 to i8*
%10 = bitcast %struct.msghdr* %6 to i8*
%11 = load i32, i32* @AF_NETLINK
%12 = load i32, i32* @SOCK_RAW
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 23)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %54
17: ; preds = %2
%18 = bitcast %struct.msghdr* %6 to %struct.sockaddr_nl*
%19 = call i32 @memset(%struct.sockaddr_nl* %18, i32 0, i32 40)
%20 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 40)
%21 = call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 40)
%22 = load i32, i32* @AF_NETLINK
%23 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 6
store i32 %22, i32* %23
%24 = call i32 (...) @getpid()
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 5
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i32 1, i32* %26
%27 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%28 = call i64 @bind(i32 %13, %struct.sockaddr* %27, i32 40)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @close(i32 %13)
br label %54
33: ; preds = %17
%34 = call i32 @NLMSG_SPACE(i32 1024)
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to %struct.sockaddr_nl*
%37 = call i32 @NLMSG_SPACE(i32 1024)
%38 = call i32 @memset(%struct.sockaddr_nl* %36, i32 0, i32 %37)
%39 = inttoptr i64 %35 to i8*
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i8* %39, i8** %40
%41 = call i32 @NLMSG_SPACE(i32 1024)
%42 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 4
%44 = bitcast i8** %43 to %struct.sockaddr_nl**
store %struct.sockaddr_nl* %4, %struct.sockaddr_nl** %44
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i32 40, i32* %45
%46 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store %struct.iovec* %5, %struct.iovec** %46
%47 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i32 1, i32* %47
br label %48
48: ; preds = %48, %33
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%50 = call i32 @recvmsg(i32 %13, %struct.sockaddr_nl* %18, i32 0)
%51 = call i64 @NLMSG_DATA(%struct.sockaddr_nl* %36)
%52 = inttoptr i64 %51 to i8*
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %52)
br label %48
54: ; preds = %30, %15
%55 = phi i32 [ -1, %15 ], [ -2, %30 ]
ret i32 %55
}