#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 nl_groups;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_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 /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int create_nl_socket(int protocol, int groups) {
  socklen_t addr_len;
  int fd;
  struct sockaddr_nl local;
  fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  if (fd < 0) {
    perror("socket");
    return -1;
  }
  memset(&local, 0, sizeof(local));
  local.nl_family = AF_NETLINK;
  local.nl_groups = groups;
  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    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119d <create_nl_socket+0x4d>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <create_nl_socket+0xa9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11df <create_nl_socket+0x8f>
jmp    11ea <create_nl_socket+0x9a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <create_nl_socket+0xa9>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_nl_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%edx
mov    0x2e7d(%rip),%r15
mov    (%r15),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1070 <socket@plt>
test   %eax,%eax
js     11c4 <create_nl_socket+0x74>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
test   %rax,%rax
jns    11b7 <create_nl_socket+0x67>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
jmp    11b2 <create_nl_socket+0x62>

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
define i32 @create_nl_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_nl
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_NETLINK
%10 = load i32, i32* @SOCK_RAW
%11 = load i32, i32* %4
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %33
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_nl* %8, i32 0, i32 8)
%19 = load i32, i32* @AF_NETLINK
%20 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %8, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %8, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %7
%24 = bitcast %struct.sockaddr_nl* %8 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %17
br label %30
28: ; preds = %17
%29 = load i32, i32* %7
store i32 %29, i32* %3
br label %33
30: ; preds = %27
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %33
33: ; preds = %30, %28, %15
%34 = load i32, i32* %3
ret i32 %34
}

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