#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 */
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int SOCK_CLOEXEC;
int SOCK_RAW;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ randomize_buffer(int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int, int);
int netlink_sock(int *sock, uint32_t *seq_nr, int proto) {
  if (*sock > 0) {
    seq_nr++;
    return 0;
  }
  *sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, proto);
  if (*sock <= 0) {
    ksft_print_msg("%d[%u] "
                   "socket(AF_NETLINK)"
                   ": %m"
                   "\n",
                   getpid(), 163);
    return -1;
  }
  randomize_buffer(seq_nr, sizeof(*seq_nr));
  return 0;
}


x86 assembly (clang, -O0):
<netlink_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1178 <netlink_sock+0x38>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    11eb <netlink_sock+0xab>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1060 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jg     11d6 <netlink_sock+0x96>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe40(%rip),%rdi
mov    $0xa3,%edx
call   1050 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <netlink_sock+0xab>
mov    -0x18(%rbp),%rdi
mov    $0x4,%esi
call   1040 <randomize_buffer@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_sock>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
cmpl   $0x0,(%rdi)
jg     11a5 <netlink_sock+0x65>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    0x2e7f(%rip),%rax
mov    0x2e88(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <socket@plt>
mov    %eax,(%rbx)
test   %eax,%eax
jle    1186 <netlink_sock+0x46>
mov    %r14,%rdi
mov    $0x4,%esi
call   1040 <randomize_buffer@plt>
jmp    11a5 <netlink_sock+0x65>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6c(%rip),%rdi
mov    %eax,%esi
mov    $0xa3,%edx
call   1050 <ksft_print_msg@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str = [31 x i8] c"%d[%u] socket(AF_NETLINK): %m\0A\00"
define i32 @netlink_sock(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%8 = load i32*, i32** %5
%9 = load i32, i32* %8
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32*, i32** %6
%13 = getelementptr inbounds i32, i32* %12, i32 1
store i32* %13, i32** %6
store i32 0, i32* %4
br label %31
14: ; preds = %3
%15 = load i32, i32* @AF_NETLINK
%16 = load i32, i32* @SOCK_RAW
%17 = load i32, i32* @SOCK_CLOEXEC
%18 = or i32 %16, %17
%19 = load i32, i32* %7
%20 = call i32 @socket(i32 %15, i32 %18, i32 %19)
%21 = load i32*, i32** %5
store i32 %20, i32* %21
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 (...) @getpid()
%27 = call i32 @ksft_print_msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %26, i32 163)
store i32 -1, i32* %4
br label %31
28: ; preds = %14
%29 = load i32*, i32** %6
%30 = call i32 @randomize_buffer(i32* %29, i32 4)
store i32 0, i32* %4
br label %31
31: ; preds = %28, %25, %11
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@AF_NETLINK = global i32 0
@SOCK_RAW = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str = [31 x i8] c"%d[%u] socket(AF_NETLINK): %m\0A\00"
define i32 @netlink_sock(i32* nocapture %0, i32* %1, i32 %2) {
%4 = load i32, i32* %0
%5 = icmp sgt i32 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = load i32, i32* @AF_NETLINK
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @SOCK_CLOEXEC
%10 = or i32 %9, %8
%11 = tail call i32 @socket(i32 %7, i32 %10, i32 %2)
store i32 %11, i32* %0
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 @ksft_print_msg(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %14, i32 163)
br label %18
16: ; preds = %6
%17 = tail call i32 @randomize_buffer(i32* %1, i32 4)
br label %18
18: ; preds = %3, %16, %13
%19 = phi i32 [ -1, %13 ], [ 0, %16 ], [ 0, %3 ]
ret i32 %19
}