#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 nl_groups;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int znl_open(int protocol, int groups) {
  struct sockaddr_nl addr;
  int fd, buf = 128 * 1024;
  fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  if (fd < 0)
    return -1;
  if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0)
    goto error;
  if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
    goto error;
  if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf, sizeof(buf)) < 0)
    goto error;
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = groups;
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    goto error;
  return fd;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<znl_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x20000,-0x20(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a8 <znl_open+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <znl_open+0x148>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    11f1 <znl_open+0x91>
jmp    1299 <znl_open+0x139>
mov    -0x1c(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    1219 <znl_open+0xb9>
jmp    1299 <znl_open+0x139>
mov    -0x1c(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    124c <znl_open+0xec>
jmp    1299 <znl_open+0x139>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    128e <znl_open+0x12e>
jmp    1299 <znl_open+0x139>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a8 <znl_open+0x148>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<znl_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%edx
movl   $0x20000,0x4(%rsp)
mov    0x2e50(%rip),%r12
mov    (%r12),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1080 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1260 <znl_open+0x100>
mov    %eax,%ebp
mov    0x2e07(%rip),%rax
mov    (%rax),%ebx
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e1c(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1259 <znl_open+0xf9>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
test   %eax,%eax
js     1259 <znl_open+0xf9>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dba(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1259 <znl_open+0xf9>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1259 <znl_open+0xf9>
mov    %ebp,%r15d
jmp    1260 <znl_open+0x100>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
define i32 @znl_open(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_nl
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 131072, i32* %8
%9 = load i32, i32* @AF_NETLINK
%10 = load i32, i32* @SOCK_RAW
%11 = load i32, i32* %4
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %57
16: ; preds = %2
%17 = load i32, i32* %7
%18 = load i32, i32* @F_SETFL
%19 = load i32, i32* %7
%20 = load i32, i32* @F_GETFL
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
%24 = call i32 @fcntl(i32 %17, i32 %18, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %16
br label %54
27: ; preds = %16
%28 = load i32, i32* %7
%29 = load i32, i32* @F_SETFD
%30 = load i32, i32* @FD_CLOEXEC
%31 = call i32 @fcntl(i32 %28, i32 %29, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %54
34: ; preds = %27
%35 = load i32, i32* %7
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_RCVBUF
%38 = call i64 @setsockopt(i32 %35, i32 %36, i32 %37, i32* %8, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
br label %54
41: ; preds = %34
%42 = call i32 @memset(%struct.sockaddr_nl* %6, i32 0, i32 8)
%43 = load i32, i32* @AF_NETLINK
%44 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* %5
%46 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %7
%48 = bitcast %struct.sockaddr_nl* %6 to %struct.sockaddr*
%49 = call i64 @bind(i32 %47, %struct.sockaddr* %48, i32 8)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %41
br label %54
52: ; preds = %41
%53 = load i32, i32* %7
store i32 %53, i32* %3
br label %57
54: ; preds = %51, %40, %33, %26
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
store i32 -1, i32* %3
br label %57
57: ; preds = %54, %52, %15
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
define i32 @znl_open(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_nl
%4 = alloca i32
%5 = bitcast %struct.sockaddr_nl* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 131072, i32* %4
%7 = load i32, i32* @AF_NETLINK
%8 = load i32, i32* @SOCK_RAW
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 %0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %39, label %11
11: ; preds = %2
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @F_GETFL
%14 = tail call i32 @fcntl(i32 %9, i32 %13, i32 0)
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %15, %14
%17 = tail call i32 @fcntl(i32 %9, i32 %12, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %37, label %19
19: ; preds = %11
%20 = load i32, i32* @F_SETFD
%21 = load i32, i32* @FD_CLOEXEC
%22 = tail call i32 @fcntl(i32 %9, i32 %20, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %19
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_RCVBUF
%27 = call i64 @setsockopt(i32 %9, i32 %25, i32 %26, i32* %4, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %24
%30 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 8)
%31 = load i32, i32* @AF_NETLINK
%32 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i32 %1, i32* %33
%34 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%35 = call i64 @bind(i32 %9, %struct.sockaddr* %34, i32 8)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %29, %24, %19, %11
%38 = call i32 @close(i32 %9)
br label %39
39: ; preds = %29, %2, %37
%40 = phi i32 [ -1, %37 ], [ -1, %2 ], [ %9, %29 ]
ret i32 %40
}