#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 */
union bpf_attr {
  void *value;
  void *key;
  int /*<<< orphan*/ map_fd;
};
typedef int /*<<< orphan*/ attr;
typedef void *__u64;
typedef int /*<<< orphan*/ __u32;

/* Variables and functions */
int /*<<< orphan*/ BPF_MAP_GET_NEXT_KEY;
int /*<<< orphan*/ BPF_MAP_LOOKUP_ELEM;
int ENOENT;
int /*<<< orphan*/ SYS_bpf;
int errno;
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, union bpf_attr *, int);
int bpf_map_load_multi(__u32 map_fd, void *current_key, int max_num,
                       int key_stride, void *keys_out, int value_stride,
                       void *values_out) {
  int count = 0;
  union bpf_attr attr = {};
  attr.map_fd = map_fd;
  attr.key = (__u64)(unsigned long)current_key;
  for (int i = 0; i < max_num; i++) {
    attr.value = (__u64)(unsigned long)keys_out;
    int rc = syscall(SYS_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
    if (rc != 0) {
      if (errno == ENOENT) {
        return count;
      }
      return -errno;
    }
    attr.key = (__u64)(unsigned long)keys_out;
    attr.value = (__u64)(unsigned long)values_out;
    rc = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
    if (rc != 0) {
      return -errno;
    }
    keys_out += key_stride;
    values_out += value_stride;
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<bpf_map_load_multi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    126d <bpf_map_load_multi+0x14d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
mov    $0x8,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     11e4 <bpf_map_load_multi+0xc4>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
jne    11d1 <bpf_map_load_multi+0xb1>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <bpf_map_load_multi+0x153>
xor    %eax,%eax
mov    0x2dee(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <bpf_map_load_multi+0x153>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
mov    $0x8,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1234 <bpf_map_load_multi+0x114>
xor    %eax,%eax
mov    0x2d9e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <bpf_map_load_multi+0x153>
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x10(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    116d <bpf_map_load_multi+0x4d>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_map_load_multi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    11a9 <bpf_map_load_multi+0x99>
mov    %r8,%rbx
mov    %edx,%r15d
mov    0x50(%rsp),%rbp
movslq %ecx,%rax
mov    %rax,0x10(%rsp)
movslq %r9d,%rax
mov    %rax,0x8(%rsp)
xor    %r12d,%r12d
mov    0x2e8d(%rip),%r13
mov    %rsp,%r14
xchg   %ax,%ax
mov    %rbx,(%rsp)
mov    0x0(%r13),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
mov    $0x8,%ecx
call   1030 <syscall@plt>
test   %eax,%eax
jne    11ae <bpf_map_load_multi+0x9e>
mov    %rbp,(%rsp)
mov    0x0(%r13),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
mov    $0x8,%ecx
call   1030 <syscall@plt>
test   %eax,%eax
jne    11cc <bpf_map_load_multi+0xbc>
add    0x10(%rsp),%rbx
add    0x8(%rsp),%rbp
add    $0x1,%r12d
cmp    %r12d,%r15d
jne    1150 <bpf_map_load_multi+0x40>
jmp    11d9 <bpf_map_load_multi+0xc9>
xor    %r15d,%r15d
jmp    11d9 <bpf_map_load_multi+0xc9>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
mov    %eax,%r15d
neg    %r15d
cmp    (%rcx),%eax
cmove  %r12d,%r15d
jmp    11d9 <bpf_map_load_multi+0xc9>
mov    0x2df5(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.bpf_attr = type { i8* }
@SYS_bpf = global i32 0
@BPF_MAP_GET_NEXT_KEY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@BPF_MAP_LOOKUP_ELEM = global i32 0
define i32 @bpf_map_load_multi(i32 %0, i8* %1, i32 %2, i32 %3, i8* %4, i32 %5, i8* %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca %union.bpf_attr
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i32 %5, i32* %14
store i8* %6, i8** %15
store i32 0, i32* %16
%20 = bitcast %union.bpf_attr* %17 to i8*
%21 = load i32, i32* %9
%22 = bitcast %union.bpf_attr* %17 to i32*
store i32 %21, i32* %22
%23 = load i8*, i8** %10
%24 = ptrtoint i8* %23 to i64
%25 = inttoptr i64 %24 to i8*
%26 = bitcast %union.bpf_attr* %17 to i8**
store i8* %25, i8** %26
store i32 0, i32* %18
br label %27
27: ; preds = %78, %7
%28 = load i32, i32* %18
%29 = load i32, i32* %11
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %81
31: ; preds = %27
%32 = load i8*, i8** %13
%33 = ptrtoint i8* %32 to i64
%34 = inttoptr i64 %33 to i8*
%35 = bitcast %union.bpf_attr* %17 to i8**
store i8* %34, i8** %35
%36 = load i32, i32* @SYS_bpf
%37 = load i32, i32* @BPF_MAP_GET_NEXT_KEY
%38 = call i32 @syscall(i32 %36, i32 %37, %union.bpf_attr* %17, i32 8)
store i32 %38, i32* %19
%39 = load i32, i32* %19
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %31
%42 = load i32, i32* @errno
%43 = load i32, i32* @ENOENT
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i32, i32* %16
store i32 %46, i32* %8
br label %83
47: ; preds = %41
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
store i32 %49, i32* %8
br label %83
50: ; preds = %31
%51 = load i8*, i8** %13
%52 = ptrtoint i8* %51 to i64
%53 = inttoptr i64 %52 to i8*
%54 = bitcast %union.bpf_attr* %17 to i8**
store i8* %53, i8** %54
%55 = load i8*, i8** %15
%56 = ptrtoint i8* %55 to i64
%57 = inttoptr i64 %56 to i8*
%58 = bitcast %union.bpf_attr* %17 to i8**
store i8* %57, i8** %58
%59 = load i32, i32* @SYS_bpf
%60 = load i32, i32* @BPF_MAP_LOOKUP_ELEM
%61 = call i32 @syscall(i32 %59, i32 %60, %union.bpf_attr* %17, i32 8)
store i32 %61, i32* %19
%62 = load i32, i32* %19
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %50
%65 = load i32, i32* @errno
%66 = sub nsw i32 0, %65
store i32 %66, i32* %8
br label %83
67: ; preds = %50
%68 = load i32, i32* %12
%69 = load i8*, i8** %13
%70 = sext i32 %68 to i64
%71 = getelementptr i8, i8* %69, i64 %70
store i8* %71, i8** %13
%72 = load i32, i32* %14
%73 = load i8*, i8** %15
%74 = sext i32 %72 to i64
%75 = getelementptr i8, i8* %73, i64 %74
store i8* %75, i8** %15
%76 = load i32, i32* %16
%77 = add nsw i32 %76, 1
store i32 %77, i32* %16
br label %78
78: ; preds = %67
%79 = load i32, i32* %18
%80 = add nsw i32 %79, 1
store i32 %80, i32* %18
br label %27
81: ; preds = %27
%82 = load i32, i32* %16
store i32 %82, i32* %8
br label %83
83: ; preds = %81, %64, %47, %45
%84 = load i32, i32* %8
ret i32 %84
}

LLVM IR (-O2):
%union.bpf_attr = type { i8* }
@SYS_bpf = global i32 0
@BPF_MAP_GET_NEXT_KEY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@BPF_MAP_LOOKUP_ELEM = global i32 0
define i32 @bpf_map_load_multi(i32 %0, i8* %1, i32 %2, i32 %3, i8* %4, i32 %5, i8* %6) {
%8 = alloca %union.bpf_attr
%9 = bitcast %union.bpf_attr* %8 to i8*
%10 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %8, i64 0, i32 0
%11 = icmp sgt i32 %2, 0
br i1 %11, label %12, label %42
12: ; preds = %7
%13 = sext i32 %3 to i64
%14 = sext i32 %5 to i64
br label %15
15: ; preds = %12, %37
%16 = phi i32 [ 0, %12 ], [ %40, %37 ]
%17 = phi i8* [ %6, %12 ], [ %39, %37 ]
%18 = phi i8* [ %4, %12 ], [ %38, %37 ]
store i8* %18, i8** %10
%19 = load i32, i32* @SYS_bpf
%20 = load i32, i32* @BPF_MAP_GET_NEXT_KEY
%21 = call i32 @syscall(i32 %19, i32 %20, %union.bpf_attr* %8, i32 8)
%22 = icmp eq i32 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %15
%24 = load i32, i32* @errno
%25 = load i32, i32* @ENOENT
%26 = icmp eq i32 %24, %25
%27 = sub nsw i32 0, %24
%28 = select i1 %26, i32 %16, i32 %27
br label %42
29: ; preds = %15
store i8* %17, i8** %10
%30 = load i32, i32* @SYS_bpf
%31 = load i32, i32* @BPF_MAP_LOOKUP_ELEM
%32 = call i32 @syscall(i32 %30, i32 %31, %union.bpf_attr* %8, i32 8)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = sub nsw i32 0, %35
br label %42
37: ; preds = %29
%38 = getelementptr i8, i8* %18, i64 %13
%39 = getelementptr i8, i8* %17, i64 %14
%40 = add nuw nsw i32 %16, 1
%41 = icmp eq i32 %40, %2
br i1 %41, label %42, label %15
42: ; preds = %37, %7, %23, %34
%43 = phi i32 [ %28, %23 ], [ %36, %34 ], [ 0, %7 ], [ %2, %37 ]
ret i32 %43
}