#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_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int SOCK_CLOEXEC;
int SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
void check_netlink(void) {
  int sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, 0);
  if (sock == -1) {
    printf("GOOD: I cannot connect to netlink socket. Network utilities such "
           "as iproute2 will not work in the sandbox.\n");
    return;
  }
  struct sockaddr_nl local;
  memset(&local, 0, sizeof(local));
  local.nl_family = AF_NETLINK;
  local.nl_groups = 0;
  if (bind(sock, (struct sockaddr *)&local, sizeof(local)) < 0) {
    printf("GOOD: I cannot connect to netlink socket. Network utilities such "
           "as iproute2 will not work in the sandbox.\n");
    close(sock);
    return;
  }
  close(sock);
  printf("MAYBE: I can connect to netlink socket. Network utilities such as "
         "iproute2 will work fine in the sandbox. ");
  printf("You can use \"--protocol\" to disable the socket.\n");
}


x86 assembly (clang, -O0):
<check_netlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e7f(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1198 <check_netlink+0x48>
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
jmp    1210 <check_netlink+0xc0>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11f0 <check_netlink+0xa0>
lea    0xe22(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
jmp    1210 <check_netlink+0xc0>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
lea    0xecc(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_netlink>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r15
mov    (%r15),%edi
mov    0x2e66(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11e9 <check_netlink+0x89>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     11fe <check_netlink+0x9e>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
jmp    1211 <check_netlink+0xb1>
lea    0xeab(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <puts@plt>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str = [108 x i8] c"GOOD: I cannot connect to netlink socket. Network utilities such as iproute2 will not work in the sandbox.\0A\00"
@.str.1 = [107 x i8] c"MAYBE: I can connect to netlink socket. Network utilities such as iproute2 will work fine in the sandbox. \00"
@.str.2 = [49 x i8] c"You can use \22--protocol\22 to disable the socket.\0A\00"
define void @check_netlink() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_nl
%3 = load i32, i32* @AF_NETLINK
%4 = load i32, i32* @SOCK_RAW
%5 = load i32, i32* @SOCK_CLOEXEC
%6 = or i32 %4, %5
%7 = call i32 @socket(i32 %3, i32 %6, i32 0)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str, i64 0, i64 0))
br label %30
12: ; preds = %0
%13 = call i32 @memset(%struct.sockaddr_nl* %2, i32 0, i32 16)
%14 = load i32, i32* @AF_NETLINK
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %2, i32 0, i32 0
store i64 0, i64* %16
%17 = load i32, i32* %1
%18 = bitcast %struct.sockaddr_nl* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 16)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* %1
%24 = call i32 @close(i32 %23)
br label %30
25: ; preds = %12
%26 = load i32, i32* %1
%27 = call i32 @close(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([107 x i8], [107 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %25, %21, %10
ret void
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str.1 = [107 x i8] c"MAYBE: I can connect to netlink socket. Network utilities such as iproute2 will work fine in the sandbox. \00"
@str = [48 x i8] c"You can use \22--protocol\22 to disable the socket.\00"
@str.4 = [107 x i8] c"GOOD: I cannot connect to netlink socket. Network utilities such as iproute2 will not work in the sandbox.\00"
define void @check_netlink() {
%1 = alloca %struct.sockaddr_nl
%2 = load i32, i32* @AF_NETLINK
%3 = load i32, i32* @SOCK_RAW
%4 = load i32, i32* @SOCK_CLOEXEC
%5 = or i32 %4, %3
%6 = tail call i32 @socket(i32 %2, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([107 x i8], [107 x i8]* @str.4, i64 0, i64 0))
br label %27
10: ; preds = %0
%11 = bitcast %struct.sockaddr_nl* %1 to i8*
%12 = call i32 @memset(%struct.sockaddr_nl* %1, i32 0, i32 16)
%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 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %1, i64 0, i32 0
store i64 0, i64* %15
%16 = bitcast %struct.sockaddr_nl* %1 to %struct.sockaddr*
%17 = call i64 @bind(i32 %6, %struct.sockaddr* %16, i32 16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([107 x i8], [107 x i8]* @str.4, i64 0, i64 0))
%21 = call i32 @close(i32 %6)
br label %26
22: ; preds = %10
%23 = call i32 @close(i32 %6)
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([107 x i8], [107 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %22, %19
br label %27
27: ; preds = %26, %8
ret void
}