#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_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ nladdr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ nl_close(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int nl_open(pid_t *pid) {
  struct sockaddr_nl nladdr;
  int sd;
  sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sd < 0)
    return -1;
  memset(&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  if (bind(sd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
    nl_close(sd);
    return -1;
  }
  if (pid) {
    socklen_t len = sizeof(nladdr);
    if (getsockname(sd, (struct sockaddr *)&nladdr, &len) < 0) {
      nl_close(sd);
      return -1;
    }
    *pid = nladdr.nl_pid;
  }
  return sd;
}


x86 assembly (clang, -O0):
<nl_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1195 <nl_open+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <nl_open+0xdf>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11e0 <nl_open+0x90>
mov    -0x1c(%rbp),%edi
call   1030 <nl_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <nl_open+0xdf>
cmpq   $0x0,-0x10(%rbp)
je     1229 <nl_open+0xd9>
movl   $0x8,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <getsockname@plt>
cmp    $0x0,%rax
jge    1220 <nl_open+0xd0>
mov    -0x1c(%rbp),%edi
call   1030 <nl_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <nl_open+0xdf>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11f2 <nl_open+0xa2>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11eb <nl_open+0x9b>
test   %r14,%r14
je     11e7 <nl_open+0x97>
movl   $0x8,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebp,%edi
call   1060 <getsockname@plt>
test   %rax,%rax
js     11eb <nl_open+0x9b>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%ebx
jmp    11f2 <nl_open+0xa2>
mov    %ebp,%edi
call   1030 <nl_close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@AF_NETLINK = global i32 0
define i32 @nl_open(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.sockaddr_nl
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
%7 = load i32, i32* @PF_NETLINK
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @NETLINK_ROUTE
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %42
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 8)
%16 = load i32, i32* @AF_NETLINK
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* %5
%19 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%20 = call i64 @bind(i32 %18, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* %5
%24 = call i32 @nl_close(i32 %23)
store i32 -1, i32* %2
br label %42
25: ; preds = %14
%26 = load i32*, i32** %3
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %40
28: ; preds = %25
store i32 8, i32* %6
%29 = load i32, i32* %5
%30 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%31 = call i64 @getsockname(i32 %29, %struct.sockaddr* %30, i32* %6)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* %5
%35 = call i32 @nl_close(i32 %34)
store i32 -1, i32* %2
br label %42
36: ; preds = %28
%37 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load i32*, i32** %3
store i32 %38, i32* %39
br label %40
40: ; preds = %36, %25
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %33, %22, %13
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@AF_NETLINK = global i32 0
define i32 @nl_open(i32* writeonly %0) {
%2 = alloca %struct.sockaddr_nl
%3 = alloca i32
%4 = bitcast %struct.sockaddr_nl* %2 to i8*
%5 = load i32, i32* @PF_NETLINK
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @NETLINK_ROUTE
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_nl* %2, i32 0, i32 8)
%12 = load i32, i32* @AF_NETLINK
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%15 = call i64 @bind(i32 %8, %struct.sockaddr* %14, i32 8)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @nl_close(i32 %8)
br label %30
19: ; preds = %10
%20 = icmp eq i32* %0, null
br i1 %20, label %30, label %21
21: ; preds = %19
%22 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%23 = call i64 @getsockname(i32 %8, %struct.sockaddr* %14, i32* %3)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @nl_close(i32 %8)
br label %30
27: ; preds = %21
%28 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 0
%29 = load i32, i32* %28
store i32 %29, i32* %0
br label %30
30: ; preds = %19, %27, %25, %1, %17
%31 = phi i32 [ -1, %17 ], [ -1, %1 ], [ -1, %25 ], [ %8, %27 ], [ %8, %19 ]
ret i32 %31
}