#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 */
typedef int /*<<< orphan*/ timeout;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
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 /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
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;
  }
  struct timeval timeout;
  timeout.tv_sec = 5;
  timeout.tv_usec = 0;
  rc = setsockopt(nl_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                  sizeof(timeout));
  if (rc == -1) {
    perror("error: setsockopt");
    close(nl_sock);
    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    $0x30,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%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   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <nl_connect+0x113>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    120d <nl_connect+0xad>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <nl_connect+0x113>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    126d <nl_connect+0x10d>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <nl_connect+0x113>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_connect>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1206 <nl_connect+0xa6>
mov    %eax,%ebx
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1214 <nl_connect+0xb4>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     121d <nl_connect+0xbd>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdf3(%rip),%rdi
call   1070 <perror@plt>
jmp    1230 <nl_connect+0xd0>
lea    0xdf3(%rip),%rdi
jmp    1224 <nl_connect+0xc4>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    11fe <nl_connect+0x9e>

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%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"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.1 = [18 x i8] c"error: setsockopt\00"
@AF_NETLINK = global i32 0
@CN_IDX_PROC = global i32 0
@.str.2 = [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 = alloca %struct.timeval
%6 = load i32, i32* @PF_NETLINK
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @NETLINK_CONNECTOR
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %46
14: ; preds = %0
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 5, i32* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %16
%17 = load i32, i32* %3
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_RCVTIMEO
%20 = bitcast %struct.timeval* %5 to i8*
%21 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i32 16)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %1
br label %46
28: ; preds = %14
%29 = load i32, i32* @AF_NETLINK
%30 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @CN_IDX_PROC
%32 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 1
store i32 %31, i32* %32
%33 = call i32 (...) @getpid()
%34 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %3
%36 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
store i32 %37, i32* %2
%38 = load i32, i32* %2
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %28
%41 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %1
br label %46
44: ; preds = %28
%45 = load i32, i32* %3
store i32 %45, i32* %1
br label %46
46: ; preds = %44, %40, %24, %12
%47 = load i32, i32* %1
ret i32 %47
}

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