#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 *key;
  void *value;
  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 = {};
  __u64 last_good_key = (__u64)(unsigned long)current_key;
  attr.map_fd = map_fd;
  attr.key = last_good_key;
  for (int i = 0; i < max_num; i++) {
  get_next_key:
    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) {
      if (errno == ENOENT) {
        attr.key = last_good_key;
        goto get_next_key;
      }
      return -errno;
    }
    last_good_key = attr.key;
    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    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12a7 <bpf_map_load_multi+0x187>
jmp    1186 <bpf_map_load_multi+0x66>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
mov    $0x8,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     11f1 <bpf_map_load_multi+0xd1>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%eax
jne    11de <bpf_map_load_multi+0xbe>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <bpf_map_load_multi+0x18d>
xor    %eax,%eax
mov    0x2de1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <bpf_map_load_multi+0x18d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
mov    $0x8,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     1266 <bpf_map_load_multi+0x146>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%eax
jne    1253 <bpf_map_load_multi+0x133>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1186 <bpf_map_load_multi+0x66>
xor    %eax,%eax
mov    0x2d6c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <bpf_map_load_multi+0x18d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
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    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1175 <bpf_map_load_multi+0x55>
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    $0x28,%rsp
test   %edx,%edx
jle    11f2 <bpf_map_load_multi+0xe2>
mov    %r8,%rbx
mov    0x60(%rsp),%rbp
movslq %ecx,%rax
mov    %rax,0x20(%rsp)
movslq %r9d,%rax
mov    %rax,0x18(%rsp)
xor    %eax,%eax
mov    0x2e91(%rip),%r13
mov    0x2e92(%rip),%rcx
lea    0x8(%rsp),%r12
mov    0x2e8e(%rip),%r15
mov    %edx,0x14(%rsp)
mov    %eax,0x4(%rsp)
mov    %rbx,0x8(%rsp)
mov    0x0(%r13),%edi
mov    %rcx,%r14
xchg   %ax,%ax
mov    (%rcx),%esi
mov    %r12,%rdx
mov    $0x8,%ecx
call   1030 <syscall@plt>
test   %eax,%eax
jne    11f6 <bpf_map_load_multi+0xe6>
mov    %rbp,0x8(%rsp)
mov    0x0(%r13),%edi
mov    (%r15),%esi
mov    %r12,%rdx
mov    $0x8,%ecx
call   1030 <syscall@plt>
test   %eax,%eax
je     11d0 <bpf_map_load_multi+0xc0>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%eax
jne    1213 <bpf_map_load_multi+0x103>
mov    %r14,%rcx
mov    %rbx,0x8(%rsp)
mov    0x0(%r13),%edi
jmp    1170 <bpf_map_load_multi+0x60>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    0x20(%rsp),%rbx
add    0x18(%rsp),%rbp
mov    0x4(%rsp),%eax
add    $0x1,%eax
mov    0x14(%rsp),%edx
cmp    %edx,%eax
mov    %r14,%rcx
jne    115e <bpf_map_load_multi+0x4e>
jmp    1217 <bpf_map_load_multi+0x107>
xor    %edx,%edx
jmp    1217 <bpf_map_load_multi+0x107>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,%edx
neg    %edx
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%eax
cmove  0x4(%rsp),%edx
jmp    1217 <bpf_map_load_multi+0x107>
neg    %eax
mov    %eax,%edx
mov    %edx,%eax
add    $0x28,%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 i8*
%19 = alloca i32
%20 = 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
%21 = bitcast %union.bpf_attr* %17 to i8*
%22 = load i8*, i8** %10
%23 = ptrtoint i8* %22 to i64
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %18
%25 = load i32, i32* %9
%26 = bitcast %union.bpf_attr* %17 to i32*
store i32 %25, i32* %26
%27 = load i8*, i8** %18
%28 = bitcast %union.bpf_attr* %17 to i8**
store i8* %27, i8** %28
store i32 0, i32* %19
br label %29
29: ; preds = %90, %7
%30 = load i32, i32* %19
%31 = load i32, i32* %11
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %93
33: ; preds = %29
br label %34
34: ; preds = %71, %33
%35 = load i8*, i8** %13
%36 = ptrtoint i8* %35 to i64
%37 = inttoptr i64 %36 to i8*
%38 = bitcast %union.bpf_attr* %17 to i8**
store i8* %37, i8** %38
%39 = load i32, i32* @SYS_bpf
%40 = load i32, i32* @BPF_MAP_GET_NEXT_KEY
%41 = call i32 @syscall(i32 %39, i32 %40, %union.bpf_attr* %17, i32 8)
store i32 %41, i32* %20
%42 = load i32, i32* %20
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %34
%45 = load i32, i32* @errno
%46 = load i32, i32* @ENOENT
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %16
store i32 %49, i32* %8
br label %95
50: ; preds = %44
%51 = load i32, i32* @errno
%52 = sub nsw i32 0, %51
store i32 %52, i32* %8
br label %95
53: ; preds = %34
%54 = load i8*, i8** %13
%55 = ptrtoint i8* %54 to i64
%56 = inttoptr i64 %55 to i8*
%57 = bitcast %union.bpf_attr* %17 to i8**
store i8* %56, i8** %57
%58 = load i8*, i8** %15
%59 = ptrtoint i8* %58 to i64
%60 = inttoptr i64 %59 to i8*
%61 = bitcast %union.bpf_attr* %17 to i8**
store i8* %60, i8** %61
%62 = load i32, i32* @SYS_bpf
%63 = load i32, i32* @BPF_MAP_LOOKUP_ELEM
%64 = call i32 @syscall(i32 %62, i32 %63, %union.bpf_attr* %17, i32 8)
store i32 %64, i32* %20
%65 = load i32, i32* %20
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %77
67: ; preds = %53
%68 = load i32, i32* @errno
%69 = load i32, i32* @ENOENT
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = load i8*, i8** %18
%73 = bitcast %union.bpf_attr* %17 to i8**
store i8* %72, i8** %73
br label %34
74: ; preds = %67
%75 = load i32, i32* @errno
%76 = sub nsw i32 0, %75
store i32 %76, i32* %8
br label %95
77: ; preds = %53
%78 = bitcast %union.bpf_attr* %17 to i8**
%79 = load i8*, i8** %78
store i8* %79, i8** %18
%80 = load i32, i32* %12
%81 = load i8*, i8** %13
%82 = sext i32 %80 to i64
%83 = getelementptr i8, i8* %81, i64 %82
store i8* %83, i8** %13
%84 = load i32, i32* %14
%85 = load i8*, i8** %15
%86 = sext i32 %84 to i64
%87 = getelementptr i8, i8* %85, i64 %86
store i8* %87, i8** %15
%88 = load i32, i32* %16
%89 = add nsw i32 %88, 1
store i32 %89, i32* %16
br label %90
90: ; preds = %77
%91 = load i32, i32* %19
%92 = add nsw i32 %91, 1
store i32 %92, i32* %19
br label %29
93: ; preds = %29
%94 = load i32, i32* %16
store i32 %94, i32* %8
br label %95
95: ; preds = %93, %74, %50, %48
%96 = load i32, i32* %8
ret i32 %96
}

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 %50
12: ; preds = %7
%13 = sext i32 %3 to i64
%14 = sext i32 %5 to i64
br label %15
15: ; preds = %12, %45
%16 = phi i32 [ 0, %12 ], [ %48, %45 ]
%17 = phi i8* [ %6, %12 ], [ %47, %45 ]
%18 = phi i8* [ %4, %12 ], [ %46, %45 ]
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, %38
%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 %50
29: ; preds = %15, %38
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 %45, label %34
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = load i32, i32* @ENOENT
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %43
38: ; preds = %34
store i8* %18, i8** %10
%39 = load i32, i32* @SYS_bpf
%40 = load i32, i32* @BPF_MAP_GET_NEXT_KEY
%41 = call i32 @syscall(i32 %39, i32 %40, %union.bpf_attr* %8, i32 8)
%42 = icmp eq i32 %41, 0
br i1 %42, label %29, label %23
43: ; preds = %34
%44 = sub nsw i32 0, %35
br label %50
45: ; preds = %29
%46 = getelementptr i8, i8* %18, i64 %13
%47 = getelementptr i8, i8* %17, i64 %14
%48 = add nuw nsw i32 %16, 1
%49 = icmp eq i32 %48, %2
br i1 %49, label %50, label %15
50: ; preds = %45, %7, %23, %43
%51 = phi i32 [ %28, %23 ], [ %44, %43 ], [ 0, %7 ], [ %2, %45 ]
ret i32 %51
}