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

#define _GNU_SOURCE

#include <arpa/inet.h>
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include <linux/futex.h>
#include <linux/genetlink.h>
#include <linux/if_addr.h>
#include <linux/if_link.h>
#include <linux/in6.h>
#include <linux/loop.h>
#include <linux/neighbour.h>
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/veth.h>

#ifndef __NR_memfd_create
#define __NR_memfd_create 319
#endif

static unsigned long long procid;

static __thread int clone_ongoing;
static __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* ctx)
{
  if (__atomic_load_n(&clone_ongoing, __ATOMIC_RELAXED) != 0) {
    exit(sig);
  }
  uintptr_t addr = (uintptr_t)info->si_addr;
  const uintptr_t prog_start = 1 << 20;
  const uintptr_t prog_end = 100 << 20;
  int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
  int valid = addr < prog_start || addr > prog_end;
  if (skip && valid) {
    _longjmp(segv_env, 1);
  }
  exit(sig);
}

static void install_segv_handler(void)
{
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
  memset(&sa, 0, sizeof(sa));
  sa.sa_sigaction = segv_handler;
  sa.sa_flags = SA_NODEFER | SA_SIGINFO;
  sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGBUS, &sa, NULL);
}

#define NONFAILING(...)                                                        \
  ({                                                                           \
    int ok = 1;                                                                \
    __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    if (_setjmp(segv_env) == 0) {                                              \
      __VA_ARGS__;                                                             \
    } else                                                                     \
      ok = 0;                                                                  \
    __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    ok;                                                                        \
  })

static void sleep_ms(uint64_t ms)
{
  usleep(ms * 1000);
}

static uint64_t current_time_ms(void)
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    exit(1);
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static void use_temporary_dir(void)
{
  char tmpdir_template[] = "./syzkaller.XXXXXX";
  char* tmpdir = mkdtemp(tmpdir_template);
  if (!tmpdir)
    exit(1);
  if (chmod(tmpdir, 0777))
    exit(1);
  if (chdir(tmpdir))
    exit(1);
}

static void thread_start(void* (*fn)(void*), void* arg)
{
  pthread_t th;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr, 128 << 10);
  int i = 0;
  for (; i < 100; i++) {
    if (pthread_create(&th, &attr, fn, arg) == 0) {
      pthread_attr_destroy(&attr);
      return;
    }
    if (errno == EAGAIN) {
      usleep(50);
      continue;
    }
    break;
  }
  exit(1);
}

typedef struct {
  int state;
} event_t;

static void event_init(event_t* ev)
{
  ev->state = 0;
}

static void event_reset(event_t* ev)
{
  ev->state = 0;
}

static void event_set(event_t* ev)
{
  if (ev->state)
    exit(1);
  __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
}

static void event_wait(event_t* ev)
{
  while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}

static int event_isset(event_t* ev)
{
  return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}

static int event_timedwait(event_t* ev, uint64_t timeout)
{
  uint64_t start = current_time_ms();
  uint64_t now = start;
  for (;;) {
    uint64_t remain = timeout - (now - start);
    struct timespec ts;
    ts.tv_sec = remain / 1000;
    ts.tv_nsec = (remain % 1000) * 1000 * 1000;
    syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
    if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
      return 1;
    now = current_time_ms();
    if (now - start > timeout)
      return 0;
  }
}

static bool write_file(const char* file, const char* what, ...)
{
  char buf[1024];
  va_list args;
  va_start(args, what);
  vsnprintf(buf, sizeof(buf), what, args);
  va_end(args);
  buf[sizeof(buf) - 1] = 0;
  int len = strlen(buf);
  int fd = open(file, O_WRONLY | O_CLOEXEC);
  if (fd == -1)
    return false;
  if (write(fd, buf, len) != len) {
    int err = errno;
    close(fd);
    errno = err;
    return false;
  }
  close(fd);
  return true;
}

struct nlmsg {
  char* pos;
  int nesting;
  struct nlattr* nested[8];
  char buf[4096];
};

static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
                         const void* data, int size)
{
  memset(nlmsg, 0, sizeof(*nlmsg));
  struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  hdr->nlmsg_type = typ;
  hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
  memcpy(hdr + 1, data, size);
  nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
}

static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
                         int size)
{
  struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  attr->nla_len = sizeof(*attr) + size;
  attr->nla_type = typ;
  if (size > 0)
    memcpy(attr + 1, data, size);
  nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
}

static void netlink_nest(struct nlmsg* nlmsg, int typ)
{
  struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  attr->nla_type = typ;
  nlmsg->pos += sizeof(*attr);
  nlmsg->nested[nlmsg->nesting++] = attr;
}

static void netlink_done(struct nlmsg* nlmsg)
{
  struct nlattr* attr = nlmsg->nested[--nlmsg->nesting];
  attr->nla_len = nlmsg->pos - (char*)attr;
}

static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
                            int* reply_len, bool dofail)
{
  if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
    exit(1);
  struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
  struct sockaddr_nl addr;
  memset(&addr, 0, sizeof(addr));
  addr.nl_family = AF_NETLINK;
  ssize_t n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
                     (struct sockaddr*)&addr, sizeof(addr));
  if (n != (ssize_t)hdr->nlmsg_len) {
    if (dofail)
      exit(1);
    return -1;
  }
  n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  if (reply_len)
    *reply_len = 0;
  if (n < 0) {
    if (dofail)
      exit(1);
    return -1;
  }
  if (n < (ssize_t)sizeof(struct nlmsghdr)) {
    errno = EINVAL;
    if (dofail)
      exit(1);
    return -1;
  }
  if (hdr->nlmsg_type == NLMSG_DONE)
    return 0;
  if (reply_len && hdr->nlmsg_type == reply_type) {
    *reply_len = n;
    return 0;
  }
  if (n < (ssize_t)(sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))) {
    errno = EINVAL;
    if (dofail)
      exit(1);
    return -1;
  }
  if (hdr->nlmsg_type != NLMSG_ERROR) {
    errno = EINVAL;
    if (dofail)
      exit(1);
    return -1;
  }
  errno = -((struct nlmsgerr*)(hdr + 1))->error;
  return -errno;
}

static int netlink_send(struct nlmsg* nlmsg, int sock)
{
  return netlink_send_ext(nlmsg, sock, 0, NULL, true);
}

static int netlink_query_family_id(struct nlmsg* nlmsg, int sock,
                                   const char* family_name, bool dofail)
{
  struct genlmsghdr genlhdr;
  memset(&genlhdr, 0, sizeof(genlhdr));
  genlhdr.cmd = CTRL_CMD_GETFAMILY;
  netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
  netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name,
               strnlen(family_name, GENL_NAMSIZ - 1) + 1);
  int n = 0;
  int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n, dofail);
  if (err < 0) {
    return -1;
  }
  uint16_t id = 0;
  struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
                                         NLMSG_ALIGN(sizeof(genlhdr)));
  for (; (char*)attr < nlmsg->buf + n;
       attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
    if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
      id = *(uint16_t*)(attr + 1);
      break;
    }
  }
  if (!id) {
    errno = EINVAL;
    return -1;
  }
  recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  return id;
}

static void netlink_add_device_impl(struct nlmsg* nlmsg, const char* type,
                                    const char* name, bool up)
{
  struct ifinfomsg hdr;
  memset(&hdr, 0, sizeof(hdr));
  if (up)
    hdr.ifi_flags = hdr.ifi_change = IFF_UP;
  netlink_init(nlmsg, RTM_NEWLINK, NLM_F_EXCL | NLM_F_CREATE, &hdr,
               sizeof(hdr));
  if (name)
    netlink_attr(nlmsg, IFLA_IFNAME, name, strlen(name));
  netlink_nest(nlmsg, IFLA_LINKINFO);
  netlink_attr(nlmsg, IFLA_INFO_KIND, type, strlen(type));
}

static void netlink_device_change(struct nlmsg* nlmsg, int sock,
                                  const char* name, bool up, const char* master,
                                  const void* mac, int macsize,
                                  const char* new_name)
{
  struct ifinfomsg hdr;
  memset(&hdr, 0, sizeof(hdr));
  if (up)
    hdr.ifi_flags = hdr.ifi_change = IFF_UP;
  hdr.ifi_index = if_nametoindex(name);
  netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr));
  if (new_name)
    netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name));
  if (master) {
    int ifindex = if_nametoindex(master);
    netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex));
  }
  if (macsize)
    netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize);
  int err = netlink_send(nlmsg, sock);
  if (err < 0) {
  }
}

static struct nlmsg nlmsg;

//% This code is derived from puff.{c,h}, found in the zlib development. The
//% original files come with the following copyright notice:

//% Copyright (C) 2002-2013 Mark Adler, all rights reserved
//% version 2.3, 21 Jan 2013
//% This software is provided 'as-is', without any express or implied
//% warranty.  In no event will the author be held liable for any damages
//% arising from the use of this software.
//% Permission is granted to anyone to use this software for any purpose,
//% including commercial applications, and to alter it and redistribute it
//% freely, subject to the following restrictions:
//% 1. The origin of this software must not be misrepresented; you must not
//%    claim that you wrote the original software. If you use this software
//%    in a product, an acknowledgment in the product documentation would be
//%    appreciated but is not required.
//% 2. Altered source versions must be plainly marked as such, and must not be
//%    misrepresented as being the original software.
//% 3. This notice may not be removed or altered from any source distribution.
//% Mark Adler    madler@alumni.caltech.edu

//% BEGIN CODE DERIVED FROM puff.{c,h}

#define MAXBITS 15
#define MAXLCODES 286
#define MAXDCODES 30
#define MAXCODES (MAXLCODES + MAXDCODES)
#define FIXLCODES 288

struct puff_state {
  unsigned char* out;
  unsigned long outlen;
  unsigned long outcnt;
  const unsigned char* in;
  unsigned long inlen;
  unsigned long incnt;
  int bitbuf;
  int bitcnt;
  jmp_buf env;
};
static int puff_bits(struct puff_state* s, int need)
{
  long val = s->bitbuf;
  while (s->bitcnt < need) {
    if (s->incnt == s->inlen)
      longjmp(s->env, 1);
    val |= (long)(s->in[s->incnt++]) << s->bitcnt;
    s->bitcnt += 8;
  }
  s->bitbuf = (int)(val >> need);
  s->bitcnt -= need;
  return (int)(val & ((1L << need) - 1));
}
static int puff_stored(struct puff_state* s)
{
  s->bitbuf = 0;
  s->bitcnt = 0;
  if (s->incnt + 4 > s->inlen)
    return 2;
  unsigned len = s->in[s->incnt++];
  len |= s->in[s->incnt++] << 8;
  if (s->in[s->incnt++] != (~len & 0xff) ||
      s->in[s->incnt++] != ((~len >> 8) & 0xff))
    return -2;
  if (s->incnt + len > s->inlen)
    return 2;
  if (s->outcnt + len > s->outlen)
    return 1;
  for (; len--; s->outcnt++, s->incnt++) {
    if (s->in[s->incnt])
      s->out[s->outcnt] = s->in[s->incnt];
  }
  return 0;
}
struct puff_huffman {
  short* count;
  short* symbol;
};
static int puff_decode(struct puff_state* s, const struct puff_huffman* h)
{
  int first = 0;
  int index = 0;
  int bitbuf = s->bitbuf;
  int left = s->bitcnt;
  int code = first = index = 0;
  int len = 1;
  short* next = h->count + 1;
  while (1) {
    while (left--) {
      code |= bitbuf & 1;
      bitbuf >>= 1;
      int count = *next++;
      if (code - count < first) {
        s->bitbuf = bitbuf;
        s->bitcnt = (s->bitcnt - len) & 7;
        return h->symbol[index + (code - first)];
      }
      index += count;
      first += count;
      first <<= 1;
      code <<= 1;
      len++;
    }
    left = (MAXBITS + 1) - len;
    if (left == 0)
      break;
    if (s->incnt == s->inlen)
      longjmp(s->env, 1);
    bitbuf = s->in[s->incnt++];
    if (left > 8)
      left = 8;
  }
  return -10;
}
static int puff_construct(struct puff_huffman* h, const short* length, int n)
{
  int len;
  for (len = 0; len <= MAXBITS; len++)
    h->count[len] = 0;
  int symbol;
  for (symbol = 0; symbol < n; symbol++)
    (h->count[length[symbol]])++;
  if (h->count[0] == n)
    return 0;
  int left = 1;
  for (len = 1; len <= MAXBITS; len++) {
    left <<= 1;
    left -= h->count[len];
    if (left < 0)
      return left;
  }
  short offs[MAXBITS + 1];
  offs[1] = 0;
  for (len = 1; len < MAXBITS; len++)
    offs[len + 1] = offs[len] + h->count[len];
  for (symbol = 0; symbol < n; symbol++)
    if (length[symbol] != 0)
      h->symbol[offs[length[symbol]]++] = symbol;
  return left;
}
static int puff_codes(struct puff_state* s, const struct puff_huffman* lencode,
                      const struct puff_huffman* distcode)
{
  static const short lens[29] = {3,  4,  5,  6,   7,   8,   9,   10,  11, 13,
                                 15, 17, 19, 23,  27,  31,  35,  43,  51, 59,
                                 67, 83, 99, 115, 131, 163, 195, 227, 258};
  static const short lext[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
                                 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
  static const short dists[30] = {
      1,    2,    3,    4,    5,    7,    9,    13,    17,    25,
      33,   49,   65,   97,   129,  193,  257,  385,   513,   769,
      1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
  static const short dext[30] = {0, 0, 0,  0,  1,  1,  2,  2,  3,  3,
                                 4, 4, 5,  5,  6,  6,  7,  7,  8,  8,
                                 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  int symbol;
  do {
    symbol = puff_decode(s, lencode);
    if (symbol < 0)
      return symbol;
    if (symbol < 256) {
      if (s->outcnt == s->outlen)
        return 1;
      if (symbol)
        s->out[s->outcnt] = symbol;
      s->outcnt++;
    } else if (symbol > 256) {
      symbol -= 257;
      if (symbol >= 29)
        return -10;
      int len = lens[symbol] + puff_bits(s, lext[symbol]);
      symbol = puff_decode(s, distcode);
      if (symbol < 0)
        return symbol;
      unsigned dist = dists[symbol] + puff_bits(s, dext[symbol]);
      if (dist > s->outcnt)
        return -11;
      if (s->outcnt + len > s->outlen)
        return 1;
      while (len--) {
        if (dist <= s->outcnt && s->out[s->outcnt - dist])
          s->out[s->outcnt] = s->out[s->outcnt - dist];
        s->outcnt++;
      }
    }
  } while (symbol != 256);
  return 0;
}
static int puff_fixed(struct puff_state* s)
{
  static int virgin = 1;
  static short lencnt[MAXBITS + 1], lensym[FIXLCODES];
  static short distcnt[MAXBITS + 1], distsym[MAXDCODES];
  static struct puff_huffman lencode, distcode;
  if (virgin) {
    lencode.count = lencnt;
    lencode.symbol = lensym;
    distcode.count = distcnt;
    distcode.symbol = distsym;
    short lengths[FIXLCODES];
    int symbol;
    for (symbol = 0; symbol < 144; symbol++)
      lengths[symbol] = 8;
    for (; symbol < 256; symbol++)
      lengths[symbol] = 9;
    for (; symbol < 280; symbol++)
      lengths[symbol] = 7;
    for (; symbol < FIXLCODES; symbol++)
      lengths[symbol] = 8;
    puff_construct(&lencode, lengths, FIXLCODES);
    for (symbol = 0; symbol < MAXDCODES; symbol++)
      lengths[symbol] = 5;
    puff_construct(&distcode, lengths, MAXDCODES);
    virgin = 0;
  }
  return puff_codes(s, &lencode, &distcode);
}
static int puff_dynamic(struct puff_state* s)
{
  static const short order[19] = {16, 17, 18, 0, 8,  7, 9,  6, 10, 5,
                                  11, 4,  12, 3, 13, 2, 14, 1, 15};
  int nlen = puff_bits(s, 5) + 257;
  int ndist = puff_bits(s, 5) + 1;
  int ncode = puff_bits(s, 4) + 4;
  if (nlen > MAXLCODES || ndist > MAXDCODES)
    return -3;
  short lengths[MAXCODES];
  int index;
  for (index = 0; index < ncode; index++)
    lengths[order[index]] = puff_bits(s, 3);
  for (; index < 19; index++)
    lengths[order[index]] = 0;
  short lencnt[MAXBITS + 1], lensym[MAXLCODES];
  struct puff_huffman lencode = {lencnt, lensym};
  int err = puff_construct(&lencode, lengths, 19);
  if (err != 0)
    return -4;
  index = 0;
  while (index < nlen + ndist) {
    int symbol;
    int len;
    symbol = puff_decode(s, &lencode);
    if (symbol < 0)
      return symbol;
    if (symbol < 16)
      lengths[index++] = symbol;
    else {
      len = 0;
      if (symbol == 16) {
        if (index == 0)
          return -5;
        len = lengths[index - 1];
        symbol = 3 + puff_bits(s, 2);
      } else if (symbol == 17)
        symbol = 3 + puff_bits(s, 3);
      else
        symbol = 11 + puff_bits(s, 7);
      if (index + symbol > nlen + ndist)
        return -6;
      while (symbol--)
        lengths[index++] = len;
    }
  }
  if (lengths[256] == 0)
    return -9;
  err = puff_construct(&lencode, lengths, nlen);
  if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
    return -7;
  short distcnt[MAXBITS + 1], distsym[MAXDCODES];
  struct puff_huffman distcode = {distcnt, distsym};
  err = puff_construct(&distcode, lengths + nlen, ndist);
  if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
    return -8;
  return puff_codes(s, &lencode, &distcode);
}
static int puff(unsigned char* dest, unsigned long* destlen,
                const unsigned char* source, unsigned long sourcelen)
{
  struct puff_state s = {
      .out = dest,
      .outlen = *destlen,
      .outcnt = 0,
      .in = source,
      .inlen = sourcelen,
      .incnt = 0,
      .bitbuf = 0,
      .bitcnt = 0,
  };
  int err;
  if (setjmp(s.env) != 0)
    err = 2;
  else {
    int last;
    do {
      last = puff_bits(&s, 1);
      int type = puff_bits(&s, 2);
      err = type == 0 ? puff_stored(&s)
                      : (type == 1 ? puff_fixed(&s)
                                   : (type == 2 ? puff_dynamic(&s) : -1));
      if (err != 0)
        break;
    } while (!last);
  }
  *destlen = s.outcnt;
  return err;
}

//% END CODE DERIVED FROM puff.{c,h}

#define ZLIB_HEADER_WIDTH 2

static int puff_zlib_to_file(const unsigned char* source,
                             unsigned long sourcelen, int dest_fd)
{
  if (sourcelen < ZLIB_HEADER_WIDTH)
    return 0;
  source += ZLIB_HEADER_WIDTH;
  sourcelen -= ZLIB_HEADER_WIDTH;
  const unsigned long max_destlen = 132 << 20;
  void* ret = mmap(0, max_destlen, PROT_WRITE | PROT_READ,
                   MAP_PRIVATE | MAP_ANON, -1, 0);
  if (ret == MAP_FAILED)
    return -1;
  unsigned char* dest = (unsigned char*)ret;
  unsigned long destlen = max_destlen;
  int err = puff(dest, &destlen, source, sourcelen);
  if (err) {
    munmap(dest, max_destlen);
    errno = -err;
    return -1;
  }
  if (write(dest_fd, dest, destlen) != (ssize_t)destlen) {
    munmap(dest, max_destlen);
    return -1;
  }
  return munmap(dest, destlen);
}

static int setup_loop_device(unsigned char* data, unsigned long size,
                             const char* loopname, int* loopfd_p)
{
  int err = 0, loopfd = -1;
  int memfd = syscall(__NR_memfd_create, "syzkaller", 0);
  if (memfd == -1) {
    err = errno;
    goto error;
  }
  if (puff_zlib_to_file(data, size, memfd)) {
    err = errno;
    goto error_close_memfd;
  }
  loopfd = open(loopname, O_RDWR);
  if (loopfd == -1) {
    err = errno;
    goto error_close_memfd;
  }
  if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
    if (errno != EBUSY) {
      err = errno;
      goto error_close_loop;
    }
    ioctl(loopfd, LOOP_CLR_FD, 0);
    usleep(1000);
    if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
      err = errno;
      goto error_close_loop;
    }
  }
  close(memfd);
  *loopfd_p = loopfd;
  return 0;

error_close_loop:
  close(loopfd);
error_close_memfd:
  close(memfd);
error:
  errno = err;
  return -1;
}

static long syz_mount_image(volatile long fsarg, volatile long dir,
                            volatile long flags, volatile long optsarg,
                            volatile long change_dir,
                            volatile unsigned long size, volatile long image)
{
  unsigned char* data = (unsigned char*)image;
  int res = -1, err = 0, loopfd = -1, need_loop_device = !!size;
  char* mount_opts = (char*)optsarg;
  char* target = (char*)dir;
  char* fs = (char*)fsarg;
  char* source = NULL;
  char loopname[64];
  if (need_loop_device) {
    memset(loopname, 0, sizeof(loopname));
    snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
    if (setup_loop_device(data, size, loopname, &loopfd) == -1)
      return -1;
    source = loopname;
  }
  mkdir(target, 0777);
  char opts[256];
  memset(opts, 0, sizeof(opts));
  if (strlen(mount_opts) > (sizeof(opts) - 32)) {
  }
  strncpy(opts, mount_opts, sizeof(opts) - 32);
  if (strcmp(fs, "iso9660") == 0) {
    flags |= MS_RDONLY;
  } else if (strncmp(fs, "ext", 3) == 0) {
    bool has_remount_ro = false;
    char* remount_ro_start = strstr(opts, "errors=remount-ro");
    if (remount_ro_start != NULL) {
      char after = *(remount_ro_start + strlen("errors=remount-ro"));
      char before = remount_ro_start == opts ? '\0' : *(remount_ro_start - 1);
      has_remount_ro = ((before == '\0' || before == ',') &&
                        (after == '\0' || after == ','));
    }
    if (strstr(opts, "errors=panic") || !has_remount_ro)
      strcat(opts, ",errors=continue");
  } else if (strcmp(fs, "xfs") == 0) {
    strcat(opts, ",nouuid");
  }
  res = mount(source, target, fs, flags, opts);
  if (res == -1) {
    err = errno;
    goto error_clear_loop;
  }
  res = open(target, O_RDONLY | O_DIRECTORY);
  if (res == -1) {
    err = errno;
    goto error_clear_loop;
  }
  if (change_dir) {
    res = chdir(target);
    if (res == -1) {
      err = errno;
    }
  }

error_clear_loop:
  if (need_loop_device) {
    ioctl(loopfd, LOOP_CLR_FD, 0);
    close(loopfd);
  }
  errno = err;
  return res;
}

#define FS_IOC_SETFLAGS _IOW('f', 2, long)
static void remove_dir(const char* dir)
{
  int iter = 0;
  DIR* dp = 0;
retry:
  while (umount2(dir, MNT_DETACH | UMOUNT_NOFOLLOW) == 0) {
  }
  dp = opendir(dir);
  if (dp == NULL) {
    if (errno == EMFILE) {
      exit(1);
    }
    exit(1);
  }
  struct dirent* ep = 0;
  while ((ep = readdir(dp))) {
    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
      continue;
    char filename[FILENAME_MAX];
    snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
    while (umount2(filename, MNT_DETACH | UMOUNT_NOFOLLOW) == 0) {
    }
    struct stat st;
    if (lstat(filename, &st))
      exit(1);
    if (S_ISDIR(st.st_mode)) {
      remove_dir(filename);
      continue;
    }
    int i;
    for (i = 0;; i++) {
      if (unlink(filename) == 0)
        break;
      if (errno == EPERM) {
        int fd = open(filename, O_RDONLY);
        if (fd != -1) {
          long flags = 0;
          if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
          }
          close(fd);
          continue;
        }
      }
      if (errno == EROFS) {
        break;
      }
      if (errno != EBUSY || i > 100)
        exit(1);
      if (umount2(filename, MNT_DETACH | UMOUNT_NOFOLLOW))
        exit(1);
    }
  }
  closedir(dp);
  for (int i = 0;; i++) {
    if (rmdir(dir) == 0)
      break;
    if (i < 100) {
      if (errno == EPERM) {
        int fd = open(dir, O_RDONLY);
        if (fd != -1) {
          long flags = 0;
          if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
          }
          close(fd);
          continue;
        }
      }
      if (errno == EROFS) {
        break;
      }
      if (errno == EBUSY) {
        if (umount2(dir, MNT_DETACH | UMOUNT_NOFOLLOW))
          exit(1);
        continue;
      }
      if (errno == ENOTEMPTY) {
        if (iter < 100) {
          iter++;
          goto retry;
        }
      }
    }
    exit(1);
  }
}

static void kill_and_wait(int pid, int* status)
{
  kill(-pid, SIGKILL);
  kill(pid, SIGKILL);
  for (int i = 0; i < 100; i++) {
    if (waitpid(-1, status, WNOHANG | __WALL) == pid)
      return;
    usleep(1000);
  }
  DIR* dir = opendir("/sys/fs/fuse/connections");
  if (dir) {
    for (;;) {
      struct dirent* ent = readdir(dir);
      if (!ent)
        break;
      if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
        continue;
      char abort[300];
      snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
               ent->d_name);
      int fd = open(abort, O_WRONLY);
      if (fd == -1) {
        continue;
      }
      if (write(fd, abort, 1) < 0) {
      }
      close(fd);
    }
    closedir(dir);
  } else {
  }
  while (waitpid(-1, status, __WALL) != pid) {
  }
}

static void reset_loop()
{
  char buf[64];
  snprintf(buf, sizeof(buf), "/dev/loop%llu", procid);
  int loopfd = open(buf, O_RDWR);
  if (loopfd != -1) {
    ioctl(loopfd, LOOP_CLR_FD, 0);
    close(loopfd);
  }
}

static void setup_test()
{
  prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  setpgrp();
  write_file("/proc/self/oom_score_adj", "1000");
  if (symlink("/dev/binderfs", "./binderfs")) {
  }
}

#define NL802154_CMD_SET_SHORT_ADDR 11
#define NL802154_ATTR_IFINDEX 3
#define NL802154_ATTR_SHORT_ADDR 10

static void setup_802154()
{
  int sock_route = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  if (sock_route == -1)
    exit(1);
  int sock_generic = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  if (sock_generic < 0)
    exit(1);
  int nl802154_family_id =
      netlink_query_family_id(&nlmsg, sock_generic, "nl802154", true);
  for (int i = 0; i < 2; i++) {
    char devname[] = "wpan0";
    devname[strlen(devname) - 1] += i;
    uint64_t hwaddr = 0xaaaaaaaaaaaa0002 + (i << 8);
    uint16_t shortaddr = 0xaaa0 + i;
    int ifindex = if_nametoindex(devname);
    struct genlmsghdr genlhdr;
    memset(&genlhdr, 0, sizeof(genlhdr));
    genlhdr.cmd = NL802154_CMD_SET_SHORT_ADDR;
    netlink_init(&nlmsg, nl802154_family_id, 0, &genlhdr, sizeof(genlhdr));
    netlink_attr(&nlmsg, NL802154_ATTR_IFINDEX, &ifindex, sizeof(ifindex));
    netlink_attr(&nlmsg, NL802154_ATTR_SHORT_ADDR, &shortaddr,
                 sizeof(shortaddr));
    int err = netlink_send(&nlmsg, sock_generic);
    if (err < 0) {
    }
    netlink_device_change(&nlmsg, sock_route, devname, true, 0, &hwaddr,
                          sizeof(hwaddr), 0);
    if (i == 0) {
      netlink_add_device_impl(&nlmsg, "lowpan", "lowpan0", false);
      netlink_done(&nlmsg);
      netlink_attr(&nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
      int err = netlink_send(&nlmsg, sock_route);
      if (err < 0) {
      }
    }
  }
  close(sock_route);
  close(sock_generic);
}

struct thread_t {
  int created, call;
  event_t ready, done;
};

static struct thread_t threads[16];
static void execute_call(int call);
static int running;

static void* thr(void* arg)
{
  struct thread_t* th = (struct thread_t*)arg;
  for (;;) {
    event_wait(&th->ready);
    event_reset(&th->ready);
    execute_call(th->call);
    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
    event_set(&th->done);
  }
  return 0;
}

static void execute_one(void)
{
  int i, call, thread;
  for (call = 0; call < 3; call++) {
    for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
         thread++) {
      struct thread_t* th = &threads[thread];
      if (!th->created) {
        th->created = 1;
        event_init(&th->ready);
        event_init(&th->done);
        event_set(&th->done);
        thread_start(thr, th);
      }
      if (!event_isset(&th->done))
        continue;
      event_reset(&th->done);
      th->call = call;
      __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
      event_set(&th->ready);
      if (call == 1)
        break;
      event_timedwait(&th->done, 50 + (call == 0 ? 4000 : 0));
      break;
    }
  }
  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
    sleep_ms(1);
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
  int iter = 0;
  for (;; iter++) {
    char cwdbuf[32];
    sprintf(cwdbuf, "./%d", iter);
    if (mkdir(cwdbuf, 0777))
      exit(1);
    reset_loop();
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      if (chdir(cwdbuf))
        exit(1);
      setup_test();
      execute_one();
      exit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      sleep_ms(1);
      if (current_time_ms() - start < 5000)
        continue;
      kill_and_wait(pid, &status);
      break;
    }
    remove_dir(cwdbuf);
  }
}

void execute_call(int call)
{
  switch (call) {
  case 0:
    NONFAILING(memcpy((void*)0x20000c00, "udf\000", 4));
    NONFAILING(memcpy((void*)0x20000200, "./file0\000", 8));
    NONFAILING(memcpy((void*)0x20000080, "adinicb", 7));
    NONFAILING(*(uint8_t*)0x20000087 = 0x2c);
    NONFAILING(memcpy((void*)0x20000088, "nostrict", 8));
    NONFAILING(*(uint8_t*)0x20000090 = 0x2c);
    NONFAILING(memcpy((void*)0x20000091, "novrs", 5));
    NONFAILING(*(uint8_t*)0x20000096 = 0x2c);
    NONFAILING(memcpy((void*)0x20000097, "noadinicb", 9));
    NONFAILING(*(uint8_t*)0x200000a0 = 0x2c);
    NONFAILING(memcpy((void*)0x200000a1, "umask", 5));
    NONFAILING(*(uint8_t*)0x200000a6 = 0x3d);
    NONFAILING(sprintf((char*)0x200000a7, "%023llo", (long long)0x2a1));
    NONFAILING(*(uint8_t*)0x200000be = 0x2c);
    NONFAILING(memcpy((void*)0x200000bf, "shortad", 7));
    NONFAILING(*(uint8_t*)0x200000c6 = 0x2c);
    NONFAILING(*(uint8_t*)0x200000c7 = 0);
    NONFAILING(memcpy(
        (void*)0x20001840,
        "\x78\x9c\xec\xdd\x4f\x6c\x1c\xd7\x7d\x07\xf0\xdf\x1b\x92\x22\x25\xb7"
        "\x15\x13\x3b\xaa\xdd\xc6\xc5\xa6\x2d\x52\x99\xb1\x5c\xfd\x8b\xa9\x58"
        "\x85\xbb\xaa\x69\xb6\x01\x64\x99\x08\xc5\xdc\x02\x70\x45\xae\xd4\x85"
        "\xa9\x25\x41\x52\x8d\x6c\xa4\x2d\xd3\x4b\x0f\x3d\x04\x28\x8a\x1e\x72"
        "\x22\xd0\x1a\x05\x52\x34\x30\x9a\x22\xe8\x91\x6d\x5d\x20\xb9\xf8\x50"
        "\xe4\xd4\x13\xd1\xc2\x46\x50\xf4\xc0\x16\x01\x72\x0a\x18\xcc\xec\x5b"
        "\x71\x29\x51\x96\x64\x8a\x12\x69\x7f\x3e\x36\xf5\xdd\x9d\x7d\x6f\xe6"
        "\xbd\x37\xeb\x19\x59\xd0\x9b\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x40\xc4\xef\xbd\x7a\xe1\xe4\xa9\xf4\xb8\x5b\x01"
        "\x00\x3c\x4a\x97\x26\xbf\x72\xf2\xb4\xfb\x3f\x00\x7c\xa2\x5c\xf6\xff"
        "\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb0\xdf\xa5\x28\xe2"
        "\xc9\x48\xb1\x70\x69\x23\x4d\x57\xef\x3b\x86\x2e\xb6\xda\x37\x6e\x4e"
        "\x8d\x8d\xef\x5c\xed\x70\xaa\x6a\xf6\x55\xe5\xcb\x9f\xa1\x53\xa7\xcf"
        "\x9c\xfd\xe2\x8b\xa3\xe7\xba\xf9\xe1\xf5\x1f\xb6\x67\xe2\xf5\xc9\xcb"
        "\x17\x6a\xaf\xcc\x5f\x5f\x58\x6c\x2e\x2d\x35\x67\x6b\x53\xed\xd6\xcc"
        "\xfc\x6c\xf3\xbe\xf7\xb0\xdb\xfa\xb7\x1b\xa9\x06\xa0\x76\xfd\x8d\x1b"
        "\xb3\x57\xaf\x2e\xd5\x4e\xbf\x70\x66\xdb\xc7\x37\x87\x3f\x18\x7c\xe2"
        "\xd8\xf0\xf9\xd1\xe7\x4e\x3c\xdb\x2d\x3b\x35\x36\x3e\x3e\xd9\x53\xa6"
        "\x7f\xe0\x23\x1f\xfd\x0e\x77\x9b\xe1\x71\x28\x8a\x38\x11\x29\x9e\xff"
        "\xee\x8f\x53\x23\x22\x8a\xd8\xfd\x58\xdc\xe3\xbb\xb3\xd7\x0e\x57\x9d"
        "\x18\xa9\x3a\x31\x35\x36\x5e\x75\x64\xae\xd5\x68\x2f\x97\x1f\x4e\x74"
        "\x07\xa2\x88\xa8\xf5\x54\xaa\x77\xc7\xe8\x11\x9c\x8b\x5d\xa9\x47\xac"
        "\x94\xcd\x2f\x1b\x3c\x52\x76\x6f\x72\xa1\xb1\xd8\xb8\x32\xd7\xac\x4d"
        "\x34\x16\x97\x5b\xcb\xad\xf9\xf6\x44\xea\xb4\xb6\xec\x4f\x2d\x8a\x38"
        "\x97\x22\x56\x23\x62\x7d\xf0\xce\xdd\x0d\x44\x11\xfd\x91\xe2\xdb\x47"
        "\x37\xd2\x95\x88\xe8\xeb\x8e\xc3\x17\xaa\x89\xc1\x77\x6f\x47\xb1\x87"
        "\x7d\xbc\x0f\x65\x3b\x6b\x03\x11\xab\xc5\x01\x38\x67\xfb\xd8\x60\x14"
        "\xf1\x5a\xa4\xf8\xc9\xbb\x45\xcc\x94\x63\x96\x7f\xe2\xf3\x11\xaf\x95"
        "\xf9\xfd\x88\xb7\xcb\x7c\x39\x22\x95\x5f\x8c\xb3\x11\xef\xef\xf0\x3d"
        "\xe2\x60\xea\x8f\x22\xfe\xa2\x3c\xff\xe7\x37\xd2\x6c\x75\x3d\xe8\x5e"
        "\x57\x2e\x7e\xb5\xf6\xe5\xf6\xd5\xf9\x9e\xb2\xdd\xeb\xca\x81\xbf\x3f"
        "\x3c\x4a\xfb\xfc\xda\x34\x14\x45\x34\xaa\x2b\xfe\x46\xfa\xe8\xbf\xd9"
        "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x61\x3b\x1c"
        "\x45\x3c\x13\x29\x5e\xfd\x8f\x3f\xaa\xe6\x15\x47\x35\x2f\xfd\xe8\xf9"
        "\xd1\xdf\x1f\xfe\xc5\xde\x39\xe3\x4f\xdf\x63\x3f\x65\xd9\x17\x22\x62"
        "\xa5\xb8\xbf\x39\xb9\x87\xf2\x14\xe2\x89\x34\x91\xd2\x63\x9e\x4b\xfc"
        "\x49\x36\x14\x45\xfc\x71\x9e\xff\xf7\xcd\xc7\xdd\x18\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x4f\xb4\x22\x7e\x14\x29"
        "\x5e\x7a\xef\x78\x5a\x8d\xde\x35\xc5\x5b\xed\x6b\xb5\xcb\x8d\x2b\x73"
        "\x9d\x55\x61\xbb\x6b\xff\x76\xd7\x4c\xdf\xdc\xdc\xdc\xac\xa5\x4e\xd6"
        "\x73\x4e\xe7\x5c\xc9\xb9\x9a\x73\x2d\xe7\x7a\xce\x28\x72\xfd\x9c\xf5"
        "\x9c\xd3\x39\x57\x72\xae\xe6\x5c\xcb\xb9\x9e\x33\xfa\x72\xfd\x9c\xf5"
        "\x9c\xd3\x39\x57\x72\xae\xe6\x5c\xcb\xb9\x9e\x33\xfa\x73\xfd\x9c\xf5"
        "\x9c\xd3\x39\x57\x72\xae\xe6\x5c\xcb\xb9\x9e\x33\xf6\xc9\xda\xbd\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x27\x45\x14\xf1"
        "\xb3\x48\xf1\xad\xaf\x6f\xa4\x48\x11\x51\x8f\x98\x8e\x4e\xae\x0d\x3e"
        "\xee\xd6\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa5"
        "\xc1\x54\xc4\xf7\x22\x45\xed\x0f\xea\xb7\xb6\xf5\x47\x44\xaa\xfe\xed"
        "\x38\x5e\xfe\x72\x36\xea\x87\xca\xfc\x74\xd4\x47\xcb\x7c\x39\xea\x17"
        "\x72\x36\xaa\xec\xaf\x7f\xf3\x31\xb4\x9f\xdd\x19\x48\x45\xfc\x30\x52"
        "\x0c\x0e\xbd\x73\xeb\x84\xe7\xf3\x3f\xd0\x79\x77\xeb\x6b\x10\x6f\x7f"
        "\x63\xeb\xdd\xaf\xf4\x77\xb2\xaf\xfb\xe1\xf0\x07\x83\x4f\x1c\x3b\x7a"
        "\x7e\x74\xfc\xd7\x9e\xbe\xdb\xeb\xb4\x53\x03\x46\x2e\xb6\xda\x37\x6e"
        "\xd6\xa6\xc6\xc6\xc7\x27\x7b\x36\xf7\xe7\xa3\x7f\xba\x67\xdb\x70\x3e"
        "\x6e\xf1\x70\xba\x4e\x44\x2c\xbd\xf9\xd6\x1b\x8d\xb9\xb9\xe6\xa2\x17"
        "\x5e\x78\xe1\xc5\xad\x17\x8f\xfb\xca\xc4\xa3\x50\xde\xff\xdf\x8f\x14"
        "\xbf\xfd\xde\x7f\x76\x6f\xf8\xdd\xfb\xff\x2f\x74\xde\xdd\xba\xc3\xc7"
        "\x4f\xff\x64\xeb\xfe\xff\xd2\xed\x3b\xda\xa3\xfb\xff\x93\x3d\xdb\x5e"
        "\xca\xbf\x1b\x19\xe8\x8f\x18\x5a\xbe\xbe\x30\x70\x2c\x62\x68\xe9\xcd"
        "\xb7\x4e\xb4\xae\x37\xae\x35\xaf\x35\xdb\x67\x4f\x9e\xfc\xd2\xe8\xe8"
        "\x97\xce\x9c\x1c\x38\x14\x31\x74\xb5\x35\xd7\xec\x79\xb5\xeb\xa1\x02"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x78\xb4\x52\x11\xbf"
        "\x1b\x29\x1a\x3f\xdc\x48\xb5\x88\xb8\x59\xcd\xd7\x1a\x3e\x3f\xfa\xdc"
        "\x89\x67\xfb\xa2\xaf\x9a\x6f\xb5\x6d\xde\xd6\xeb\x93\x97\x2f\xd4\x5e"
        "\x99\xbf\xbe\xb0\xd8\x5c\x5a\x6a\xce\xd6\xa6\xda\xad\x99\xf9\xd9\xe6"
        "\xfd\x1e\x6e\xa8\x9a\xee\x35\x35\x36\xbe\x27\x9d\xb9\xa7\xc3\x7b\xdc"
        "\xfe\xc3\x43\xaf\xcc\x2f\xbc\xb9\xd8\xba\xf6\x87\xcb\x3b\x7e\x7e\x64"
        "\xe8\xc2\x95\xa5\xe5\xc5\xc6\xcc\xce\x1f\xc7\xe1\x28\x22\xea\xbd\x5b"
        "\x46\xaa\x06\x4f\x8d\x8d\x57\x8d\x9e\x6b\x35\xda\x55\xd5\x89\x1d\x27"
        "\xd3\x3d\xb8\x81\x54\xc4\x7f\x45\x8a\x99\xb3\xb5\xf4\xb9\xbc\x2d\xcf"
        "\xff\xbb\x7d\x86\xff\xb6\xf9\xff\x2b\xb7\xef\x68\x8f\xe6\xff\x7d\xaa"
        "\x67\x5b\x79\xcc\x94\x8a\xf8\x69\xa4\xf8\xad\xbf\x7c\x3a\x3e\x57\xb5"
        "\xf3\x48\xdc\x31\x66\xb9\xdc\xdf\x46\x8a\x91\x73\x9f\xcd\xe5\xe2\x50"
        "\x59\xae\xdb\x86\xce\x73\x05\x3a\x33\x03\xcb\xb2\xff\x17\x29\xfe\xf1"
        "\x67\xdb\xcb\x76\xe7\x43\x3e\xb9\x55\xf6\xd4\x7d\x0f\xec\x01\x51\x9e"
        "\xff\xa3\x91\xe2\x7b\x7f\xfe\x9d\xf8\xf5\xbc\x6d\xfb\xf3\x1f\x76\x3e"
        "\xff\x47\x6e\xdf\xd1\x1e\x9d\xff\xa7\x7a\xb6\x1d\xd9\xf6\xbc\x82\x5d"
        "\x77\x9d\x7c\xfe\x4f\x44\x8a\x97\x9f\x7c\x27\x7e\x23\x6f\xfb\xb0\xe7"
        "\x7f\x14\xb1\xb9\xb9\xf9\xa7\x11\xc7\x73\xe1\x5b\xcf\xe7\xd8\xa3\xf3"
        "\xff\x99\x9e\x6d\xc3\xd1\x39\xee\x6f\x3e\xbc\xee\x03\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x1c\x58\x03\xa9\x88\xbf\x8b\x14\xcf\x8e\xf7\xa7\x17"
        "\xf3\xb6\xfb\xf9\xfb\x7f\xb3\xb7\xef\x68\x8f\xfe\xfe\xd7\x2f\xf7\x6c"
        "\x9b\x7d\x44\xeb\x15\xed\x7a\x50\x01\x00\x00\x00\x60\x9f\x18\x48\x45"
        "\xfc\x28\x52\x5c\x5b\x7e\xe7\xd6\x1c\xea\xed\xf3\xbf\x7b\xe6\x7f\xfe"
        "\xce\xd6\xda\xeb\x63\xe9\xb6\x4f\xab\x3f\xe7\xfb\xa5\xea\xb9\x01\x0f"
        "\xf3\xcf\xff\x7a\x0d\xe7\xe3\x4e\xef\xbe\xdb\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb0\xaf\xa4\x54\xc4\x8b\x79\x3d"
        "\xf5\xe9\x7b\xac\xa7\xbe\x16\x29\x5e\xfd\x9f\xe7\x73\xb9\x74\xac\x2c"
        "\xd7\x5d\x07\x7e\xb8\xfa\x75\xe8\xd2\x7c\xfb\xc4\x85\xb9\xb9\xf9\x99"
        "\xc6\x72\xe3\xca\x5c\xb3\x36\xb9\xd0\x98\x69\x96\x75\x9f\x8a\x14\x1b"
        "\x7f\xf3\xd9\x5c\xb7\xa8\xd6\x57\xef\xae\x37\xdf\x59\xe3\x7d\x6b\x2d"
        "\xf6\xc5\x48\x31\xfe\xf7\xdd\xb2\x9d\xb5\xd8\xbb\x6b\x93\x3f\xb5\x55"
        "\xf6\x54\x59\xf6\x53\x91\xe2\xbf\xff\x61\x7b\xd9\xee\x3a\xd6\x9f\xd9"
        "\x2a\x7b\xba\x2c\xfb\xd7\x91\xe2\x6b\xff\xbc\x73\xd9\x63\x5b\x65\xcf"
        "\x94\x65\xbf\x13\x29\x7e\xf0\xb5\x5a\xb7\xec\x91\xb2\x6c\xf7\xf9\xa8"
        "\x9d\x67\x92\x0e\xf5\xc7\x5c\xf3\x85\x99\xf9\xb9\x3b\x1e\x85\x0a\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x6a\x20"
        "\x15\xf1\x67\x91\xe2\x7f\xaf\xaf\xc6\x4a\x9e\xf6\x9f\xd7\xff\xef\xae"
        "\xc0\xdf\xdf\x2d\xfb\xf6\x37\x7a\xd6\xfb\xbf\xcd\xcd\x6a\x9d\xff\xe1"
        "\x6a\xfd\xff\xbb\xbd\xfe\x28\xeb\xff\x0f\x3f\xb4\x9e\x02\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xc1\x91\xa2\x88\xb7\x22"
        "\xc5\xc2\xa5\x8d\xb4\x36\x58\xbe\xef\x18\xba\xd8\x6a\xdf\xb8\x39\x35"
        "\x36\xbe\x73\xb5\xc3\xa9\xaa\xd9\x57\x95\x2f\x7f\x86\x4e\x9d\x3e\x73"
        "\xf6\x8b\x2f\x8e\x9e\xeb\xe6\x87\xd7\x7f\xd8\x9e\x89\xd7\x27\x2f\x5f"
        "\xa8\xbd\x32\x7f\x7d\x61\xb1\xb9\xb4\xd4\x9c\xad\x4d\xb5\x5b\x33\xf3"
        "\xb3\xcd\xfb\xde\xc3\x6e\xeb\x6f\x0d\x5d\xc7\x48\x35\x00\xb5\xeb\x6f"
        "\xdc\x98\xbd\x7a\x75\xa9\x76\xfa\x85\x33\xdb\x3e\xbe\x39\xfc\xc1\xe0"
        "\x13\xc7\x86\xcf\x8f\x3e\x77\xe2\xd9\x6e\xd9\xa9\xb1\xf1\xf1\xc9\x9e"
        "\x32\xfd\x03\x0f\x70\xf4\x07\x6a\xdc\x96\x43\x51\xc4\x5f\x45\x8a\xe7"
        "\xbf\xfb\xe3\xf4\x2f\x83\x11\x45\xec\x7e\x2c\xee\xf1\xdd\xd9\x6b\x87"
        "\xab\x4e\x8c\x54\x9d\x98\x1a\x1b\xaf\x3a\x32\xd7\x6a\xb4\x97\xcb\x0f"
        "\x27\xba\x03\x51\x44\xd4\x7a\x2a\xd5\xbb\x63\xf4\x08\xce\xc5\xae\xd4"
        "\x23\x56\xca\xe6\x97\x0d\x1e\x29\xbb\x37\xb9\xd0\x58\x6c\x5c\x99\x6b"
        "\xd6\x26\x1a\x8b\xcb\xad\xe5\xd6\x7c\x7b\x22\x75\x5a\x5b\xf6\xa7\x16"
        "\x45\x9c\x4b\x11\xab\x11\xb1\x3e\x78\xe7\xee\x06\xa2\x88\x37\x22\xc5"
        "\xb7\x8f\x6e\xa4\x7f\x1d\x8c\xe8\xeb\x8e\xc3\x17\x2e\x4d\x7e\xe5\xe4"
        "\xe9\xbb\xb7\xa3\xd8\xc3\x3e\xde\x87\xb2\x9d\xb5\x81\x88\xd5\xe2\x00"
        "\x9c\xb3\x7d\x6c\x30\x8a\xf8\xa7\x48\xf1\x93\x77\x8f\xc7\xbf\x0d\x46"
        "\xf4\x47\xe7\x27\x3e\x1f\xf1\x5a\x99\xdf\x8f\x78\xbb\xcc\x97\x23\x52"
        "\xf9\xc5\x38\x1b\xf1\xfe\x0e\xdf\x23\x0e\xa6\xfe\x28\xe2\xff\xcb\xf3"
        "\x7f\x7e\x23\xbd\x3b\x58\x5e\x0f\xba\xd7\x95\x8b\x5f\xad\x7d\xb9\x7d"
        "\x75\xbe\xa7\x6c\xf7\xba\x72\xe0\xef\x0f\x8f\xd2\x3e\xbf\x36\x0d\x45"
        "\x11\x3f\xa8\xae\xf8\x1b\xe9\xdf\xfd\x77\x0d\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\xb0\x8f\x14\xf1\xab\x91\xe2\xa5\xf7\x8e\xa7"
        "\x6a\x7e\xf0\xad\x39\xc5\xad\xf6\xb5\xda\xe5\xc6\x95\xb9\xce\xb4\xbe"
        "\xee\xdc\xbf\xee\x9c\xe9\xcd\xcd\xcd\xcd\x5a\xea\x64\x3d\xe7\x74\xce"
        "\x95\x9c\xab\x39\xd7\x72\xae\xe7\x8c\x22\xd7\xcf\x59\xcf\x39\x9d\x73"
        "\x25\xe7\x6a\xce\xb5\x9c\xeb\x39\xa3\x2f\xd7\xcf\x59\xcf\x39\x9d\x73"
        "\x25\xe7\x6a\xce\xb5\x9c\xeb\x39\xa3\x3f\xd7\xcf\x59\xcf\x39\x9d\x73"
        "\x25\xe7\x6a\xce\xb5\x9c\xeb\x39\x63\x9f\xcc\xdd\x03\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3e\x5e\x8a\xea\x9f\x14\xdf"
        "\xfa\xfa\x46\xda\x1c\xec\xac\x2f\x3d\x1d\x9d\x5c\xb3\x1e\xe8\xc7\xde"
        "\xcf\x03\x00\x00\xff\xff\x26\xbe\x01\x7a",
        3070));
    NONFAILING(syz_mount_image(0x20000c00, 0x20000200, 0x818444, 0x20000080, 1,
                               0xbfe, 0x20001840));
    break;
  case 1:
    NONFAILING(memcpy((void*)0x20000040, "./file0\000", 8));
    NONFAILING(syz_mount_image(0, 0x20000040, 0, 0, 0, 0, 0));
    break;
  case 2:
    NONFAILING(memcpy((void*)0x20000040, "memory.events\000", 14));
    syscall(__NR_openat, 0xffffff9c, 0x20000040ul, 0x275aul, 0x12ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
  syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  setup_802154();
  install_segv_handler();
  use_temporary_dir();
  loop();
  return 0;
}