#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 sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buffersize;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_KOBJECT_UEVENT;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_nl *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_hotplug_sock() {
  const int buffersize = 1024;
  int ret;
  struct sockaddr_nl snl;
  bzero(&snl, sizeof(struct sockaddr_nl));
  snl.nl_family = AF_NETLINK;
  snl.nl_pid = getpid();
  snl.nl_groups = 1;
  int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  if (s == -1) {
    perror("socket");
    return -1;
  }
  setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
  ret = bind(s, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
  if (ret < 0) {
    perror("bind");
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<init_hotplug_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x400,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ef <init_hotplug_sock+0x7f>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <init_hotplug_sock+0xe7>
mov    -0x1c(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1251 <init_hotplug_sock+0xe1>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <init_hotplug_sock+0xe7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_hotplug_sock>:
push   %rbx
sub    $0x20,%rsp
movl   $0x400,0xc(%rsp)
lea    0x10(%rsp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x14(%rsp)
movl   $0x1,0x10(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1214 <init_hotplug_sock+0xa4>
mov    %eax,%ebx
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
js     1222 <init_hotplug_sock+0xb2>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
jmp    1235 <init_hotplug_sock+0xc5>
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    120c <init_hotplug_sock+0x9c>

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define i32 @init_hotplug_sock() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_nl
%5 = alloca i32
store i32 1024, i32* %2
%6 = call i32 @bzero(%struct.sockaddr_nl* %4, i32 12)
%7 = load i32, i32* @AF_NETLINK
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 2
store i32 %7, i32* %8
%9 = call i32 (...) @getpid()
%10 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
store i32 1, i32* %11
%12 = load i32, i32* @PF_NETLINK
%13 = load i32, i32* @SOCK_DGRAM
%14 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %36
20: ; preds = %0
%21 = load i32, i32* %5
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_RCVBUF
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %2, i32 4)
%25 = load i32, i32* %5
%26 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %1
br label %36
34: ; preds = %20
%35 = load i32, i32* %5
store i32 %35, i32* %1
br label %36
36: ; preds = %34, %30, %18
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define i32 @init_hotplug_sock() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = bitcast i32* %1 to i8*
store i32 1024, i32* %1
%4 = bitcast %struct.sockaddr_nl* %2 to i8*
%5 = call i32 @bzero(%struct.sockaddr_nl* %2, i32 12)
%6 = load i32, i32* @AF_NETLINK
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 (...) @getpid()
%9 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 0
store i32 1, i32* %10
%11 = load i32, i32* @PF_NETLINK
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %28
18: ; preds = %0
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_RCVBUF
%21 = call i32 @setsockopt(i32 %14, i32 %19, i32 %20, i32* %1, i32 4)
%22 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %14, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @close(i32 %14)
br label %28
28: ; preds = %18, %25, %16
%29 = phi i32 [ -1, %16 ], [ -1, %25 ], [ %14, %18 ]
ret i32 %29
}