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

#define _GNU_SOURCE

#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.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[54];
void* thr(void* arg)
{
  switch ((long)arg) {
  case 0:
    r[0] = syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul,
                   0xfffffffffffffffful, 0x0ul);
    break;
  case 1:
    r[1] = syscall(__NR_socket, 0x26ul, 0x5ul, 0x0ul);
    break;
  case 2:
    *(uint16_t*)0x20f74fa8 = (uint16_t)0x26;
    memcpy((void*)0x20f74faa,
           "\x73\x6b\x63\x69\x70\x68\x65\x72\x00\x00\x00\x00\x00\x00",
           14);
    *(uint32_t*)0x20f74fb8 = (uint32_t)0x0;
    *(uint32_t*)0x20f74fbc = (uint32_t)0x0;
    memcpy((void*)0x20f74fc0,
           "\x6c\x72\x77\x28\x74\x77\x6f\x66\x69\x73\x68\x29\x00\x00"
           "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
           "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\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);
    r[7] = syscall(__NR_bind, r[1], 0x20f74fa8ul, 0x58ul);
    break;
  case 3:
    memcpy((void*)0x201ec000, "\x0a\x07\x75\xb0\xd5\xe3\x83\xe5\xb3\xb6"
                              "\x0c\xed\x5c\x54\xdb\xb7\x29\x5d\xf0\xdf"
                              "\x82\x17\xad\x40\x00\x00\x00\x00\x00\x00"
                              "\x00\xe6",
           32);
    r[9] = syscall(__NR_setsockopt, r[1], 0x117ul, 0x1ul, 0x201ec000ul,
                   0x20ul);
    break;
  case 4:
    r[10] = syscall(__NR_accept, r[1], 0x0ul, 0x0ul);
    break;
  case 5:
    *(uint64_t*)0x20f74fc8 = (uint64_t)0x0;
    *(uint32_t*)0x20f74fd0 = (uint32_t)0x0;
    *(uint64_t*)0x20f74fd8 = (uint64_t)0x20f74000;
    *(uint64_t*)0x20f74fe0 = (uint64_t)0x1;
    *(uint64_t*)0x20f74fe8 = (uint64_t)0x20df0000;
    *(uint64_t*)0x20f74ff0 = (uint64_t)0x60;
    *(uint32_t*)0x20f74ff8 = (uint32_t)0x20008090;
    *(uint64_t*)0x20f74000 = (uint64_t)0x2004b000;
    *(uint64_t*)0x20f74008 = (uint64_t)0x80;
    memcpy((void*)0x2004b000,
           "\x5d\xfb\xc3\x3d\xc1\x9c\xb8\x70\x84\x3d\xf3\x02\x73\xb3"
           "\x81\xfa\xa8\xd6\x2a\x74\xea\xc9\x3d\x92\x5f\x73\x14\x76"
           "\x83\xc8\x0e\x60\x33\x71\x91\xa5\x8d\xf0\xc2\xc6\xd5\xb8"
           "\x70\xca\x6e\x04\xca\xf4\x1e\xab\x5e\x60\x6a\x54\x00\xab"
           "\x3f\x2b\x56\xcf\xac\x84\x40\x12\x00\x88\x5e\x8e\xd0\x98"
           "\xaa\x66\x15\xba\x7d\x2e\x3a\x54\x5e\x7c\x9e\xbb\x48\x84"
           "\x3f\x0a\x66\x11\x47\x64\x42\x88\xac\x33\xc0\x34\xe2\x1d"
           "\x98\x66\xce\xa3\x16\x44\x1b\x3f\xca\x92\x94\x25\xac\x31"
           "\xac\x96\xfa\xe3\x8c\x27\x9f\xaa\x18\xcb\xd3\xfc\x29\x2a"
           "\xab\x32",
           128);
    *(uint64_t*)0x20df0000 = (uint64_t)0x18;
    *(uint32_t*)0x20df0008 = (uint32_t)0x117;
    *(uint32_t*)0x20df000c = (uint32_t)0x4;
    *(uint32_t*)0x20df0010 = (uint32_t)0x6;
    *(uint64_t*)0x20df0018 = (uint64_t)0x18;
    *(uint32_t*)0x20df0020 = (uint32_t)0x117;
    *(uint32_t*)0x20df0024 = (uint32_t)0x3;
    *(uint32_t*)0x20df0028 = (uint32_t)0x1;
    *(uint64_t*)0x20df0030 = (uint64_t)0x18;
    *(uint32_t*)0x20df0038 = (uint32_t)0x117;
    *(uint32_t*)0x20df003c = (uint32_t)0x3;
    *(uint32_t*)0x20df0040 = (uint32_t)0x1;
    *(uint64_t*)0x20df0048 = (uint64_t)0x18;
    *(uint32_t*)0x20df0050 = (uint32_t)0x117;
    *(uint32_t*)0x20df0054 = (uint32_t)0x4;
    *(uint32_t*)0x20df0058 = (uint32_t)0x5;
    r[37] = syscall(__NR_sendmsg, r[10], 0x20f74fc8ul, 0x41ul);
    break;
  case 6:
    *(uint64_t*)0x20b2dfc8 = (uint64_t)0x207ebffa;
    *(uint32_t*)0x20b2dfd0 = (uint32_t)0x6;
    *(uint64_t*)0x20b2dfd8 = (uint64_t)0x20f75000;
    *(uint64_t*)0x20b2dfe0 = (uint64_t)0x4;
    *(uint64_t*)0x20b2dfe8 = (uint64_t)0x20ec7000;
    *(uint64_t*)0x20b2dff0 = (uint64_t)0x0;
    *(uint32_t*)0x20b2dff8 = (uint32_t)0xffffdffb;
    *(uint64_t*)0x20f75000 = (uint64_t)0x2088bfef;
    *(uint64_t*)0x20f75008 = (uint64_t)0x11;
    *(uint64_t*)0x20f75010 = (uint64_t)0x2074b000;
    *(uint64_t*)0x20f75018 = (uint64_t)0x0;
    *(uint64_t*)0x20f75020 = (uint64_t)0x20f11000;
    *(uint64_t*)0x20f75028 = (uint64_t)0x0;
    *(uint64_t*)0x20f75030 = (uint64_t)0x20f75000;
    *(uint64_t*)0x20f75038 = (uint64_t)0xe4;
    r[53] = syscall(__NR_recvmsg, r[10], 0x20b2dfc8ul, 0x2021ul);
    break;
  }
  return 0;
}

void test()
{
  long i;
  pthread_t th[14];

  memset(r, -1, sizeof(r));
  srand(getpid());
  for (i = 0; i < 7; i++) {
    pthread_create(&th[i], 0, thr, (void*)i);
    usleep(rand() % 10000);
  }
  for (i = 0; i < 7; i++) {
    pthread_create(&th[7 + i], 0, thr, (void*)i);
    if (rand() % 2)
      usleep(rand() % 10000);
  }
  usleep(rand() % 100000);
}

int main()
{
  int i;
  for (i = 0; i < 8; i++) {
    if (fork() == 0) {
      loop();
      return 0;
    }
  }
  sleep(1000000);
  return 0;
}