#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*/ 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) {
  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,-0x10(%rbp)
cmpl   $0x0,-0x10(%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    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x18(%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    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f9 <create_nl_socket+0xa9>
mov    -0x10(%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 %struct.sockaddr_nl
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_NETLINK
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* %4
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %32
16: ; preds = %2
%17 = call i32 @memset(%struct.sockaddr_nl* %7, i32 0, i32 8)
%18 = load i32, i32* @AF_NETLINK
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %5
%21 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %6
%23 = bitcast %struct.sockaddr_nl* %7 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 8)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %16
br label %29
27: ; preds = %16
%28 = load i32, i32* %6
store i32 %28, i32* %3
br label %32
29: ; preds = %26
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %3
br label %32
32: ; preds = %29, %27, %14
%33 = load i32, i32* %3
ret i32 %33
}

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
}