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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ADD_MEMBERSHIP;
int /*<<< orphan*/ NETLINK_USERSOCK;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsockopt(int, int, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_netlink(void) {
  int sock;
  struct sockaddr_nl addr;
  int group = 21;
  sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_USERSOCK);
  if (sock < 0) {
    printf("sock < 0.\n");
    return sock;
  }
  memset((void *)&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_pid = getpid();
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    printf("bind < 0.\n");
    return -1;
  }
  if (setsockopt(sock, 270, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group)) <
      0) {
    printf("setsockopt < 0\n");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_netlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x15,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b3 <open_netlink+0x53>
lea    0xe5d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <open_netlink+0xf4>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    120c <open_netlink+0xac>
lea    0xe10(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <open_netlink+0xf4>
mov    -0x8(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    $0x10e,%esi
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    124e <open_netlink+0xee>
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <open_netlink+0xf4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_netlink>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x15,0x4(%rsp)
mov    0x2e60(%rip),%r15
mov    (%r15),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1200 <open_netlink+0xa0>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     120e <open_netlink+0xae>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10e,%esi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    121f <open_netlink+0xbf>
lea    0xe02(%rip),%rdi
jmp    1215 <open_netlink+0xb5>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
jmp    121f <open_netlink+0xbf>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_USERSOCK = global i32 0
@NETLINK_ADD_MEMBERSHIP = global i32 0
@str = [15 x i8] c"setsockopt < 0\00"
@str.3 = [10 x i8] c"bind < 0.\00"
@str.4 = [10 x i8] c"sock < 0.\00"
define i32 @open_netlink() {
%1 = alloca %struct.sockaddr_nl
%2 = alloca i32
%3 = bitcast %struct.sockaddr_nl* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 21, i32* %2
%5 = load i32, i32* @AF_NETLINK
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @NETLINK_USERSOCK
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
br label %29
12: ; preds = %0
%13 = call i32 @memset(i8* %3, i32 0, i32 8)
%14 = load i32, i32* @AF_NETLINK
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = call i32 (...) @getpid()
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%19 = call i64 @bind(i32 %8, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.3, i64 0, i64 0))
br label %29
23: ; preds = %12
%24 = load i32, i32* @NETLINK_ADD_MEMBERSHIP
%25 = call i64 @setsockopt(i32 %8, i32 270, i32 %24, i32* %2, i32 4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %23, %27, %21, %10
%30 = phi i32 [ %8, %10 ], [ -1, %21 ], [ -1, %27 ], [ %8, %23 ]
ret i32 %30
}