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

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.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/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/loop.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;
}

//% 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, max_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 void reset_loop_device(const char* loopname)
{
  int loopfd = open(loopname, O_RDWR);
  if (loopfd == -1) {
    return;
  }
  if (ioctl(loopfd, LOOP_CLR_FD, 0)) {
  }
  close(loopfd);
}

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, 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) {
    int loopfd;
    memset(loopname, 0, sizeof(loopname));
    snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
    if (setup_loop_device(data, size, loopname, &loopfd) == -1)
      return -1;
    close(loopfd);
    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)
    reset_loop_device(loopname);
  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;
  const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW;

retry:
  while (umount2(dir, umount_flags) == 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, umount_flags) == 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, umount_flags))
        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, umount_flags))
          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")) {
  }
}

static void setup_sysctl()
{
  int cad_pid = fork();
  if (cad_pid < 0)
    exit(1);
  if (cad_pid == 0) {
    for (;;)
      sleep(100);
  }
  char tmppid[32];
  snprintf(tmppid, sizeof(tmppid), "%d", cad_pid);
  struct {
    const char* name;
    const char* data;
  } files[] = {
      {"/sys/kernel/debug/x86/nmi_longest_ns", "10000000000"},
      {"/proc/sys/kernel/hung_task_check_interval_secs", "20"},
      {"/proc/sys/net/core/bpf_jit_kallsyms", "1"},
      {"/proc/sys/net/core/bpf_jit_harden", "0"},
      {"/proc/sys/kernel/kptr_restrict", "0"},
      {"/proc/sys/kernel/softlockup_all_cpu_backtrace", "1"},
      {"/proc/sys/fs/mount-max", "100"},
      {"/proc/sys/vm/oom_dump_tasks", "0"},
      {"/proc/sys/debug/exception-trace", "0"},
      {"/proc/sys/kernel/printk", "7 4 1 3"},
      {"/proc/sys/kernel/keys/gc_delay", "1"},
      {"/proc/sys/vm/oom_kill_allocating_task", "1"},
      {"/proc/sys/kernel/ctrl-alt-del", "0"},
      {"/proc/sys/kernel/cad_pid", tmppid},
  };
  for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
    if (!write_file(files[i].name, files[i].data)) {
    }
  }
  kill(cad_pid, SIGKILL);
  while (waitpid(cad_pid, NULL, 0) != cad_pid)
    ;
}

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)
{
  if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  }
  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);
      if (call == 1 || call == 3)
        break;
      event_timedwait(&th->done,
                      50 + (call == 0 ? 4000 : 0) + (call == 1 ? 4000 : 0) +
                          (call == 2 ? 4000 : 0) + (call == 3 ? 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 (;;) {
      sleep_ms(10);
      if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
        break;
      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*)0x20000140, "hfs\000", 4));
    NONFAILING(memcpy((void*)0x20000080, "./file1\000", 8));
    NONFAILING(*(uint32_t*)0x200000c0 = 0);
    NONFAILING(memcpy(
        (void*)0x200009c0,
        "\x78\x9c\xec\xdd\x4f\x6b\x13\x41\x18\xc7\xf1\xdf\xec\xa6\x49\x6a\x6b"
        "\x5d\x6d\x45\x10\x0f\x52\x2d\x7a\x92\xb6\x5e\xc4\x4b\x40\x8a\xaf\xc1"
        "\x8b\xa2\x36\x11\x8a\xa1\xc5\x5a\x41\xbd\x18\x3c\x8b\x2f\xc0\xbb\x17"
        "\x5f\x80\x2f\xc2\x93\x08\x9e\xf5\xe4\xc9\x17\xd0\xdb\xca\x4c\x26\xcd"
        "\x6c\xb3\x9b\x34\x2d\xcd\xb6\xf8\xfd\x40\x9a\xcd\xee\xfc\x79\x66\xb3"
        "\xc9\xce\xb3\x90\xae\x00\xfc\xb7\xee\xad\xfd\xfa\x7c\xfb\x8f\x7d\x18"
        "\x29\x56\x2c\xe9\xae\x14\xd9\x4d\x57\x55\x91\x74\x51\x97\xea\xaf\x36"
        "\x77\x36\x76\xda\xad\xe6\xb0\x86\x62\xa9\x2e\xf7\x30\x92\xab\x69\x06"
        "\xca\xac\x6f\xb6\xf2\xaa\xda\x7a\xae\x86\x97\xd8\x57\x15\xcd\x86\xeb"
        "\x70\x3c\xd2\x34\x4d\x7f\x97\x1d\x04\xca\x54\xf7\xcf\x71\xde\xc6\x48"
        "\xaa\xf9\x4f\x67\x1c\x16\x3e\x4d\xa6\xf6\xbd\xee\xc4\x52\xa7\xa4\x58"
        "\x4e\x0a\xb3\xab\x5d\xbd\xd6\x5c\xd9\x71\x00\x00\xca\x65\xba\xe7\xf7"
        "\xc8\x9f\xe7\x67\xfd\xfc\x3d\x8a\xa4\x25\x7f\xda\x0f\xcf\xff\x3f\xcf"
        "\x96\x1c\xef\xd1\xdc\xd0\x6e\xd9\x21\x94\x2c\x38\xff\xbb\x2c\x2b\x35"
        "\xf6\xfd\x3d\xe7\x36\xf5\xf3\x3d\x97\xc2\xd9\xed\x51\x2f\x4b\x1c\xb7"
        "\x1f\x3b\x79\xac\xaa\x7b\x64\x65\x26\x98\x26\x9b\x55\x0e\x26\x8b\x2e"
        "\x96\x68\xfa\xd9\x46\xbb\x75\x6b\x7d\xab\xdd\x8c\xf4\x5e\x0d\xc7\x84"
        "\x79\xe1\x82\xa4\x86\x9a\x3e\x67\xf5\x32\xd1\x0e\x36\xbd\x98\xb3\x2e"
        "\xab\xaa\xa2\xd6\xc6\x32\xe3\xc6\x30\x65\xc7\xb0\xda\x8f\xbf\xd1\x08"
        "\x8a\xcc\xe7\x75\x7a\xf8\x1e\x47\x33\xdf\xcc\x77\xf3\xc8\x24\xfa\xa4"
        "\xe6\xde\xfc\xaf\x92\x9a\x70\xcc\xfe\x9d\xba\x3f\xd5\x8f\x7f\xb9\xa8"
        "\xb9\xad\x17\x0f\xed\x73\xd2\x2d\x55\x30\xca\xf3\xae\x93\xcb\xd9\x1d"
        "\x3b\x74\x94\x71\x51\x46\x22\xbf\xa7\xd2\x58\xd9\x0b\x04\x49\x36\xce"
        "\x6a\x6e\xad\xaa\xf6\xd5\xea\x8e\x6e\xa5\xa8\x27\xdf\xce\x7c\x6e\xad"
        "\xd5\x11\xb5\x16\x6c\xad\x2f\x41\xad\xfe\xd1\x5c\x5c\xf3\xb8\x99\x8f"
        "\xe6\x81\x59\xd4\x5f\x7d\xd5\x5a\x30\xff\x8f\xec\xde\x5e\xd2\xe0\x27"
        "\x33\xbf\x11\x57\xd2\x1f\x19\x43\xc7\x53\x71\x25\x93\x70\x55\xe7\x4a"
        "\x6e\xc9\x68\xfc\xb1\x60\x4c\xfd\x7d\xfc\x41\x4f\x75\x47\x73\x2f\xdf"
        "\xbc\x7d\xfe\xa4\xdd\x6e\x6d\x4f\x7a\xc1\xc6\x30\xf1\x4e\x59\xe8\x2e"
        "\xf4\x0e\x82\x93\x12\xcf\xe1\x16\xec\x77\x6c\xb8\x46\x49\x6b\xbb\x32"
        "\x99\xde\xab\x47\xde\x75\x75\x8d\x2a\x53\x0b\x0b\x67\x47\xba\x7f\xa1"
        "\xf7\xb1\x3e\x40\xef\xbd\x0b\x78\x23\x23\x9c\xf0\x37\x13\x4a\xd1\x7f"
        "\xd3\x8b\xcb\xbc\x9b\x64\x40\x98\x34\xfb\xe5\x61\xba\xf9\x5f\x90\xaf"
        "\x2c\xbb\x14\xc9\xfe\x49\x86\xcc\xd3\xd3\x51\xd3\xb6\xa0\xc5\x95\x9c"
        "\xdc\xa0\xb6\x57\xf0\x4c\xd0\x92\xf1\xd7\xe7\x8b\x33\xa0\x99\xdc\x0c"
        "\xae\xa3\x31\x72\xae\x6b\x37\xa5\xeb\xc1\xca\x11\x39\x57\xa2\x0b\xd2"
        "\xf4\xd0\xb1\x9e\x26\x66\x4d\x3f\xf4\x98\xeb\xff\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa7\xcd\x24\x7e\xad\x11\x74"
        "\xc7\x7f\xf4\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\xe0\x10\x8a\xef\xff\x5b\xd7\x31\xde\xff\x37\xf3\x3b\x80"
        "\x03\xdf\xff\xb7\x76\x84\x81\x02\x18\xf0\x2f\x00\x00\xff\xff\xf4\xef"
        "\x72\xc4",
        699));
    NONFAILING(syz_mount_image(/*fs=*/0x20000140, /*dir=*/0x20000080,
                               /*flags=MS_NODEV|MS_DIRSYNC*/ 0x84,
                               /*opts=*/0x200000c0, /*chdir=*/0x8b,
                               /*size=*/0x2bb, /*img=*/0x200009c0));
    break;
  case 1:
    NONFAILING(memcpy((void*)0x20000200, "vfat\000", 5));
    NONFAILING(memcpy(
        (void*)0x20000240,
        "\023\023w\305\3745\324\024T\325\324\035)\255\032`)"
        "Y\201F\346\276\026nA\255\r\275@T\003<\2373\273\332\202$"
        "\242\363\327r\347cnH\263<\277p\203r\350\361\271\223>"
        "\305\022wC\276\"\006 \236\360-\371\313\362\366\350\200\3238/\000",
        78));
    NONFAILING(syz_mount_image(
        /*fs=*/0x20000200, /*dir=*/0x20000240,
        /*flags=MS_LAZYTIME|MS_I_VERSION|MS_SHARED|MS_POSIXACL|MS_SYNCHRONOUS|MS_RELATIME|MS_RDONLY|0x244c*/
        0x2b1245d, /*opts=*/0, /*chdir=*/0xfc, /*size=*/0, /*img=*/0x200000c0));
    break;
  case 2:
    NONFAILING(memcpy((void*)0x20000200, "vfat\000", 5));
    NONFAILING(memcpy(
        (void*)0x20000240,
        "\023\023w\305\3745\324\024T\325\324\035)\255\032`)"
        "Y\201F\346\276\026nA\255\r\275@T\003<\2373\273\332\202$"
        "\242\363\327r\347cnH\263<\277p\203r\350\361\271\223>"
        "\305\022wC\276\"\006 \236\360-\371\313\362\366\350\200\3238/\000",
        78));
    NONFAILING(syz_mount_image(
        /*fs=*/0x20000200, /*dir=*/0x20000240,
        /*flags=MS_LAZYTIME|MS_I_VERSION|MS_SHARED|MS_POSIXACL|MS_SYNCHRONOUS|MS_RELATIME|MS_RDONLY|0x244c*/
        0x2b1245d, /*opts=*/0, /*chdir=*/0xfc, /*size=*/0, /*img=*/0x200000c0));
    break;
  case 3:
    NONFAILING(memcpy((void*)0x20000180, "iso9660\000", 8));
    NONFAILING(memcpy((void*)0x20000280, "./file0\000", 8));
    NONFAILING(memcpy(
        (void*)0x20000b00,
        "\x6d\x61\x70\x3d\x6f\x66\x66\x2c\x6d\x61\x70\x3d\x61\x63\x6f\x72\x6e"
        "\x00\x6e\x6f\x72\x6f\x63\x6b\x2c\x73\x65\x73\x73\x69\x6f\x6e\x3d\x30"
        "\x78\x30\x20\x30\x30\x30\x30\x30\x30\x30\x30\x33\x2c\x63\x68\x65\x63"
        "\x6b\x3d\x72\x65\x6c\x61\x78\x65\x64\x2c\x6e\x6f\x72\x6f\x63\x6b\x41"
        "\x6e\x04\x00\x00\x00\x00\x00\x00\x00\xc8\x6b\x2c\x6d\x6f\x44\x65\x3d"
        "\x30\x78\x30\x30\x30\x30\x30\x30\x30\x90\xcf\x5c\xee\x73\xe4\xfd\xbc"
        "\xcf\x1a\xd8\xf6\x23\x8d\x71\x48\x30\x30\x30\x30\x30\x30\x30\x75\x6e"
        "\x68\x69\x64\x65\x2c\x6d\x61\x70\x3d\x61\x63\x6f\x72\x6e\x2c\x6f\x76"
        "\x65\x72\x72\x69\x64\x67\x72\x6f\x63\x6b\x70\x65\x72\x91\x69\x64\x65"
        "\x2c\x00\xad\xd5\x1e\xa2\xb3\x52\x49\xa1\x35\xfa\x47\x9f\x90\x69\x02"
        "\x83\x97\xe9\x80\x8a\x38\x5f\x6e\x0c\x9a\xb4\x4f\xb5\x5e\x32\x7a\xd8"
        "\x12\xfe\x29\x3a\x63\x47\xf7\xf5\x02\x63\xaf\x07\xe2\x63\x8e\x5c\x55"
        "\x22\xfc\x04\x6b\xde\xb7\x5a\xc5\xae\x4d\xfa\x74\xf4\x2b\x39\x4b\x15"
        "\x87\x06\x8e\xcf\xa5\xac\xd4\x3d\x2c\xa2\x9a\x5d\x67\xad\xb9\xbf\xb8"
        "\x7c\xe6\xcc\xb8\x5c\xb9\xae\x05\xac\x00\x00\x00\x00\x00",
        252));
    NONFAILING(sprintf((char*)0x20000bfc, "%020llu", (long long)-1));
    NONFAILING(memcpy(
        (void*)0x20000c40,
        "\x78\x9c\xec\xdd\x5d\x8f\xdb\x58\x19\x07\xf0\xff\x49\x32\x49\x26\x85"
        "\xaa\x02\x54\xad\xaa\x6e\xe7\x74\xca\x4a\x53\x31\xa4\x4e\x66\x9b\x2a"
        "\x2a\x48\x18\xe7\x24\x63\x48\xe2\xc8\xf6\xc0\x8c\x84\xb4\x2a\x74\x66"
        "\x35\x6a\xa6\x40\x5b\x24\x9a\x9b\x76\x6e\x78\x91\x96\x2f\xc0\xdd\xde"
        "\x70\xc1\x87\x58\x89\x0b\xae\xf6\x5b\xc0\x15\x48\x2b\x10\x37\xac\x40"
        "\xc8\xc8\xc7\x76\x5e\x26\x4e\x66\xd2\x49\xdb\xed\xee\xff\x37\xda\x8d"
        "\x63\x3f\x3e\xe7\xf1\xb1\xeb\x53\x77\xec\x63\x10\x11\x11\x11\x11\x11"
        "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
        "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
        "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
        "\x11\x11\x11\x11\x11\x11\x11\x84\xd5\x30\x8c\x8a\x40\xdb\xee\xee\xec"
        "\xca\xd9\xac\x86\xeb\x74\x92\x2f\x59\x4c\x2d\x8f\x4a\x5b\xc1\xcd\x68"
        "\xe2\xe6\xbc\x3a\x57\xc2\x7a\x01\x11\xfe\x87\x62\x11\x6f\x45\xb3\xdf"
        "\xfa\xda\x28\xe4\x72\xf8\xbf\x75\x5c\x8d\xbe\x5d\x45\x31\xfc\x28\x62"
        "\x70\xe1\xf2\xa5\xbb\x5f\xcd\x65\x92\xf5\xe7\x24\xfc\x22\xb0\x68\x81"
        "\x8f\x9f\x0e\x1e\xdc\xeb\xf7\xf7\x1f\x9d\x21\x36\x8b\x85\x8b\x7f\x9d"
        "\x90\x39\x43\x50\x4b\x75\x6d\xcf\xb1\x3b\x66\x4b\x49\xdb\x73\x64\xbd"
        "\x56\x33\x6e\x6d\x37\x3d\xd9\xb4\xdb\xca\xdb\xf3\x7c\xd5\x91\x96\xab"
        "\x32\xbe\xe3\xca\x0d\xeb\xa6\xac\xd4\xeb\x5b\x52\x95\xf7\x9c\x9d\x6e"
        "\xab\x61\xb6\x55\x32\xf3\xce\x37\xab\x86\x51\x93\xdf\x2b\x44\x3b\x1a"
        "\x40\xd9\xb3\xb6\xed\x76\xdb\xee\xb6\x74\x4c\xb8\x38\x8c\xb9\x23\x3f"
        "\xf8\x51\x14\xa0\xcc\x8e\x94\x07\x87\xfd\xfd\xad\xb1\x7c\x9e\xa5\xb5"
        "\x71\x18\x54\x49\x49\x3e\x33\x1e\x1c\x06\x55\x4f\xdb\xdc\xaa\x51\xad"
        "\x56\x2a\xd5\x6a\xa5\x76\xbb\x7e\xfb\x8e\x61\xe4\xa6\x66\x18\x21\x61"
        "\x0c\x61\x2a\x62\xe9\x07\x2d\xbd\x61\x96\x7b\x02\x27\x3a\x87\x8c\xd5"
        "\xf8\xaf\xf1\x17\x01\xb4\x51\x44\x17\x3b\xd8\x85\x4c\xfd\xb1\xd0\x80"
        "\x0b\x07\x9d\x19\xcb\x63\x49\xff\xff\xce\x2d\x35\xb7\xde\xf1\xfe\x3f"
        "\xe9\xe5\x57\x46\x8b\xaf\x40\xf7\xff\xd7\xa2\x6f\xd7\x66\xf5\xff\x33"
        "\x72\x91\x90\x7a\x85\xb4\x25\x62\xc6\xfc\xc5\x7e\x56\x74\x46\x12\x8f"
        "\xf1\x14\x03\x3c\xc0\x3d\xf4\xd1\xc7\x3e\x1e\x2d\xa1\x6c\x09\xb9\x76"
        "\xee\x12\xf2\x46\xdc\x92\x4b\xc9\x67\xe6\x4f\x0b\x0a\x5d\xd8\xf0\xe0"
        "\xc0\x46\x07\x26\x5a\xf8\x32\x64\x3c\x47\xa2\x8e\x1a\x6a\x30\xf0\x1e"
        "\xb6\xd1\x84\x07\x89\x26\x6c\xb4\xa1\xe0\x61\x0f\x1e\x7c\xa8\xf0\x88"
        "\xca\x87\x99\x2a\x98\xf0\xe1\xc0\x85\xc4\x06\x2c\xdc\x84\x44\x05\x75"
        "\xd4\xb1\x05\x09\x85\x32\xf6\xe0\x60\x07\x5d\xb4\xd0\x80\x89\x7f\x07"
        "\x41\x70\x80\x43\xdd\xee\x5b\x71\x3e\xcf\x53\xb6\x1a\x49\x50\x65\xc6"
        "\x46\x14\x90\x4b\x8e\xbb\x7d\x54\xe7\x6c\xed\xac\xfe\xff\xc7\xcf\xa2"
        "\xb5\xe3\xfe\xdf\x60\xff\xff\x45\x15\x1d\x07\x85\xe8\xe3\xe3\x79\x31"
        "\x44\x9f\x01\x41\x7c\xfd\xbf\xa0\xb5\x97\x93\x0d\x11\x11\x11\x11\x11"
        "\x11\x11\xbd\x0c\x42\xff\xeb\xbb\xd0\xbf\x95\x7f\x1b\x40\x80\xa6\xdd"
        "\x56\xc6\x44\x4c\xe1\xb5\x65\x47\x44\x44\x44\x44\x44\x44\x44\xcb\x20"
        "\x10\x14\x70\x15\x22\xba\x2b\x1f\x6f\x43\x4c\x5f\xff\x13\x11\x11\x11"
        "\x11\x11\x11\xd1\x9b\x4d\xe8\x67\xec\x04\x80\x92\xbe\xa9\x5f\x8c\x1e"
        "\x97\x3a\xcb\x3f\x02\x64\x5f\x41\x8a\x44\x44\x44\x44\x44\x44\x44\x74"
        "\x4e\xfa\xc9\xff\x6b\x79\x20\xd0\x77\xf9\xaf\x41\x2c\x74\xfd\x4f\x44"
        "\x44\x44\x44\x44\x44\x44\x6f\x80\xdf\x8c\x8d\xb1\x9f\xcb\xc6\x63\xec"
        "\x06\xc9\xaf\xf5\x33\x00\xd6\xfe\x5a\x10\x1f\xfd\xa3\x00\x77\x45\x1c"
        "\xf7\x76\xbf\x2e\x8e\xcc\x70\x89\x79\x14\xc7\x4c\xdd\x01\xe0\x37\xaf"
        "\x88\x8b\xf1\x40\xbd\xfa\x23\x0f\x40\x7f\xb3\xd4\x55\x11\xd7\x16\x0f"
        "\x82\x39\x1c\x77\xf0\x93\x83\xf4\xb1\xfe\x9f\x07\x11\x21\x84\x7b\x22"
        "\x81\x7c\x76\xbc\x80\x19\x09\x88\xb0\xe6\x5a\x2e\xfe\x86\x0f\x70\x3d"
        "\x5a\xe5\x7a\x3c\xce\xfc\xfd\x41\x06\x7a\x49\x34\xa2\x70\xa9\x69\xb7"
        "\x55\xd9\x72\xda\x77\x2b\x30\xcd\x8b\x19\x5f\xed\xfa\xbf\x78\x78\xf8"
        "\x4b\xc0\x1d\x6e\xe7\xc1\x61\x7f\xbf\xfc\x93\x9f\xf6\xef\xeb\x5c\x8e"
        "\xc3\x59\xc7\x47\x61\xa1\xcf\x26\xd2\xc9\xa4\x37\xc6\x28\x97\x27\x7a"
        "\xbc\x05\xfd\xcc\x45\xda\xe8\xc6\xab\x68\x26\x55\xfe\xb6\xdb\x29\x09"
        "\x5d\xaf\x91\x6c\x7f\x16\xe6\x51\x66\xbc\xa2\x79\x3b\xa0\xb6\x0a\x44"
        "\x5b\xf9\x2b\xac\x47\xfb\x6c\x3d\x88\x62\x4b\x83\xe1\x88\xfb\x02\x58"
        "\xd3\x83\x3f\x54\xca\x7a\x97\x4d\x6c\xbd\xbb\x22\x46\x59\x54\x4e\x6e"
        "\x79\xda\x8e\x98\xb1\xe5\x45\x9d\xc5\x8d\x28\xe6\xc6\xc6\x8d\xe8\x23"
        "\x69\x93\xb0\x9c\x8c\x28\x7e\x23\x0b\x54\xcb\xd3\xfb\x60\x22\x8b\xea"
        "\x78\x16\xa7\xb7\x85\xf8\xe7\x89\xf6\x9f\x9f\x05\x44\x31\x6c\x8b\xad"
        "\x30\x8b\x3f\x85\x05\x9d\xc8\xe2\x87\x1f\x45\x2b\x6f\xf5\x76\x93\xe1"
        "\x31\xce\x92\xc5\xd4\x51\x40\x44\xf4\xba\x1c\x8c\x7a\x21\x3d\x88\xf9"
        "\xd4\x18\xfb\x49\xf7\x90\x9c\xd4\xce\xde\xef\x20\x07\xc4\x67\xb9\x19"
        "\xbd\xfb\xa8\x96\x20\xee\x3f\x92\xde\xfd\xc9\xef\x83\xa8\x87\xca\x02"
        "\xb9\xf8\x77\x13\xe9\xb5\x24\xfd\x0a\xc2\x33\xfa\x86\xd0\xe5\xe4\xa3"
        "\x01\xdd\x73\x57\x52\xce\xe8\x46\xf9\xd3\x20\x08\xcc\x8b\x45\x8c\x9f"
        "\xd1\xff\x17\xff\x3d\x63\xd8\xc7\x9e\xad\x77\x3b\x0e\x82\xe0\xe4\x19"
        "\xfd\x8f\xa3\x77\x20\xc5\x69\x4f\x65\xf1\x9f\x20\x08\xee\x56\x74\x4f"
        "\xf2\xbb\x13\xbd\xea\x87\xe1\x0a\x1f\xce\xac\xd7\x6b\x57\xb3\x61\x13"
        "\xde\x7a\x72\xf4\x33\x3d\x00\x7e\xe8\xfd\xfd\xf7\xf7\x1f\x56\xab\x5b"
        "\x35\xe3\x5d\xc3\xb8\x5d\xc5\x8a\xfe\xab\x42\xfc\x91\x05\xfb\x1e\x22"
        "\x22\x9a\x72\xfa\x3b\x76\x74\x44\x66\x4e\x84\x78\x17\xd7\xa3\x32\xae"
        "\xdf\xff\xfb\x3b\xd1\xd4\x44\x8f\xf7\x95\xf8\x96\x02\xed\x16\xd0\xc7"
        "\x7d\x6c\x26\xaf\x10\x58\x4b\x2f\xb5\x84\x03\x7c\xfb\x5f\xd1\x6d\x08"
        "\x9b\xd1\x55\x2b\xb0\x5e\x8a\x3e\x4b\x03\x79\xf9\xd2\xdd\xf0\xaa\x76"
        "\x18\x7b\x28\x72\xfa\x0d\x2f\x9b\x33\xaf\xea\x74\x5f\x1a\xc5\xea\xdb"
        "\x1b\xaa\xc3\xd8\xe4\xbd\x43\x27\xaf\x00\x47\xb1\x5b\x2f\x79\x2f\x10"
        "\x11\x11\xbd\x5a\xeb\x33\xfa\x61\x60\xa2\xff\xc7\x64\xff\x5f\x9c\xe8"
        "\xff\x37\xb1\x11\x45\x6c\x5c\x49\xbd\xee\x2e\x8d\xdd\x52\xb8\x99\x5c"
        "\x1d\x0f\x2f\xe9\x07\x17\x8e\x53\x63\x2b\xa7\x27\xff\x9d\x25\x37\x06"
        "\x11\x11\xd1\x17\x84\x72\x3f\x11\x25\xff\xd7\xc2\x75\xed\xde\x7b\x95"
        "\x7a\xbd\x62\xfa\xdb\x4a\xba\x8e\xf5\x7d\xe9\xda\x8d\x96\x92\x76\xd7"
        "\x57\xae\xb5\x6d\x76\x5b\x4a\xf6\x5c\xc7\x77\x2c\xa7\x2d\x7b\x2e\x0a"
        "\xf6\xaa\xf2\xa4\xb7\xd3\xeb\x39\xae\x2f\x9b\x8e\x2b\x7b\x8e\x67\xef"
        "\xea\x37\xbf\xcb\xf8\xd5\xef\x9e\xea\x98\x5d\xdf\xb6\xbc\x5e\x5b\x99"
        "\x9e\x92\x96\xd3\xf5\x4d\xcb\x97\x0d\xdb\xb3\x64\x6f\xe7\xbb\x6d\xdb"
        "\xdb\x56\xae\x5e\xd9\xeb\x29\xcb\x6e\xda\x96\xe9\xdb\x4e\x57\x7a\xce"
        "\x8e\x6b\xa9\xb2\x94\x9e\x52\x63\x81\x76\x43\x75\x7d\xbb\x69\x87\x93"
        "\x5d\xd9\x73\xed\x8e\xe9\xee\xc9\x1f\x38\xed\x9d\x8e\x92\x0d\xe5\x59"
        "\xae\xdd\xf3\x9d\xa8\xc0\xa4\x2e\xbb\xdb\x74\xdc\x8e\x2e\xb6\x8c\x60"
        "\xe1\x17\x1d\x12\x11\x11\x7d\x1e\x3d\x7e\x3a\x78\x70\xaf\xdf\xdf\x7f"
        "\x74\x72\x62\x35\xbc\x34\x8f\xe6\x1c\x63\x46\xcc\xf4\x44\x3e\xa5\x40"
        "\x8e\x11\x44\x44\x44\xf4\x19\x33\xea\xae\x17\x58\xa9\xf8\x12\x13\x22"
        "\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22\x22"
        "\x22\x22\x22\x22\x22\xa2\x29\xa7\x3f\xd2\xb7\xe0\xc4\x4a\xda\xc3\x82"
        "\xc0\x70\xce\xcf\x2f\xc6\x73\xf0\x1c\xa3\x47\x0c\xa7\xca\x11\x38\x6f"
        "\x3e\x9f\xba\x7f\xd6\x03\xfb\xbd\xc8\xea\x99\x45\x2b\x4d\x1e\x89\x18"
        "\x3c\xf8\x78\x4e\xf0\xea\x70\x4e\xd2\xfc\xe3\x31\xc7\x8b\x54\x8a\x4b"
        "\xc0\x0b\xb7\xcf\xdf\xbe\x04\x5c\xd0\x73\x10\xcd\xc9\x2d\xf1\x00\x98"
        "\x7e\x7e\x74\xe9\xc7\x58\xda\xc4\xb7\x0e\xa2\x16\x9d\x15\xa3\x17\xa6"
        "\x2e\x2a\x0c\xf7\x45\x6e\xf9\x7f\x1c\xc2\x89\x87\x7f\x98\x5e\x24\xc2"
        "\x96\x0f\x82\x20\x98\xbf\x7a\x61\xb2\x0d\xf3\x67\x3f\x9e\x73\x00\x1e"
        "\xe5\xcf\xb1\x0b\x5e\xf5\x99\x88\x88\x5e\xb5\xff\x07\x00\x00\xff\xff"
        "\x89\x8c\x40\xf4",
        1789));
    NONFAILING(syz_mount_image(
        /*fs=*/0x20000180, /*dir=*/0x20000280,
        /*flags=MS_POSIXACL|MS_REC|MS_NOSUID|MS_NODIRATIME|0x4*/ 0x14806,
        /*opts=*/0x20000b00, /*chdir=*/1, /*size=*/0x6fd, /*img=*/0x20000c40));
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul,
          /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
          /*offset=*/0ul);
  setup_sysctl();
  const char* reason;
  (void)reason;
  install_segv_handler();
  for (procid = 0; procid < 5; procid++) {
    if (fork() == 0) {
      use_temporary_dir();
      loop();
    }
  }
  sleep(1000000);
  return 0;
}