// File: /workspace/github_c_makefile_star2_O0/arachsys[P]init/daemon.c
// Function: main
int main(int argc, char **argv) {
  char *dir = NULL, *options, *path;
  int fd, inotify, option, pwd, tail, waitargs;
  size_t doublefork = 0, limit = -1, restart = 0;
  struct passwd *user;

  /* Redirect stdin from /dev/null. */
  if ((fd = open("/dev/null", O_RDWR)) < 0)
    err(EXIT_FAILURE, "open /dev/null");
  if (fd != STDIN_FILENO) {
    if ((dup2(fd, STDIN_FILENO)) < 0)
      err(EXIT_FAILURE, "dup2");
    close(fd);
  }

  /* Redirect stdout and/or stderr to /dev/null if closed. */
  if (fcntl(STDOUT_FILENO, F_GETFD) < 0 && errno == EBADF)
    if ((dup2(STDIN_FILENO, STDOUT_FILENO)) < 0)
      err(EXIT_FAILURE, "dup2");
  if (fcntl(STDERR_FILENO, F_GETFD) < 0 && errno == EBADF)
    if ((dup2(STDIN_FILENO, STDERR_FILENO)) < 0)
      err(EXIT_FAILURE, "dup2");

  options = "+:cd:fl:n:p:rs:t:u:w:", waitargs = 0;
  while ((option = getopt(argc, argv, options)) > 0)
    switch (option) {
      case 'c':
        /* Special case of -d DIR, for compatibility with BSD daemon(1). */
        dir = "/";
        break;
      case 'd':
        dir = optarg;
        if ((fd = open(dir, O_RDONLY | O_DIRECTORY)) < 0)
          err(EXIT_FAILURE, "%s", dir);
        close(fd);
        break;
      case 'f':
        doublefork = 1;
        break;
      case 'l':
        logger_setup(optarg);
        break;
      case 'n':
        if (sscanf(optarg, "%zu%n", &limit, &tail) >= 1)
          if (optarg[tail] == 0)
            break;
        errx(EXIT_FAILURE, "Invalid connection limit");
      case 'p':
        pidfile_open(optarg);
        break;
      case 'r':
        restart = 1;
        break;
      case 's':
        listen_unix(optarg);
        break;
      case 't':
        listen_tcp(optarg);
        break;
      case 'u':
        if (sscanf(optarg, "%u:%u%n", &uid, &gid, &tail) >= 2)
          if (optarg[tail] == 0)
            break;
        if ((user = getpwnam(optarg))) {
          uid = user->pw_uid;
          gid = user->pw_gid;
          break;
        }
        errx(EXIT_FAILURE, "Invalid username");
      case 'w':
        waitargs++;
        break;
      default:
        usage(argv[0]);
    }

  /* When run with just -w arguments, we await paths in the foreground. */
  if (waitargs > 0 && argc == 2 * waitargs + 1)
    goto await;

  if (argc <= optind)
    usage(argv[0]);

  /* Fork into the background then create a session and process group. */
  switch (fork()) {
    case -1:
      err(EXIT_FAILURE, "fork");
    case 0:
      setsid(); /* Ignore errors but should always work after fork. */
      break;
    default:
      _exit(EXIT_SUCCESS); /* Don't delete pidfile in atexit() handler. */
  }

  if (doublefork) {
    /* Fork again to ensure we are not the session leader. */
    switch (fork()) {
      case -1:
        err(EXIT_FAILURE, "fork");
      case 0:
        break;
      default:
        _exit(EXIT_SUCCESS); /* Don't delete pidfile in atexit() handler. */
    }
  }

  logger_start();
  pidfile_write();

await:
  if (waitargs > 0) {
    if ((inotify = inotify_init1(IN_CLOEXEC | IN_NONBLOCK)) < 0)
      err(EXIT_FAILURE, "inotify_init1");

    /* Open the working directory so we can restore it after each await(). */
    if ((pwd = open(".", O_RDONLY | O_DIRECTORY)) < 0)
      err(EXIT_FAILURE, "open pwd");

    optind = 0; /* Need to reset optind to reprocess -w arguments. */
    while ((option = getopt(argc, argv, options)) > 0)
      if (option == 'w') {
        if (!(path = strdup(optarg)))
          err(EXIT_FAILURE, "strdup");
        await(path, inotify, 0);
        free(path);
        if (fchdir(pwd) < 0)
          err(EXIT_FAILURE, "fchdir");
      }

    close(inotify);
    close(pwd);
  }

  /* Exit if we were just awaiting paths in the foreground. */
  if (argc <= optind)
    return EXIT_SUCCESS;

  if (dir && chdir(dir) < 0)
    err(EXIT_FAILURE, "chdir");

  /* If we don't need to supervise it, just exec the command. */
  if (!restart && !pidfile.path && !listeners)
    execute(argv + optind);

  /* Use a signals pipe to avoid async-unsafe handlers. */
  if (pipe2(signals, O_CLOEXEC) < 0)
    err(EXIT_FAILURE, "pipe");

  /* Avoid using SIG_IGN as this disposition persists across exec. */
  signal(SIGHUP, signal_put);
  signal(SIGINT, signal_put);
  signal(SIGPIPE, signal_put);
  signal(SIGTERM, signal_put);
  signal(SIGCHLD, signal_put);
  signal(SIGUSR1, signal_put);
  signal(SIGUSR2, signal_put);

  if (listeners > 0)
    return serve(argv + optind, limit);
  return supervise(argv + optind, restart);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x48(%rbp)
movq $-0x1, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
leaq 0x1b40(%rip), %rdi      # 0x405d
movl $0x2, %esi
movb $0x0, %al
callq 0x22e0
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jge 0x2544
movl $0x1, %edi
leaq 0x1b1b(%rip), %rsi      # 0x4058
movb $0x0, %al
callq 0x2230
cmpl $0x0, -0x2c(%rbp)
je 0x2574
movl -0x2c(%rbp), %edi
xorl %esi, %esi
callq 0x20f0
cmpl $0x0, %eax
jge 0x256c
movl $0x1, %edi
leaq 0x1b02(%rip), %rsi      # 0x4067
movb $0x0, %al
callq 0x2230
movl -0x2c(%rbp), %edi
callq 0x2160
movl $0x1, %esi
movl %esi, %edi
movb $0x0, %al
callq 0x2090
cmpl $0x0, %eax
jge 0x25b7
callq 0x2040
cmpl $0x9, (%rax)
jne 0x25b7
xorl %edi, %edi
movl $0x1, %esi
callq 0x20f0
cmpl $0x0, %eax
jge 0x25b5
movl $0x1, %edi
leaq 0x1ab9(%rip), %rsi      # 0x4067
movb $0x0, %al
callq 0x2230
jmp 0x25b7
movl $0x2, %edi
movl $0x1, %esi
movb $0x0, %al
callq 0x2090
cmpl $0x0, %eax
jge 0x25fd
callq 0x2040
cmpl $0x9, (%rax)
jne 0x25fd
xorl %edi, %edi
movl $0x2, %esi
callq 0x20f0
cmpl $0x0, %eax
jge 0x25fb
movl $0x1, %edi
leaq 0x1a73(%rip), %rsi      # 0x4067
movb $0x0, %al
callq 0x2230
jmp 0x25fd
leaq 0x1a68(%rip), %rax      # 0x406c
movq %rax, -0x20(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x22f0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jle 0x281c
movl -0x34(%rbp), %eax
addl $-0x63, %eax
movl %eax, %ecx
movq %rcx, -0x68(%rbp)
subl $0x14, %eax
ja 0x280b
movq -0x68(%rbp), %rax
leaq 0x19b9(%rip), %rcx      # 0x4004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2077(%rip), %rax      # 0x46d2
movq %rax, -0x18(%rbp)
jmp 0x2817
movq 0x396d(%rip), %rax      # 0x5fd8
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movl $0x10000, %esi          # imm = 0x10000
movb $0x0, %al
callq 0x22e0
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jge 0x26a1
movq -0x18(%rbp), %rdx
movl $0x1, %edi
leaq 0x1b5b(%rip), %rsi      # 0x41f5
movb $0x0, %al
callq 0x2230
movl -0x2c(%rbp), %edi
callq 0x2160
jmp 0x2817
movq $0x1, -0x48(%rbp)
jmp 0x2817
movq 0x3916(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x2b50
jmp 0x2817
movq 0x3902(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x19a2(%rip), %rsi      # 0x4082
leaq -0x50(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
movb $0x0, %al
callq 0x2260
cmpl $0x1, %eax
jl 0x2712
movq 0x38dd(%rip), %rax      # 0x5fd8
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x2710
jmp 0x2817
jmp 0x2712
movl $0x1, %edi
leaq 0x196a(%rip), %rsi      # 0x4088
movb $0x0, %al
callq 0x20e0
movq 0x38ac(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x2dd0
jmp 0x2817
movq $0x1, -0x58(%rbp)
jmp 0x2817
movq 0x388b(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x2ec0
jmp 0x2817
movq 0x3877(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x3000
jmp 0x2817
movq 0x3863(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1922(%rip), %rsi      # 0x40a1
leaq 0x3a82(%rip), %rdx      # 0x6208
leaq 0x3a7f(%rip), %rcx      # 0x620c
leaq -0x3c(%rbp), %r8
movb $0x0, %al
callq 0x2260
cmpl $0x2, %eax
jl 0x27b8
movq 0x3834(%rip), %rax      # 0x5fd8
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x27b6
jmp 0x2817
jmp 0x27b8
movq 0x3819(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x21e0
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x27ed
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x3a2a(%rip)      # 0x6208
movq -0x60(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x3a21(%rip)      # 0x620c
jmp 0x2817
movl $0x1, %edi
leaq 0x18b0(%rip), %rsi      # 0x40a9
movb $0x0, %al
callq 0x20e0
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2817
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3240
jmp 0x260f
cmpl $0x0, -0x40(%rbp)
jle 0x2836
movl -0x8(%rbp), %eax
movl -0x40(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2836
jmp 0x28d4
movl -0x8(%rbp), %eax
movq 0x3790(%rip), %rcx      # 0x5fd0
cmpl (%rcx), %eax
jg 0x2850
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3240
callq 0x23c0
movl %eax, %ecx
movl %ecx, -0x6c(%rbp)
subl $-0x1, %eax
je 0x286a
jmp 0x2861
movl -0x6c(%rbp), %eax
testl %eax, %eax
je 0x287d
jmp 0x2884
movl $0x1, %edi
leaq 0x1844(%rip), %rsi      # 0x40ba
movb $0x0, %al
callq 0x2230
callq 0x2190
jmp 0x288b
xorl %edi, %edi
callq 0x2070
cmpq $0x0, -0x48(%rbp)
je 0x28ca
callq 0x23c0
movl %eax, %ecx
movl %ecx, -0x70(%rbp)
subl $-0x1, %eax
je 0x28ac
jmp 0x28a3
movl -0x70(%rbp), %eax
testl %eax, %eax
je 0x28bf
jmp 0x28c1
movl $0x1, %edi
leaq 0x1802(%rip), %rsi      # 0x40ba
movb $0x0, %al
callq 0x2230
jmp 0x28c8
xorl %edi, %edi
callq 0x2070
jmp 0x28ca
callq 0x3280
callq 0x3520
cmpl $0x0, -0x40(%rbp)
jle 0x29d6
movl $0x80800, %edi          # imm = 0x80800
callq 0x23b0
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0x2903
movl $0x1, %edi
leaq 0x17c3(%rip), %rsi      # 0x40bf
movb $0x0, %al
callq 0x2230
leaq 0x17c3(%rip), %rdi      # 0x40cd
movl $0x10000, %esi          # imm = 0x10000
movb $0x0, %al
callq 0x22e0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x2931
movl $0x1, %edi
leaq 0x17a5(%rip), %rsi      # 0x40cf
movb $0x0, %al
callq 0x2230
movq 0x3698(%rip), %rax      # 0x5fd0
movl $0x0, (%rax)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x22f0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jle 0x29c6
cmpl $0x77, -0x34(%rbp)
jne 0x29c1
movq 0x3675(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x2380
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x2988
movl $0x1, %edi
leaq 0x1757(%rip), %rsi      # 0x40d8
movb $0x0, %al
callq 0x2230
movq -0x28(%rbp), %rdi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x3580
movq -0x28(%rbp), %rdi
callq 0x2030
movl -0x38(%rbp), %edi
callq 0x2280
cmpl $0x0, %eax
jge 0x29bf
movl $0x1, %edi
leaq 0x1727(%rip), %rsi      # 0x40df
movb $0x0, %al
callq 0x2230
jmp 0x29c1
jmp 0x293e
movl -0x30(%rbp), %edi
callq 0x2160
movl -0x38(%rbp), %edi
callq 0x2160
movl -0x8(%rbp), %eax
movq 0x35f0(%rip), %rcx      # 0x5fd0
cmpl (%rcx), %eax
jg 0x29f0
movl $0x0, -0x4(%rbp)
jmp 0x2b3d
cmpq $0x0, -0x18(%rbp)
je 0x2a18
movq -0x18(%rbp), %rdi
callq 0x20d0
cmpl $0x0, %eax
jge 0x2a18
movl $0x1, %edi
leaq 0x16cf(%rip), %rsi      # 0x40e0
movb $0x0, %al
callq 0x2230
cmpq $0x0, -0x58(%rbp)
jne 0x2a4d
cmpq $0x0, 0x37e9(%rip)      # 0x6210
jne 0x2a4d
cmpq $0x0, 0x37ef(%rip)      # 0x6220
jne 0x2a4d
movq -0x10(%rbp), %rdi
movq 0x3592(%rip), %rax      # 0x5fd0
movslq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0x38a0
leaq 0x37d4(%rip), %rdi      # 0x6228
movl $0x80000, %esi          # imm = 0x80000
callq 0x2340
cmpl $0x0, %eax
jge 0x2a76
movl $0x1, %edi
leaq 0x1677(%rip), %rsi      # 0x40e6
movb $0x0, %al
callq 0x2230
movl $0x1, %edi
leaq 0xeae(%rip), %rsi       # 0x3930
callq 0x21d0
movl $0x2, %edi
leaq 0xe9d(%rip), %rsi       # 0x3930
callq 0x21d0
movl $0xd, %edi
leaq 0xe8c(%rip), %rsi       # 0x3930
callq 0x21d0
movl $0xf, %edi
leaq 0xe7b(%rip), %rsi       # 0x3930
callq 0x21d0
movl $0x11, %edi
leaq 0xe6a(%rip), %rsi       # 0x3930
callq 0x21d0
movl $0xa, %edi
leaq 0xe59(%rip), %rsi       # 0x3930
callq 0x21d0
movl $0xc, %edi
leaq 0xe48(%rip), %rsi       # 0x3930
callq 0x21d0
cmpq $0x0, 0x372b(%rip)      # 0x6220
jbe 0x2b1a
movq -0x10(%rbp), %rdi
movq 0x34ce(%rip), %rax      # 0x5fd0
movslq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
callq 0x3970
movl %eax, -0x4(%rbp)
jmp 0x2b3d
movq -0x10(%rbp), %rdi
movq 0x34ab(%rip), %rax      # 0x5fd0
movslq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
callq 0x3be0
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)