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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ CN_IDX_PROC;
int /*<<< orphan*/ NETLINK_CONNECTOR;
int /*<<< orphan*/ PF_NETLINK;
int SOCK_CLOEXEC;
int SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ log_perror(char *);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int32_t netlink_connect(void) {
  struct sockaddr_nl addr;
  int32_t netlink_socket =
      socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_CONNECTOR);
  if (netlink_socket == -1) {
    log_perror("nyx: socket");
    return -1;
  }
  memset(&addr, 0, sizeof(struct sockaddr_nl));
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = CN_IDX_PROC;
  addr.nl_pid = getpid();
  int32_t rc = bind(netlink_socket, (struct sockaddr *)&addr, sizeof(addr));
  if (rc == -1) {
    log_perror("nyx: bind");
    close(netlink_socket);
    return -1;
  }
  return netlink_socket;
}


x86 assembly (clang, -O0):
<netlink_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b6 <netlink_connect+0x56>
lea    0xe5b(%rip),%rdi
call   1070 <log_perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <netlink_connect+0xcc>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1226 <netlink_connect+0xc6>
lea    0xdff(%rip),%rdi
call   1070 <log_perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <netlink_connect+0xcc>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_connect>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f9 <netlink_connect+0x99>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    120a <netlink_connect+0xaa>
lea    0xe21(%rip),%rdi
call   1070 <log_perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1205 <netlink_connect+0xa5>
lea    0xe00(%rip),%rdi
call   1070 <log_perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

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