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

#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <linux/futex.h>
#include <linux/net.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/socket.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 <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <string.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 __thread int skip_segv;
static __thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* uctx)
{
  uintptr_t addr = (uintptr_t)info->si_addr;
  const uintptr_t prog_start = 1 << 20;
  const uintptr_t prog_end = 100 << 20;
  if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
      (addr < prog_start || addr > prog_end)) {
    _longjmp(segv_env, 1);
  }
  doexit(sig);
}

static void install_segv_handler()
{
  struct sigaction sa;

  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = SIG_IGN;
  syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);

  memset(&sa, 0, sizeof(sa));
  sa.sa_sigaction = segv_handler;
  sa.sa_flags = SA_NODEFER | SA_SIGINFO;
  sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGBUS, &sa, NULL);
}

#define NONFAILING(...)                                                        \
  {                                                                            \
    __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
    if (_setjmp(segv_env) == 0) {                                              \
      __VA_ARGS__;                                                             \
    }                                                                          \
    __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST);                       \
  }

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;
}

struct ipt_getinfo {
  char name[32];
  unsigned int valid_hooks;
  unsigned int hook_entry[5];
  unsigned int underflow[5];
  unsigned int num_entries;
  unsigned int size;
};

struct ipt_get_entries {
  char name[32];
  unsigned int size;
  void* entrytable[1024 / sizeof(void*)];
};

struct xt_counters {
  uint64_t pcnt, bcnt;
};

struct ipt_replace {
  char name[32];
  unsigned int valid_hooks;
  unsigned int num_entries;
  unsigned int size;
  unsigned int hook_entry[5];
  unsigned int underflow[5];
  unsigned int num_counters;
  struct xt_counters* counters;
  char entrytable[1024];
};

struct ipt_table_desc {
  const char* name;
  struct ipt_getinfo info;
  struct ipt_get_entries entries;
  struct ipt_replace replace;
  struct xt_counters counters[10];
};

static struct ipt_table_desc ipv4_tables[] = {
    {.name = "filter"}, {.name = "nat"},      {.name = "mangle"},
    {.name = "raw"},    {.name = "security"},
};

#define IPT_BASE_CTL 64
#define IPT_SO_SET_REPLACE (IPT_BASE_CTL)
#define IPT_SO_GET_INFO (IPT_BASE_CTL)
#define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1)

static void checkpoint_net_namespace(void)
{
  socklen_t optlen;
  unsigned i;
  int fd;

  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd == -1)
    fail("socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)");
  for (i = 0; i < sizeof(ipv4_tables) / sizeof(ipv4_tables[0]); i++) {
    struct ipt_table_desc* table = &ipv4_tables[i];
    strcpy(table->info.name, table->name);
    strcpy(table->entries.name, table->name);
    strcpy(table->replace.name, table->name);
    optlen = sizeof(table->info);
    if (getsockopt(fd, SOL_IP, IPT_SO_GET_INFO, &table->info, &optlen)) {
      switch (errno) {
      case EPERM:
      case ENOENT:
      case ENOPROTOOPT:
        continue;
      }
      fail("getsockopt(IPT_SO_GET_INFO)");
    }
    if (table->info.size > sizeof(table->entries.entrytable))
      fail("table size is too large: %u", table->info.size);
    if (table->info.num_entries >
        sizeof(table->counters) / sizeof(table->counters[0]))
      fail("too many counters: %u", table->info.num_entries);
    table->entries.size = table->info.size;
    optlen = sizeof(table->entries) - sizeof(table->entries.entrytable) +
             table->info.size;
    if (getsockopt(fd, SOL_IP, IPT_SO_GET_ENTRIES, &table->entries, &optlen))
      fail("getsockopt(IPT_SO_GET_ENTRIES)");
    table->replace.valid_hooks = table->info.valid_hooks;
    table->replace.num_entries = table->info.num_entries;
    table->replace.counters = table->counters;
    table->replace.size = table->info.size;
    memcpy(table->replace.hook_entry, table->info.hook_entry,
           sizeof(table->replace.hook_entry));
    memcpy(table->replace.underflow, table->info.underflow,
           sizeof(table->replace.underflow));
    memcpy(table->replace.entrytable, table->entries.entrytable,
           table->info.size);
  }
  close(fd);
}

static void reset_net_namespace(void)
{
  struct ipt_get_entries entries;
  struct ipt_getinfo info;
  socklen_t optlen;
  unsigned i;
  int fd;

  memset(&info, 0, sizeof(info));
  memset(&entries, 0, sizeof(entries));
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd == -1)
    fail("socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)");
  for (i = 0; i < sizeof(ipv4_tables) / sizeof(ipv4_tables[0]); i++) {
    struct ipt_table_desc* table = &ipv4_tables[i];
    if (table->info.valid_hooks == 0)
      continue;
    strcpy(info.name, table->name);
    optlen = sizeof(info);
    if (getsockopt(fd, SOL_IP, IPT_SO_GET_INFO, &info, &optlen))
      fail("getsockopt(IPT_SO_GET_INFO)");
    if (memcmp(&table->info, &info, sizeof(table->info)) == 0) {
      strcpy(entries.name, table->name);
      entries.size = table->info.size;
      optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size;
      if (getsockopt(fd, SOL_IP, IPT_SO_GET_ENTRIES, &entries, &optlen))
        fail("getsockopt(IPT_SO_GET_ENTRIES)");
      if (memcmp(&table->entries, &entries, optlen) == 0)
        continue;
    }
    table->replace.num_counters = info.num_entries;
    optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) +
             table->replace.size;
    if (setsockopt(fd, SOL_IP, IPT_SO_SET_REPLACE, &table->replace, optlen))
      fail("setsockopt(IPT_SO_SET_REPLACE)");
  }
  close(fd);
}

static void test();

void loop()
{
  int iter;
  checkpoint_net_namespace();
  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;
      }
    }
    reset_net_namespace();
  }
}

struct thread_t {
  int created, running, call;
  pthread_t th;
};

static struct thread_t threads[16];
static void execute_call(int call);
static int running;
static int collide;

static void* thr(void* arg)
{
  struct thread_t* th = (struct thread_t*)arg;
  for (;;) {
    while (!__atomic_load_n(&th->running, __ATOMIC_ACQUIRE))
      syscall(SYS_futex, &th->running, FUTEX_WAIT, 0, 0);
    execute_call(th->call);
    __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
    __atomic_store_n(&th->running, 0, __ATOMIC_RELEASE);
    syscall(SYS_futex, &th->running, FUTEX_WAKE);
  }
  return 0;
}

static void execute(int num_calls)
{
  int call, thread;
  running = 0;
  for (call = 0; call < num_calls; call++) {
    for (thread = 0; thread < sizeof(threads) / sizeof(threads[0]); thread++) {
      struct thread_t* th = &threads[thread];
      if (!th->created) {
        th->created = 1;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 128 << 10);
        pthread_create(&th->th, &attr, thr, th);
      }
      if (!__atomic_load_n(&th->running, __ATOMIC_ACQUIRE)) {
        th->call = call;
        __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
        __atomic_store_n(&th->running, 1, __ATOMIC_RELEASE);
        syscall(SYS_futex, &th->running, FUTEX_WAKE);
        if (collide && call % 2)
          break;
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = 20 * 1000 * 1000;
        syscall(SYS_futex, &th->running, FUTEX_WAIT, 1, &ts);
        if (running)
          usleep((call == num_calls - 1) ? 10000 : 1000);
        break;
      }
    }
  }
}

long r[3];
void execute_call(int call)
{
  switch (call) {
  case 0:
    r[0] = syscall(__NR_socket, 0xa, 3, 0x3a);
    break;
  case 1:
    NONFAILING(*(uint32_t*)0x2095affc = 2);
    syscall(__NR_setsockopt, r[0], 0x29, 1, 0x2095affc, 4);
    break;
  case 2:
    syscall(__NR_mmap, 0x20000000, 0x1000, 3, 0x32, -1, 0);
    break;
  case 3:
    syscall(__NR_socketpair, 0xa, 2, 0, 0x20000ff8);
    break;
  case 4:
    syscall(__NR_mmap, 0x20000000, 0xfff000, 3, 0x32, -1, 0);
    break;
  case 5:
    r[1] = syscall(__NR_socket, 0xa, 0x80001, 0);
    break;
  case 6:
    NONFAILING(*(uint32_t*)0x2000cffc = 0x54);
    syscall(__NR_getsockopt, r[1], 0x29, 0x40, 0x2000efac, 0x2000cffc);
    break;
  case 7:
    syscall(__NR_mmap, 0x20000000, 0x1000, 3, 0x32, -1, 0);
    break;
  case 8:
    syscall(__NR_socketpair, 2, 0x800, 0x1f, 0x20000ff8);
    break;
  case 9:
    NONFAILING(*(uint32_t*)0x205a5000 = 0);
    NONFAILING(*(uint16_t*)0x205a5004 = 0xa);
    NONFAILING(*(uint16_t*)0x205a5006 = htobe16(0x4e21));
    NONFAILING(*(uint32_t*)0x205a5008 = 7);
    NONFAILING(*(uint8_t*)0x205a500c = -1);
    NONFAILING(*(uint8_t*)0x205a500d = 1);
    NONFAILING(*(uint8_t*)0x205a500e = 0);
    NONFAILING(*(uint8_t*)0x205a500f = 0);
    NONFAILING(*(uint8_t*)0x205a5010 = 0);
    NONFAILING(*(uint8_t*)0x205a5011 = 0);
    NONFAILING(*(uint8_t*)0x205a5012 = 0);
    NONFAILING(*(uint8_t*)0x205a5013 = 0);
    NONFAILING(*(uint8_t*)0x205a5014 = 0);
    NONFAILING(*(uint8_t*)0x205a5015 = 0);
    NONFAILING(*(uint8_t*)0x205a5016 = 0);
    NONFAILING(*(uint8_t*)0x205a5017 = 0);
    NONFAILING(*(uint8_t*)0x205a5018 = 0);
    NONFAILING(*(uint8_t*)0x205a5019 = 0);
    NONFAILING(*(uint8_t*)0x205a501a = 0);
    NONFAILING(*(uint8_t*)0x205a501b = 1);
    NONFAILING(*(uint32_t*)0x205a501c = 1);
    NONFAILING(*(uint64_t*)0x205a5024 = 0);
    NONFAILING(*(uint64_t*)0x205a502c = 0);
    NONFAILING(*(uint64_t*)0x205a5034 = 0);
    NONFAILING(*(uint64_t*)0x205a503c = 0);
    NONFAILING(*(uint64_t*)0x205a5044 = 0);
    NONFAILING(*(uint64_t*)0x205a504c = 0);
    NONFAILING(*(uint64_t*)0x205a5054 = 0);
    NONFAILING(*(uint64_t*)0x205a505c = 0);
    NONFAILING(*(uint64_t*)0x205a5064 = 0);
    NONFAILING(*(uint64_t*)0x205a506c = 0);
    NONFAILING(*(uint64_t*)0x205a5074 = 0);
    NONFAILING(*(uint64_t*)0x205a507c = 0);
    NONFAILING(*(uint32_t*)0x20000000 = 0x8c);
    syscall(__NR_getsockopt, 0xffffff9c, 0x84, 6, 0x205a5000, 0x20000000);
    break;
  case 10:
    r[2] = syscall(__NR_socket, 2, 0x80005, 0);
    break;
  case 11:
    NONFAILING(memcpy((void*)0x20013c90, "\x66\x69\x6c\x74\x65\x72\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00",
                      32));
    NONFAILING(*(uint32_t*)0x20013cb0 = 0xe);
    NONFAILING(*(uint32_t*)0x20013cb4 = 4);
    NONFAILING(*(uint32_t*)0x20013cb8 = 0x298);
    NONFAILING(*(uint32_t*)0x20013cbc = -1);
    NONFAILING(*(uint32_t*)0x20013cc0 = 0);
    NONFAILING(*(uint32_t*)0x20013cc4 = 0);
    NONFAILING(*(uint32_t*)0x20013cc8 = 0);
    NONFAILING(*(uint32_t*)0x20013ccc = -1);
    NONFAILING(*(uint32_t*)0x20013cd0 = -1);
    NONFAILING(*(uint32_t*)0x20013cd4 = 0);
    NONFAILING(*(uint32_t*)0x20013cd8 = 0);
    NONFAILING(*(uint32_t*)0x20013cdc = 0);
    NONFAILING(*(uint32_t*)0x20013ce0 = -1);
    NONFAILING(*(uint32_t*)0x20013ce4 = 4);
    NONFAILING(*(uint64_t*)0x20013ce8 = 0x20001000);
    NONFAILING(*(uint8_t*)0x20013cf0 = 0);
    NONFAILING(*(uint8_t*)0x20013cf1 = 0);
    NONFAILING(*(uint8_t*)0x20013cf2 = 0);
    NONFAILING(*(uint8_t*)0x20013cf3 = 0);
    NONFAILING(*(uint8_t*)0x20013cf4 = 0);
    NONFAILING(*(uint8_t*)0x20013cf5 = 0);
    NONFAILING(*(uint8_t*)0x20013cf6 = 0);
    NONFAILING(*(uint8_t*)0x20013cf7 = 0);
    NONFAILING(*(uint8_t*)0x20013cf8 = 0);
    NONFAILING(*(uint8_t*)0x20013cf9 = 0);
    NONFAILING(*(uint8_t*)0x20013cfa = 0);
    NONFAILING(*(uint8_t*)0x20013cfb = 0);
    NONFAILING(*(uint8_t*)0x20013cfc = 0);
    NONFAILING(*(uint8_t*)0x20013cfd = 0);
    NONFAILING(*(uint8_t*)0x20013cfe = 0);
    NONFAILING(*(uint8_t*)0x20013cff = 0);
    NONFAILING(*(uint8_t*)0x20013d00 = 0);
    NONFAILING(*(uint8_t*)0x20013d01 = 0);
    NONFAILING(*(uint8_t*)0x20013d02 = 0);
    NONFAILING(*(uint8_t*)0x20013d03 = 0);
    NONFAILING(*(uint8_t*)0x20013d04 = 0);
    NONFAILING(*(uint8_t*)0x20013d05 = 0);
    NONFAILING(*(uint8_t*)0x20013d06 = 0);
    NONFAILING(*(uint8_t*)0x20013d07 = 0);
    NONFAILING(*(uint8_t*)0x20013d08 = 0);
    NONFAILING(*(uint8_t*)0x20013d09 = 0);
    NONFAILING(*(uint8_t*)0x20013d0a = 0);
    NONFAILING(*(uint8_t*)0x20013d0b = 0);
    NONFAILING(*(uint8_t*)0x20013d0c = 0);
    NONFAILING(*(uint8_t*)0x20013d0d = 0);
    NONFAILING(*(uint8_t*)0x20013d0e = 0);
    NONFAILING(*(uint8_t*)0x20013d0f = 0);
    NONFAILING(*(uint8_t*)0x20013d10 = 0);
    NONFAILING(*(uint8_t*)0x20013d11 = 0);
    NONFAILING(*(uint8_t*)0x20013d12 = 0);
    NONFAILING(*(uint8_t*)0x20013d13 = 0);
    NONFAILING(*(uint8_t*)0x20013d14 = 0);
    NONFAILING(*(uint8_t*)0x20013d15 = 0);
    NONFAILING(*(uint8_t*)0x20013d16 = 0);
    NONFAILING(*(uint8_t*)0x20013d17 = 0);
    NONFAILING(*(uint8_t*)0x20013d18 = 0);
    NONFAILING(*(uint8_t*)0x20013d19 = 0);
    NONFAILING(*(uint8_t*)0x20013d1a = 0);
    NONFAILING(*(uint8_t*)0x20013d1b = 0);
    NONFAILING(*(uint8_t*)0x20013d1c = 0);
    NONFAILING(*(uint8_t*)0x20013d1d = 0);
    NONFAILING(*(uint8_t*)0x20013d1e = 0);
    NONFAILING(*(uint8_t*)0x20013d1f = 0);
    NONFAILING(*(uint8_t*)0x20013d20 = 0);
    NONFAILING(*(uint8_t*)0x20013d21 = 0);
    NONFAILING(*(uint8_t*)0x20013d22 = 0);
    NONFAILING(*(uint8_t*)0x20013d23 = 0);
    NONFAILING(*(uint8_t*)0x20013d24 = 0);
    NONFAILING(*(uint8_t*)0x20013d25 = 0);
    NONFAILING(*(uint8_t*)0x20013d26 = 0);
    NONFAILING(*(uint8_t*)0x20013d27 = 0);
    NONFAILING(*(uint8_t*)0x20013d28 = 0);
    NONFAILING(*(uint8_t*)0x20013d29 = 0);
    NONFAILING(*(uint8_t*)0x20013d2a = 0);
    NONFAILING(*(uint8_t*)0x20013d2b = 0);
    NONFAILING(*(uint8_t*)0x20013d2c = 0);
    NONFAILING(*(uint8_t*)0x20013d2d = 0);
    NONFAILING(*(uint8_t*)0x20013d2e = 0);
    NONFAILING(*(uint8_t*)0x20013d2f = 0);
    NONFAILING(*(uint8_t*)0x20013d30 = 0);
    NONFAILING(*(uint8_t*)0x20013d31 = 0);
    NONFAILING(*(uint8_t*)0x20013d32 = 0);
    NONFAILING(*(uint8_t*)0x20013d33 = 0);
    NONFAILING(*(uint8_t*)0x20013d34 = 0);
    NONFAILING(*(uint8_t*)0x20013d35 = 0);
    NONFAILING(*(uint8_t*)0x20013d36 = 0);
    NONFAILING(*(uint8_t*)0x20013d37 = 0);
    NONFAILING(*(uint8_t*)0x20013d38 = 0);
    NONFAILING(*(uint8_t*)0x20013d39 = 0);
    NONFAILING(*(uint8_t*)0x20013d3a = 0);
    NONFAILING(*(uint8_t*)0x20013d3b = 0);
    NONFAILING(*(uint8_t*)0x20013d3c = 0);
    NONFAILING(*(uint8_t*)0x20013d3d = 0);
    NONFAILING(*(uint8_t*)0x20013d3e = 0);
    NONFAILING(*(uint8_t*)0x20013d3f = 0);
    NONFAILING(*(uint8_t*)0x20013d40 = 0);
    NONFAILING(*(uint8_t*)0x20013d41 = 0);
    NONFAILING(*(uint8_t*)0x20013d42 = 0);
    NONFAILING(*(uint8_t*)0x20013d43 = 0);
    NONFAILING(*(uint32_t*)0x20013d44 = 0);
    NONFAILING(*(uint16_t*)0x20013d48 = 0x70);
    NONFAILING(*(uint16_t*)0x20013d4a = 0x98);
    NONFAILING(*(uint32_t*)0x20013d4c = 0);
    NONFAILING(*(uint64_t*)0x20013d50 = 0);
    NONFAILING(*(uint64_t*)0x20013d58 = 0);
    NONFAILING(*(uint16_t*)0x20013d60 = 0x28);
    NONFAILING(memcpy((void*)0x20013d62, "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00",
                      29));
    NONFAILING(*(uint8_t*)0x20013d7f = 0);
    NONFAILING(*(uint32_t*)0x20013d80 = 0xfffffffe);
    NONFAILING(*(uint8_t*)0x20013d88 = 0);
    NONFAILING(*(uint8_t*)0x20013d89 = 0);
    NONFAILING(*(uint8_t*)0x20013d8a = 0);
    NONFAILING(*(uint8_t*)0x20013d8b = 0);
    NONFAILING(*(uint8_t*)0x20013d8c = 0);
    NONFAILING(*(uint8_t*)0x20013d8d = 0);
    NONFAILING(*(uint8_t*)0x20013d8e = 0);
    NONFAILING(*(uint8_t*)0x20013d8f = 0);
    NONFAILING(*(uint8_t*)0x20013d90 = 0);
    NONFAILING(*(uint8_t*)0x20013d91 = 0);
    NONFAILING(*(uint8_t*)0x20013d92 = 0);
    NONFAILING(*(uint8_t*)0x20013d93 = 0);
    NONFAILING(*(uint8_t*)0x20013d94 = 0);
    NONFAILING(*(uint8_t*)0x20013d95 = 0);
    NONFAILING(*(uint8_t*)0x20013d96 = 0);
    NONFAILING(*(uint8_t*)0x20013d97 = 0);
    NONFAILING(*(uint8_t*)0x20013d98 = 0);
    NONFAILING(*(uint8_t*)0x20013d99 = 0);
    NONFAILING(*(uint8_t*)0x20013d9a = 0);
    NONFAILING(*(uint8_t*)0x20013d9b = 0);
    NONFAILING(*(uint8_t*)0x20013d9c = 0);
    NONFAILING(*(uint8_t*)0x20013d9d = 0);
    NONFAILING(*(uint8_t*)0x20013d9e = 0);
    NONFAILING(*(uint8_t*)0x20013d9f = 0);
    NONFAILING(*(uint8_t*)0x20013da0 = 0);
    NONFAILING(*(uint8_t*)0x20013da1 = 0);
    NONFAILING(*(uint8_t*)0x20013da2 = 0);
    NONFAILING(*(uint8_t*)0x20013da3 = 0);
    NONFAILING(*(uint8_t*)0x20013da4 = 0);
    NONFAILING(*(uint8_t*)0x20013da5 = 0);
    NONFAILING(*(uint8_t*)0x20013da6 = 0);
    NONFAILING(*(uint8_t*)0x20013da7 = 0);
    NONFAILING(*(uint8_t*)0x20013da8 = 0);
    NONFAILING(*(uint8_t*)0x20013da9 = 0);
    NONFAILING(*(uint8_t*)0x20013daa = 0);
    NONFAILING(*(uint8_t*)0x20013dab = 0);
    NONFAILING(*(uint8_t*)0x20013dac = 0);
    NONFAILING(*(uint8_t*)0x20013dad = 0);
    NONFAILING(*(uint8_t*)0x20013dae = 0);
    NONFAILING(*(uint8_t*)0x20013daf = 0);
    NONFAILING(*(uint8_t*)0x20013db0 = 0);
    NONFAILING(*(uint8_t*)0x20013db1 = 0);
    NONFAILING(*(uint8_t*)0x20013db2 = 0);
    NONFAILING(*(uint8_t*)0x20013db3 = 0);
    NONFAILING(*(uint8_t*)0x20013db4 = 0);
    NONFAILING(*(uint8_t*)0x20013db5 = 0);
    NONFAILING(*(uint8_t*)0x20013db6 = 0);
    NONFAILING(*(uint8_t*)0x20013db7 = 0);
    NONFAILING(*(uint8_t*)0x20013db8 = 0);
    NONFAILING(*(uint8_t*)0x20013db9 = 0);
    NONFAILING(*(uint8_t*)0x20013dba = 0);
    NONFAILING(*(uint8_t*)0x20013dbb = 0);
    NONFAILING(*(uint8_t*)0x20013dbc = 0);
    NONFAILING(*(uint8_t*)0x20013dbd = 0);
    NONFAILING(*(uint8_t*)0x20013dbe = 0);
    NONFAILING(*(uint8_t*)0x20013dbf = 0);
    NONFAILING(*(uint8_t*)0x20013dc0 = 0);
    NONFAILING(*(uint8_t*)0x20013dc1 = 0);
    NONFAILING(*(uint8_t*)0x20013dc2 = 0);
    NONFAILING(*(uint8_t*)0x20013dc3 = 0);
    NONFAILING(*(uint8_t*)0x20013dc4 = 0);
    NONFAILING(*(uint8_t*)0x20013dc5 = 0);
    NONFAILING(*(uint8_t*)0x20013dc6 = 0);
    NONFAILING(*(uint8_t*)0x20013dc7 = 0);
    NONFAILING(*(uint8_t*)0x20013dc8 = 0);
    NONFAILING(*(uint8_t*)0x20013dc9 = 0);
    NONFAILING(*(uint8_t*)0x20013dca = 0);
    NONFAILING(*(uint8_t*)0x20013dcb = 0);
    NONFAILING(*(uint8_t*)0x20013dcc = 0);
    NONFAILING(*(uint8_t*)0x20013dcd = 0);
    NONFAILING(*(uint8_t*)0x20013dce = 0);
    NONFAILING(*(uint8_t*)0x20013dcf = 0);
    NONFAILING(*(uint8_t*)0x20013dd0 = 0);
    NONFAILING(*(uint8_t*)0x20013dd1 = 0);
    NONFAILING(*(uint8_t*)0x20013dd2 = 0);
    NONFAILING(*(uint8_t*)0x20013dd3 = 0);
    NONFAILING(*(uint8_t*)0x20013dd4 = 0);
    NONFAILING(*(uint8_t*)0x20013dd5 = 0);
    NONFAILING(*(uint8_t*)0x20013dd6 = 0);
    NONFAILING(*(uint8_t*)0x20013dd7 = 0);
    NONFAILING(*(uint8_t*)0x20013dd8 = 0);
    NONFAILING(*(uint8_t*)0x20013dd9 = 0);
    NONFAILING(*(uint8_t*)0x20013dda = 0);
    NONFAILING(*(uint8_t*)0x20013ddb = 0);
    NONFAILING(*(uint32_t*)0x20013ddc = 0);
    NONFAILING(*(uint16_t*)0x20013de0 = 0x70);
    NONFAILING(*(uint16_t*)0x20013de2 = 0x98);
    NONFAILING(*(uint32_t*)0x20013de4 = 0);
    NONFAILING(*(uint64_t*)0x20013de8 = 0);
    NONFAILING(*(uint64_t*)0x20013df0 = 0);
    NONFAILING(*(uint16_t*)0x20013df8 = 0x28);
    NONFAILING(memcpy((void*)0x20013dfa, "\x52\x45\x4a\x45\x43\x54\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00",
                      29));
    NONFAILING(*(uint8_t*)0x20013e17 = 0);
    NONFAILING(*(uint32_t*)0x20013e18 = 0);
    NONFAILING(*(uint32_t*)0x20013e20 = htobe32(-1));
    NONFAILING(*(uint8_t*)0x20013e24 = 0xac);
    NONFAILING(*(uint8_t*)0x20013e25 = 0x14);
    NONFAILING(*(uint8_t*)0x20013e26 = 0);
    NONFAILING(*(uint8_t*)0x20013e27 = 0xaa);
    NONFAILING(*(uint32_t*)0x20013e28 = htobe32(0));
    NONFAILING(*(uint32_t*)0x20013e2c = htobe32(-1));
    NONFAILING(memcpy(
        (void*)0x20013e30,
        "\x73\x69\x74\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
        16));
    NONFAILING(memcpy(
        (void*)0x20013e40,
        "\x65\x71\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
        16));
    NONFAILING(*(uint8_t*)0x20013e50 = 0);
    NONFAILING(*(uint8_t*)0x20013e51 = 0);
    NONFAILING(*(uint8_t*)0x20013e52 = 0);
    NONFAILING(*(uint8_t*)0x20013e53 = 0);
    NONFAILING(*(uint8_t*)0x20013e54 = 0);
    NONFAILING(*(uint8_t*)0x20013e55 = 0);
    NONFAILING(*(uint8_t*)0x20013e56 = 0);
    NONFAILING(*(uint8_t*)0x20013e57 = 0);
    NONFAILING(*(uint8_t*)0x20013e58 = 0);
    NONFAILING(*(uint8_t*)0x20013e59 = 0);
    NONFAILING(*(uint8_t*)0x20013e5a = 0);
    NONFAILING(*(uint8_t*)0x20013e5b = 0);
    NONFAILING(*(uint8_t*)0x20013e5c = 0);
    NONFAILING(*(uint8_t*)0x20013e5d = 0);
    NONFAILING(*(uint8_t*)0x20013e5e = 0);
    NONFAILING(*(uint8_t*)0x20013e5f = 0);
    NONFAILING(*(uint8_t*)0x20013e60 = 0);
    NONFAILING(*(uint8_t*)0x20013e61 = 0);
    NONFAILING(*(uint8_t*)0x20013e62 = 0);
    NONFAILING(*(uint8_t*)0x20013e63 = 0);
    NONFAILING(*(uint8_t*)0x20013e64 = 0);
    NONFAILING(*(uint8_t*)0x20013e65 = 0);
    NONFAILING(*(uint8_t*)0x20013e66 = 0);
    NONFAILING(*(uint8_t*)0x20013e67 = 0);
    NONFAILING(*(uint8_t*)0x20013e68 = 0);
    NONFAILING(*(uint8_t*)0x20013e69 = 0);
    NONFAILING(*(uint8_t*)0x20013e6a = 0);
    NONFAILING(*(uint8_t*)0x20013e6b = 0);
    NONFAILING(*(uint8_t*)0x20013e6c = 0);
    NONFAILING(*(uint8_t*)0x20013e6d = 0);
    NONFAILING(*(uint8_t*)0x20013e6e = 0);
    NONFAILING(*(uint8_t*)0x20013e6f = 0);
    NONFAILING(*(uint16_t*)0x20013e70 = 0);
    NONFAILING(*(uint8_t*)0x20013e72 = 0);
    NONFAILING(*(uint8_t*)0x20013e73 = 0);
    NONFAILING(*(uint32_t*)0x20013e74 = 0);
    NONFAILING(*(uint16_t*)0x20013e78 = 0x70);
    NONFAILING(*(uint16_t*)0x20013e7a = 0xd0);
    NONFAILING(*(uint32_t*)0x20013e7c = 0);
    NONFAILING(*(uint64_t*)0x20013e80 = 0);
    NONFAILING(*(uint64_t*)0x20013e88 = 0);
    NONFAILING(*(uint16_t*)0x20013e90 = 0x60);
    NONFAILING(memcpy((void*)0x20013e92, "\x43\x4c\x55\x53\x54\x45\x52\x49\x50"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00",
                      29));
    NONFAILING(*(uint8_t*)0x20013eaf = 0);
    NONFAILING(*(uint32_t*)0x20013eb0 = 1);
    NONFAILING(*(uint8_t*)0x20013eb4 = 1);
    NONFAILING(*(uint8_t*)0x20013eb5 = 0x80);
    NONFAILING(*(uint8_t*)0x20013eb6 = 0xc2);
    NONFAILING(*(uint8_t*)0x20013eb7 = 0);
    NONFAILING(*(uint8_t*)0x20013eb8 = 0);
    NONFAILING(*(uint8_t*)0x20013eb9 = 0);
    NONFAILING(*(uint16_t*)0x20013eba = 0);
    NONFAILING(*(uint16_t*)0x20013ebc = 0);
    NONFAILING(*(uint16_t*)0x20013ebe = 0);
    NONFAILING(*(uint16_t*)0x20013ec0 = 0);
    NONFAILING(*(uint16_t*)0x20013ec2 = 0);
    NONFAILING(*(uint16_t*)0x20013ec4 = 0);
    NONFAILING(*(uint16_t*)0x20013ec6 = 0);
    NONFAILING(*(uint16_t*)0x20013ec8 = 0);
    NONFAILING(*(uint16_t*)0x20013eca = 0);
    NONFAILING(*(uint16_t*)0x20013ecc = 0);
    NONFAILING(*(uint16_t*)0x20013ece = 0);
    NONFAILING(*(uint16_t*)0x20013ed0 = 0);
    NONFAILING(*(uint16_t*)0x20013ed2 = 0);
    NONFAILING(*(uint16_t*)0x20013ed4 = 0);
    NONFAILING(*(uint16_t*)0x20013ed6 = 0);
    NONFAILING(*(uint16_t*)0x20013ed8 = 0);
    NONFAILING(*(uint16_t*)0x20013eda = 0);
    NONFAILING(*(uint16_t*)0x20013edc = 0);
    NONFAILING(*(uint32_t*)0x20013ee0 = 0);
    NONFAILING(*(uint32_t*)0x20013ee4 = 0);
    NONFAILING(*(uint64_t*)0x20013ee8 = 0);
    NONFAILING(*(uint8_t*)0x20013ef0 = 0);
    NONFAILING(*(uint8_t*)0x20013ef1 = 0);
    NONFAILING(*(uint8_t*)0x20013ef2 = 0);
    NONFAILING(*(uint8_t*)0x20013ef3 = 0);
    NONFAILING(*(uint8_t*)0x20013ef4 = 0);
    NONFAILING(*(uint8_t*)0x20013ef5 = 0);
    NONFAILING(*(uint8_t*)0x20013ef6 = 0);
    NONFAILING(*(uint8_t*)0x20013ef7 = 0);
    NONFAILING(*(uint8_t*)0x20013ef8 = 0);
    NONFAILING(*(uint8_t*)0x20013ef9 = 0);
    NONFAILING(*(uint8_t*)0x20013efa = 0);
    NONFAILING(*(uint8_t*)0x20013efb = 0);
    NONFAILING(*(uint8_t*)0x20013efc = 0);
    NONFAILING(*(uint8_t*)0x20013efd = 0);
    NONFAILING(*(uint8_t*)0x20013efe = 0);
    NONFAILING(*(uint8_t*)0x20013eff = 0);
    NONFAILING(*(uint8_t*)0x20013f00 = 0);
    NONFAILING(*(uint8_t*)0x20013f01 = 0);
    NONFAILING(*(uint8_t*)0x20013f02 = 0);
    NONFAILING(*(uint8_t*)0x20013f03 = 0);
    NONFAILING(*(uint8_t*)0x20013f04 = 0);
    NONFAILING(*(uint8_t*)0x20013f05 = 0);
    NONFAILING(*(uint8_t*)0x20013f06 = 0);
    NONFAILING(*(uint8_t*)0x20013f07 = 0);
    NONFAILING(*(uint8_t*)0x20013f08 = 0);
    NONFAILING(*(uint8_t*)0x20013f09 = 0);
    NONFAILING(*(uint8_t*)0x20013f0a = 0);
    NONFAILING(*(uint8_t*)0x20013f0b = 0);
    NONFAILING(*(uint8_t*)0x20013f0c = 0);
    NONFAILING(*(uint8_t*)0x20013f0d = 0);
    NONFAILING(*(uint8_t*)0x20013f0e = 0);
    NONFAILING(*(uint8_t*)0x20013f0f = 0);
    NONFAILING(*(uint8_t*)0x20013f10 = 0);
    NONFAILING(*(uint8_t*)0x20013f11 = 0);
    NONFAILING(*(uint8_t*)0x20013f12 = 0);
    NONFAILING(*(uint8_t*)0x20013f13 = 0);
    NONFAILING(*(uint8_t*)0x20013f14 = 0);
    NONFAILING(*(uint8_t*)0x20013f15 = 0);
    NONFAILING(*(uint8_t*)0x20013f16 = 0);
    NONFAILING(*(uint8_t*)0x20013f17 = 0);
    NONFAILING(*(uint8_t*)0x20013f18 = 0);
    NONFAILING(*(uint8_t*)0x20013f19 = 0);
    NONFAILING(*(uint8_t*)0x20013f1a = 0);
    NONFAILING(*(uint8_t*)0x20013f1b = 0);
    NONFAILING(*(uint8_t*)0x20013f1c = 0);
    NONFAILING(*(uint8_t*)0x20013f1d = 0);
    NONFAILING(*(uint8_t*)0x20013f1e = 0);
    NONFAILING(*(uint8_t*)0x20013f1f = 0);
    NONFAILING(*(uint8_t*)0x20013f20 = 0);
    NONFAILING(*(uint8_t*)0x20013f21 = 0);
    NONFAILING(*(uint8_t*)0x20013f22 = 0);
    NONFAILING(*(uint8_t*)0x20013f23 = 0);
    NONFAILING(*(uint8_t*)0x20013f24 = 0);
    NONFAILING(*(uint8_t*)0x20013f25 = 0);
    NONFAILING(*(uint8_t*)0x20013f26 = 0);
    NONFAILING(*(uint8_t*)0x20013f27 = 0);
    NONFAILING(*(uint8_t*)0x20013f28 = 0);
    NONFAILING(*(uint8_t*)0x20013f29 = 0);
    NONFAILING(*(uint8_t*)0x20013f2a = 0);
    NONFAILING(*(uint8_t*)0x20013f2b = 0);
    NONFAILING(*(uint8_t*)0x20013f2c = 0);
    NONFAILING(*(uint8_t*)0x20013f2d = 0);
    NONFAILING(*(uint8_t*)0x20013f2e = 0);
    NONFAILING(*(uint8_t*)0x20013f2f = 0);
    NONFAILING(*(uint8_t*)0x20013f30 = 0);
    NONFAILING(*(uint8_t*)0x20013f31 = 0);
    NONFAILING(*(uint8_t*)0x20013f32 = 0);
    NONFAILING(*(uint8_t*)0x20013f33 = 0);
    NONFAILING(*(uint8_t*)0x20013f34 = 0);
    NONFAILING(*(uint8_t*)0x20013f35 = 0);
    NONFAILING(*(uint8_t*)0x20013f36 = 0);
    NONFAILING(*(uint8_t*)0x20013f37 = 0);
    NONFAILING(*(uint8_t*)0x20013f38 = 0);
    NONFAILING(*(uint8_t*)0x20013f39 = 0);
    NONFAILING(*(uint8_t*)0x20013f3a = 0);
    NONFAILING(*(uint8_t*)0x20013f3b = 0);
    NONFAILING(*(uint8_t*)0x20013f3c = 0);
    NONFAILING(*(uint8_t*)0x20013f3d = 0);
    NONFAILING(*(uint8_t*)0x20013f3e = 0);
    NONFAILING(*(uint8_t*)0x20013f3f = 0);
    NONFAILING(*(uint8_t*)0x20013f40 = 0);
    NONFAILING(*(uint8_t*)0x20013f41 = 0);
    NONFAILING(*(uint8_t*)0x20013f42 = 0);
    NONFAILING(*(uint8_t*)0x20013f43 = 0);
    NONFAILING(*(uint32_t*)0x20013f44 = 0);
    NONFAILING(*(uint16_t*)0x20013f48 = 0x70);
    NONFAILING(*(uint16_t*)0x20013f4a = 0x98);
    NONFAILING(*(uint32_t*)0x20013f4c = 0);
    NONFAILING(*(uint64_t*)0x20013f50 = 0);
    NONFAILING(*(uint64_t*)0x20013f58 = 0);
    NONFAILING(*(uint16_t*)0x20013f60 = 0x28);
    NONFAILING(memcpy((void*)0x20013f62, "\x52\x45\x4a\x45\x43\x54\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
                                         "\x00\x00",
                      29));
    NONFAILING(*(uint8_t*)0x20013f7f = 0);
    NONFAILING(*(uint32_t*)0x20013f80 = 0);
    NONFAILING(*(uint64_t*)0x20001000 = 0);
    NONFAILING(*(uint64_t*)0x20001008 = 0);
    NONFAILING(*(uint64_t*)0x20001010 = 0);
    NONFAILING(*(uint64_t*)0x20001018 = 0);
    NONFAILING(*(uint64_t*)0x20001020 = 0);
    NONFAILING(*(uint64_t*)0x20001028 = 0);
    NONFAILING(*(uint64_t*)0x20001030 = 0);
    NONFAILING(*(uint64_t*)0x20001038 = 0);
    syscall(__NR_setsockopt, r[2], 0, 0x40, 0x20013c90, 0x2f8);
    break;
  case 12:
    NONFAILING(*(uint16_t*)0x2000aff0 = 2);
    NONFAILING(*(uint16_t*)0x2000aff2 = htobe16(0x4e20));
    NONFAILING(*(uint8_t*)0x2000aff4 = 0xac);
    NONFAILING(*(uint8_t*)0x2000aff5 = 0x14);
    NONFAILING(*(uint8_t*)0x2000aff6 = 0);
    NONFAILING(*(uint8_t*)0x2000aff7 = 0xaa);
    NONFAILING(*(uint8_t*)0x2000aff8 = 0);
    NONFAILING(*(uint8_t*)0x2000aff9 = 0);
    NONFAILING(*(uint8_t*)0x2000affa = 0);
    NONFAILING(*(uint8_t*)0x2000affb = 0);
    NONFAILING(*(uint8_t*)0x2000affc = 0);
    NONFAILING(*(uint8_t*)0x2000affd = 0);
    NONFAILING(*(uint8_t*)0x2000affe = 0);
    NONFAILING(*(uint8_t*)0x2000afff = 0);
    syscall(__NR_bind, r[2], 0x2000aff0, 0x10);
    break;
  }
}

void test()
{
  memset(r, -1, sizeof(r));
  execute(13);
  collide = 1;
  execute(13);
}

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