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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ CN_IDX_PROC;
int EPROTONOSUPPORT;
int /*<<< orphan*/ NETLINK_CONNECTOR;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int netlink_connect(void) {
  int sock;
  struct sockaddr_nl addr;
  if ((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR)) < 0) {
    if (errno == EPROTONOSUPPORT)
      return -EPROTONOSUPPORT;
    (void)fprintf(stderr, "socket failed: errno=%d (%s).\n", errno,
                  strerror(errno));
    return -1;
  }
  (void)memset(&addr, 0, sizeof(addr));
  addr.nl_pid = getpid();
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = CN_IDX_PROC;
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    (void)fprintf(stderr, "Bind failed: errno=%d (%s).\n", errno,
                  strerror(errno));
    (void)close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<netlink_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1216 <netlink_connect+0xa6>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%eax
jne    11cf <netlink_connect+0x5f>
xor    %eax,%eax
mov    0x2dfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <netlink_connect+0x148>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <netlink_connect+0x148>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    12b2 <netlink_connect+0x142>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xd86(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <netlink_connect+0x148>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1223 <netlink_connect+0xb3>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1263 <netlink_connect+0xf3>
mov    0x2dd5(%rip),%rax
mov    (%rax),%r14d
mov    0x2db3(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe12(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    125e <netlink_connect+0xee>
mov    0x2d86(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8d(%rip),%rax
cmp    (%rax),%ebx
jne    123b <netlink_connect+0xcb>
neg    %ebx
jmp    1263 <netlink_connect+0xf3>
mov    0x2d86(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdae(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
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
@NETLINK_CONNECTOR = global i32 0
@errno = global i32 0
@EPROTONOSUPPORT = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"socket failed: errno=%d (%s).\0A\00"
@AF_NETLINK = global i32 0
@CN_IDX_PROC = global i32 0
@.str.1 = [29 x i8] c"Bind failed: errno=%d (%s).\0A\00"
define i32 @netlink_connect() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = load i32, i32* @PF_NETLINK
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @NETLINK_CONNECTOR
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = load i32, i32* @EPROTONOSUPPORT
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @EPROTONOSUPPORT
%15 = sub nsw i32 0, %14
store i32 %15, i32* %1
br label %44
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %18, i8* %20)
store i32 -1, i32* %1
br label %44
22: ; preds = %0
%23 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 12)
%24 = call i32 (...) @getpid()
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @AF_NETLINK
%27 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @CN_IDX_PROC
%29 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %2
%31 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %22
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %36, i8* %38)
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %1
br label %44
42: ; preds = %22
%43 = load i32, i32* %2
store i32 %43, i32* %1
br label %44
44: ; preds = %42, %34, %16, %13
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_CONNECTOR = global i32 0
@errno = global i32 0
@EPROTONOSUPPORT = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"socket failed: errno=%d (%s).\0A\00"
@AF_NETLINK = global i32 0
@CN_IDX_PROC = global i32 0
@.str.1 = [29 x i8] c"Bind failed: errno=%d (%s).\0A\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* @NETLINK_CONNECTOR
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = load i32, i32* @EPROTONOSUPPORT
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = sub nsw i32 0, %9
br label %35
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i8* @strerror(i32 %9)
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %16)
br label %35
18: ; preds = %0
%19 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%20 = call i32 (...) @getpid()
%21 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @AF_NETLINK
%23 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @CN_IDX_PROC
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%27 = call i64 @bind(i32 %6, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %31, i8* %32)
%34 = call i32 @close(i32 %6)
br label %35
35: ; preds = %18, %29, %14, %12
%36 = phi i32 [ %13, %12 ], [ -1, %14 ], [ -1, %29 ], [ %6, %18 ]
ret i32 %36
}