#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_RCVBUFFORCE;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int init_hotplug_sock(void) {
  struct sockaddr_nl snl;
  const int buffersize = 16 * 1024 * 1024;
  int retval;
  memset(&snl, 0x00, sizeof(struct sockaddr_nl));
  snl.nl_family = AF_NETLINK;
  snl.nl_pid = getpid();
  snl.nl_groups = 1;
  int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  if (hotplug_sock == -1) {
    printf("error getting socket: %s", strerror(errno));
    return -1;
  }
  setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize,
             sizeof(buffersize));
  retval =
      bind(hotplug_sock, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
  if (retval < 0) {
    printf("bind failed: %s", strerror(errno));
    close(hotplug_sock);
    hotplug_sock = -1;
    return -1;
  }
  return hotplug_sock;
}


x86 assembly (clang, -O0):
<init_hotplug_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1000000,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1212 <init_hotplug_sock+0x92>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdff(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <init_hotplug_sock+0x112>
mov    -0x1c(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    128c <init_hotplug_sock+0x10c>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdad(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <init_hotplug_sock+0x112>
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   $0x1000000,0xc(%rsp)
01
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e23(%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    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1244 <init_hotplug_sock+0xc4>
mov    %eax,%ebx
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%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   1080 <bind@plt>
test   %eax,%eax
jns    1266 <init_hotplug_sock+0xe6>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde6(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1261 <init_hotplug_sock+0xe1>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda7(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

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 = [25 x i8] c"error getting socket: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVBUFFORCE = global i32 0
@.str.1 = [16 x i8] c"bind failed: %s\00"
define i32 @init_hotplug_sock() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 16777216, i32* %3
%6 = call i32 @memset(%struct.sockaddr_nl* %2, i32 0, i32 12)
%7 = load i32, i32* @AF_NETLINK
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i32 0, i32 2
store i32 %7, i32* %8
%9 = call i32 (...) @getpid()
%10 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, 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 %22
18: ; preds = %0
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %1
br label %40
22: ; preds = %0
%23 = load i32, i32* %5
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_RCVBUFFORCE
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %3, i32 4)
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %22
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %5
store i32 -1, i32* %1
br label %40
38: ; preds = %22
%39 = load i32, i32* %5
store i32 %39, i32* %1
br label %40
40: ; preds = %38, %32, %18
%41 = load i32, i32* %1
ret i32 %41
}

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 = [25 x i8] c"error getting socket: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVBUFFORCE = global i32 0
@.str.1 = [16 x i8] c"bind failed: %s\00"
define i32 @init_hotplug_sock() {
%1 = alloca %struct.sockaddr_nl
%2 = alloca i32
%3 = bitcast %struct.sockaddr_nl* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 16777216, i32* %2
%5 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%6 = load i32, i32* @AF_NETLINK
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 (...) @getpid()
%9 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, 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 = call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %32
20: ; preds = %0
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVBUFFORCE
%23 = call i32 @setsockopt(i32 %14, i32 %21, i32 %22, i32* %2, i32 4)
%24 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%25 = call i32 @bind(i32 %14, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @close(i32 %14)
br label %32
32: ; preds = %20, %27, %16
%33 = phi i32 [ -1, %16 ], [ -1, %27 ], [ %14, %20 ]
ret i32 %33
}