#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 {
  scalar_t__ nl_groups;
  int /*<<< orphan*/ nl_pid;
  scalar_t__ nl_pad;
  int /*<<< orphan*/ nl_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ NETLINK_ROUTE;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ getpid();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sysf_printf(char *, char *);
int nl_open(void) {
  int rc;
  int sock = -1;
  struct sockaddr_nl addr;
  sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  do {
    if ((sock < 0))
      sysf_printf("Error creating netlink socket", "");
  } while (0);
  addr.nl_family = AF_NETLINK;
  addr.nl_pad = 0;
  addr.nl_pid = getpid();
  addr.nl_groups = 0;
  rc = bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_nl));
  do {
    if ((rc < 0))
      sysf_printf("Error binding netlink socket", "");
  } while (0);
  return sock;
}


x86 assembly (clang, -O0):
<nl_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118f <nl_open+0x4f>
lea    0xe7d(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1040 <sysf_printf@plt>
jmp    1194 <nl_open+0x54>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x20,%edx
call   1050 <bind@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11eb <nl_open+0xab>
lea    0xe3f(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1040 <sysf_printf@plt>
jmp    11f0 <nl_open+0xb0>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_open>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e92(%rip),%r14
mov    (%r14),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1181 <nl_open+0x41>
lea    0xe8b(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1040 <sysf_printf@plt>
mov    (%r14),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x20,%edx
call   1050 <bind@plt>
test   %eax,%eax
jns    11cd <nl_open+0x8d>
lea    0xe5d(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1040 <sysf_printf@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i64, i32, i64, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [30 x i8] c"Error creating netlink socket\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [29 x i8] c"Error binding netlink socket\00"
define i32 @nl_open() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_nl
store i32 -1, i32* %2
%4 = load i32, i32* @AF_NETLINK
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @NETLINK_ROUTE
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %2
br label %8
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @sysf_printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
br label %14
14: ; preds = %13
%15 = load i32, i32* @AF_NETLINK
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 3
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 2
store i64 0, i64* %17
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i32 0, i32 0
store i64 0, i64* %20
%21 = load i32, i32* %2
%22 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 32)
store i32 %23, i32* %1
br label %24
24: ; preds = %14
%25 = load i32, i32* %1
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @sysf_printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
br label %30
30: ; preds = %29
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i64, i32, i64, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@NETLINK_ROUTE = global i32 0
@.str = [30 x i8] c"Error creating netlink socket\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [29 x i8] c"Error binding netlink socket\00"
define i32 @nl_open() {
%1 = alloca %struct.sockaddr_nl
%2 = bitcast %struct.sockaddr_nl* %1 to i8*
%3 = load i32, i32* @AF_NETLINK
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @NETLINK_ROUTE
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @sysf_printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* @AF_NETLINK
%12 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 2
store i64 0, i64* %13
%14 = tail call i32 (...) @getpid()
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i64 0, i64* %16
%17 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%18 = call i32 @bind(i32 %6, %struct.sockaddr* %17, i32 32)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @sysf_printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %10
ret i32 %6
}