#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*/ nladdr;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_netlink_socket() {
  int sock;
  if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
    return -1;
  }
  struct sockaddr_nl nladdr;
  memset(&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  if (bind(sock, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<get_netlink_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1170 <get_netlink_socket+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <get_netlink_socket+0x89>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    11b3 <get_netlink_socket+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <get_netlink_socket+0x89>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_netlink_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ea0(%rip),%r15
mov    (%r15),%edi
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
test   %eax,%eax
js     1197 <get_netlink_socket+0x67>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    119c <get_netlink_socket+0x6c>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
define i32 @get_netlink_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = load i32, i32* @AF_NETLINK
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @NETLINK_ROUTE
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %21
10: ; preds = %0
%11 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 4)
%12 = load i32, i32* @AF_NETLINK
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %2
%15 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%16 = call i64 @bind(i32 %14, %struct.sockaddr* %15, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 -1, i32* %1
br label %21
19: ; preds = %10
%20 = load i32, i32* %2
store i32 %20, i32* %1
br label %21
21: ; preds = %19, %18, %9
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
define i32 @get_netlink_socket() {
%1 = alloca %struct.sockaddr_nl
%2 = load i32, i32* @AF_NETLINK
%3 = load i32, i32* @SOCK_RAW
%4 = load i32, i32* @NETLINK_ROUTE
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %0
%8 = bitcast %struct.sockaddr_nl* %1 to i8*
%9 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 4)
%10 = load i32, i32* @AF_NETLINK
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%13 = call i64 @bind(i32 %5, %struct.sockaddr* %12, i32 4)
%14 = icmp slt i64 %13, 0
%15 = select i1 %14, i32 -1, i32 %5
br label %16
16: ; preds = %0, %7
%17 = phi i32 [ %15, %7 ], [ -1, %0 ]
ret i32 %17
}