// https://syzkaller.appspot.com/bug?id=0b1ac5e39c478b05355e189451b5379dc925fd2e
// 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/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;

struct fs_image_segment {
  void* data;
  uintptr_t size;
  uintptr_t offset;
};
static int setup_loop_device(long unsigned size, long unsigned nsegs,
                             struct fs_image_segment* segs,
                             const char* loopname, int* memfd_p, int* loopfd_p)
{
  int err = 0, loopfd = -1;
  int memfd = syscall(__NR_memfd_create, "syzkaller", 0);
  if (memfd == -1) {
    err = errno;
    goto error;
  }
  if (ftruncate(memfd, size)) {
    err = errno;
    goto error_close_memfd;
  }
  for (size_t i = 0; i < nsegs; i++) {
    if (pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset) < 0) {
    }
  }
  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;
    }
  }
  *memfd_p = 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 unsigned long size,
                            volatile unsigned long nsegs,
                            volatile long segments, volatile long flags,
                            volatile long optsarg, volatile long change_dir)
{
  struct fs_image_segment* segs = (struct fs_image_segment*)segments;
  int res = -1, err = 0, loopfd = -1, memfd = -1, need_loop_device = !!segs;
  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(size, nsegs, segs, loopname, &memfd, &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) {
    if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0)
      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);
    close(memfd);
  }
  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 < 4; 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);
      event_timedwait(&th->done, 50 + (call == 0 ? 50 : 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*)0x20000400, "vfat\000", 5));
    NONFAILING(memcpy((void*)0x20000080, "./file0\000", 8));
    NONFAILING(*(uint64_t*)0x200002c0 = 0x20000180);
    NONFAILING(
        memcpy((void*)0x20000180,
               "\x60\x1c\x6d\x6b\x64\x6f\x73\x66\x90\xe6\xb1\x00\x08\x01\x01"
               "\x00\x04\x40\x00\x20\x00\xf8\x01\x00\x10\x00\x02\x00\x03\x00"
               "\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"
               "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x19\x7d\x92\xd6\xcb"
               "\xe5\xd9\x15\x00\x7b\xf7\xd7\xef\xdf\x73\x0c\x3d\x67\xac\x38"
               "\x9a\x1c\xda\x44\x0a\x25\xe1\xc3\x0c\x10\xfc\xd6\xdc",
               88));
    NONFAILING(*(uint64_t*)0x200002c8 = 0x58);
    NONFAILING(*(uint64_t*)0x200002d0 = 0);
    NONFAILING(*(uint64_t*)0x200002d8 = 0x20000b00);
    NONFAILING(memcpy(
        (void*)0x20000b00,
        "\x53\x59\x5a\x4b\x41\x4c\x4c\x45\x52\x20\x20\x08\x00\x00\x07\x60\x2c"
        "\x55\x2c\x55\x00\x00\x15\x60\x2c\x55\x00\x00\x00\x00\x00\x00\x41\x66"
        "\x00\x69\x00\x6c\x00\x65\x00\x30\x80\x0f\x00\xfc\x00\x01\x00\xff\xff"
        "\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xdf\xf2\xff\x46\x49\x4c\x45"
        "\x30\x20\x20\x20\x20\x20\x20\x14\x00\x7f\x15\x60\x2c\x55\x2c\x55\x00"
        "\x00\x15\x60\x2c\x55\x03\x00\x00\x00\x00\x00\x6f\x7a\x00\x69\x00\x6c"
        "\x00\x65\x00\x31\x00\x0f\x00\x10\x00\x00\xff\xff\xff\x97\x21\xb7\x96"
        "\xe6\x46\x49\x4c\x45\x31\x20\x20\x20\x20\x00\x04\xbc\x48\x93\x00\x35"
        "\x8a\x7e\x13\x34\x40\x4e\x7f\x15\x60\x2c\x55\x2c\x55\x00\x00\x15\x60"
        "\x2c\x01\x00\x00\x00\x00\x00\x00\xae\x42\x00\x69\x00\x6c\x00\x65\x00"
        "\x32\x00\x0f\x00\x14\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
        "\x00\x00\xff\xff\xff\xff\x46\x49\x4c\x45\x32\x20\x20\x20\x20\x20\x20"
        "\x20\x00\x7f\x15\x60\x2c\x85\x32\xe7\xf4\x7a\xb7\x6b\xcb\x55\x06\x00"
        "\x28\x01\x00\xa8\x3b\x30\xab\x33\x71\x8a\x43\x27\x00\x41\x66\x00\x69"
        "\x00\x6c\x00\x65\x00\x3e\x00\x0f\xd2\xd2\x63\x00\x6f\x00\x6c\x00\x64"
        "\x00\x00\x00\xff\xff\x00\x00\xff\xff\xff\xff\x46\x49\x4c\x45\x7e\x31"
        "\x20\x20\x43\x4f\x4c\x20\x00\x7f\x15\x60\x2c\x55\x2c\x55\x00\x00\x15"
        "\x60\x2c\x55\x0b\x7f\xb3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\xcc\x19\x14\x59\x4e\xad\x9c\xcb\x47\x69\xa3\xd4\x16\x9f"
        "\x87\xf3\x19\x30\xb0\xa5\x2d\xa1\x74\x14\xbe\x0a\x1b\x27\x33\xa3\xdd"
        "\x1b\x8e\x4f\xee\xa2\x33\xbc\xa2\x86\x56\x04\x14\x7c\x58\xbc\x24\x5d"
        "\x00\x39\x9f\xd1\x06\xed\x56\x68\xca\xd4\xf5\xbf\x66\xbb\x1e\x8b\x0a"
        "\xdf\x8a\xc2\xf5\x14\x57\x2e\xba\x10\x56\xcf\x86\x1b\x1b\x5f\x3c\x77"
        "\xf9\xf2\x7e\x61\x22\xa2\x48\xe8\x7c\xa8\x7c\x58\x61\xa0\x50\x5b\x3f"
        "\xff\xd8\x64\x8d\xd7\xa9\x96\x78\xd1\x2d\x6c\xaf\xb0\x4a\xd5\xd6\xac"
        "\x08\x86\xd8\xf8\x18\xb7\x55\xcc\x84\x3d\x40\xe0\x95\xd1\x74\x11\xca"
        "\x66\x67\x14\x81\x4a\x24\x6a\x7d\x31\x6c\xbf\x3e\x40\x24\xeb\xcd\x62"
        "\xae\xc2\xa1\xfd\xac\x43\xa5\x24\x61\x4b\xad\xf8\x0c\x63\x8e\xd9\x92"
        "\x20\x62\x0f\x0a\x4b\xa0\x67\x03\xfc\x6f\xf8\x85\xd1\x4a\xbb\x02\xb7"
        "\x02\xac\x0b\x6e\xcf\x15\xd1\xd5\x91\x61\xe3\x74\x78\x21\xb2\xef\x23"
        "\x50\xbc\x29\xc6\x97\xce\x57\x37\xfd\xa5\x74\x02\x00\x16\xa3\xdd\x75"
        "\x2d\x96\x53\xb5\x05\x34\x8e\x85\xf4\x83\x1b\x52\xef\x2f\x81\x74\x32"
        "\xca\x74\xa3\xe1\x9f\x49\x3d\x7b\x46\xa0\x6f\xb6\xda\x92\xc8\x90\x30"
        "\xa3\xc8\x06\xe1\x26\x02\x01\x97\x95\x00\x62\x4e\x5f\x82\x5f\x34\x73"
        "\x4e\x76\x61\x0b\xd1\xba\xcb\xcb\xf3\x69\x90\x24\x74\xd7\x25\x0f\xd6"
        "\xc4\x05\x6e\x34\xa9\x29\x1c\x11\x9d\x3f\xcb\x63\x72\x32\x71\xc3\x61"
        "\x0a\x28\xb8\x9e\x68\x26\x6e\x52\x20\x63\x15\x6e\x2e\x4c\x3e\xeb\xcc"
        "\xb6\xa8\xe9\x02\x10\xd2\x2d\x32\x11\xe4\xc0\xe8\xf8\xfc\x32\xbf\x59"
        "\x22\x4b\x68\x69\xcd\xb3\xc6\x4f\xf7\x65\xfb\xdb\x48\x42\x64\xd5\x7c"
        "\x05\x73\xdd\xc2\xdb\x2a\xe3\x95\x8b\x23\xa0\x31\x06\x10\x18\x11\xeb"
        "\x65\x46\x50\xc8\x57\xca\xb5\xd1\x75\x15\x93\x29\x67\xa7\xc8\x4d\xf8"
        "\xbd\x46\xc2\x00\x4c\x18\x0b\x0a\x05\x71\xfb\x66\xd8\x49\x6e\xd1\x36"
        "\x23\x1c\xb6\x12\x7d\x0a\xc9\x28\x4a\x16\x19\x46\x03\x90\xe1\xb2\x95"
        "\x3b\xee\xb0\xbb\x15\xe3\xe7\x2c\xf6\x50\x4d\x93\xcd\x69\x27\x82\xf7"
        "\xe6\xab\xb0\x17\x6c\x9e\x05\x88\x0c\x7b\x8d\x1f\x9d\x4a\x0f\x41\x2d"
        "\x01\xd5\xfe\x6a\xd5\xfc\x34\x28\x5c\x24\xfa\x6e\x44\x08\xd1\xa9\x79"
        "\x0b\x5d\xa5\xfb\x65\x38\x1c\xaa\xa0\xe4\xcf\xed\x6b\x98\x17\xf0\xf3"
        "\x33\x2f\xc0\xc1\x5a\x22\x96\xb0\xb6\xa1\x68\x6b\x8b\x8b\xf5\xb9\x9c"
        "\x7e\x51\x83\x3c\x00\x58\xa3\x53\xa1\x42\x49\xd6\xd3\x79\x5b\x8d\x3b"
        "\x51\xe4\x19\x08\xba\xa8\x66\x4d\x51\x1d\x32\x44\x8a\x52\x7b\xaa\xaa"
        "\xb2\xd9\x34\x55\xb0\x85\xfb\xd8\xd0\x25\xbf\x2c\xf2\x63\x2d\xed\x10"
        "\xd6\xcd\x9e\x28\x5b\x14\xea\xcb\xe1\x3d\x5a\x96\xf4\x92\x83\x4c\x4e"
        "\x00\x45\xad\xd4\x80\xed\x05\x8b\xa0\xed\x49\x02\x96\x59\x6f\x55\xc9"
        "\xc5\xc3\x62\x2b\x93\x23\xe7\xf3\xcb\x9c\xfe\xb0\xab\xd4\x4d\xf1\xef"
        "\x29\x78\x7c\x18\x2a\x71\x7f\xff\x86\x6d\xdc\x83\x2e\xa2\x74\x44\x4d"
        "\x72\x5d\xec\xc9\xb2\x71\x54\x0b\xf8\x49\x6a\x44\xaf\x6a\xfd\x9c\x27"
        "\x69\x7f\x80\xd9\x35\x59\x22\x9d\xb2\x47\xa7\xef\x10\xf1\x31\xac\x9b"
        "\xcb\xcd\x73\x8f\x23\x01\xb0\x3e\xf0\x34\x13\xca\x2a\xb6\x24\x19\x53"
        "\x18\xfa\xf8\xf3\xc8\x62\x5d\x7f\x85\x77\xa4\xe4\xd3\x52\x6b\x82\x52"
        "\xb1\x38\x5f\x24\x1f\xdf\x11\x3f\x09\x73\xbe\xb0\x82\x5b\xe4\x39\x70"
        "\x0d\x87\xe4\xd6\x23\x4f\x28\xbc\xd7\xc0\x8d\xe8\xf3\x50\x3c\x41\x42"
        "\xe7\x27\xdc\xee\xc4\xbd\x3e\x5e\x06\x03\x64\x53\x87\xdd\x1d\x4f\xcd"
        "\xb6\xc0\xbd\x2c\xd6\x53\x18\xb1\x6f\xf2\xaa\x3b\xa8\x9c\x3a\x40\x9d"
        "\x16\x17\x36\x2d\xfb\x4a\x4d\xf2",
        1079));
    NONFAILING(*(uint64_t*)0x200002e0 = 0x437);
    NONFAILING(*(uint64_t*)0x200002e8 = 0x2800);
    NONFAILING(*(uint64_t*)0x200002f0 = 0x20000140);
    NONFAILING(
        memcpy((void*)0x20000140, "\x00\xba\x1f\x9d\xf7\x25\x7e\xb9\x87", 9));
    NONFAILING(*(uint64_t*)0x200002f8 = 9);
    NONFAILING(*(uint64_t*)0x20000300 = 0x4000);
    NONFAILING(*(uint64_t*)0x20000308 = 0x20000100);
    NONFAILING(memcpy((void*)0x20000100,
                      "\xf8\xff\x07\x00\xf0\xff\x04\x40\xfe\x26\x84\x00\x09\xa0"
                      "\x00\xc8\xe6\x00",
                      18));
    NONFAILING(*(uint64_t*)0x20000310 = 0x12);
    NONFAILING(*(uint64_t*)0x20000318 = 0x4009);
    NONFAILING(*(uint64_t*)0x20000320 = 0x200009c0);
    NONFAILING(memcpy(
        (void*)0x200009c0,
        "\x73\xc0\xd2\x8b\xde\xef\xe2\x35\x25\x97\x75\xdb\xad\x79\x7a\x6b\x61"
        "\x6c\x6c\x65\x72\x73\x79\x7a\x6b\x61\x6c\xec\x65\x72\x65\x72\x73\x79"
        "\x7a\x6b\x61\x6c\x6c\x65\x6a\x73\x79\x7a\x6b\x61\x6c\x6c\x65\x72\x73"
        "\x79\x7a\x6b\xb6\x6c\x65\x72\x73\x79\x7a\x6b\x61\x6c\x6c\x65\x72\x53"
        "\x79\x7a\x6b\x61\x6c\x6c\x65\x72\x73\xee\x82\xc1\x1b\x5a\x79\x7a\x6b"
        "\x61\x6c\x6c\x65\x72\x73\x79\x7a\x6b\x62\x6c\x6c\xdf\xd0\x57\x65\x72"
        "\x73\x00\x00\x08\x00\x00\x00\x00\x00\x00\x0d\x6b\x06\x94\x7c\xb3\x42"
        "\x9c\xac\xcf\x7b\x04\xc1\x5b\xf7\x99\xae\x00\x29\xe7\xee\x83\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\xfa\xdc\xe0\x63\x2c"
        "\xc3\xa2\xd1\x2b\xd4\xed\xce\x55\xd5\x48\xba\x10\xd1\x36\x02\x37\x64"
        "\x1e\x2b\x8b\x58\xc5\x71\xd8\x60\x25\x50\x07\x02\x81\x65\xba\x1a\xe2"
        "\x70\x79\x77\x24\x07\x1f\xc3\xd0\xcc\x07\x3c\x7e\x13\xf7\xbb\xd2\x0e"
        "\x76\x02\x81\x9d\xe3\x39\xaf\x11\xf9\x9d\x9e\xaa\xe6\xc6\xa7\x13\x41"
        "\x5b\x83\x54\x5d\x59\xb2\x4a\xb5\xf3\x8d\x50\x24\x91\x51\x98\xba\xe4"
        "\xe0\x8b\x10\x29\xbc\x01\x1b\xa4\xf9\x76\x36\xbd\xc9\x88\x80\x24\x6c"
        "\xab\x8a\xfa\x98\xae\xa8\xfe\x88\xf5\x63\x9e\x7a\x17\x90\x36\x79\x28"
        "\xee\x02\x08\xd6\x15\x10\x53\x79\xd1\x36\xb4\x06\xea\x87\x0f\x32\xc2"
        "\x26\xd6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
        306));
    NONFAILING(*(uint64_t*)0x20000328 = 0x132);
    NONFAILING(*(uint64_t*)0x20000330 = 0x8003);
    NONFAILING(syz_mount_image(0x20000400, 0x20000080, 0x8135, 5, 0x200002c0,
                               0x8010, 0x20000800, 1));
    break;
  case 1:
    NONFAILING(memcpy((void*)0x20000000, "./file1\000", 8));
    syscall(__NR_mkdir, 0x20000000ul, 0ul);
    break;
  case 2:
    NONFAILING(memcpy((void*)0x20000040, "./file1/file0\000", 14));
    syscall(__NR_mkdir, 0x20000040ul, 0ul);
    {
      int i;
      for (i = 0; i < 64; i++) {
        syscall(__NR_mkdir, 0x20000040ul, 0ul);
      }
    }
    break;
  case 3:
    NONFAILING(memcpy((void*)0x200000c0, "./file0/file0\000", 14));
    syscall(__NR_rmdir, 0x200000c0ul);
    {
      int i;
      for (i = 0; i < 64; i++) {
        syscall(__NR_rmdir, 0x200000c0ul);
      }
    }
    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();
  for (procid = 0; procid < 6; procid++) {
    if (fork() == 0) {
      use_temporary_dir();
      loop();
    }
  }
  sleep(1000000);
  return 0;
}