#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*/ 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);
int open_netlink_mulicast(void) {
  int sock;
  struct sockaddr_nl addr;
  int group = 21;
  sock = socket(AF_NETLINK, SOCK_RAW, 31);
  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_mulicast>:
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    $0x1f,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11af <open_netlink_mulicast+0x4f>
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <open_netlink_mulicast+0xf0>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e12(%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    1208 <open_netlink_mulicast+0xa8>
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <open_netlink_mulicast+0xf0>
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
mov    $0x10e,%esi
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    124a <open_netlink_mulicast+0xea>
lea    0xddd(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <open_netlink_mulicast+0xf0>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_netlink_mulicast>:
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    $0x1f,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fc <open_netlink_mulicast+0x9c>
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     120a <open_netlink_mulicast+0xaa>
mov    0x2e13(%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    121b <open_netlink_mulicast+0xbb>
lea    0xe06(%rip),%rdi
jmp    1211 <open_netlink_mulicast+0xb1>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
jmp    121b <open_netlink_mulicast+0xbb>
lea    0xdfe(%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
@.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_mulicast() {
%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 = call i32 @socket(i32 %5, i32 %6, i32 31)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %2
store i32 %12, i32* %1
br label %35
13: ; preds = %0
%14 = bitcast %struct.sockaddr_nl* %3 to i8*
%15 = call i32 @memset(i8* %14, i32 0, i32 8)
%16 = load i32, i32* @AF_NETLINK
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %16, i32* %17
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = 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 %35
26: ; preds = %13
%27 = load i32, i32* %2
%28 = load i32, i32* @NETLINK_ADD_MEMBERSHIP
%29 = call i64 @setsockopt(i32 %27, i32 270, i32 %28, i32* %4, i32 4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = 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 %35
33: ; preds = %26
%34 = load i32, i32* %2
store i32 %34, i32* %1
br label %35
35: ; preds = %33, %31, %24, %10
%36 = load i32, i32* %1
ret i32 %36
}

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