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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int RTMGRP_IPV4_IFADDR;
int RTMGRP_IPV6_IFADDR;
int RTMGRP_LINK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int run() {
  fd_set rfds;
  int retval;
  struct sockaddr_nl addr;
  int nl_socket = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  printf("nl_sockdt %d\n", nl_socket);
  if (nl_socket < 0) {
    printf("error opening netlink socket!\n");
    return -1;
  }
  int enable = 1;
  if (setsockopt(nl_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) <
      0) {
    printf("setsockopt for reuse failed\n");
    return -2;
  }
  memset((void *)&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  addr.nl_pid = getpid();
  addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
  printf("nl_pid = getpid = %d\n", addr.nl_pid);
  int ret = bind(nl_socket, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    printf("bind ret value %d\n", ret);
    close(nl_socket);
    printf("failed to bind to netlink socket!");
    return -3;
  }
  close(nl_socket);
  return 0;
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11d0 <run+0x60>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <run+0x164>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    121f <run+0xaf>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12d4 <run+0x164>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    0x2d93(%rip),%rcx
or     (%rcx),%eax
mov    0x2d72(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%esi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12c5 <run+0x155>
mov    -0x24(%rbp),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12d4 <run+0x164>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e2f(%rip),%r15
mov    (%r15),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
js     126d <run+0xed>
movl   $0x1,0x4(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     1280 <run+0x100>
lea    0x8(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    0x2dcf(%rip),%rcx
mov    0x2db8(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d95(%rip),%rcx
or     (%rcx),%edx
mov    %edx,0xc(%rsp)
lea    0xdc6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
js     1293 <run+0x113>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    12bd <run+0x13d>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    12bd <run+0x13d>
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ebp
jmp    12bd <run+0x13d>
lea    0xd8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd86(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xfffffffd,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [14 x i8] c"nl_sockdt %d\0A\00"
@.str.1 = [31 x i8] c"error opening netlink socket!\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [29 x i8] c"setsockopt for reuse failed\0A\00"
@RTMGRP_LINK = global i32 0
@RTMGRP_IPV4_IFADDR = global i32 0
@RTMGRP_IPV6_IFADDR = global i32 0
@.str.3 = [22 x i8] c"nl_pid = getpid = %d\0A\00"
@.str.4 = [19 x i8] c"bind ret value %d\0A\00"
@.str.5 = [34 x i8] c"failed to bind to netlink socket!\00"
define i32 @run() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_nl
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = load i32, i32* @AF_NETLINK
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @NETLINK_ROUTE
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %56
18: ; preds = %0
store i32 1, i32* %6
%19 = load i32, i32* %5
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i64 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %6, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %1
br label %56
26: ; preds = %18
%27 = bitcast %struct.sockaddr_nl* %4 to i8*
%28 = call i32 @memset(i8* %27, i32 0, i32 12)
%29 = load i32, i32* @AF_NETLINK
%30 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 2
store i32 %29, i32* %30
%31 = call i32 (...) @getpid()
%32 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @RTMGRP_LINK
%34 = load i32, i32* @RTMGRP_IPV4_IFADDR
%35 = or i32 %33, %34
%36 = load i32, i32* @RTMGRP_IPV6_IFADDR
%37 = or i32 %35, %36
%38 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %5
%43 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%44 = call i32 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %26
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 -3, i32* %1
br label %56
53: ; preds = %26
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
store i32 0, i32* %1
br label %56
56: ; preds = %53, %47, %24, %16
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [14 x i8] c"nl_sockdt %d\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@RTMGRP_LINK = global i32 0
@RTMGRP_IPV4_IFADDR = global i32 0
@RTMGRP_IPV6_IFADDR = global i32 0
@.str.3 = [22 x i8] c"nl_pid = getpid = %d\0A\00"
@.str.4 = [19 x i8] c"bind ret value %d\0A\00"
@.str.5 = [34 x i8] c"failed to bind to netlink socket!\00"
@str = [28 x i8] c"setsockopt for reuse failed\00"
@str.6 = [30 x i8] c"error opening netlink socket!\00"
define i32 @run() {
%1 = alloca %struct.sockaddr_nl
%2 = alloca i32
%3 = bitcast %struct.sockaddr_nl* %1 to i8*
%4 = load i32, i32* @AF_NETLINK
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @NETLINK_ROUTE
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
br label %44
12: ; preds = %0
%13 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i64 @setsockopt(i32 %7, i32 %14, i32 %15, i32* %2, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %42
20: ; preds = %12
%21 = call i32 @memset(i8* %3, i32 0, i32 12)
%22 = load i32, i32* @AF_NETLINK
%23 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 (...) @getpid()
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @RTMGRP_LINK
%27 = load i32, i32* @RTMGRP_IPV4_IFADDR
%28 = or i32 %27, %26
%29 = load i32, i32* @RTMGRP_IPV6_IFADDR
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %30, i32* %31
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%33 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%34 = call i32 @bind(i32 %7, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %20
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%38 = call i32 @close(i32 %7)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
br label %42
40: ; preds = %20
%41 = call i32 @close(i32 %7)
br label %42
42: ; preds = %36, %40, %18
%43 = phi i32 [ -2, %18 ], [ -3, %36 ], [ 0, %40 ]
br label %44
44: ; preds = %42, %10
%45 = phi i32 [ -1, %10 ], [ %43, %42 ]
ret i32 %45
}