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

#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

__attribute__((noreturn)) static void doexit(int status)
{
  volatile unsigned i;
  syscall(__NR_exit_group, status);
  for (i = 0;; i++) {
  }
}
#include <stdint.h>
#include <string.h>

const int kFailStatus = 67;
const int kRetryStatus = 69;

static void fail(const char* msg, ...)
{
  int e = errno;
  va_list args;
  va_start(args, msg);
  vfprintf(stderr, msg, args);
  va_end(args);
  fprintf(stderr, " (errno %d)\n", e);
  doexit((e == ENOMEM || e == EAGAIN) ? kRetryStatus : kFailStatus);
}

static uint64_t current_time_ms()
{
  struct timespec ts;

  if (clock_gettime(CLOCK_MONOTONIC, &ts))
    fail("clock_gettime failed");
  return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}

static void test();

void loop()
{
  int iter;
  for (iter = 0;; iter++) {
    int pid = fork();
    if (pid < 0)
      fail("loop fork failed");
    if (pid == 0) {
      prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
      setpgrp();
      test();
      doexit(0);
    }
    int status = 0;
    uint64_t start = current_time_ms();
    for (;;) {
      int res = waitpid(-1, &status, __WALL | WNOHANG);
      if (res == pid)
        break;
      usleep(1000);
      if (current_time_ms() - start > 5 * 1000) {
        kill(-pid, SIGKILL);
        kill(pid, SIGKILL);
        while (waitpid(-1, &status, __WALL) != pid) {
        }
        break;
      }
    }
  }
}

long r[2];
void test()
{
  memset(r, -1, sizeof(r));
  syscall(__NR_mmap, 0x20000000, 0xe76000, 3, 0x32, -1, 0);
  *(uint16_t*)0x20a51fec = 0x11;
  *(uint16_t*)0x20a51fee = htobe16(4);
  *(uint32_t*)0x20a51ff0 = 0;
  *(uint16_t*)0x20a51ff4 = 1;
  *(uint8_t*)0x20a51ff6 = 2;
  *(uint8_t*)0x20a51ff7 = 6;
  *(uint8_t*)0x20a51ff8 = 0xaa;
  *(uint8_t*)0x20a51ff9 = 0xaa;
  *(uint8_t*)0x20a51ffa = 0xaa;
  *(uint8_t*)0x20a51ffb = 0xaa;
  *(uint8_t*)0x20a51ffc = 0;
  *(uint8_t*)0x20a51ffd = 0x15;
  *(uint8_t*)0x20a51ffe = 0;
  *(uint8_t*)0x20a51fff = 0;
  syscall(__NR_sendto, -1, 0x20000f05, 0, 0x20008000, 0x20a51fec, 0x14);
  *(uint32_t*)0x20001000 = 0;
  *(uint32_t*)0x20001004 = 9;
  *(uint32_t*)0x20001008 = 0x8001;
  memcpy((void*)0x2000100c,
         "\x71\x75\x65\x75\x65\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         64);
  *(uint32_t*)0x2000104c = 1;
  *(uint8_t*)0x20001050 = 0;
  *(uint8_t*)0x20001051 = 0;
  *(uint8_t*)0x20001052 = 0;
  *(uint8_t*)0x20001053 = 0;
  *(uint8_t*)0x20001054 = 0;
  *(uint8_t*)0x20001055 = 0;
  *(uint8_t*)0x20001056 = 0;
  *(uint8_t*)0x20001057 = 0;
  *(uint8_t*)0x20001058 = 0;
  *(uint8_t*)0x20001059 = 0;
  *(uint8_t*)0x2000105a = 0;
  *(uint8_t*)0x2000105b = 0;
  *(uint8_t*)0x2000105c = 0;
  *(uint8_t*)0x2000105d = 0;
  *(uint8_t*)0x2000105e = 0;
  *(uint8_t*)0x2000105f = 0;
  *(uint8_t*)0x20001060 = 0;
  *(uint8_t*)0x20001061 = 0;
  *(uint8_t*)0x20001062 = 0;
  *(uint8_t*)0x20001063 = 0;
  *(uint8_t*)0x20001064 = 0;
  *(uint8_t*)0x20001065 = 0;
  *(uint8_t*)0x20001066 = 0;
  *(uint8_t*)0x20001067 = 0;
  *(uint8_t*)0x20001068 = 0;
  *(uint8_t*)0x20001069 = 0;
  *(uint8_t*)0x2000106a = 0;
  *(uint8_t*)0x2000106b = 0;
  *(uint8_t*)0x2000106c = 0;
  *(uint8_t*)0x2000106d = 0;
  *(uint8_t*)0x2000106e = 0;
  *(uint8_t*)0x2000106f = 0;
  *(uint8_t*)0x20001070 = 0;
  *(uint8_t*)0x20001071 = 0;
  *(uint8_t*)0x20001072 = 0;
  *(uint8_t*)0x20001073 = 0;
  *(uint8_t*)0x20001074 = 0;
  *(uint8_t*)0x20001075 = 0;
  *(uint8_t*)0x20001076 = 0;
  *(uint8_t*)0x20001077 = 0;
  *(uint8_t*)0x20001078 = 0;
  *(uint8_t*)0x20001079 = 0;
  *(uint8_t*)0x2000107a = 0;
  *(uint8_t*)0x2000107b = 0;
  *(uint8_t*)0x2000107c = 0;
  *(uint8_t*)0x2000107d = 0;
  *(uint8_t*)0x2000107e = 0;
  *(uint8_t*)0x2000107f = 0;
  *(uint8_t*)0x20001080 = 0;
  *(uint8_t*)0x20001081 = 0;
  *(uint8_t*)0x20001082 = 0;
  *(uint8_t*)0x20001083 = 0;
  *(uint8_t*)0x20001084 = 0;
  *(uint8_t*)0x20001085 = 0;
  *(uint8_t*)0x20001086 = 0;
  *(uint8_t*)0x20001087 = 0;
  *(uint8_t*)0x20001088 = 0;
  *(uint8_t*)0x20001089 = 0;
  *(uint8_t*)0x2000108a = 0;
  *(uint8_t*)0x2000108b = 0;
  syscall(__NR_ioctl, -1, 0xc08c5336, 0x20001000);
  r[0] = syscall(__NR_socket, 0xa, 2, 0);
  r[1] = syscall(__NR_socket, 0x18, 1, 1);
  *(uint16_t*)0x20e71000 = 0x18;
  *(uint32_t*)0x20e71002 = 1;
  *(uint32_t*)0x20e71006 = 0;
  *(uint32_t*)0x20e7100a = r[0];
  *(uint16_t*)0x20e7100e = 3;
  *(uint16_t*)0x20e71010 = 0;
  *(uint16_t*)0x20e71012 = 3;
  *(uint16_t*)0x20e71014 = 0;
  *(uint16_t*)0x20e71016 = 0xa;
  *(uint16_t*)0x20e71018 = htobe16(0x4e22);
  *(uint32_t*)0x20e7101a = 3;
  *(uint8_t*)0x20e7101e = 0xfe;
  *(uint8_t*)0x20e7101f = 0x80;
  *(uint8_t*)0x20e71020 = 0;
  *(uint8_t*)0x20e71021 = 0;
  *(uint8_t*)0x20e71022 = 0;
  *(uint8_t*)0x20e71023 = 0;
  *(uint8_t*)0x20e71024 = 0;
  *(uint8_t*)0x20e71025 = 0;
  *(uint8_t*)0x20e71026 = 0;
  *(uint8_t*)0x20e71027 = 0;
  *(uint8_t*)0x20e71028 = 0;
  *(uint8_t*)0x20e71029 = 0;
  *(uint8_t*)0x20e7102a = 0;
  *(uint8_t*)0x20e7102b = 0;
  *(uint8_t*)0x20e7102c = 0;
  *(uint8_t*)0x20e7102d = 0xbb;
  *(uint32_t*)0x20e7102e = 4;
  syscall(__NR_connect, r[1], 0x20e71000, 0x32);
  syscall(__NR_fallocate, r[1], 0, 0xffffffff, 0x3d);
  syscall(__NR_socket, 0x18, 1, 1);
  syscall(__NR_close, r[0]);
}

int main()
{
  for (;;) {
    loop();
  }
}