#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 */
typedef int /*<<< orphan*/ sz;
struct sockaddr_nl {
  int nl_groups;
  int /*<<< orphan*/ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ addr;

/* 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_PASSCRED;
int /*<<< orphan*/ SO_RCVBUFFORCE;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_uevent_socket(void) {
  struct sockaddr_nl addr;
  int sz = 64 * 1024;
  int on = 1;
  int s;
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_pid = getpid();
  addr.nl_groups = 0xffffffff;
  s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  if (s < 0)
    return -1;
  setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
  setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<open_uevent_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x10000,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11dc <open_uevent_socket+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <open_uevent_socket+0xf9>
mov    -0x1c(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1253 <open_uevent_socket+0xf3>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <open_uevent_socket+0xf9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_uevent_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x10000,0xc(%rsp)
movl   $0x1,0x8(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e30(%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    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1238 <open_uevent_socket+0xd8>
mov    %eax,%ebp
mov    0x2e0d(%rip),%r14
mov    (%r14),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r14),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1231 <open_uevent_socket+0xd1>
mov    %ebp,%ebx
jmp    1238 <open_uevent_socket+0xd8>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
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
@SOL_SOCKET = global i32 0
@SO_RCVBUFFORCE = global i32 0
@SO_PASSCRED = global i32 0
define i32 @open_uevent_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 65536, i32* %3
store i32 1, i32* %4
%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 slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %0
store i32 -1, i32* %1
br label %37
19: ; preds = %0
%20 = load i32, i32* %5
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVBUFFORCE
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %3, i32 4)
%24 = load i32, i32* %5
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_PASSCRED
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %4, i32 4)
%28 = load i32, i32* %5
%29 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %1
br label %37
35: ; preds = %19
%36 = load i32, i32* %5
store i32 %36, i32* %1
br label %37
37: ; preds = %35, %32, %18
%38 = load i32, i32* %1
ret i32 %38
}

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
@SOL_SOCKET = global i32 0
@SO_RCVBUFFORCE = global i32 0
@SO_PASSCRED = global i32 0
define i32 @open_uevent_socket() {
%1 = alloca %struct.sockaddr_nl
%2 = alloca i32
%3 = alloca i32
%4 = bitcast %struct.sockaddr_nl* %1 to i8*
%5 = bitcast i32* %2 to i8*
store i32 65536, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%8 = load i32, i32* @AF_NETLINK
%9 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i32 (...) @getpid()
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 -1, i32* %12
%13 = load i32, i32* @PF_NETLINK
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %0
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_RCVBUFFORCE
%21 = call i32 @setsockopt(i32 %16, i32 %19, i32 %20, i32* %2, i32 4)
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_PASSCRED
%24 = call i32 @setsockopt(i32 %16, i32 %22, i32 %23, i32* %3, i32 4)
%25 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%26 = call i64 @bind(i32 %16, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = call i32 @close(i32 %16)
br label %30
30: ; preds = %18, %0, %28
%31 = phi i32 [ -1, %28 ], [ -1, %0 ], [ %16, %18 ]
ret i32 %31
}