#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_groups;
  int /*<<< orphan*/ nl_pid;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ __u32;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ PF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int netlink_open(int proto, __u32 groups) {
  int sock;
  struct sockaddr_nl addr;
  if ((sock = socket(PF_NETLINK, SOCK_RAW, proto)) < 0) {
    fprintf(stderr, "socket(PF_NETLINK): %s\n", strerror(errno));
    return sock;
  }
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_pid = getpid();
  addr.nl_groups = groups;
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    fprintf(stderr, "bind(): %s\n", strerror(errno));
    return sock;
  }
  return sock;
}


x86 assembly (clang, -O0):
<netlink_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11cb <netlink_open+0x6b>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe45(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <netlink_open+0xef>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1249 <netlink_open+0xe9>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xddf(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <netlink_open+0xef>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%edx
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ea <netlink_open+0x8a>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
jns    1212 <netlink_open+0xb2>
mov    0x2dff(%rip),%rax
mov    (%rax),%ebp
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe30(%rip),%rsi
jmp    1208 <netlink_open+0xa8>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf8(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"socket(PF_NETLINK): %s\0A\00"
@errno = global i32 0
@AF_NETLINK = global i32 0
@.str.1 = [12 x i8] c"bind(): %s\0A\00"
define i32 @netlink_open(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* @PF_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 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %39
19: ; preds = %2
%20 = call i32 @memset(%struct.sockaddr_nl* %7, i32 0, i32 12)
%21 = load i32, i32* @AF_NETLINK
%22 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 (...) @getpid()
%24 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* %5
%26 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %7, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %6
%28 = bitcast %struct.sockaddr_nl* %7 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %19
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %39
37: ; preds = %19
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %31, %13
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"socket(PF_NETLINK): %s\0A\00"
@errno = global i32 0
@AF_NETLINK = global i32 0
@.str.1 = [12 x i8] c"bind(): %s\0A\00"
define i32 @netlink_open(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_nl
%4 = bitcast %struct.sockaddr_nl* %3 to i8*
%5 = load i32, i32* @PF_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 %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %29
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 12)
%16 = load i32, i32* @AF_NETLINK
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i32 %1, i32* %20
%21 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%22 = call i64 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %14, %24, %9
ret i32 %7
}