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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_GENERIC;
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 create_nl_socket(void) {
  int fd;
  struct sockaddr_nl local;
  fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  if (fd < 0)
    return -1;
  memset(&local, 0, sizeof(local));
  local.nl_family = AF_NETLINK;
  if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
    goto error;
  return fd;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<create_nl_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1181 <create_nl_socket+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <create_nl_socket+0x97>
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    11bd <create_nl_socket+0x7d>
jmp    11c8 <create_nl_socket+0x88>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <create_nl_socket+0x97>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_nl_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e92(%rip),%r15
mov    (%r15),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11aa <create_nl_socket+0x6a>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11a3 <create_nl_socket+0x63>
mov    %ebp,%ebx
jmp    11aa <create_nl_socket+0x6a>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_GENERIC = global i32 0
define i32 @create_nl_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_GENERIC
%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 %25
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 %20
19: ; preds = %11
br label %22
20: ; preds = %11
%21 = load i32, i32* %2
store i32 %21, i32* %1
br label %25
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %1
br label %25
25: ; preds = %22, %20, %10
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_GENERIC = global i32 0
define i32 @create_nl_socket() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = load i32, i32* @AF_NETLINK
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @NETLINK_GENERIC
%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 @close(i32 %6)
br label %17
17: ; preds = %8, %0, %15
%18 = phi i32 [ -1, %15 ], [ -1, %0 ], [ %6, %8 ]
ret i32 %18
}