#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 nlmsghdr {
  int dummy;
};
struct msghdr {
  int msg_namelen;
  int msg_iovlen;
  struct iovec *msg_iov;
  void *msg_name;
};
struct iovec {
  int iov_len;
  void *iov_base;
};
typedef int /*<<< orphan*/ nladdr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
char *NLMSG_DATA(struct nlmsghdr *);
int /*<<< orphan*/ printf(char *, ...);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
void read_event_from_kernel(int sock) {
  struct sockaddr_nl nladdr;
  struct msghdr msg;
  struct iovec iov;
  char buffer[65536];
  int ret;
  iov.iov_base = (void *)buffer;
  iov.iov_len = sizeof(buffer);
  msg.msg_name = (void *)&(nladdr);
  msg.msg_namelen = sizeof(nladdr);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  printf("Ok, listening.\n");
  ret = recvmsg(sock, &msg, 0);
  if (ret < 0)
    printf("ret < 0.\n");
  else
    printf("Received message payload: %s\n",
           NLMSG_DATA((struct nlmsghdr *)&buffer));
}


x86 assembly (clang, -O0):
<read_event_from_kernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10030(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x10000,-0x30(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x4,-0x20(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
mov    %eax,-0x10034(%rbp)
cmpl   $0x0,-0x10034(%rbp)
jge    11b0 <read_event_from_kernel+0x80>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cd <read_event_from_kernel+0x9d>
lea    -0x10030(%rbp),%rdi
call   1040 <NLMSG_DATA@plt>
mov    %rax,%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_event_from_kernel>:
push   %rbx
sub    $0x10030,%rsp
mov    %edi,%ebx
lea    0x30(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x10000,0x8(%rsp)
mov    %rsp,%rax
mov    %rax,0x28(%rsp)
movabs $0x100000004,%rax
mov    %rax,0x18(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x20(%rsp)
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <recvmsg@plt>
test   %eax,%eax
js     11b8 <read_event_from_kernel+0x78>
lea    0x30(%rsp),%rdi
call   1050 <NLMSG_DATA@plt>
lea    0xe54(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c4 <read_event_from_kernel+0x84>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
add    $0x10030,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.msghdr = type { i32, i32, %struct.iovec*, i8* }
%struct.iovec = type { i32, i8* }
%struct.nlmsghdr = type { i32 }
@.str = [16 x i8] c"Ok, listening.\0A\00"
@.str.1 = [10 x i8] c"ret < 0.\0A\00"
@.str.2 = [30 x i8] c"Received message payload: %s\0A\00"
define void @read_event_from_kernel(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = alloca [65536 x i8]
%7 = alloca i32
store i32 %0, i32* %2
%8 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 1
store i8* %8, i8** %9
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 0
store i32 65536, i32* %10
%11 = bitcast %struct.sockaddr_nl* %3 to i8*
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 3
store i8* %11, i8** %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 0
store i32 4, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 2
store %struct.iovec* %5, %struct.iovec** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 1
store i32 1, i32* %15
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %2
%18 = call i32 @recvmsg(i32 %17, %struct.msghdr* %4, i32 0)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
23: ; preds = %1
%24 = bitcast [65536 x i8]* %6 to %struct.nlmsghdr*
%25 = call i8* @NLMSG_DATA(%struct.nlmsghdr* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %23, %21
ret void
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.msghdr = type { i32, i32, %struct.iovec*, i8* }
%struct.iovec = type { i32, i8* }
%struct.nlmsghdr = type { i32 }
@.str.2 = [30 x i8] c"Received message payload: %s\0A\00"
@str = [15 x i8] c"Ok, listening.\00"
@str.3 = [9 x i8] c"ret < 0.\00"
define void @read_event_from_kernel(i32 %0) {
%2 = alloca %struct.sockaddr_nl
%3 = alloca %struct.msghdr
%4 = alloca %struct.iovec
%5 = alloca [65536 x i8]
%6 = bitcast %struct.sockaddr_nl* %2 to i8*
%7 = bitcast %struct.msghdr* %3 to i8*
%8 = bitcast %struct.iovec* %4 to i8*
%9 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i8* %9, i8** %10
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i32 65536, i32* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
%13 = bitcast i8** %12 to %struct.sockaddr_nl**
store %struct.sockaddr_nl* %2, %struct.sockaddr_nl** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i32 4, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store %struct.iovec* %4, %struct.iovec** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 1, i32* %16
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.3, i64 0, i64 0))
br label %26
22: ; preds = %1
%23 = bitcast [65536 x i8]* %5 to %struct.nlmsghdr*
%24 = call i8* @NLMSG_DATA(%struct.nlmsghdr* %23)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %22, %20
ret void
}