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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int netlink_init() {
  int fd, ret;
  struct sockaddr_nl s_nladdr;
  fd = socket(AF_NETLINK, SOCK_RAW, 17);
  if (fd < 0) {
    printf("ERROR: socket() failed\n");
    return -1;
  }
  memset(&s_nladdr, 0, sizeof(s_nladdr));
  s_nladdr.nl_family = AF_NETLINK;
  s_nladdr.nl_pad = 0;
  s_nladdr.nl_pid = getpid();
  printf("ieee80211 netlink_init(): app pid = %u\n", s_nladdr.nl_pid);
  s_nladdr.nl_groups = 1;
  ret = bind(fd, (struct sockaddr *)&s_nladdr, sizeof(s_nladdr));
  if (ret < 0) {
    printf("netlink_init(): bind failed\n");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<netlink_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    $0x11,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119b <netlink_init+0x4b>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <netlink_init+0xcf>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1060 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1219 <netlink_init+0xc9>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <netlink_init+0xcf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_init>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e70(%rip),%r15
mov    (%r15),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    $0x11,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11ef <netlink_init+0x8f>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x8(%rsp)
lea    0xe3e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x1,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
test   %eax,%eax
jns    1200 <netlink_init+0xa0>
lea    0xe3b(%rip),%rdi
jmp    11f6 <netlink_init+0x96>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32, i64, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [40 x i8] c"ieee80211 netlink_init(): app pid = %u\0A\00"
@str = [28 x i8] c"netlink_init(): bind failed\00"
@str.3 = [23 x i8] c"ERROR: socket() failed\00"
define i32 @netlink_init() {
%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 = tail call i32 @socket(i32 %3, i32 %4, i32 17)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.3, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 24)
%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 = call i32 (...) @getpid()
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i32 %14, i32* %15
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 1
store i32 1, i32* %17
%18 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%19 = call i32 @bind(i32 %5, %struct.sockaddr* %18, i32 24)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %9
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %9, %21, %7
%24 = phi i32 [ -1, %7 ], [ -1, %21 ], [ %5, %9 ]
ret i32 %24
}