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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ RTMGRP_LINK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_netlink(void) {
  int sk;
  struct sockaddr_nl addr;
  sk = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sk < 0) {
    printf("ERR:Could not open netlink socket.\n");
    return (-1);
  }
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = RTMGRP_LINK;
  if (bind(sk, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    printf("ERR:Could not bind socket!\n");
    return (-1);
  }
  return sk;
}


x86 assembly (clang, -O0):
<open_netlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118d <open_netlink+0x4d>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <open_netlink+0xae>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11e8 <open_netlink+0xa8>
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <open_netlink+0xae>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_netlink>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11b2 <open_netlink+0x72>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
test   %rax,%rax
jns    11c3 <open_netlink+0x83>
lea    0xe50(%rip),%rdi
jmp    11b9 <open_netlink+0x79>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [36 x i8] c"ERR:Could not open netlink socket.\0A\00"
@AF_NETLINK = global i32 0
@RTMGRP_LINK = global i32 0
@.str.1 = [28 x i8] c"ERR:Could not bind socket!\0A\00"
define i32 @open_netlink() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
%4 = load i32, i32* @PF_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 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %26
12: ; preds = %0
%13 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 8)
%14 = load i32, i32* @AF_NETLINK
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @RTMGRP_LINK
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%20 = call i64 @bind(i32 %18, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %26
24: ; preds = %12
%25 = load i32, i32* %2
store i32 %25, i32* %1
br label %26
26: ; preds = %24, %22, %10
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@AF_NETLINK = global i32 0
@RTMGRP_LINK = global i32 0
@str = [27 x i8] c"ERR:Could not bind socket!\00"
@str.2 = [35 x i8] c"ERR:Could not open netlink socket.\00"
define i32 @open_netlink() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = load i32, i32* @PF_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 %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.2, i64 0, i64 0))
br label %21
10: ; preds = %0
%11 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 8)
%12 = load i32, i32* @AF_NETLINK
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @RTMGRP_LINK
%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 %6, %struct.sockaddr* %16, i32 8)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %10, %19, %8
%22 = phi i32 [ -1, %8 ], [ -1, %19 ], [ %6, %10 ]
ret i32 %22
}