#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 /*<<< orphan*/ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ NETLINK_KOBJECT_UEVENT;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, void *, int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  struct sockaddr_nl nls;
  struct pollfd pfd;
  char buf[512];
  memset(&nls, 0, sizeof(struct sockaddr_nl));
  nls.nl_family = AF_NETLINK;
  nls.nl_pid = getpid();
  nls.nl_groups = -1;
  pfd.events = POLLIN;
  pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  if (pfd.fd == -1)
    die("Not root\n");
  if (bind(pfd.fd, (void *)&nls, sizeof(struct sockaddr_nl)))
    die("Bind failed\n");
  while (-1 != poll(&pfd, 1, -1)) {
    int i, len = recv(pfd.fd, buf, sizeof(buf), MSG_DONTWAIT);
    if (len == -1)
      i = 0;
    while (i < len) {
      printf("%s\n", buf + i);
      i += strlen(buf + i) + 1;
    }
  }
  die("poll\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    121b <main+0x8b>
lea    0xdea(%rip),%rdi
call   10a0 <die@plt>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1242 <main+0xb2>
lea    0xdcd(%rip),%rdi
call   10a0 <die@plt>
jmp    1247 <main+0xb7>
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     130e <main+0x17e>
mov    -0x28(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    0x2d3e(%rip),%rax
mov    (%rax),%ecx
mov    $0x200,%edx
call   1030 <recv@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0xffffffff,-0x238(%rbp)
jne    12a3 <main+0x113>
movl   $0x0,-0x234(%rbp)
jmp    12a8 <main+0x118>
mov    -0x234(%rbp),%eax
cmp    -0x238(%rbp),%eax
jge    1309 <main+0x179>
lea    -0x230(%rbp),%rsi
movslq -0x234(%rbp),%rax
add    %rax,%rsi
lea    0xd45(%rip),%rdi
call   1060 <printf@plt>
lea    -0x230(%rbp),%rdi
movslq -0x234(%rbp),%rax
add    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x234(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x234(%rbp)
jmp    12a8 <main+0x118>
jmp    1247 <main+0xb7>
lea    0xd06(%rip),%rdi
call   10a0 <die@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x14(%rsp)
movl   $0xffffffff,0x10(%rsp)
ff
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
mov    %eax,0x8(%rsp)
cmp    $0xffffffff,%eax
jne    1216 <main+0x86>
lea    0xdef(%rip),%rdi
call   10a0 <die@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     1238 <main+0xa8>
lea    0xdd7(%rip),%rdi
call   10a0 <die@plt>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
cmp    $0xffffffff,%eax
je     12db <main+0x14b>
xor    %r13d,%r13d
lea    0xdb8(%rip),%r12
lea    0x8(%rsp),%r15
jmp    1287 <main+0xf7>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
cmp    $0xffffffff,%eax
je     12db <main+0x14b>
mov    0x8(%rsp),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%ecx
lea    0x20(%rsp),%rsi
mov    $0x200,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
cmove  %r13d,%ebx
cmp    %eax,%ebx
jge    1270 <main+0xe0>
mov    %eax,%ebp
movslq %ebx,%r14
lea    (%rsp,%r14,1),%rbx
add    $0x20,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%r14,%rax,1),%ebx
add    $0x1,%ebx
cmp    %ebp,%ebx
jl     12b0 <main+0x120>
jmp    1270 <main+0xe0>
lea    0xd39(%rip),%rdi
call   10a0 <die@plt>
xor    %eax,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.pollfd = type { i32, i32 }
@AF_NETLINK = global i32 0
@POLLIN = global i32 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
@.str = [10 x i8] c"Not root\0A\00"
@.str.1 = [13 x i8] c"Bind failed\0A\00"
@MSG_DONTWAIT = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [6 x i8] c"poll\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_nl
%7 = alloca %struct.pollfd
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @memset(%struct.sockaddr_nl* %6, i32 0, i32 12)
%12 = load i32, i32* @AF_NETLINK
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 2
store i32 %12, i32* %13
%14 = call i32 (...) @getpid()
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 0
store i32 -1, i32* %16
%17 = load i32, i32* @POLLIN
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @PF_NETLINK
%20 = load i32, i32* @SOCK_DGRAM
%21 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%22 = call i32 @socket(i32 %19, i32 %20, i32 %21)
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %2
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = bitcast %struct.sockaddr_nl* %6 to i8*
%33 = call i64 @bind(i32 %31, i8* %32, i32 12)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
br label %38
38: ; preds = %71, %37
%39 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 -1)
%40 = icmp ne i32 -1, %39
br i1 %40, label %41, label %72
41: ; preds = %38
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%45 = load i32, i32* @MSG_DONTWAIT
%46 = call i32 @recv(i32 %43, i8* %44, i32 512, i32 %45)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %41
store i32 0, i32* %9
br label %50
50: ; preds = %49, %41
br label %51
51: ; preds = %55, %50
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %71
55: ; preds = %51
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %59)
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = call i64 @strlen(i8* %64)
%66 = add nsw i64 %65, 1
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = add nsw i64 %68, %66
%70 = trunc i64 %69 to i32
store i32 %70, i32* %9
br label %51
71: ; preds = %51
br label %38
72: ; preds = %38
%73 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.pollfd = type { i32, i32 }
@AF_NETLINK = global i32 0
@POLLIN = global i32 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
@.str = [10 x i8] c"Not root\0A\00"
@.str.1 = [13 x i8] c"Bind failed\0A\00"
@MSG_DONTWAIT = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [6 x i8] c"poll\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_nl
%4 = alloca %struct.pollfd
%5 = alloca [512 x i8]
%6 = bitcast %struct.sockaddr_nl* %3 to i8*
%7 = bitcast %struct.pollfd* %4 to i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 12)
%10 = load i32, i32* @AF_NETLINK
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 (...) @getpid()
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i32 -1, i32* %14
%15 = load i32, i32* @POLLIN
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @PF_NETLINK
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %20, i32* %21
%22 = icmp eq i32 %20, -1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = call i64 @bind(i32 %20, i8* %6, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 -1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %55, label %37
33: ; preds = %45, %37
%34 = phi i32 [ %43, %37 ], [ %53, %45 ]
%35 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 -1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %55, label %37
37: ; preds = %30, %33
%38 = phi i32 [ %34, %33 ], [ undef, %30 ]
%39 = load i32, i32* %21
%40 = load i32, i32* @MSG_DONTWAIT
%41 = call i32 @recv(i32 %39, i8* %8, i32 512, i32 %40)
%42 = icmp eq i32 %41, -1
%43 = select i1 %42, i32 0, i32 %38
%44 = icmp slt i32 %43, %41
br i1 %44, label %45, label %33
45: ; preds = %37, %45
%46 = phi i32 [ %53, %45 ], [ %43, %37 ]
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %47
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = call i64 @strlen(i8* %48)
%51 = trunc i64 %50 to i32
%52 = add nsw i32 %46, 1
%53 = add i32 %52, %51
%54 = icmp slt i32 %53, %41
br i1 %54, label %45, label %33
55: ; preds = %33, %30
%56 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}