#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*/ EXIT_FAILURE;
int /*<<< orphan*/ NETLINK_CONNECTOR;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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("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("bind");
    close(nl_sock);
    exit(EXIT_FAILURE);
  }
  return nl_sock;
}


x86 assembly (clang, -O0):
<nl_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ad <nl_connect+0x4d>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <nl_connect+0xb5>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2df8(%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    120f <nl_connect+0xaf>
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
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    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11cc <nl_connect+0x6c>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e18(%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     11da <nl_connect+0x7a>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
jmp    11c4 <nl_connect+0x64>
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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"
@EXIT_FAILURE = global i32 0
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 ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
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 %31
25: ; preds = %13
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %13
%32 = load i32, i32* %3
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %11
%34 = load i32, i32* %1
ret i32 %34
}

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"
@EXIT_FAILURE = global i32 0
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 ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %25
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 %25
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)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %10, %8
ret i32 %6
}