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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int SOCK_CLOEXEC;
int SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int);
char *strerror(int /*<<< orphan*/);
int netlink_open(int protocol) {
  struct sockaddr_nl addr;
  int fd = -1;
  int bufsize = (1024 * 32);
  fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
  if (fd == -1) {
    printf("socket: %s\n", strerror(errno));
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize))) {
    printf("setsockopt: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize))) {
    printf("setsockopt: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    printf("bind: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<netlink_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x8000,-0x18(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
mov    -0x8(%rbp),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e2 <netlink_open+0x72>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <netlink_open+0x19e>
mov    -0x14(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1241 <netlink_open+0xd1>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <netlink_open+0x19e>
mov    -0x14(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12a0 <netlink_open+0x130>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd85(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <netlink_open+0x19e>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1308 <netlink_open+0x198>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd2d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <netlink_open+0x19e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_open>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%edx
movl   $0x8000,0x4(%rsp)
mov    0x2e3e(%rip),%r15
mov    (%r15),%edi
mov    0x2e44(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     121e <netlink_open+0xae>
mov    %eax,%ebx
mov    0x2e2e(%rip),%r14
mov    (%r14),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    11f8 <netlink_open+0x88>
mov    (%r14),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     124c <netlink_open+0xdc>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdff(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    123b <netlink_open+0xcb>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdcd(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     1240 <netlink_open+0xd0>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd8c(%rip),%rdi
jmp    120d <netlink_open+0x9d>

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@.str.1 = [16 x i8] c"setsockopt: %s\0A\00"
@SO_RCVBUF = global i32 0
@.str.2 = [10 x i8] c"bind: %s\0A\00"
define i32 @netlink_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
store i32 -1, i32* %5
store i32 32768, i32* %6
%7 = load i32, i32* @AF_NETLINK
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @SOCK_CLOEXEC
%10 = or i32 %8, %9
%11 = load i32, i32* %3
%12 = call i32 @socket(i32 %7, i32 %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %2
br label %59
19: ; preds = %1
%20 = load i32, i32* %5
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_SNDBUF
%23 = call i64 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %6, i32 4)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %59
31: ; preds = %19
%32 = load i32, i32* %5
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_RCVBUF
%35 = call i64 @setsockopt(i32 %32, i32 %33, i32 %34, i32* %6, i32 4)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %2
br label %59
43: ; preds = %31
%44 = call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 4)
%45 = load i32, i32* @AF_NETLINK
%46 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %5
%48 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%49 = call i64 @bind(i32 %47, %struct.sockaddr* %48, i32 4)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %43
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %5
%56 = call i32 @close(i32 %55)
store i32 -1, i32* %2
br label %59
57: ; preds = %43
%58 = load i32, i32* %5
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %51, %37, %25, %15
%60 = load i32, i32* %2
ret i32 %60
}

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