#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)
      die("recv\n");
    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     1315 <main+0x185>
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    12a5 <main+0x115>
lea    0xd77(%rip),%rdi
call   10a0 <die@plt>
movl   $0x0,-0x234(%rbp)
mov    -0x234(%rbp),%eax
cmp    -0x238(%rbp),%eax
jge    1310 <main+0x180>
lea    -0x230(%rbp),%rsi
movslq -0x234(%rbp),%rax
add    %rax,%rsi
lea    0xd44(%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    12af <main+0x11f>
jmp    1247 <main+0xb7>
lea    0xd05(%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     12ea <main+0x15a>
lea    0x20(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0xdb7(%rip),%r13
jmp    1293 <main+0x103>
nopl   0x0(%rax,%rax,1)
lea    0xda0(%rip),%rdi
call   10a0 <die@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
cmp    $0xffffffff,%eax
je     12ea <main+0x15a>
mov    0x8(%rsp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%ecx
mov    %r15,%rsi
mov    $0x200,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0xe0>
mov    %eax,%ebp
test   %eax,%eax
jle    127c <main+0xec>
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
movslq %eax,%r14
lea    (%rsp,%r14,1),%rbx
add    $0x20,%rbx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    %r14d,%eax
add    $0x1,%eax
cmp    %ebp,%eax
jl     12c0 <main+0x130>
jmp    127c <main+0xec>
lea    0xd30(%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 = [6 x i8] c"recv\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [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 = %72, %37
%39 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 -1)
%40 = icmp ne i32 -1, %39
br i1 %40, label %41, label %73
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 %51
49: ; preds = %41
%50 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %41
store i32 0, i32* %9
br label %52
52: ; preds = %56, %51
%53 = load i32, i32* %9
%54 = load i32, i32* %10
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %72
56: ; preds = %52
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %60)
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = call i64 @strlen(i8* %65)
%67 = add nsw i64 %66, 1
%68 = load i32, i32* %9
%69 = sext i32 %68 to i64
%70 = add nsw i64 %69, %67
%71 = trunc i64 %70 to i32
store i32 %71, i32* %9
br label %52
72: ; preds = %52
br label %38
73: ; preds = %38
%74 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, 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 = [6 x i8] c"recv\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [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 %36
33: ; preds = %45, %41, %43
%34 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 -1)
%35 = icmp eq i32 %34, -1
br i1 %35, label %55, label %36
36: ; preds = %30, %33
%37 = load i32, i32* %21
%38 = load i32, i32* @MSG_DONTWAIT
%39 = call i32 @recv(i32 %37, i8* %8, i32 512, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %33
43: ; preds = %36
%44 = icmp sgt i32 %39, 0
br i1 %44, label %45, label %33
45: ; preds = %43, %45
%46 = phi i32 [ %53, %45 ], [ 0, %43 ]
%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.3, 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, %39
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.4, i64 0, i64 0))
ret i32 0
}