// https://syzkaller.appspot.com/bug?id=0a52da0bbfd5ff2cd097edb1607fd2bda63a1c4d
// 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")) {
  }
}

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);
      event_timedwait(&th->done,
                      50 + (call == 0 ? 4000 : 0) + (call == 1 ? 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*)0x200000000040, "ext4\000", 5));
    NONFAILING(memcpy((void*)0x200000000a00, "./file0\000", 8));
    NONFAILING(memcpy((void*)0x200000000140, "noload", 6));
    NONFAILING(*(uint8_t*)0x200000000146 = 0x2c);
    NONFAILING(memcpy((void*)0x200000000147, "bsdgroups", 9));
    NONFAILING(*(uint8_t*)0x200000000150 = 0x2c);
    NONFAILING(memcpy((void*)0x200000000151, "barrier", 7));
    NONFAILING(*(uint8_t*)0x200000000158 = 0x3d);
    NONFAILING(sprintf((char*)0x200000000159, "0x%016llx", (long long)3));
    NONFAILING(*(uint8_t*)0x20000000016b = 0x2c);
    NONFAILING(memcpy((void*)0x20000000016c, "noblock_validity", 16));
    NONFAILING(*(uint8_t*)0x20000000017c = 0x2c);
    NONFAILING(*(uint8_t*)0x20000000017d = 0);
    NONFAILING(memcpy(
        (void*)0x200000000c00,
        "\x78\x9c\xec\xdd\x4d\x6f\x23\x67\x1d\x00\xf0\xff\x78\xe3\x7d\x4b\xb6"
        "\x49\x81\x03\x54\xa2\x2d\xb4\x28\xbb\x82\xb5\x93\x46\x6d\x23\x0e\xa5"
        "\x48\x08\x4e\x95\x80\x72\x5f\x42\xe2\x44\x51\x9c\x78\x15\x3b\xed\x26"
        "\xaa\x68\x56\x7c\x00\x24\x84\x00\x89\x13\x5c\xb8\x20\xf1\x01\x40\xa8"
        "\x12\x17\x8e\x08\xa9\x12\x9c\x41\x80\x40\x08\xb6\x70\x00\x89\x76\xd0"
        "\xd8\xe3\x34\x2f\x63\x27\xdd\xf5\xda\xd9\xe4\xf7\x93\x66\xe7\x79\xf1"
        "\xcc\xff\x79\x92\x9d\xf1\x3c\x33\x93\x99\x00\xce\xad\x97\xf3\xe9\xbd"
        "\x34\x4d\x6f\x44\xc4\x64\x5e\x5e\xca\xa7\x4f\x66\x99\xdd\x88\xa7\x23"
        "\xe2\x9d\x7b\x6f\x2c\x66\x53\x12\x69\xfa\xea\x3f\x92\x48\xf2\xb2\xee"
        "\xba\xd2\xb6\x4b\x31\xde\x59\xa4\xbd\x82\xaf\x7e\x29\xe2\x1b\xc9\xd1"
        "\xb8\xcd\xed\x9d\xb5\x85\x7a\xbd\xb6\x99\xe7\xab\xad\xf5\xdb\xd5\xe6"
        "\xf6\xce\xcd\xd5\xf5\x85\x95\xda\x4a\x6d\x63\x6e\x6e\xf6\x85\xf9\x17"
        "\xe7\x9f\x9f\x9f\x79\x90\xee\x2d\x8f\xe7\x89\x6b\x11\xf1\xd2\x17\xfe"
        "\xf2\xbd\x6f\xff\xe4\x8b\x2f\xfd\xf2\x33\xaf\xff\xf1\xd6\xdf\xae\x7f"
        "\x33\xe9\xb4\xf9\xcd\xc3\xfd\xf8\x80\xc6\xfa\x55\x76\x7e\x9e\xe5\xb8"
        "\xbc\xaf\x2c\x8b\xb7\x79\x9f\xc1\x4e\xa3\xb1\x7d\x89\x82\x5f\x75\xa1"
        "\xbb\xf9\x7f\x11\x00\x00\x86\x2b\x3b\x2e\xfd\x50\x7e\x9c\x7f\x23\x26"
        "\xe3\x42\xff\xc3\x59\x00\x00\x00\xe0\x11\x94\x7e\x6e\x22\xfe\x97\x74"
        "\xaf\xdd\x1d\x71\xb1\x47\x39\x00\x00\x00\xf0\x08\x29\x45\xc4\x44\x24"
        "\xa5\x4a\x7e\xbf\xef\x44\x94\x4a\x95\x4a\xb4\xef\xe1\xfd\x48\x5c\x2d"
        "\xd5\x1b\xcd\xd6\xa7\x97\x1b\x5b\x1b\x4b\x59\x5d\xc4\x54\x94\x4b\xcb"
        "\xab\xf5\xda\x4c\x7e\x6f\xeb\x54\x94\x93\x2c\x3f\x9b\xa5\x7f\xf1\x6e"
        "\x9a\xe6\xf9\xe7\xda\x75\xef\xd7\xcf\x45\xc4\xe3\x11\xf1\xdd\xc9\x2b"
        "\xed\x7c\x65\xb1\x51\x5f\x1a\xf5\xc9\x0f\x00\x00\x00\x38\x27\xc6\x0f"
        "\x8d\xff\xff\x3d\xd9\x19\xff\x03\x00\x00\x00\x67\xcc\x54\x71\xf1\xa5"
        "\x61\xb7\x03\x00\x00\x00\x78\x78\x7a\x8c\xff\x01\x00\x00\x80\x33\xc4"
        "\xf8\x1f\x00\x00\x00\xce\xb4\x2f\xbf\xf2\x4a\x36\xa5\xdd\xf7\x5f\x2f"
        "\xbd\xb6\xbd\xb5\xd6\x78\xed\xe6\x52\xad\xb9\x56\x59\xdf\x5a\xac\x2c"
        "\x36\x36\x6f\x57\x56\x1a\x8d\x95\xf6\x33\xfb\xd6\xfb\xae\x6c\xef\xd5"
        "\x81\x1b\x5b\x77\xaa\xad\x5a\xb3\x55\x6d\x6e\xef\xdc\x5a\x6f\x6c\x6d"
        "\xb4\x6e\xad\x1e\x78\x05\x36\x00\x00\x00\x30\x44\x8f\x3f\xf5\xd6\xef"
        "\x93\x88\xd8\xfd\xec\x95\xf6\x94\xb9\xb8\xaf\xfe\xbf\xf9\x7b\x02\x46"
        "\xd6\x40\xe0\xa1\xd9\x3b\x65\x17\x49\x3e\xbf\x78\xf4\x43\x7f\x78\xac"
        "\x33\xff\xf3\x90\x1a\x05\x0c\xc5\x85\x51\x37\x00\x18\x99\xb1\x51\x37"
        "\x00\x18\x99\xf2\xa8\x1b\x00\x8c\x5c\x72\x4c\x7d\xcf\x9b\x77\x7e\x93"
        "\xcf\x3f\x31\xd8\xf6\x00\x00\x00\x83\x37\xfd\xb1\xde\xd7\xff\x4b\x7d"
        "\x97\xdc\xed\x5f\x0d\x9c\x7a\x36\x62\x38\xbf\x5c\xff\x87\xf3\xab\x7d"
        "\xfd\xbf\xe0\x96\xbf\x42\x0e\x16\xe0\x4c\x29\x3b\x02\x80\x73\xef\x81"
        "\xaf\xff\x1f\xcb\xdf\x10\x01\x00\xc0\xa8\x4d\xb4\xa7\xa4\x54\xc9\x4f"
        "\xef\x4d\x44\xa9\x54\xa9\x44\x5c\x6b\xbf\x16\xa0\x9c\x2c\xaf\xd6\x6b"
        "\x33\x11\xf1\x58\x44\xfc\x6e\xb2\x7c\x29\xcb\xcf\xb6\x97\x4c\x8e\x1d"
        "\x33\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x1d\x69\x9a\x44\x0a\x00\x00\x00\x9c\x69\x11\xa5\xbf"
        "\x26\xbf\xea\x3c\xcb\x7f\x7a\xf2\xd9\x89\xc3\xe7\x07\x2e\x26\xff\x99"
        "\x8c\xfc\x15\xa1\xaf\xff\xf0\xd5\xef\xdf\x59\x68\xb5\x36\x67\xb3\xf2"
        "\x7f\xee\x95\xb7\x7e\x90\x97\x3f\x37\x8a\x33\x18\x00\x00\x00\xc0\x61"
        "\xdd\x71\x7a\x77\x1c\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x83\xf4\xce\xbd\x37\x16\xbb"
        "\xd3\x30\xe3\xfe\xfd\xf3\x11\x31\x55\x14\x7f\x2c\x2e\xb7\xe7\x97\xa3"
        "\x1c\x11\x57\xff\x95\xc4\xd8\xbe\xe5\x92\x88\xb8\x30\x80\xf8\xbb\x77"
        "\x23\xe2\xa3\x45\xf1\x93\xac\x59\x7b\x21\x8b\xe2\x5f\x79\xf8\xf1\x63"
        "\x2a\xff\x29\x14\xc5\x1f\x1f\x40\x7c\x38\xcf\xde\xca\xf6\x3f\x2f\x17"
        "\x6d\x7f\xa5\x78\xba\x3d\x2f\xde\xfe\xc6\x22\x0e\xe4\xef\x57\xef\xfd"
        "\x5f\xec\xed\xff\x2e\xf4\xd8\xfe\xaf\x9d\x30\xc6\x13\x6f\xff\xac\xda"
        "\x33\xfe\xdd\x88\x27\xc6\x8a\xf7\x3f\xdd\xf8\x49\x8f\xf8\xcf\x9c\x30"
        "\xfe\xd7\xbf\xb6\xb3\xd3\xab\x2e\xfd\x51\xc4\x74\xe1\xf7\x4f\x72\x20"
        "\x56\xb5\xb5\x7e\xbb\xda\xdc\xde\xb9\xb9\xba\xbe\xb0\x52\x5b\xa9\x6d"
        "\xcc\xcd\xcd\xbe\x30\xff\xe2\xfc\xf3\xf3\x33\xd5\xe5\xd5\xab\xb5\xec"
        "\xdf\x7a\x6d\xa6\x30\xc6\x77\x3e\xfe\xf3\xf7\xfa\xf5\xff\x6a\x8f\xf8"
        "\x53\xc7\xf4\xff\xd9\x13\xf6\xff\xdd\xb7\xef\xdc\xfb\x70\x27\x59\x2e"
        "\x8a\x7f\xfd\x99\x82\xf8\xbf\xfe\x71\xfe\x89\xa3\xf1\x4b\xf9\x77\xdf"
        "\xa7\xf2\x74\x56\x3f\xdd\x4d\xef\x76\xd2\xfb\x3d\xf9\xd3\xdf\x3e\xd9"
        "\xaf\xff\x4b\x3d\xfa\x7f\xdc\xef\xff\xfa\x09\xfb\x7f\xe3\x2b\xdf\xfa"
        "\xd3\x09\x3f\x0a\x00\x0c\x41\x73\x7b\x67\x6d\xa1\x5e\xaf\x6d\xe6\x89"
        "\xd8\x8d\x38\x58\xf2\xa8\x27\xb2\x51\xfa\x29\x68\x86\xc4\x29\x4c\xbc"
        "\x79\xb4\xea\xa9\xe8\xb9\x54\x92\xf4\x5f\x61\x9a\xa6\x69\xb6\x4d\x3d"
        "\x40\xc3\x92\xde\xd1\x87\x93\x48\xf6\x4a\x46\xbd\x67\x02\x00\x00\x06"
        "\xed\xfd\xa3\xff\x51\xb7\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\xce\xaf\x61\x3c\x57\xec\x70\xcc\xdd\xbd\x54\x32"
        "\x88\x47\x68\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\xc4\xff\x03"
        "\x00\x00\xff\xff\x06\x4b\xea\x64",
        1317));
    NONFAILING(syz_mount_image(
        /*fs=*/0x200000000040, /*dir=*/0x200000000a00,
        /*flags=MS_SYNCHRONOUS|MS_STRICTATIME|MS_SILENT|MS_RDONLY|0x402*/
        0x1008413, /*opts=*/0x200000000140, /*chdir=*/0, /*size=*/0x525,
        /*img=*/0x200000000c00));
    break;
  case 1:
    NONFAILING(memcpy((void*)0x200000000180, "ext4\000", 5));
    NONFAILING(memcpy((void*)0x2000000000c0, "./bus\000", 6));
    NONFAILING(memcpy((void*)0x200000000040, "nodelalloc", 10));
    NONFAILING(*(uint8_t*)0x20000000004a = 0x2c);
    NONFAILING(memcpy((void*)0x20000000004b, "orlov", 5));
    NONFAILING(*(uint8_t*)0x200000000050 = 0x2c);
    NONFAILING(memcpy((void*)0x200000000051, "auto_da_alloc", 13));
    NONFAILING(*(uint8_t*)0x20000000005e = 0x2c);
    NONFAILING(*(uint8_t*)0x20000000005f = 0);
    NONFAILING(memcpy(
        (void*)0x200000000a40,
        "\x78\x9c\xec\xdd\x4d\x6f\x5b\x59\x19\x00\xe0\xf7\x3a\x5f\x4e\x26\x33"
        "\xc9\x0c\xb3\x00\x04\x4c\x19\x06\x0a\xaa\xea\x24\xee\x4c\x34\x9a\x05"
        "\x0c\x2b\x84\xd0\x48\x88\x2e\x41\x6a\x43\xe2\x46\x51\xec\x38\x8a\x9d"
        "\xd2\x84\x2e\xd2\xff\x80\x44\x25\x56\xb0\xe4\x07\xb0\xee\x8a\x3d\x1b"
        "\x04\x3b\x36\x65\x81\xc4\x47\x04\x6a\x2a\xb1\x30\xba\xd7\x37\xa9\x93"
        "\xc6\x8d\x69\x3e\x1c\xc5\xcf\x53\x5d\xdd\x7b\xce\xb5\xfd\x9e\x53\xf7"
        "\x9e\x63\xbf\xae\x7d\x02\x18\x58\xd7\x22\x62\x27\x22\x46\x23\xe2\x6e"
        "\x44\x4c\xe5\xf5\x49\xbe\xc5\xa7\xed\x2d\xbd\xdd\xb3\xdd\x87\x8b\x7b"
        "\xbb\x0f\x17\x93\x68\xb5\x6e\xff\x33\x89\xf4\x4f\x5a\x17\x1d\xf7\x49"
        "\xbd\x91\x3f\x66\x31\x22\x7e\xf4\xbd\x88\x9f\x26\x2f\xc7\x6d\x6c\x6d"
        "\xaf\x2e\x54\xab\x95\x8d\x76\x71\x7c\xa6\x59\x5b\x9f\x69\x6c\x6d\xdf"
        "\x5c\xa9\x2d\x2c\x57\x96\x2b\x6b\xe5\xf2\xfc\xdc\xfc\xec\xc7\xb7\x3e"
        "\x2a\x9f\x59\x5f\xdf\xab\x8d\xe6\x47\x5f\x7e\xfa\x87\x9d\x6f\xfd\x3c"
        "\x6d\xd6\x64\x5e\xd3\xd9\x8f\xb3\xd4\xee\xfa\xc8\x41\x9c\xd4\x70\x44"
        "\xfc\xe0\x3c\x82\xf5\xc1\x50\xde\x9f\xd1\x7e\x37\x84\xd7\x52\x88\x88"
        "\x77\x22\xe2\xfd\xec\xfa\x9f\x8a\xa1\xec\xd9\x04\x00\xae\xb2\x56\x6b"
        "\x2a\x5a\x53\x9d\x65\x00\xe0\xaa\x2b\x64\x39\xb0\xa4\x50\xca\x73\x01"
        "\x93\x51\x28\x94\x4a\xed\x1c\xde\xbb\x31\x51\xa8\xd6\x1b\xcd\x1b\xf7"
        "\xea\x9b\x6b\x4b\xed\x5c\xd9\x74\x8c\x14\xee\xad\x54\x2b\xb3\x79\xae"
        "\x70\x3a\x46\x92\xb4\x3c\x97\x1d\xbf\x28\x97\x8f\x94\x6f\x45\xc4\xdb"
        "\x11\xf1\x8b\xb1\xf1\xac\x5c\x5a\xac\x57\x97\xfa\xf9\xc2\x07\x00\x06"
        "\xd8\x1b\x47\xe6\xff\xff\x8c\xb5\xe7\x7f\x00\xe0\x8a\x2b\xf6\xbb\x01"
        "\x00\xc0\x85\x33\xff\x03\xc0\xe0\x31\xff\x03\xc0\xe0\x31\xff\x03\xc0"
        "\xe0\x31\xff\x03\xc0\xe0\x31\xff\x03\xc0\xe0\x31\xff\x03\xc0\x40\xf9"
        "\xe1\x67\x9f\xa5\x5b\x6b\x2f\xff\xfd\xeb\xa5\xfb\x5b\x9b\xab\xf5\xfb"
        "\x37\x97\x2a\x8d\xd5\x52\x6d\x73\xb1\xb4\x58\xdf\x58\x2f\x2d\xd7\xeb"
        "\xcb\xd9\x6f\xf6\xd4\x4e\x7a\xbc\x6a\xbd\xbe\x3e\xf7\x61\x6c\x3e\x98"
        "\xfe\xf6\x7a\xa3\x39\xd3\xd8\xda\xbe\x53\xab\x6f\xae\x35\xef\x64\xbf"
        "\xeb\x7d\xa7\x32\x72\x21\xbd\x02\x00\x5e\xe5\xed\xf7\x9e\xfc\x39\x89"
        "\x88\x9d\x4f\xc6\xb3\x2d\x3a\xd6\x72\x30\x57\xc3\xd5\x56\xe8\x77\x03"
        "\x80\xbe\x19\xea\x77\x03\x80\xbe\xb1\xda\x17\x0c\xae\x53\xbc\xc7\x97"
        "\x1e\x80\x2b\xe2\x98\x25\x7a\x0f\x29\x46\xc4\xf8\xd1\xca\x56\xab\xd5"
        "\x3a\xbf\x26\x01\xe7\xec\xfa\x17\xe4\xff\x61\x50\x75\xe4\xff\xfd\x2f"
        "\x60\x18\x30\xf2\xff\x30\xb8\xe4\xff\x61\x70\xb5\x5a\x49\xaf\x6b\xfe"
        "\x47\xaf\x37\x04\x00\x2e\x37\x39\x7e\xa0\xcb\xe7\xff\xef\xe4\xfb\xdf"
        "\xe6\x1f\x0e\xfc\x64\xe9\xe8\x2d\x1e\x9f\x67\xab\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\xe0\x72\xdb\x5f\xff\xb7\x94\xaf\x05\x3e\x19\x85\x42\xa9\x14"
        "\xf1\x66\x44\x4c\xc7\x48\x72\x6f\xa5\x5a\x99\x8d\x88\xb7\x22\xe2\x4f"
        "\x63\x23\x63\x69\x79\xae\xcf\x6d\x06\x00\x4e\xab\xf0\xb7\x24\x5f\xff"
        "\xeb\xfa\xd4\x07\x93\x47\xcf\x8e\x26\xcf\xc7\xb2\x7d\x44\xfc\xec\x57"
        "\xb7\x7f\xf9\x60\xa1\xd9\xdc\xf8\x63\x5a\xff\xaf\x83\xfa\xe6\xe3\xbc"
        "\xbe\xdc\x8f\xf6\x03\x00\x27\xd9\x9f\xa7\xb3\x7d\xc7\x1b\xf9\x67\xbb"
        "\x0f\x17\xf7\xb7\x8b\x6c\xcf\xdf\xbf\x1b\x11\xc5\x76\xfc\xbd\xdd\xd1"
        "\xd8\x3b\x88\x3f\x1c\xc3\xd9\xbe\x18\x23\x11\x31\xf1\xef\x24\x2f\xb7"
        "\x25\x1d\xb9\x8b\xd3\xd8\x79\x14\x11\x9f\x3f\xae\xff\x49\x4c\x66\x39"
        "\x90\xf6\xca\xa7\x47\xe3\xa7\xb1\xdf\xbc\xd0\xf8\x85\x43\xf1\x0b\xd9"
        "\xb9\xf6\x3e\xfd\xbb\xf8\xdc\x19\xb4\x05\x06\xcd\x93\x74\xfc\xf9\xf4"
        "\xb8\xeb\xaf\x10\xd7\xb2\xfd\xf1\xd7\x7f\x31\x1b\xa1\x4e\x2f\x1f\xff"
        "\xd2\x87\x5a\xdc\xcb\xc6\xc0\x17\xf1\xf7\xc7\xbf\xa1\x2e\xe3\xdf\xb5"
        "\x5e\x63\x7c\xf8\xfb\xef\xb7\x8f\xc6\x5f\x3e\xf7\x28\xe2\x8b\xc3\x91"
        "\x8d\xbb\x7b\x43\xd1\x11\x3f\x39\x88\x9f\x74\x89\xff\x41\x8f\xf1\xff"
        "\xf2\xa5\xaf\xbc\xdf\xed\x5c\xeb\xd7\x11\xd7\x23\x8f\x7f\xa8\xff\xc9"
        "\xa1\x58\x33\xcd\xda\xfa\x4c\x63\x6b\xfb\xe6\x4a\x6d\x61\xb9\xb2\x5c"
        "\x59\x2b\x97\xe7\xe7\xe6\x67\x3f\xbe\xf5\x51\x79\x26\xcb\x51\xcf\x74"
        "\x9f\x0d\xfe\xf1\xc9\x8d\xb7\xba\x9d\x4b\xfb\x3f\xd1\x25\x7e\xf1\x84"
        "\xfe\x7f\xbd\xc7\xfe\xff\xe6\xbf\x77\x7f\xfc\xd5\x57\xc4\xff\xe6\xd7"
        "\x8e\x8b\x5f\x88\x77\x5f\x11\x3f\x9d\x13\xbf\xd1\x63\xfc\x85\x89\xdf"
        "\x15\xbb\x9d\x4b\xe3\x2f\x75\xe9\xff\x49\xcf\xff\x8d\x1e\xe3\x3f\xfd"
        "\xeb\xf6\x4b\xcb\x86\x03\x00\xfd\xd3\xd8\xda\x5e\x5d\xa8\x56\x2b\x1b"
        "\x17\x79\xb0\xff\x42\xe2\x42\x83\x3a\xb8\x02\x07\xe9\xbf\x9a\x4b\xd0"
        "\x8c\x63\x0f\xbe\x73\x51\xb1\x46\xe3\xff\xba\x57\xab\xf5\x5a\xb1\xba"
        "\x8d\x18\x67\x91\x75\x03\x2e\x83\x83\x8b\x3e\x22\x9e\xf7\xbb\x31\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xb1\x7a\xfa\x3e\xd0\xd0"
        "\xe9\xbe\xb1\xd4\xef\x3e\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\x70\x75\xfd\x2f\x00\x00\xff\xff\xb9\x2f\xcc\xf7",
        1274));
    NONFAILING(syz_mount_image(
        /*fs=*/0x200000000180, /*dir=*/0x2000000000c0,
        /*flags=MS_SYNCHRONOUS|MS_RELATIME|MS_NOSUID|MS_NOEXEC|0x4*/ 0x20001e,
        /*opts=*/0x200000000040, /*chdir=*/1, /*size=*/0x4fa,
        /*img=*/0x200000000a40));
    break;
  case 2:
    NONFAILING(memcpy((void*)0x200000000040, "./bus\000", 6));
    syscall(__NR_creat, /*file=*/0x200000000040ul, /*mode=*/0ul);
    break;
  case 3:
    NONFAILING(memcpy((void*)0x200000000380, "/dev/loop", 9));
    NONFAILING(*(uint8_t*)0x200000000389 = 0x30 + procid * 1);
    NONFAILING(*(uint8_t*)0x20000000038a = 0);
    NONFAILING(memcpy((void*)0x200000000140, "./bus\000", 6));
    syscall(__NR_mount, /*src=*/0x200000000380ul, /*dst=*/0x200000000140ul,
            /*type=*/0ul,
            /*flags=MS_SHARED|MS_SLAVE|MS_REC|MS_RDONLY|MS_NOATIME|0x1240*/
            0x185641ul, /*data=*/0ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
          /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
          /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
          /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
          /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  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;
}