#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_groups;
  int /*<<< orphan*/ nl_pid;
  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*/ RTMGRP_IPV4_ROUTE;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_netlink_socket() {
  int s = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (s < 0)
    return s;
  struct sockaddr_nl nladdr;
  memset((void *)&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  nladdr.nl_pid = getpid();
  nladdr.nl_groups = RTMGRP_IPV4_ROUTE;
  if (bind(s, (struct sockaddr *)&nladdr, sizeof(nladdr))) {
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<open_netlink_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1180 <open_netlink_socket+0x40>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <open_netlink_socket+0x9f>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
je     11d9 <open_netlink_socket+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <open_netlink_socket+0x9f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_netlink_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e90(%rip),%r15
mov    (%r15),%edi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <open_netlink_socket+0x76>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x4(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
neg    %rax
sbb    %eax,%eax
or     %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@RTMGRP_IPV4_ROUTE = global i32 0
define i32 @open_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 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = load i32, i32* %2
store i32 %11, i32* %1
br label %28
12: ; preds = %0
%13 = bitcast %struct.sockaddr_nl* %3 to i8*
%14 = call i32 @memset(i8* %13, i32 0, i32 12)
%15 = load i32, i32* @AF_NETLINK
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 (...) @getpid()
%18 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @RTMGRP_IPV4_ROUTE
%20 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %2
%22 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %12
store i32 -1, i32* %1
br label %28
26: ; preds = %12
%27 = load i32, i32* %2
store i32 %27, i32* %1
br label %28
28: ; preds = %26, %25, %10
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@RTMGRP_IPV4_ROUTE = global i32 0
define i32 @open_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 %20, label %7
7: ; preds = %0
%8 = bitcast %struct.sockaddr_nl* %1 to i8*
%9 = call i32 @memset(i8* %8, i32 0, i32 12)
%10 = load i32, i32* @AF_NETLINK
%11 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 (...) @getpid()
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @RTMGRP_IPV4_ROUTE
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%17 = call i64 @bind(i32 %5, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i32 %5, i32 -1
br label %20
20: ; preds = %0, %7
%21 = phi i32 [ %19, %7 ], [ %5, %0 ]
ret i32 %21
}