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

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ getpid();
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int set_nlkernel(int sfd, unsigned int nl_groups) {
  struct sockaddr_nl addr;
  do {
    memset(&addr, 0, sizeof(typeof(*&addr)));
  } while (0);
  addr.nl_family = AF_NETLINK;
  addr.nl_groups = nl_groups;
  addr.nl_pid = getpid();
  if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind()");
    return -1;
  }
  addr.nl_groups = 0;
  addr.nl_pid = 0;
  if (connect(sfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect()");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_nlkernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11be <set_nlkernel+0x6e>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <set_nlkernel+0xb7>
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1200 <set_nlkernel+0xb0>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <set_nlkernel+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_nlkernel>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rsp,%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x18,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11d5 <set_nlkernel+0x85>
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     11de <set_nlkernel+0x8e>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe24(%rip),%rdi
jmp    11e5 <set_nlkernel+0x95>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11c7 <set_nlkernel+0x77>

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

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i64, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@.str = [7 x i8] c"bind()\00"
@.str.1 = [10 x i8] c"connect()\00"
define i32 @set_nlkernel(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_nl
%4 = bitcast %struct.sockaddr_nl* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 24)
%6 = load i32, i32* @AF_NETLINK
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i32 %1, i32* %8
%9 = call i64 (...) @getpid()
%10 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
store i64 %9, i64* %10
%11 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%12 = call i64 @bind(i32 %0, %struct.sockaddr* %11, i32 24)
%13 = icmp slt i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %2
store i32 0, i32* %8
store i64 0, i64* %10
%15 = call i64 @connect(i32 %0, %struct.sockaddr* %11, i32 24)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14, %2
%18 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %14 ]
%19 = call i32 @perror(i8* %18)
br label %20
20: ; preds = %17, %14
%21 = phi i32 [ 0, %14 ], [ -1, %17 ]
ret i32 %21
}