#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_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_GENERIC;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int nlsock_open(void) {
  int nlsock, rcvsz = 256 * 1024;
  struct sockaddr_nl nlsockaddr;
  if ((nlsock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC)) == -1) {
    perror("open NETLINK socket");
    exit(1);
  }
  if (setsockopt(nlsock, SOL_SOCKET, SO_RCVBUF, &rcvsz, sizeof rcvsz) == -1) {
    perror("set length receive buffer");
    exit(1);
  }
  memset(&nlsockaddr, 0, sizeof nlsockaddr);
  nlsockaddr.nl_family = AF_NETLINK;
  if (bind(nlsock, (struct sockaddr *)&nlsockaddr, sizeof nlsockaddr) == -1) {
    perror("bind NETLINK socket");
    close(nlsock);
    exit(1);
  }
  return nlsock;
}


x86 assembly (clang, -O0):
<nlsock_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x40000,-0x8(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11c1 <nlsock_open+0x51>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1204 <nlsock_open+0x94>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1258 <nlsock_open+0xe8>
lea    0xded(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlsock_open>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x40000,0xc(%rsp)
mov    0x2e50(%rip),%r15
mov    (%r15),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     120d <nlsock_open+0x9d>
mov    %eax,%ebx
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1216 <nlsock_open+0xa6>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     122c <nlsock_open+0xbc>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdec(%rip),%rdi
jmp    121d <nlsock_open+0xad>
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_GENERIC = global i32 0
@.str = [20 x i8] c"open NETLINK socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.1 = [26 x i8] c"set length receive buffer\00"
@.str.2 = [20 x i8] c"bind NETLINK socket\00"
define i32 @nlsock_open() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
store i32 262144, i32* %2
%4 = load i32, i32* @AF_NETLINK
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @NETLINK_GENERIC
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %1
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVBUF
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %2, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 4)
%23 = load i32, i32* @AF_NETLINK
%24 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %1
%26 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %1
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_GENERIC = global i32 0
@.str = [20 x i8] c"open NETLINK socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.1 = [26 x i8] c"set length receive buffer\00"
@.str.2 = [20 x i8] c"bind NETLINK socket\00"
define i32 @nlsock_open() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = bitcast i32* %1 to i8*
store i32 262144, i32* %1
%4 = bitcast %struct.sockaddr_nl* %2 to i8*
%5 = load i32, i32* @AF_NETLINK
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @NETLINK_GENERIC
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVBUF
%16 = call i32 @setsockopt(i32 %8, i32 %14, i32 %15, i32* %1, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = call i32 @memset(%struct.sockaddr_nl* %2, i32 0, i32 4)
%23 = load i32, i32* @AF_NETLINK
%24 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%26 = call i32 @bind(i32 %8, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @close(i32 %8)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
ret i32 %8
}