#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*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ nl_close(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int nl_open(void) {
  struct sockaddr_nl nladdr;
  int sd;
  sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sd < 0)
    return -1;
  memset(&nladdr, 0, sizeof(nladdr));
  nladdr.nl_family = AF_NETLINK;
  if (bind(sd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
    nl_close(sd);
    return -1;
  }
  return sd;
}


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

x86 assembly (clang, -O2):
<nl_open>:
push   %rbp
push   %r14
push   %rbx
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    $0xffffffff,%ebx
test   %eax,%eax
js     11b3 <nl_open+0x73>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11ac <nl_open+0x6c>
mov    %ebp,%ebx
jmp    11b3 <nl_open+0x73>
mov    %ebp,%edi
call   1030 <nl_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 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@AF_NETLINK = global i32 0
define i32 @nl_open() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = alloca i32
%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* %3
%8 = load i32, i32* %3
%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* %2, i32 0, i32 4)
%13 = load i32, i32* @AF_NETLINK
%14 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %3
%16 = bitcast %struct.sockaddr_nl* %2 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* %3
%21 = call i32 @nl_close(i32 %20)
store i32 -1, i32* %1
br label %24
22: ; preds = %11
%23 = load i32, i32* %3
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 @nl_open() {
%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 %17, label %8
8: ; preds = %0
%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 %6, %struct.sockaddr* %12, i32 4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i32 @nl_close(i32 %6)
br label %17
17: ; preds = %8, %0, %15
%18 = phi i32 [ -1, %15 ], [ -1, %0 ], [ %6, %8 ]
ret i32 %18
}