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

#define _GNU_SOURCE

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <stdint.h>
#include <string.h>

static uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t a2)
{
  if (a0 == 0xc || a0 == 0xb) {
    char buf[128];
    sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block",
            (uint8_t)a1, (uint8_t)a2);
    return open(buf, O_RDWR, 0);
  } else {
    char buf[1024];
    char* hash;
    strncpy(buf, (char*)a0, sizeof(buf));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}

static void test();

void loop()
{
  while (1) {
    test();
  }
}

#ifndef __NR_mmap
#define __NR_mmap 90
#endif
#ifndef __NR_socket
#define __NR_socket 359
#endif
#ifndef __NR_dup2
#define __NR_dup2 63
#endif
#ifndef __NR_ioctl
#define __NR_ioctl 54
#endif
#undef __NR_mmap
#define __NR_mmap __NR_mmap2

long r[8];
void test()
{
  memset(r, -1, sizeof(r));
  r[0] = syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul,
                 0xfffffffffffffffful, 0x0ul);
  r[1] = syscall(__NR_socket, 0x2ul, 0x1ul, 0x0ul);
  memcpy((void*)0x2051fff3,
         "\x2f\x64\x65\x76\x2f\x6e\x65\x74\x2f\x74\x75\x6e\x00", 13);
  r[3] = syz_open_dev(0x2051fff3ul, 0x0ul, 0xaul);
  r[4] = syscall(__NR_dup2, r[3], r[1]);
  memcpy((void*)0x2087ffd8, "\xb8\x3a\x8d\x87\xc2\x08\x88\xce\x5a\x25"
                            "\x4f\x74\x77\x39\xda\xec",
         16);
  memcpy((void*)0x2087ffe8, "\x57\x9f\xeb\xcc\xcd\xc5\xb7\x92\x17\xa7"
                            "\xd4\x43\x63\x17\x46\x06",
         16);
  r[7] = syscall(__NR_ioctl, r[4], 0x400454caul, 0x2087ffd8ul);
}

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