// https://syzkaller.appspot.com/bug?id=bab9168b8199dd72cb854044bf5fef64c1bf021e
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <linux/kvm.h>

#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len)               \
  *(type*)(addr) =                                                             \
      htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) |           \
            (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))

static long syz_kvm_setup_cpu(volatile long a0, volatile long a1,
                              volatile long a2, volatile long a3,
                              volatile long a4, volatile long a5,
                              volatile long a6, volatile long a7)
{
  return 0;
}

#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#ifndef __NR_mmap
#define __NR_mmap 192
#endif
#ifndef __NR_openat
#define __NR_openat 295
#endif
#ifndef __NR_perf_event_open
#define __NR_perf_event_open 336
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};

int main(void)
{
  syscall(__NR_mmap, 0x1ffff000, 0x1000, 0, 0x32, -1, 0);
  syscall(__NR_mmap, 0x20000000, 0x1000000, 7, 0x32, -1, 0);
  syscall(__NR_mmap, 0x21000000, 0x1000, 0, 0x32, -1, 0);
  intptr_t res = 0;
  memcpy((void*)0x20000040, "/dev/kvm\000", 9);
  res = syscall(__NR_openat, 0xffffff9c, 0x20000040, 0, 0);
  if (res != -1)
    r[0] = res;
  res = syscall(__NR_ioctl, (intptr_t)r[0], 0xae01, 0);
  if (res != -1)
    r[1] = res;
  *(uint32_t*)0x200000c0 = 8;
  *(uint32_t*)0x200000c4 = 0x20000080;
  memcpy((void*)0x20000080, "\xba\xa1\x00\xb0\x00\xee\xf3\x6c\xba\x21\x00\xec"
                            "\x66\xb9\x80\x00\x00\xc0\x0f\x32\x66\x35\x00\x10"
                            "\x00\x00\x0f\x30\xba\xd1\x04\xec\xc8\x00\x80\xd2"
                            "\x67\xd9\xf8\xf3\x0f\x1b\xb4\x29\x00\x0f\x20\xc0"
                            "\x66\x35\x20\x00\x00\x00\x0f\x22\xc0\x67\xf3\xaf",
         60);
  *(uint32_t*)0x200000c8 = 0x3c;
  syz_kvm_setup_cpu(-1, -1, 0x20000000, 0x200000c0, 1, 0, 0, 0);
  res = syscall(__NR_ioctl, (intptr_t)r[1], 0xae41, 0);
  if (res != -1)
    r[2] = res;
  *(uint32_t*)0x20000040 = 0;
  *(uint32_t*)0x20000044 = 0x70;
  *(uint8_t*)0x20000048 = 0;
  *(uint8_t*)0x20000049 = 0;
  *(uint8_t*)0x2000004a = 0;
  *(uint8_t*)0x2000004b = 0;
  *(uint32_t*)0x2000004c = 0;
  *(uint64_t*)0x20000050 = 0;
  *(uint64_t*)0x20000058 = 0;
  *(uint64_t*)0x20000060 = 0;
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 0, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 1, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 2, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 3, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 4, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 5, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 6, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 7, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 8, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 9, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 10, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 11, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 12, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 13, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 14, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 15, 2);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 17, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 18, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 19, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 20, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 21, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 22, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 23, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 24, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 25, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 26, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 27, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 28, 1);
  STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 29, 35);
  *(uint32_t*)0x20000070 = 0;
  *(uint32_t*)0x20000074 = 0;
  *(uint64_t*)0x20000078 = 0;
  *(uint64_t*)0x20000080 = 0;
  *(uint64_t*)0x20000088 = 0x10040;
  *(uint64_t*)0x20000090 = 0;
  *(uint32_t*)0x20000098 = 0;
  *(uint32_t*)0x2000009c = 0;
  *(uint64_t*)0x200000a0 = 0;
  *(uint32_t*)0x200000a8 = 0;
  *(uint16_t*)0x200000ac = 0;
  *(uint16_t*)0x200000ae = 0;
  syscall(__NR_perf_event_open, 0x20000040, -1, 0, -1, 0);
  *(uint32_t*)0x20bf7000 = 0;
  *(uint32_t*)0x20bf7004 = 0;
  *(uint64_t*)0x20bf7008 = 0;
  *(uint64_t*)0x20bf7010 = 0x1000;
  *(uint64_t*)0x20bf7018 = 0x20000000;
  syscall(__NR_ioctl, (intptr_t)r[1], 0x4020ae46, 0x20bf7000);
  *(uint64_t*)0x20000400 = 0xfffffffffffffffe;
  *(uint64_t*)0x20000408 = 0;
  *(uint64_t*)0x20000410 = 0;
  *(uint64_t*)0x20000418 = 0;
  *(uint64_t*)0x20000420 = 0;
  *(uint64_t*)0x20000428 = 0;
  *(uint64_t*)0x20000430 = 0x10000ff;
  *(uint64_t*)0x20000438 = 0;
  *(uint64_t*)0x20000440 = 0;
  *(uint64_t*)0x20000448 = 0;
  *(uint64_t*)0x20000450 = 0;
  *(uint64_t*)0x20000458 = 0;
  *(uint64_t*)0x20000460 = 0;
  *(uint64_t*)0x20000468 = 0;
  *(uint64_t*)0x20000470 = 0;
  *(uint64_t*)0x20000478 = 0;
  *(uint64_t*)0x20000480 = 0x1f000;
  *(uint64_t*)0x20000488 = 0;
  syscall(__NR_ioctl, (intptr_t)r[2], 0x4090ae82, 0x20000400);
  syscall(__NR_ioctl, (intptr_t)r[2], 0xae80, 0);
  return 0;
}