#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 {
  scalar_t__ nl_family;
  scalar_t__ nl_groups;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sndbuf;
typedef int /*<<< orphan*/ rcvbuf;
typedef int /*<<< orphan*/ local;

/* Variables and functions */
scalar_t__ AF_NETLINK;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(scalar_t__, int /*<<< orphan*/, int);
int netlink_open(int protocol) {
  int fd, ret;
  socklen_t socklen;
  struct sockaddr_nl local;
  int sndbuf = 32768;
  int rcvbuf = 32768;
  int err = -1;
  fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  if (fd < 0)
    return -1;
  ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
  if (ret < 0)
    goto out;
  ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
  if (ret < 0)
    goto out;
  memset(&local, 0, sizeof(local));
  local.nl_family = AF_NETLINK;
  local.nl_groups = 0;
  ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
  if (ret < 0)
    goto out;
  socklen = sizeof(local);
  ret = getsockname(fd, (struct sockaddr *)&local, &socklen);
  if (ret < 0)
    goto out;
  errno = -EINVAL;
  if (socklen != sizeof(local))
    goto out;
  errno = -EINVAL;
  if (local.nl_family != AF_NETLINK)
    goto out;
  return fd;
out:
  close(fd);
  return err;
}


x86 assembly (clang, -O0):
<netlink_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x8000,-0x2c(%rbp)
movl   $0x8000,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b4 <netlink_open+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <netlink_open+0x19f>
mov    -0xc(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ea <netlink_open+0x8a>
jmp    12f1 <netlink_open+0x191>
mov    -0xc(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1220 <netlink_open+0xc0>
jmp    12f1 <netlink_open+0x191>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1269 <netlink_open+0x109>
jmp    12f1 <netlink_open+0x191>
movl   $0x10,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <getsockname@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1292 <netlink_open+0x132>
jmp    12f1 <netlink_open+0x191>
xor    %ecx,%ecx
mov    0x2d15(%rip),%rax
sub    (%rax),%ecx
mov    0x2d14(%rip),%rax
mov    %ecx,(%rax)
movslq -0x14(%rbp),%rax
cmp    $0x10,%rax
je     12b9 <netlink_open+0x159>
jmp    12f1 <netlink_open+0x191>
xor    %ecx,%ecx
mov    0x2cee(%rip),%rax
sub    (%rax),%ecx
mov    0x2ced(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x2cf8(%rip),%rcx
cmp    (%rcx),%rax
je     12e6 <netlink_open+0x186>
jmp    12f1 <netlink_open+0x191>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <netlink_open+0x19f>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%edx
movl   $0x8000,0xc(%rsp)
movl   $0x8000,0x8(%rsp)
mov    0x2e4b(%rip),%r15
mov    (%r15),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1080 <socket@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     1279 <netlink_open+0x119>
mov    %eax,%ebp
mov    0x2e3b(%rip),%rbx
mov    (%rbx),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1272 <netlink_open+0x112>
mov    (%rbx),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1272 <netlink_open+0x112>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    (%r15),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
test   %eax,%eax
js     1272 <netlink_open+0x112>
movl   $0x10,0x4(%rsp)
lea    0x10(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1070 <getsockname@plt>
test   %eax,%eax
js     1272 <netlink_open+0x112>
mov    0x2d61(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    0x2d5e(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x10,0x4(%rsp)
jne    1272 <netlink_open+0x112>
mov    0x10(%rsp),%rax
cmp    (%r15),%rax
jne    1272 <netlink_open+0x112>
mov    %ebp,%r12d
jmp    1279 <netlink_open+0x119>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i64, i64 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i64 0
@SOCK_RAW = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @netlink_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_nl
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 32768, i32* %8
store i32 32768, i32* %9
store i32 -1, i32* %10
%11 = load i64, i64* @AF_NETLINK
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* %3
%14 = call i32 @socket(i64 %11, i32 %12, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %73
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_SNDBUF
%22 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %8, i32 4)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
br label %69
26: ; preds = %18
%27 = load i32, i32* %4
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_RCVBUF
%30 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %9, i32 4)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26
br label %69
34: ; preds = %26
%35 = call i32 @memset(%struct.sockaddr_nl* %7, i32 0, i32 16)
%36 = load i64, i64* @AF_NETLINK
%37 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 0
store i64 %36, i64* %37
%38 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 1
store i64 0, i64* %38
%39 = load i32, i32* %4
%40 = bitcast %struct.sockaddr_nl* %7 to %struct.sockaddr*
%41 = call i32 @bind(i32 %39, %struct.sockaddr* %40, i32 16)
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %34
br label %69
45: ; preds = %34
store i32 16, i32* %6
%46 = load i32, i32* %4
%47 = bitcast %struct.sockaddr_nl* %7 to %struct.sockaddr*
%48 = call i32 @getsockname(i32 %46, %struct.sockaddr* %47, i32* %6)
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
br label %69
52: ; preds = %45
%53 = load i32, i32* @EINVAL
%54 = sub nsw i32 0, %53
store i32 %54, i32* @errno
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = icmp ne i64 %56, 16
br i1 %57, label %58, label %59
58: ; preds = %52
br label %69
59: ; preds = %52
%60 = load i32, i32* @EINVAL
%61 = sub nsw i32 0, %60
store i32 %61, i32* @errno
%62 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 0
%63 = load i64, i64* %62
%64 = load i64, i64* @AF_NETLINK
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %59
br label %69
67: ; preds = %59
%68 = load i32, i32* %4
store i32 %68, i32* %2
br label %73
69: ; preds = %66, %58, %51, %44, %33, %25
%70 = load i32, i32* %4
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %10
store i32 %72, i32* %2
br label %73
73: ; preds = %69, %67, %17
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i64, i64 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i64 0
@SOCK_RAW = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @netlink_open(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.sockaddr_nl* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 32768, i32* %4
%9 = bitcast i32* %5 to i8*
store i32 32768, i32* %5
%10 = load i64, i64* @AF_NETLINK
%11 = load i32, i32* @SOCK_RAW
%12 = tail call i32 @socket(i64 %10, i32 %11, i32 %0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %46, label %14
14: ; preds = %1
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_SNDBUF
%17 = call i32 @setsockopt(i32 %12, i32 %15, i32 %16, i32* %4, i32 4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %44, label %19
19: ; preds = %14
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_RCVBUF
%22 = call i32 @setsockopt(i32 %12, i32 %20, i32 %21, i32* %5, i32 4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %44, label %24
24: ; preds = %19
%25 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 16)
%26 = load i64, i64* @AF_NETLINK
%27 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i64 %26, i64* %27
%28 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
store i64 0, i64* %28
%29 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %12, %struct.sockaddr* %29, i32 16)
%31 = icmp slt i32 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %24
store i32 16, i32* %2
%33 = call i32 @getsockname(i32 %12, %struct.sockaddr* %29, i32* %2)
%34 = icmp slt i32 %33, 0
br i1 %34, label %44, label %35
35: ; preds = %32
%36 = load i32, i32* @EINVAL
%37 = sub nsw i32 0, %36
store i32 %37, i32* @errno
%38 = load i32, i32* %2
%39 = icmp eq i32 %38, 16
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i64, i64* %27
%42 = load i64, i64* @AF_NETLINK
%43 = icmp eq i64 %41, %42
br i1 %43, label %46, label %44
44: ; preds = %40, %35, %32, %24, %19, %14
%45 = call i32 @close(i32 %12)
br label %46
46: ; preds = %40, %1, %44
%47 = phi i32 [ -1, %44 ], [ -1, %1 ], [ %12, %40 ]
ret i32 %47
}