#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;
struct sockaddr_nl {
  int nl_family;
  int /*<<< orphan*/ nl_groups;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ snl;

/* Variables and functions */
int AF_NETLINK;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getpid();
scalar_t__ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ ksft_print_msg(char *, int /*<<< orphan*/, int, ...);
scalar_t__ netlink_sock(int *, int /*<<< orphan*/ *, int);
int netlink_sock_bind(int *sock, uint32_t *seq, int proto, uint32_t groups) {
  struct sockaddr_nl snl = {};
  socklen_t addr_len;
  int ret = -1;
  snl.nl_family = AF_NETLINK;
  snl.nl_groups = groups;
  if (netlink_sock(sock, seq, proto)) {
    ksft_print_msg("%d[%u] "
                   "Failed to open xfrm netlink socket"
                   "\n",
                   getpid(), 1347);
    return -1;
  }
  if (bind(*sock, (struct sockaddr *)&snl, sizeof(snl)) < 0) {
    ksft_print_msg("%d[%u] "
                   "bind()"
                   ": %m"
                   "\n",
                   getpid(), 1352);
    goto out_close;
  }
  addr_len = sizeof(snl);
  if (getsockname(*sock, (struct sockaddr *)&snl, &addr_len) < 0) {
    ksft_print_msg("%d[%u] "
                   "getsockname()"
                   ": %m"
                   "\n",
                   getpid(), 1358);
    goto out_close;
  }
  if (addr_len != sizeof(snl)) {
    ksft_print_msg("%d[%u] "
                   "Wrong address length %d"
                   "\n",
                   getpid(), 1362, addr_len);
    goto out_close;
  }
  if (snl.nl_family != AF_NETLINK) {
    ksft_print_msg("%d[%u] "
                   "Wrong address family %d"
                   "\n",
                   getpid(), 1366, snl.nl_family);
    goto out_close;
  }
  return 0;
out_close:
  close(*sock);
  return ret;
}


x86 assembly (clang, -O0):
<netlink_sock_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movl   $0xffffffff,-0x30(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <netlink_sock@plt>
cmp    $0x0,%rax
je     11f1 <netlink_sock_bind+0x81>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe27(%rip),%rdi
mov    $0x543,%edx
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <netlink_sock_bind+0x18a>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1230 <netlink_sock_bind+0xc0>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe0c(%rip),%rdi
mov    $0x548,%edx
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
jmp    12e9 <netlink_sock_bind+0x179>
movl   $0x8,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <getsockname@plt>
cmp    $0x0,%rax
jge    1275 <netlink_sock_bind+0x105>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdda(%rip),%rdi
mov    $0x54e,%edx
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
jmp    12e9 <netlink_sock_bind+0x179>
movslq -0x2c(%rbp),%rax
cmp    $0x8,%rax
je     12a7 <netlink_sock_bind+0x137>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x2c(%rbp),%ecx
lea    0xdc2(%rip),%rdi
mov    $0x552,%edx
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
jmp    12e9 <netlink_sock_bind+0x179>
mov    -0x28(%rbp),%eax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%eax
je     12dd <netlink_sock_bind+0x16d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%ecx
lea    0xdac(%rip),%rdi
mov    $0x556,%edx
mov    $0x0,%al
call   1060 <ksft_print_msg@plt>
jmp    12e9 <netlink_sock_bind+0x179>
movl   $0x0,-0x4(%rbp)
jmp    12fa <netlink_sock_bind+0x18a>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_sock_bind>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e77(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %ecx,0x14(%rsp)
call   1080 <netlink_sock@plt>
test   %rax,%rax
je     11a7 <netlink_sock_bind+0x47>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6c(%rip),%rdi
mov    %eax,%esi
mov    $0x543,%edx
xor    %eax,%eax
call   1050 <ksft_print_msg@plt>
jmp    1264 <netlink_sock_bind+0x104>
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1208 <netlink_sock_bind+0xa8>
movl   $0x8,0xc(%rsp)
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <getsockname@plt>
test   %rax,%rax
js     121f <netlink_sock_bind+0xbf>
cmpl   $0x8,0xc(%rsp)
jne    123d <netlink_sock_bind+0xdd>
mov    0x10(%rsp),%ecx
xor    %eax,%eax
cmp    (%r14),%ecx
je     1269 <netlink_sock_bind+0x109>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x10(%rsp),%ecx
lea    0xe79(%rip),%rdi
mov    %eax,%esi
mov    $0x556,%edx
jmp    1256 <netlink_sock_bind+0xf6>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe15(%rip),%rdi
mov    %eax,%esi
mov    $0x548,%edx
jmp    1234 <netlink_sock_bind+0xd4>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe11(%rip),%rdi
mov    %eax,%esi
mov    $0x54e,%edx
xor    %eax,%eax
call   1050 <ksft_print_msg@plt>
jmp    125d <netlink_sock_bind+0xfd>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0xc(%rsp),%ecx
lea    0xe09(%rip),%rdi
mov    %eax,%esi
mov    $0x552,%edx
xor    %eax,%eax
call   1050 <ksft_print_msg@plt>
mov    (%rbx),%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@.str = [43 x i8] c"%d[%u] Failed to open xfrm netlink socket\0A\00"
@.str.1 = [19 x i8] c"%d[%u] bind(): %m\0A\00"
@.str.2 = [26 x i8] c"%d[%u] getsockname(): %m\0A\00"
@.str.3 = [32 x i8] c"%d[%u] Wrong address length %d\0A\00"
@.str.4 = [32 x i8] c"%d[%u] Wrong address family %d\0A\00"
define i32 @netlink_sock_bind(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_nl
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = bitcast %struct.sockaddr_nl* %10 to i8*
store i32 -1, i32* %12
%14 = load i32, i32* @AF_NETLINK
%15 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %9
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32*, i32** %6
%19 = load i32*, i32** %7
%20 = load i32, i32* %8
%21 = call i64 @netlink_sock(i32* %18, i32* %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %4
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %24, i32 1347)
store i32 -1, i32* %5
br label %68
26: ; preds = %4
%27 = load i32*, i32** %6
%28 = load i32, i32* %27
%29 = bitcast %struct.sockaddr_nl* %10 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 8)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 (...) @getpid()
%34 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 1352)
br label %63
35: ; preds = %26
store i32 8, i32* %11
%36 = load i32*, i32** %6
%37 = load i32, i32* %36
%38 = bitcast %struct.sockaddr_nl* %10 to %struct.sockaddr*
%39 = call i64 @getsockname(i32 %37, %struct.sockaddr* %38, i32* %11)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 (...) @getpid()
%43 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 1358)
br label %63
44: ; preds = %35
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = icmp ne i64 %46, 8
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 (...) @getpid()
%50 = load i32, i32* %11
%51 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 1362, i32 %50)
br label %63
52: ; preds = %44
%53 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 0
%54 = load i32, i32* %53
%55 = load i32, i32* @AF_NETLINK
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = call i32 (...) @getpid()
%59 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %58, i32 1366, i32 %60)
br label %63
62: ; preds = %52
store i32 0, i32* %5
br label %68
63: ; preds = %57, %48, %41, %32
%64 = load i32*, i32** %6
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %12
store i32 %67, i32* %5
br label %68
68: ; preds = %63, %62, %23
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_NETLINK = global i32 0
@.str = [43 x i8] c"%d[%u] Failed to open xfrm netlink socket\0A\00"
@.str.1 = [19 x i8] c"%d[%u] bind(): %m\0A\00"
@.str.2 = [26 x i8] c"%d[%u] getsockname(): %m\0A\00"
@.str.3 = [32 x i8] c"%d[%u] Wrong address length %d\0A\00"
@.str.4 = [32 x i8] c"%d[%u] Wrong address family %d\0A\00"
define i32 @netlink_sock_bind(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to %struct.sockaddr_nl*
%7 = alloca i32
%8 = bitcast i64* %5 to i8*
%9 = bitcast i32* %7 to i8*
%10 = load i32, i32* @AF_NETLINK
%11 = bitcast i64* %5 to i32*
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %6, i64 0, i32 1
store i32 %3, i32* %12
%13 = tail call i64 @netlink_sock(i32* %0, i32* %1, i32 %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %4
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %16, i32 1347)
br label %51
18: ; preds = %4
%19 = load i32, i32* %0
%20 = bitcast i64* %5 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 1352)
br label %48
26: ; preds = %18
store i32 8, i32* %7
%27 = load i32, i32* %0
%28 = call i64 @getsockname(i32 %27, %struct.sockaddr* %20, i32* %7)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 1358)
br label %48
33: ; preds = %26
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 8
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = call i32 (...) @getpid()
%38 = load i32, i32* %7
%39 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 1362, i32 %38)
br label %48
40: ; preds = %33
%41 = load i32, i32* %11
%42 = load i32, i32* @AF_NETLINK
%43 = icmp eq i32 %41, %42
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = call i32 (...) @getpid()
%46 = load i32, i32* %11
%47 = call i32 (i8*, i32, i32, ...) @ksft_print_msg(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %45, i32 1366, i32 %46)
br label %48
48: ; preds = %44, %36, %30, %23
%49 = load i32, i32* %0
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %40, %48, %15
%52 = phi i32 [ -1, %15 ], [ -1, %48 ], [ 0, %40 ]
ret i32 %52
}