// https://syzkaller.appspot.com/bug?id=5df329c4f6f07966e1c5db0215f8796f0eea8f12
// 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
#ifndef __NR_renameat2
#define __NR_renameat2 316
#endif

static unsigned long long procid;

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;
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")) {
  }
}

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 < 7; 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 == 2 ? 4000 : 0));
      break;
    }
  }
  for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
    sleep_ms(1);
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

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

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

void execute_call(int call)
{
  intptr_t res = 0;
  switch (call) {
  case 0:
    memcpy((void*)0x20000240, "hfsplus\000", 8);
    memcpy((void*)0x20000200, "./file2\000", 8);
    memcpy(
        (void*)0x20000d00,
        "\x78\x9c\xec\xdd\xcd\x6f\x1c\x67\x19\x00\xf0\x67\xd6\x9b\xb5\x37\x40"
        "\xea\xb6\x49\x1b\x50\xa5\x58\x8d\x54\x10\x16\x89\xed\x95\x0b\x3e\x35"
        "\x20\x84\x7c\xa8\x50\x55\x0e\x9c\x38\x58\xc9\xa6\x59\x65\x93\x16\x7b"
        "\x8b\xdc\x0a\x51\x53\xbe\xae\x1c\xfa\x07\x94\x83\x6f\x9c\x90\xb8\x47"
        "\x2a\x17\x2e\x70\xeb\xd5\xc7\x48\x48\x5c\x7a\xc1\x08\xa4\x41\x33\x3b"
        "\xb3\xde\xd8\x6b\x67\x9d\x78\xbd\xeb\xf0\xfb\x45\xe3\xf7\x9d\x79\x67"
        "\xde\x79\xde\x67\xbe\xf6\x43\xd1\x06\xf0\x7f\x6b\x75\x3e\xaa\x0f\x22"
        "\x89\xd5\xf9\x37\x37\xb3\xf9\x9d\xed\x46\x7b\x67\xbb\x71\xaf\xac\x47"
        "\xc4\x74\x44\x54\x22\xaa\xdd\x22\x92\x7f\xa5\x69\xfa\x59\xc4\x8d\xe8"
        "\x4e\xf1\xd5\x6c\x61\xd1\x5d\x72\xd8\x7e\x3e\x69\xad\xbc\xfd\xf9\x17"
        "\x3b\x0f\x23\x62\xaa\x6f\x71\x52\x76\xfa\x74\xa3\xd8\x2a\xa6\x98\x2b"
        "\xfa\x9f\xeb\x6f\xad\x3c\x5d\x7f\x37\x8b\xf2\x8d\x27\x0e\x2f\xe9\x8d"
        "\x30\x4b\xd8\xd5\x32\x71\x30\x6e\xe7\x22\xa2\x9a\x96\xe6\xd2\x34\xfd"
        "\xe9\x5f\xbf\xdc\x6b\xe9\x53\x1f\xb4\xf5\xcc\xa9\xc4\x08\x8c\x56\xf2"
        "\xe8\x73\xb9\x67\x36\xe2\x7c\x71\xa1\x67\xaf\x03\xba\x4f\xc5\x27\x79"
        "\xa4\x4e\x98\xad\x71\x07\x00\x00\x00\x00\xa7\xe0\xb9\xdd\xd8\x8d\xcd"
        "\xb8\x30\xee\x38\x00\x00\x00\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\x2c\x29"
        "\x7e\xff\x3f\x29\xa6\x4a\x59\x9f\x8b\xa4\xfc\xfd\xff\x5a\xb1\x2c\x8a"
        "\xfa\x64\xb9\x72\xbc\xd5\x1f\x8c\x2a\x0e\x00\x00\x00\x00\x00\x00\x00"
        "\x38\x45\x57\x76\x63\x37\x36\xe3\x42\x39\x9f\x26\xf9\x77\xfe\xaf\xe6"
        "\x33\x17\xf3\xbf\x5f\x8a\xf7\x63\x23\x9a\xb1\x1e\xd7\x62\x33\xd6\xa2"
        "\x13\x9d\x58\x8f\xc5\x88\x98\xed\xeb\xa8\xb6\xb9\xd6\xe9\xac\x2f\xf6"
        "\xb6\x9c\x19\xb0\xe5\x54\xbe\xe5\xd2\xc0\x2d\x97\x1e\x13\xe8\x74\x51"
        "\xd6\x4f\x6a\xe4\x00\x00\x00\x00\x00\x00\x00\xf0\x4c\xf9\x38\x56\xf7"
        "\xbe\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x49"
        "\x90\x44\x4c\x75\x8b\x7c\xba\x58\xd6\x67\xa3\x52\x8d\x88\x99\x88\xa8"
        "\x65\xeb\x6d\x45\xfc\xbd\xac\x9f\x09\x33\x83\x17\x3f\x38\xed\x38\x00"
        "\x00\x00\x60\x0c\x9e\xdb\x8d\xdd\xd8\x8c\x0b\xe5\x7c\x9a\xe4\xef\xf9"
        "\x5f\xca\xdf\xf7\xcf\xc4\xfb\x71\x3f\x3a\xd1\x8a\x4e\xb4\xa3\x19\xb7"
        "\xf2\xcf\x02\xba\xef\xfa\x2b\x3b\xdb\x8d\xf6\xce\x76\xe3\x5e\x36\x1d"
        "\xec\xf7\xbb\xff\x3c\x56\x18\x79\x8f\xd1\xfd\xec\x61\xf0\x9e\x2f\xe7"
        "\x6b\xd4\xe3\x76\xb4\xf2\x25\xd7\xe2\x66\xbc\x1b\xed\xb8\x15\x95\x7c"
        "\xcb\xcc\xe5\x32\x9e\xc1\x71\xfd\x32\x8b\x29\x79\xa3\x30\x64\x64\xb7"
        "\x8a\x32\x1b\xf9\xef\x8b\x72\x44\x8e\xf9\x61\xca\x6c\x9e\x91\x73\xbd"
        "\x8c\x2c\x14\xb1\x65\xd9\x78\xfe\xe8\x4c\x1c\xf3\xe8\xec\xdf\xd3\x62"
        "\x54\x7a\xc1\x5e\xdc\xb7\xa7\x7d\x83\x38\x22\xe7\x53\x87\xee\xef\x7c"
        "\x51\x66\xe3\xf9\xed\x68\x73\x7e\x4c\xfb\x33\xb1\xd4\x77\xf6\xbd\x74"
        "\x74\xce\x23\xbe\xfe\xe7\x3f\xfe\xf8\x4e\xfb\xfe\xdd\x3b\xb7\x37\xe6"
        "\x27\x67\x48\xc3\x29\x0f\x56\x5a\x2e\xd8\x9f\x89\x46\x7c\xfc\x93\x87"
        "\xd3\xdd\xb6\x97\x9f\xe5\x4c\x1c\xb0\x90\x67\xe2\x52\x6f\x7e\x35\x7e"
        "\x10\x3f\x8a\xf9\x98\x8b\xb7\x62\x3d\x5a\xf1\xb3\x58\x8b\x4e\x34\x63"
        "\x2e\xbe\x9f\xd7\xd6\x8a\xf3\x39\xe9\xbb\xe4\x0f\xc9\xd4\x8d\x47\xe6"
        "\xde\x7a\x5c\x24\xb5\xe2\xb8\x74\x0f\xd6\x30\x31\x55\x23\x8a\x98\x5e"
        "\xcd\xb7\xbd\x10\xad\xf8\x61\xbc\x1b\xb7\xa2\x19\xaf\xe7\xff\x96\x62"
        "\x31\xbe\x1d\xcb\xb1\x1c\x2b\x7d\xe7\xfa\xa5\x21\xee\xb4\x95\x43\xee"
        "\xb4\xe9\x57\x06\x06\x7f\xf5\x1b\x45\xa5\x1e\x11\xbf\x2b\xca\x3e\xff"
        "\x7d\xdc\xe8\x47\x27\xcb\xeb\xf3\x7d\x79\xed\xbf\xe7\xce\xe6\x6d\xfd"
        "\x4b\xf6\xb2\xf4\xc2\xc9\x3f\x8f\xaa\x5f\x2b\x2a\xd9\x3e\x7e\x55\x94"
        "\x93\x61\x7f\x26\x16\xfb\x32\xf1\xe2\xd1\x99\xf8\x43\x7e\x5b\xd9\x68"
        "\xdf\xbf\xbb\x7e\x67\xed\xbd\x21\xf7\xf7\x5a\x51\x66\xd7\xd1\x6f\x86"
        "\x7f\x4a\x1c\xfe\xd8\x39\x31\xd9\xf9\xf2\x42\x76\xb0\xf2\xb9\x47\xcf"
        "\x8e\xac\xed\xc5\x81\x6d\x8b\x79\xdb\xc5\x5e\x5b\xe5\x40\xdb\xa5\x5e"
        "\x5b\xf7\x4a\xdd\x3a\x70\xa5\xa6\x1f\x75\xaf\xd4\x5a\xf1\x1a\xee\x60"
        "\x4f\x4b\x79\xdb\xcb\x03\xdb\x1a\x79\xdb\xe5\xbe\xb6\x41\xaf\xb7\x00"
        "\x98\x78\xe7\xbf\x79\xbe\x56\xff\x47\xfd\x6f\xf5\x4f\xeb\xbf\xae\xdf"
        "\xa9\xbf\x39\xf3\xbd\xe9\xef\x4c\xbf\x52\x8b\x73\x7f\x39\x17\xd5\x85"
        "\xa9\xd7\x2a\xaf\x24\x7f\x8a\x4f\xe3\x17\x7b\xef\xff\x01\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x80\x27\xb7\xf1\xc1\x87\x77\xd7\xda\xed\xe6\xfa\xbe\x4a\x9a"
        "\xa6\x1f\x1d\xd2\x74\x96\x2b\xe5\x8f\x00\x8d\x37\x8c\x5a\x4c\x46\x36"
        "\x4e\xaa\xf2\x9f\xc7\xaf\xf3\xef\x34\x4d\x8b\x25\xc9\x44\xc4\x7c\x64"
        "\x25\x2d\x4c\x4a\x3c\x47\x56\xae\xc4\x48\x7a\x1e\xf3\x8d\x09\x18\xb9"
        "\xeb\x9d\x7b\xef\x5d\xdf\xf8\xe0\xc3\x6f\xb5\xee\xad\xbd\xd3\x7c\xa7"
        "\x79\x7f\x65\x79\x79\x65\x61\x65\xf9\xf5\xc6\xf5\xdb\xad\x76\x73\xa1"
        "\xfb\x77\xdc\x51\x02\xa3\xb0\xf7\xd0\x1f\x77\x24\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\xc0\xb0\x4e\xe3\x3f\x2a\x8c\x7b\x8c\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\xc0\xd9\xb6\x3a\x1f\xd5\x07\x91\xc4\xe2\xc2\xb5\x85\x6c\x7e"
        "\x67\xbb\xd1\xce\xa6\xb2\xbe\xb7\x66\x35\x22\x2a\x11\x91\xfc\x3c\x22"
        "\xf9\x2c\xe2\x46\x74\xa7\x98\xed\xeb\x2e\x39\x6c\x3f\x9f\xb4\x56\xde"
        "\xfe\xfc\x8b\x9d\x87\x7b\x7d\x55\xcb\xf5\x2b\x47\x6d\x37\x9c\xad\x62"
        "\x8a\xb9\x88\x98\x2a\xca\x93\xea\xef\xe6\x53\xf7\x97\xf4\x46\x98\x25"
        "\xec\x6a\x99\x38\x18\xb7\xff\x05\x00\x00\xff\xff\x61\x91\x13\x6b",
        1631);
    syz_mount_image(/*fs=*/0x20000240, /*dir=*/0x20000200, /*flags=*/0x10108d4,
                    /*opts=*/0x200000c0, /*chdir=*/1, /*size=*/0x65f,
                    /*img=*/0x20000d00);
    break;
  case 1:
    memcpy((void*)0x20000280, "./file2\000", 8);
    memcpy((void*)0x20000000,
           "./"
           "file0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
           "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\000",
           250);
    syscall(__NR_symlink, /*old=*/0x20000280ul, /*new=*/0x20000000ul);
    break;
  case 2:
    memcpy((void*)0x20000600, "hfsplus\000", 8);
    memcpy((void*)0x20000140, "./bus\000", 6);
    memcpy(
        (void*)0x20000dc0,
        "\x66\x6f\x72\x63\x65\x00\x00\x69\x61\xe3\x8c\x80\x00\x00\x00\x00\x00"
        "\x00\x00\x30\x30\x35\x30\x30\x30\x07\x3f\x41\x10\x36\xb0\x30\xc3\xe5"
        "\xc5\x19\x49\xce\xc5\x46\x39\x2c\x63\x72\x65\x61\x36\xd9\xbb\x3d\x25"
        "\xf4\xea\x83\x21\xf0\x0c\xad\xc2\x4c\xf9\x13\x02\x00\x6f\x72\x71\x24"
        "\x02\xbd\xc8\x02\x3a\x1c\xeb\x6c\xff\x75\x72\x4a\xcd\xc6\xbf\x74\x17"
        "\x01\x00\x53\x45\xd3\x03\x00\x7f\xb7\x82\x97\x50\x34\x06\x44\x60\x8b"
        "\xa8\x72\x92\xd3\xe1\x82\x1a\x70\x4e\x46\x58\x4f\xb9\x46\xcf\x3b\x27"
        "\x7b\x74\xf3\xd9\x4d\x7f\x3f\x3b\x27\xb1\xb9\x53\xc9\x28\xa6\x3d\x77"
        "\x86\xe2\x3b\x2d\xcf\x98\xf4\xbb\xb4\x90\x3a\x06\xab\x8c\x62\x7b\x7b"
        "\xb9\x32\x95\xbe\x12\xb8\x2c\x45\x8f\x84\xc3\xae\x25\xbc\xf2\xd8\x53"
        "\xe9\x8b\x87\x25\xd8\xae\xba\xbb\x35\x96\x57\x99\x7a\x19\x39\xb7\x9f"
        "\x37\xab\x02\x81\x0b\xde\x52\xe8\xc3\xd1\xe3\xa0\x66\xb8\xf2\x50\x09"
        "\xb6\x91\xec\xa2\x0d\xa6\xe0\x03\x90\xf1\xc7\x37\x91\xba\xd7\x84\x7f"
        "\x51\x16\xb5\x44\xb9\xf3\x72\x66\x39\x13\xff\xa7\x89\xb2\x10\x26\x0b"
        "\xe4\x78\x0c\x06\x1f\xdf\xed\x19\x1d\xf0\x7d\x52\xd6\x8d\xb9\x29\x63"
        "\xef\x8f\x96\xe4\x62\xaf\x51\x1b\xbe\x85\x11\xae\x0c\xe4\xab\xce\xb6"
        "\x51\x6b\x98\x73\x78\x78\x83\xad\x79\x74\xdb\xec\xb6\x45\xbe\x4a\x0f"
        "\x56\x66\x75\xca\x9e\x56\xf4\xcc\xcc\x8d\xe7\x8f\x7c\xa8\x03\xd7\xc0"
        "\xf2\x66\xc3\xb5\xce\x9b\x12\x0d\xd6\xe9\x26\xa5\xb8\xcc\x43\x53\x31"
        "\xa9\xa7\x5b\xb9\xf9\xca\x0f\x4d\x00\x56\xa1\x14\xda\xeb\x4c\x5e\x31"
        "\x71\xde\x79\xf9\x04\xf8\x01\xc5\xc2\x4d\x06\x17\xa0\x95\xc5\xa5\xde"
        "\xa5\xff\x7e\x54\x5d\x1f\x7d\x0e\x91\x27\xd8\xfd\xd8\x30\x51\xdb\xb6"
        "\xfc\xb2\x5b\x8d\x09\x96\x72\x93\x85\x5e\x13\xc0\xd4\xbb\x8e\xec\x9b"
        "\xf8\x1b\x53\xf9\xff\xff\xff\xed\x05\xb5\xb6\x0c\xb8\xcd\xf1\x58\xf1"
        "\x8d\xed\xe7\x22\x4c\x46\x55\xe9\x01\x0c\x60\x31\x5b\x96\x45\xa7\x87"
        "\x67\xa5\xca\x8e\xbf\xd7\xd2\xd4\xe6\xae\x97\x06\xf0\x0a\xe9\xd5\x97"
        "\xe9\x00\x00\x62\x89\x7e\x9a\x38\x9d\x91\x43\xe6\xeb\x06\x9e\x03\x7c"
        "\x29\xb9\x75\x68\xc5\x90\x65\xc2\x93\xa5\x13\xbc\xd7\xfe\x78\x80\xf6"
        "\x67\x54\x64\xcb\xaa\x3b\x7d\x66\x0c\xff\xff\xff\xff\xff\xff\xff\xff"
        "\x72\xb6\xfa\x8a\x57\x92\x21\xc6\x77\x6c\xc0\x15\xb2\x6e\x66\xf6\x15"
        "\x51\x06\x9b\x4b\x16\xdf\xdc\x1f\x6e\x72\x5b\xc7\x5a\xa9\x87\x0b\xf6"
        "\xc8\x5a\x78\x17\x95\x18\xc6\xf1\xec\x9a\x1f\xbb\xa7\xb1\x1c\xbe\x38"
        "\x58\x59\x97\x2f\x41\xba\x4c\x2d\xd6\x30\x3f\xdb\x36\xd9\x82\x9c\x00"
        "\x7c\xba\x31\xa1\x50\xc0\xf0\x5d\x0a\xa8\x3f\xaf\x53\x36\xe0\x0b\x07"
        "\x00\x00\x00\x00\x00\x00\x00\x5e\x5f\x99\x12\xfa\x11\x39\x4a\x7d\x34"
        "\x01\x00\x00\x00\x00\xd4\x23\x28\xe0\x75\xd2\x2c\xa6\xe0\xf2\xf1\xde"
        "\x32\x6c\x5b\x96\x86\x93\xc4\x43\x20\x06\x7b\x81\xae\x9c\x93\xcf\x2f"
        "\x99\x22\x09\x56\x5d\x76\xaa\xa1\x44\x7c\x34\x3e\x11\x38\x7d\x69\x19"
        "\x03\xf3\x41\x0a\x3c\x31\xe4\xf1\x3f\xca\xf3\xf0\xb9\x57\xf8\xd7\x2b"
        "\xea\x2c\x76\xc7\x04\x1b\x06\x2a\xbd\x8c\xb3\x8a\x98\x6e\x3d\x51\xd5"
        "\x00\x51\x80\x76\x2e\x4f\xf3\xe7\x3f\x25\x5e\x9b\x2c\xa5\xf6\xb7\x6b"
        "\x38\xc6\xc8\xe1\xf1\xde\xaa\x0e\x5a\xfc\x20\x1d\x59\x6f\x66\xf4\xb2"
        "\x38\xc7\x9f\x77\xb6\xe3\xb7\xd1\x63\x41\x1c\x5e\x5d\xb8\x15\xaa\xe6"
        "\xf3\x77\xbe\x1f\xd0\xb8\x65\x48\xa8\x38\x36\x3b\x56\x1c\x08\xe1\x39"
        "\x76\x5c\x52\xbf\x05\x90\xb0\x83\x98\x26\x6c\x8a\x41\x4d\x56\x69\xf3"
        "\x00\x07\x5c\x87\xe9\xd3\xf9\xfa\xb6\xbf\xbd\x59\xb1\x2a\x2e\x0c\x0d"
        "\xe4\x4b\x19\xc1\x0e\xeb\x9d\x96\x0e\xa1\x4e\x4d\x28\x5c\x61\x0f\xdb"
        "\x6c\x58\x98\xc0\x7d\x43\xfb\x97\x32\xaf\x8e\x7a\xa1\x96\x5b\x1b\x7d"
        "\xcd\x1b\xd3\xec\x59\xfe\x27\xaf\x9e\xfa\x7a\xf3\x55\xfd\xe1\x69\x5d"
        "\xbe\x95\x91\x8d\xc9\x13\xce\xf6\x01\x01\xd4\x5c\xcb\x6e\xe5\xaf\x09"
        "\xf8\x57\xc0\x68\x1b\xe5\x4b\xb1\xcf\x9f\x96\x3c\xec\x8b\x93\x13\xe3"
        "\x4f\x72\xa0\xb1\x9c\xd5\x04\x21\x5c\x97\x77\xf5\xa7\xa7\xbd\x3e\x9a"
        "\x5c\xc3\xe1\xaa\x33\x8d\xea\x66\x23\x5b\xa6\x7e\x5c\xf3\x1e\x27\xb1"
        "\x3e\xa0\x6f\x3e\x3e\xa4\x3b\x9e\x49\xe9\xb0\x14\xaf\xe1\x74\x00\x50"
        "\x46\xac\x5d\xb7\x58\xfc\x17\xfd\xf7\x9f\x10\x18\x24\xbd\x7b\x45\x5e"
        "\x7d\x99\x0e\x83\x1f\xce\xb0\x37\x15\x7f\xfe\x84\xb9\xb7\xb3\x29\x68"
        "\xcc\x4b\x73\x0b\xf2\x8d\x5b\x5b\xe3\x2b\xe1\x7d\xac\xb7\xc6\x27\x3e"
        "\xc1\xac\xef\x44\xc9\xcd\x81\x2c\x81\x41\x6d\xf9\x0a\x84\xbd\xcc\xf4"
        "\x1f\xd6\xc9\xcc\x69\x93\x87\xb1\x44\x0a\xb1\x84\x7b\xe5\x71\x35\x49"
        "\xb6\x09\x26\x50\x8d\x00\x00\x00\x00\x00\x00\x84\xbe\xb7\xf2\x45\xe2"
        "\x43\x14\x6d\x9a\x31\x39\x8b\x76\xf4\xe9\xc0\x32\xc0\xd8\xda\x00\xa1"
        "\xd3\x03\x8c\x3b\xae\xe7\x1f\x78\x30\x8e\xf8\x35\xf0\x0b\x41\x72\x20"
        "\x5f\xca\x3f\xda\x47\x82\xbf\xaf\x55\xe9\x79\x42\xf7\xfd\x39\x32\x44"
        "\xce\xda\x1f\x95\x62\x7e\x07\x59\x41\x47\x04\x88\x06\x69\x05\xb9\x6b"
        "\xeb\x0e\xdd\x6b\xd2\xcf\x01\x55\xd1\x62\x91\x0a\x03\xdf\x05\x90\x9b"
        "\x50\x31\x1a\x60\x58\xe8\x40\x9d",
        1113);
    *(uint32_t*)0x20001219 = -1;
    *(uint8_t*)0x2000121d = -1;
    memcpy(
        (void*)0x20000640,
        "\x78\x9c\xec\xdd\x4f\x68\x1c\xd7\x1d\x07\xf0\xef\xac\x57\xb2\xe5\x82"
        "\xa3\x24\x76\x63\x4a\xa0\x22\x86\xb4\x54\xd4\x96\xb4\x28\xad\x7a\xa9"
        "\x5b\x4a\xd1\x21\x94\x90\x1e\x7a\x16\xf6\x3a\x5e\xbc\x56\x82\xb4\x29"
        "\x4a\x28\xc5\xfd\x7f\xed\x21\xd7\x42\x7a\xd0\xad\xa7\x42\xef\x86\xf6"
        "\xdc\xdc\x72\xd5\x31\x50\xe8\x25\x27\xdd\x5c\x66\x76\x56\x5a\x59\x7f"
        "\x22\xf9\xdf\xae\xaa\xcf\xc7\xbc\x79\xef\xcd\x9b\x79\xf3\xe6\x37\xff"
        "\x76\x56\x98\x0d\x70\x66\x2d\xcf\xa6\xf9\x30\x45\x96\x67\xdf\xde\x28"
        "\xeb\x5b\x9b\xad\xee\xd6\x66\xeb\x7c\xdd\xdc\x4d\x52\x96\x1b\x49\xb3"
        "\x9f\xa5\x58\x4d\x9a\x7f\x49\x6e\xa6\x9f\x32\x31\xd4\x5d\x71\xd8\x76"
        "\x3e\xe9\x2c\xbd\xfb\xf9\x97\x5b\x5f\xf4\x6b\xcd\x3a\x55\xcb\x37\xf6"
        "\x76\xf1\x24\x1e\xd4\x29\x33\x49\xce\xd5\xf9\x7e\xc7\xde\xcc\x9e\xfe"
        "\x6e\x1d\xda\xdf\x71\x15\x3b\x91\x29\x03\x76\xad\xcc\x1f\x3c\x55\x87"
        "\xf0\x6c\x3c\xda\xe7\x44\x67\xe6\xa1\xd7\x3b\x70\x7a\x14\xfd\xe7\xe6"
        "\x3e\xd3\xc9\xc5\x24\x17\xea\xcf\x01\xa9\xef\x0e\x8d\x17\x3b\xba\x67"
        "\xcf\xf3\x17\x00\x00\x80\xb3\xe0\xa5\xed\x6c\x67\x23\x97\x46\x3d\x0e"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x38\x4d\xea\xdf\xff\x2f\xea"
        "\xd4\x18\x94\x67\x52\x0c\x7e\xff\x7f\xb2\x9e\x97\xba\x7c\xaa\x3d\x1c"
        "\xf5\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x19\xf8\xe6\x76\xb6\xb3\x91"
        "\x4b\x83\xfa\xa3\xa2\xfa\x9b\xff\x1b\x55\xe5\x72\x35\xfd\x5a\x3e\xcc"
        "\x7a\xda\x59\xcb\xf5\x6c\x64\x25\xbd\xf4\xb2\x96\xf9\x24\xd3\x43\x1d"
        "\x4d\x6e\xac\xf4\x7a\x6b\xf3\xc7\x58\x73\xe1\xc0\x35\x17\x5e\xcc\xfe"
        "\x02\x00\x00\x00\x00\x00\x00\xc0\xff\xa9\xdf\x66\x79\xf7\xef\xff\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x30\x0e\x8a\xe4\x5c"
        "\x3f\xab\xd2\xe5\x41\x79\x3a\x8d\x66\x92\x0b\x49\x26\xcb\xe5\x1e\x24"
        "\x9f\x0d\xca\xa7\xd9\xc3\x51\x0f\x00\x00\x00\x00\x5e\x80\x97\xb6\xb3"
        "\x9d\x8d\x5c\x1a\xd4\x1f\x15\xd5\x3b\xff\xd7\xab\xf7\xfe\x0b\xf9\x30"
        "\xab\xe9\xa5\x93\x5e\xba\x69\xe7\x76\xf5\x5d\x40\xff\xad\xbf\xb1\xb5"
        "\xd9\xea\x6e\x6d\xb6\xee\x97\x69\x7f\xbf\x3f\xfa\xef\x89\x86\x51\xf5"
        "\x98\xfe\x77\x0f\x07\x6f\xf9\x6a\xb5\xc4\x54\xee\xa4\x53\xcd\xb9\x9e"
        "\x5b\x79\x3f\xdd\xdc\x4e\xa3\x5a\xb3\x74\x75\x30\x9e\x83\xc7\xf5\x9b"
        "\x72\x4c\xc5\x0f\x6b\xc7\x1c\xd9\xed\x3a\x2f\xf7\xfc\xcf\x75\x3e\x1e"
        "\xa6\xab\x88\x4c\xec\x44\x64\xae\x1e\x5b\x19\x8d\x97\x8f\x8e\xc4\x09"
        "\x8f\xce\xe3\x5b\x9a\x4f\x63\xe7\x9b\x9f\xcb\xcf\x21\xe6\x17\xeb\xbc"
        "\xdc\x9f\x3f\x8e\x75\xcc\x17\xea\xb3\xef\xb3\xf2\x9a\x39\x3a\x12\xc9"
        "\xb7\xfe\xf1\xb7\x5f\xdc\xed\xae\xde\xbb\x7b\x67\x7d\x76\x7c\x76\xe9"
        "\x09\x3d\x1e\x89\xd6\xd0\x75\xf8\xda\x99\x8a\xc4\x5c\x15\x89\x2b\x3b"
        "\xf5\xe5\xfc\x34\x3f\xcf\x6c\x66\xf2\x4e\xd6\xd2\xc9\x2f\xb3\x92\x5e"
        "\xda\x99\xc9\x4f\xaa\xd2\x4a\x7d\x3e\x97\xd3\xe9\xa3\x23\x75\x73\x4f"
        "\xed\x9d\xaf\x1a\xc9\x64\x7d\x5c\xfa\x77\xd1\x93\x8d\xe9\x8d\x6a\xdd"
        "\x4b\xe9\xe4\x67\x79\x3f\xb7\xd3\xce\x5b\xd5\xbf\x85\xcc\xe7\x7b\x59"
        "\xcc\x62\x96\x86\x8e\xf0\x95\x63\x5c\xf5\x8d\x93\x5d\xf5\xd7\xbe\x5d"
        "\x17\xa6\x92\xfc\xa9\xce\xc7\x43\x19\xd7\x97\x87\xe2\x3a\x7c\xcf\x9d"
        "\xae\xda\x86\xe7\xec\x46\xe9\x95\xc3\xa3\x54\x3c\xe1\xbd\xb1\xf9\x8d"
        "\xba\x50\x6e\xe3\x77\x75\x3e\x1e\x1e\x8f\xc4\xfc\x50\x24\x5e\x3d\xfa"
        "\x7c\xf9\xeb\xa3\x72\xba\xde\x5d\xbd\xb7\x76\x77\xe5\x83\x63\x6e\xef"
        "\xcd\x3a\x2f\x43\xf9\x87\xb1\x7a\x4a\x94\xe7\xcb\x2b\xe5\xc1\xaa\x6a"
        "\x7b\xcf\x8e\xb2\xed\xd5\x03\xdb\xe6\xab\xb6\xcb\x3b\x6d\x8d\x7d\x6d"
        "\x57\x76\xda\xbe\xea\x4a\x9d\xac\x3f\xc3\xed\xef\x69\xa1\x6a\x7b\xed"
        "\xc0\xb6\x56\xd5\x76\x75\xa8\xed\xa0\xcf\x5b\x00\x8c\xbd\x8b\xdf\xb9"
        "\x38\x39\xf5\x9f\xa9\x7f\x4f\x7d\x3a\xf5\xfb\xa9\xbb\x53\x6f\x5f\xf8"
        "\xf1\xf9\xef\x9f\x7f\x7d\x32\x13\xff\x9a\xf8\x41\x73\xee\xdc\x9b\x8d"
        "\xd7\x8b\xbf\xe7\xd3\xfc\x7a\xf7\xfd\x1f\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x78"
        "\x72\xeb\x1f\x7d\x7c\x6f\xa5\xdb\x6d\xaf\x8d\xb8\x50\xd4\x3f\xe4\x33"
        "\x2e\xe3\x51\x50\x38\xdb\x85\x51\xdf\x99\x80\xe7\xed\x46\xef\xfe\x07"
        "\x37\xd6\x3f\xfa\xf8\xbb\x9d\xfb\x2b\xef\xb5\xdf\x6b\xaf\x4e\x2c\x2e"
        "\x2e\xcd\x2d\x2d\xbe\xd5\xba\x71\xa7\xd3\x6d\xcf\xf5\xa7\xa3\x1e\x25"
        "\xf0\x3c\xec\x3e\xf4\x47\x3d\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\xe0\xb8\x5e\xc4\x7f\x27\x18\xf5\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\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa7"
        "\xdb\xf2\x6c\x9a\x0f\x53\x64\x7e\xee\xfa\x5c\x59\xdf\xda\x6c\x75\xcb"
        "\x34\x28\xef\x2e\xd9\x4c\xd2\x48\x52\xfc\x2a\x29\xfe\x99\xdc\x4c\x3f"
        "\x65\x7a\xa8\xbb\xe2\xb0\xed\x7c\xd2\x59\x7a\xf7\xf3\x2f\xb7\xbe\xd8"
        "\xed\xab\x39\x58\xbe\x71\xd4\x7a\x07\x9a\x78\x7c\xc6\x83\x3a\x65\x26"
        "\xc9\xb9\x3a\x7f\x0a\x7b\xfa\xbb\xf5\xd4\xfd\x15\x3b\x7b\x58\x06\xec"
        "\xda\x20\x70\x30\x6a\xff\x0b\x00\x00\xff\xff\xa0\x96\x0b\x15",
        1511);
    syz_mount_image(/*fs=*/0x20000600, /*dir=*/0x20000140, /*flags=*/0x3a00014,
                    /*opts=*/0x20000dc0, /*chdir=*/3, /*size=*/0x5e7,
                    /*img=*/0x20000640);
    break;
  case 3:
    memcpy((void*)0x20000040, ".\000", 2);
    res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000040ul,
                  /*flags=*/0ul, /*mode=*/0ul);
    if (res != -1)
      r[0] = res;
    break;
  case 4:
    memcpy((void*)0x20000040, ".\000", 2);
    res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000040ul,
                  /*flags=*/0ul, /*mode=*/0ul);
    if (res != -1)
      r[1] = res;
    break;
  case 5:
    memcpy((void*)0x200000c0, "./file2\000", 8);
    memcpy((void*)0x20000180, "./file1\000", 8);
    syscall(__NR_renameat2, /*oldfd=*/r[1], /*old=*/0x200000c0ul,
            /*newfd=*/r[0], /*new=*/0x20000180ul, /*flags=*/0ul);
    break;
  case 6:
    syscall(__NR_renameat2, /*oldfd=*/r[0], /*old=*/0ul, /*newfd=*/r[1],
            /*new=*/0ul, /*flags=*/5ul);
    break;
  }
}
int main(void)
{
  syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/7ul,
          /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
  syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
          /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul);
  for (procid = 0; procid < 6; procid++) {
    if (fork() == 0) {
      use_temporary_dir();
      loop();
    }
  }
  sleep(1000000);
  return 0;
}