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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_netlink_socket() {
  struct sockaddr_nl nl_addr;
  int sd;
  dprintf(LOG_DEBUG, "open_netlink_socket called");
  sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sd < 0)
    return -1;
  memset(&nl_addr, 0, sizeof(nl_addr));
  nl_addr.nl_family = AF_NETLINK;
  if (bind(sd, (struct sockaddr *)&nl_addr, sizeof(nl_addr)) < 0) {
    dprintf(LOG_ERR, "netlink bind error");
    close(sd);
    return -1;
  }
  return sd;
}


x86 assembly (clang, -O0):
<open_netlink_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
call   1050 <dprintf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a6 <open_netlink_socket+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <open_netlink_socket+0xbc>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1206 <open_netlink_socket+0xb6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <dprintf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <open_netlink_socket+0xbc>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_netlink_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
call   1050 <dprintf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11ed <open_netlink_socket+0x9d>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11d1 <open_netlink_socket+0x81>
mov    %ebp,%ebx
jmp    11ed <open_netlink_socket+0x9d>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1050 <dprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@LOG_DEBUG = global i32 0
@.str = [27 x i8] c"open_netlink_socket called\00"
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@AF_NETLINK = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [19 x i8] c"netlink bind error\00"
define i32 @open_netlink_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = alloca i32
%4 = load i32, i32* @LOG_DEBUG
%5 = call i32 @dprintf(i32 %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @PF_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* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 -1, i32* %1
br label %28
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_nl* %2, i32 0, i32 4)
%15 = load i32, i32* @AF_NETLINK
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %3
%18 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i32, i32* @LOG_ERR
%23 = call i32 @dprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %1
br label %28
26: ; preds = %13
%27 = load i32, i32* %3
store i32 %27, i32* %1
br label %28
28: ; preds = %26, %21, %12
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@LOG_DEBUG = global i32 0
@.str = [27 x i8] c"open_netlink_socket called\00"
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@AF_NETLINK = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [19 x i8] c"netlink bind error\00"
define i32 @open_netlink_socket() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = load i32, i32* @LOG_DEBUG
%4 = tail call i32 @dprintf(i32 %3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @PF_NETLINK
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @NETLINK_ROUTE
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %0
%11 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 4)
%12 = load i32, i32* @AF_NETLINK
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%15 = call i64 @bind(i32 %8, %struct.sockaddr* %14, i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = load i32, i32* @LOG_ERR
%19 = call i32 @dprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @close(i32 %8)
br label %21
21: ; preds = %10, %0, %17
%22 = phi i32 [ -1, %17 ], [ -1, %0 ], [ %8, %10 ]
ret i32 %22
}