#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;
int 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 rc;
  int nl_sock;
  struct sockaddr_nl sa_nl;
  nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  if (nl_sock == -1) {
    perror("error: socket");
    return -1;
  }
  sa_nl.nl_family = AF_NETLINK;
  sa_nl.nl_groups = CN_IDX_PROC;
  sa_nl.nl_pid = getpid();
  rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
  if (rc == -1) {
    perror("error: 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,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    119d <nl_connect+0x4d>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <nl_connect+0xb3>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fd <nl_connect+0xad>
lea    0xe2a(%rip),%rdi
call   1060 <perror@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <nl_connect+0xb3>
mov    -0xc(%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>
cmp    $0xffffffff,%eax
je     11bc <nl_connect+0x6c>
mov    %eax,%ebx
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e30(%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>
cmp    $0xffffffff,%eax
je     11ca <nl_connect+0x7a>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
jmp    11dd <nl_connect+0x8d>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11b4 <nl_connect+0x64>

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

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 = [14 x i8] c"error: socket\00"
@AF_NETLINK = global i32 0
@CN_IDX_PROC = global i32 0
@.str.1 = [12 x i8] c"error: 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 eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 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 i32 @bind(i32 %6, %struct.sockaddr* %17, i32 12)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 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
}