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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct sockaddr_nl *malloc(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 /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int _nl_socket_init(void) {
  int fd = 0;
  if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
    fprintf(stderr, "failed to get socket: %s\n", strerror(errno));
    return 0;
  }
  int sndbuf = 32768;
  int rcvbuf = 32768;
  if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) {
    fprintf(stderr, "failed to set send buffer: %s\n", strerror(errno));
    return 0;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) < 0) {
    fprintf(stderr, "failed to set recieve buffer: %s\n", strerror(errno));
    return 0;
  }
  struct sockaddr_nl *sa = malloc(sizeof(struct sockaddr_nl));
  memset(sa, 0, sizeof(struct sockaddr_nl));
  sa->nl_family = AF_NETLINK;
  sa->nl_groups = 0;
  if (bind(fd, (struct sockaddr *)sa, sizeof(struct sockaddr)) < 0) {
    fprintf(stderr, "failed to bind socket: %s\n", strerror(errno));
    return 0;
  }
  return fd;
}


x86 assembly (clang, -O0):
<_nl_socket_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x8(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11e3 <_nl_socket_init+0x73>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134f <_nl_socket_init+0x1df>
movl   $0x8000,-0xc(%rbp)
movl   $0x8000,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1257 <_nl_socket_init+0xe7>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdd4(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134f <_nl_socket_init+0x1df>
mov    -0x8(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12bd <_nl_socket_init+0x14d>
mov    0x2d2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd8d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134f <_nl_socket_init+0x1df>
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1349 <_nl_socket_init+0x1d9>
mov    0x2ca0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd23(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134f <_nl_socket_init+0x1df>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nl_socket_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e52(%rip),%r15
mov    (%r15),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1247 <_nl_socket_init+0xd7>
mov    %eax,%ebx
movl   $0x8000,0x4(%rsp)
movl   $0x8000,(%rsp)
mov    0x2e30(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1269 <_nl_socket_init+0xf9>
mov    0x0(%rbp),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1272 <_nl_socket_init+0x102>
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%r14)
movq   $0x0,(%r14)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    129f <_nl_socket_init+0x12f>
lea    0xe16(%rip),%rbx
jmp    1279 <_nl_socket_init+0x109>
mov    0x2d6a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd9b(%rip),%rsi
mov    %ebx,%edi
jmp    1295 <_nl_socket_init+0x125>
lea    0xdaa(%rip),%rbx
jmp    1279 <_nl_socket_init+0x109>
lea    0xdc0(%rip),%rbx
mov    0x2d38(%rip),%rax
mov    (%rax),%ebp
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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