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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
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*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int netlink_socket(void) {
  int l_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (l_socket < 0) {
    return -1;
  }
  struct sockaddr_nl l_addr;
  memset(&l_addr, 0, sizeof(l_addr));
  l_addr.nl_family = AF_NETLINK;
  if (bind(l_socket, (struct sockaddr *)&l_addr, sizeof(l_addr)) < 0) {
    close(l_socket);
    return -1;
  }
  return l_socket;
}


x86 assembly (clang, -O0):
<netlink_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%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    1181 <netlink_socket+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <netlink_socket+0x92>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11cc <netlink_socket+0x8c>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <netlink_socket+0x92>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11a3 <netlink_socket+0x63>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
jns    11a8 <netlink_socket+0x68>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { 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
define i32 @netlink_socket() {
%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 %11
10: ; preds = %0
store i32 -1, i32* %1
br label %24
11: ; preds = %0
%12 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 4)
%13 = load i32, i32* @AF_NETLINK
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %2
%16 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%17 = call i64 @bind(i32 %15, %struct.sockaddr* %16, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %1
br label %24
22: ; preds = %11
%23 = load i32, i32* %2
store i32 %23, i32* %1
br label %24
24: ; preds = %22, %19, %10
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { 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
define i32 @netlink_socket() {
%1 = alloca %struct.sockaddr_nl
%2 = load i32, i32* @PF_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 %19, 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
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = call i32 @close(i32 %5)
br label %17
17: ; preds = %7, %15
%18 = phi i32 [ -1, %15 ], [ %5, %7 ]
br label %19
19: ; preds = %0, %17
%20 = phi i32 [ %18, %17 ], [ -1, %0 ]
ret i32 %20
}