#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 /*<<< orphan*/ sa_nl;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ CN_IDX_PROC;
int /*<<< orphan*/ NETLINK_CONNECTOR;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int nl_connect() {
  int nl_sock;
  struct sockaddr_nl sa_nl;
  if ((nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR)) < 0) {
    perror("socket");
    return -1;
  }
  sa_nl.nl_family = AF_NETLINK;
  sa_nl.nl_groups = CN_IDX_PROC;
  sa_nl.nl_pid = getpid();
  if (bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl)) < 0) {
    perror("bind");
    close(nl_sock);
    return -1;
  }
  return nl_sock;
}


x86 assembly (clang, -O0):
<nl_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    119c <nl_connect+0x4c>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <nl_connect+0xaf>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11f9 <nl_connect+0xa9>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <nl_connect+0xaf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_connect>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11bb <nl_connect+0x6b>
mov    %eax,%ebx
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11c9 <nl_connect+0x79>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
jmp    11dc <nl_connect+0x8c>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11b3 <nl_connect+0x63>

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
@.str = [7 x i8] c"socket\00"
@AF_NETLINK = global i32 0
@CN_IDX_PROC = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define i32 @nl_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 %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
11: ; preds = %0
%12 = load i32, i32* @AF_NETLINK
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @CN_IDX_PROC
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 (...) @getpid()
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%20 = call i64 @bind(i32 %18, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %1
br label %28
26: ; preds = %11
%27 = load i32, i32* %2
store i32 %27, i32* %1
br label %28
28: ; preds = %26, %22, %9
%29 = load i32, i32* %1
ret i32 %29
}

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
@.str = [7 x i8] c"socket\00"
@AF_NETLINK = global i32 0
@CN_IDX_PROC = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define i32 @nl_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 %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %0
%11 = load i32, i32* @AF_NETLINK
%12 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @CN_IDX_PROC
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call i32 (...) @getpid()
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%18 = call i64 @bind(i32 %6, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @close(i32 %6)
br label %23
23: ; preds = %10, %20, %8
%24 = phi i32 [ -1, %8 ], [ -1, %20 ], [ %6, %10 ]
ret i32 %24
}