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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ NETLINK_KOBJECT_UEVENT;
int O_NONBLOCK;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int netlink_create(void) {
  struct sockaddr_nl snl;
  int sockfd;
  int ret;
  memset(&snl, 0, sizeof(snl));
  snl.nl_family = AF_NETLINK;
  snl.nl_pid = getpid();
  snl.nl_groups = 1;
  errno = 0;
  sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK);
  if (sockfd >= 0) {
    ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
    if (ret < 0) {
      close(sockfd);
      sockfd = -1;
    }
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<netlink_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e14(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmpl   $0x0,-0x14(%rbp)
jl     123f <netlink_create+0xdf>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    123a <netlink_create+0xda>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    123f <netlink_create+0xdf>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_create>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0xc(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2e12(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    0x2dea(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2dff(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %ebx,%ebx
js     121d <netlink_create+0xbd>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jns    121d <netlink_create+0xbd>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@errno = global i64 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @netlink_create() {
%1 = alloca %struct.sockaddr_nl
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%5 = load i32, i32* @AF_NETLINK
%6 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i32 0, i32 2
store i32 %5, i32* %6
%7 = call i32 (...) @getpid()
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i32 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i32 0, i32 0
store i32 1, i32* %9
store i64 0, i64* @errno
%10 = load i32, i32* @PF_NETLINK
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* %2
%17 = load i32, i32* @F_GETFL
%18 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17)
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15, i32 %20)
%22 = load i32, i32* %2
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %0
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %2
br label %33
33: ; preds = %30, %24
br label %34
34: ; preds = %33, %0
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@errno = global i64 0
@PF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_KOBJECT_UEVENT = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @netlink_create() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 12)
%4 = load i32, i32* @AF_NETLINK
%5 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %4, i32* %5
%6 = call i32 (...) @getpid()
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 1, i32* %8
store i64 0, i64* @errno
%9 = load i32, i32* @PF_NETLINK
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @NETLINK_KOBJECT_UEVENT
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @F_GETFL
%15 = call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %14)
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %16, %15
%18 = call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %13, i32 %17)
%19 = icmp sgt i32 %12, -1
br i1 %19, label %20, label %26
20: ; preds = %0
%21 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %12, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @close(i32 %12)
br label %26
26: ; preds = %20, %24, %0
%27 = phi i32 [ -1, %24 ], [ %12, %20 ], [ %12, %0 ]
ret i32 %27
}