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

#define _GNU_SOURCE

#include <endian.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#ifndef SYS___lstat50
#define SYS___lstat50 441
#endif
#ifndef SYS_accept
#define SYS_accept 30
#endif
#ifndef SYS_chdir
#define SYS_chdir 12
#endif
#ifndef SYS_compat_40_mount
#define SYS_compat_40_mount 21
#endif
#ifndef SYS_compat_50___msgctl13
#define SYS_compat_50___msgctl13 302
#endif
#ifndef SYS_lchown
#define SYS_lchown 275
#endif
#ifndef SYS_mkdir
#define SYS_mkdir 136
#endif
#ifndef SYS_mknod
#define SYS_mknod 450
#endif
#ifndef SYS_mmap
#define SYS_mmap 197
#endif
#ifndef SYS_open
#define SYS_open 5
#endif

static unsigned long long procid;

static void kill_and_wait(int pid, int* status)
{
  kill(pid, SIGKILL);
  while (waitpid(-1, status, 0) != pid) {
  }
}

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 execute_one(void);

#define WAIT_FLAGS 0

static void loop(void)
{
  int iter = 0;
  for (;; iter++) {
    int pid = fork();
    if (pid < 0)
      exit(1);
    if (pid == 0) {
      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;
    }
  }
}

uint64_t r[1] = {0x0};

void execute_one(void)
{
  intptr_t res = 0;
  *(uint32_t*)0x20000300 = 0;
  syscall(SYS_accept, /*fd=*/0xffffff9c, /*peer=*/0ul,
          /*peerlen=*/0x20000300ul);
  memcpy((void*)0x20001180, "./file0\000", 8);
  syscall(SYS_mkdir, /*path=*/0x20001180ul, /*mode=*/0ul);
  *(uint64_t*)0x20000260 = 0;
  *(uint64_t*)0x20000268 = 0x200001c0;
  *(uint64_t*)0x200001c0 = 0x20000100;
  *(uint64_t*)0x20000100 = 0;
  *(uint64_t*)0x20000108 = 0;
  *(uint16_t*)0x20000110 = 0;
  *(uint16_t*)0x20000112 = 0;
  *(uint64_t*)0x200001c8 = -1;
  *(uint16_t*)0x200001d0 = 0;
  *(uint16_t*)0x200001d2 = 0;
  syscall(SYS_compat_50___msgctl13, /*msqid=*/0, /*cmd=*/2ul,
          /*buf=*/0x20000200ul);
  memcpy((void*)0x20000100, "./file0/../file0\000", 17);
  syscall(SYS_lchown, /*file=*/0x20000100ul, /*uid=*/0, /*gid=*/0);
  memcpy((void*)0x20000140, "union\000", 6);
  memcpy((void*)0x20000040, "./file0\000", 8);
  syscall(SYS_compat_40_mount, /*type=*/0x20000140ul, /*path=*/0x20000040ul,
          /*flags=*/0ul, /*data=*/0x200001c0ul);
  memcpy((void*)0x20000000, "overlay\000", 8);
  memcpy((void*)0x20000080, "./file0\000", 8);
  syscall(SYS_compat_40_mount, /*type=*/0x20000000ul, /*path=*/0x20000080ul,
          /*flags=*/0ul, /*data=*/0x20000280ul);
  memcpy((void*)0x20000280, "./file0\000", 8);
  syscall(SYS_chdir, /*dir=*/0x20000280ul);
  memcpy((void*)0x20000000, ".\000", 2);
  res = syscall(SYS___lstat50, /*file=*/0x20000000ul, /*ua=*/0x200000c0ul);
  if (res != -1)
    r[0] = *(uint64_t*)0x200000c0;
  memcpy((void*)0x20000000, "./file0\000", 8);
  syscall(SYS_mknod, /*file=*/0x20000000ul, /*mode=*/0x2011ul, /*dev=*/r[0]);
  memcpy((void*)0x20000000, "./file0\000", 8);
  syscall(SYS_open, /*file=*/0x20000000ul, /*flags=*/0ul, /*mode=*/0ul);
  {
    int i;
    for (i = 0; i < 64; i++) {
      syscall(SYS_open, /*file=*/0x20000000ul, /*flags=*/0ul, /*mode=*/0ul);
    }
  }
}
int main(void)
{
  syscall(SYS_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/3ul,
          /*flags=*/0x1012ul, /*fd=*/-1, /*pad=*/0ul, /*offset=*/0ul);
  for (procid = 0; procid < 6; procid++) {
    if (fork() == 0) {
      loop();
    }
  }
  sleep(1000000);
  return 0;
}