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

#define _GNU_SOURCE

#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[25];
void test()
{
  memset(r, -1, sizeof(r));
  r[0] = syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul,
                 0xfffffffffffffffful, 0x0ul);
  r[1] = syscall(__NR_socket, 0x26ul, 0x5ul, 0x0ul);
  *(uint16_t*)0x20859000 = (uint16_t)0x26;
  memcpy((void*)0x20859002,
         "\x61\x65\x61\x64\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
         14);
  *(uint32_t*)0x20859010 = (uint32_t)0x0;
  *(uint32_t*)0x20859014 = (uint32_t)0x0;
  memcpy((void*)0x20859018,
         "\x67\x63\x6d\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], 0x20859000ul, 0x58ul);
  memcpy((void*)0x20e95ff0, "\x64\x9c\x47\xec\xff\xff\xff\x00\x54\x2d"
                            "\xb5\x0f\x41\x9d\x4d\x00",
         16);
  r[9] = syscall(__NR_setsockopt, r[1], 0x117ul, 0x1ul, 0x20e95ff0ul,
                 0x10ul);
  r[10] = syscall(__NR_accept, r[1], 0x0ul, 0x0ul);
  memcpy((void*)0x20f3cf75,
         "\x35\x95\xc9\x17\xd6\x71\xb6\xb5\x53\x5f\x00\x42\xbd\x83\xa6"
         "\xd2\x6b\x71\xaa\x9a\x6d\xea\xf4\xce\xde\xed\x63\xf6\xad\x0a"
         "\x14\xcf\xab\x6e\x65\xf4\xbc\xd7\x40\x82\x9a\x3a\x50\xfa\xc8"
         "\xec\x4c\x77\xb3\x74\x9b\x24\x4a\xbc\xf0\xb5\xa2\x8c\x60\xf6"
         "\xd0\x43\x0a\xce\x54\xba\xa4\x15\x6e\xbb\xfa\xa2\xff\xdf\xf4"
         "\xbb\xa9\x89\xf4\xae\xbc\xcd\x1f\x87\x87\x87\xd9\xfc\x26\xac"
         "\x60\x20\x14\x40\xff\x95\x83\xcc\x2d\x54\x39\x16\x8d\xd6\x23"
         "\xe5\xcb\xbe\xc8\x5d\x30\xdb\x1a\xb1\x64\x17\x4f\xe4\x0b\x99"
         "\x98\x89\xf0\x29\x88\x36\x9b\xe7\x57\x03\x71\x78\xac\x1e\x74"
         "\x89\x49\x2f\xc2",
         139);
  r[12] = syscall(__NR_write, r[10], 0x20f3cf75ul, 0x8bul);
  *(uint64_t*)0x20aecfc8 = (uint64_t)0x20f3d000;
  *(uint32_t*)0x20aecfd0 = (uint32_t)0x10;
  *(uint64_t*)0x20aecfd8 = (uint64_t)0x20f3d000;
  *(uint64_t*)0x20aecfe0 = (uint64_t)0x2;
  *(uint64_t*)0x20aecfe8 = (uint64_t)0x20f3d000;
  *(uint64_t*)0x20aecff0 = (uint64_t)0x0;
  *(uint32_t*)0x20aecff8 = (uint32_t)0x100000000;
  *(uint64_t*)0x20f3d000 = (uint64_t)0x20f3df84;
  *(uint64_t*)0x20f3d008 = (uint64_t)0x0;
  *(uint64_t*)0x20f3d010 = (uint64_t)0x20f3dfaf;
  *(uint64_t*)0x20f3d018 = (uint64_t)0x51;
  r[24] = syscall(__NR_recvmsg, r[10], 0x20aecfc8ul, 0x10002ul);
}

int main()
{
  loop();
  return 0;
}