// File: /workspace/github_c_makefile_star2_O3/arachsys[P]init/daemon.c
// Function: await
static void await(const char *path, int inotify, int parent) {
  struct pollfd fd = { .fd = inotify, .events = POLLIN };
  struct stat test;
  char *slash;
  int watch;

  /* Take a short-cut if path already exists and is a parent dir. */
  if (parent) {
    if (chdir(path) >= 0)
      return;
    if (errno != ENOENT)
      err(EXIT_FAILURE, "chdir %s", path);
  }

  /* If leading slashes are present, chdir to the root and remove them. */
  if (*path == '/') {
    if (chdir("/") < 0)
      err(EXIT_FAILURE, "chdir /");
    while (*path == '/')
      path++;
  }

  /* Remove any stray trailing slashes. */
  slash = strrchr(path, '/');
  if (slash && slash[1] == 0) {
    while (*slash == '/')
      *slash-- = 0;
    slash = strrchr(path, '/');
  }

  /* Recurse to await the parent dir if necessary. */
  if (slash) {
    *slash = 0;
    await(path, inotify, 1);
    path = slash + 1;
  }

  if (*path == 0)
    return;

  /* Now wait for the correct leaf name to arrive in our working dir. */
  watch = inotify_add_watch(inotify, ".", IN_CREATE | IN_MOVED_TO);
  if (watch < 0)
    err(EXIT_FAILURE, "inotify_add_watch");

  while (1) {
    /* Check if it already exists after setting watch to avoid a race. */
    if (parent) {
      if (chdir(path) >= 0)
        goto out;
      if (errno != ENOENT)
        err(EXIT_FAILURE, "chdir %s", path);
    } else {
      if (lstat(path, &test) >= 0)
        goto out;
      if (errno != ENOENT)
        err(EXIT_FAILURE, "lstat %s", path);
    }

    if (event == NULL)
      event = malloc(sizeof(*event) + PATH_MAX + 1);
    if (event == NULL)
      err(EXIT_FAILURE, "malloc");

    /* Otherwise, wait one second for a matching create/move-into event. */
    while (1) {
      while (poll(&fd, 1, 1000) < 0)
        if (errno != EAGAIN && errno != EINTR)
          err(EXIT_FAILURE, "poll");
      if (fd.revents == 0)
        break;

      if (read(inotify, event, sizeof(*event) + PATH_MAX + 1) < 0) {
        if (errno != EAGAIN && errno != EINTR)
          err(EXIT_FAILURE, "read");
        break;
      }

      /* Read only succeeds if it can fill the entire struct. */
      if (!strcmp(path, event->name))
        break;
    }
  }

out:
  inotify_rm_watch(inotify, watch);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
movl %esi, 0x10(%rsp)
movl $0x1, 0x14(%rsp)
testl %edx, %edx
je 0x36b5
movq %rbx, %rdi
callq 0x20d0
testl %eax, %eax
jns 0x3889
callq 0x2040
cmpl $0x2, (%rax)
jne 0x38ae
cmpb $0x2f, (%rbx)
jne 0x36e1
leaq 0x108d(%rip), %rdi      # 0x474e
callq 0x20d0
testl %eax, %eax
js 0x38df
decq %rbx
movq %rbx, %rax
leaq 0x1(%rax), %rbx
cmpb $0x2f, 0x1(%rax)
movq %rbx, %rax
je 0x36d4
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2120
testq %rax, %rax
je 0x373f
movq %rax, %r15
cmpb $0x0, 0x1(%rax)
jne 0x3726
cmpb $0x2f, (%r15)
jne 0x3711
movb $0x0, (%r15)
cmpb $0x2f, -0x1(%r15)
leaq -0x1(%r15), %r15
je 0x3702
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2120
movq %rax, %r15
testq %rax, %rax
je 0x373f
movb $0x0, (%r15)
movq %rbx, %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x366e
incq %r15
movq %r15, %rbx
cmpb $0x0, (%rbx)
je 0x3889
leaq 0x9fa(%rip), %rsi       # 0x4149
movl %ebp, %edi
movl $0x180, %edx            # imm = 0x180
callq 0x2230
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x38d6
leaq 0x18(%rsp), %r12
leaq 0x10(%rsp), %r13
movq %rbx, %rdi
testl %r14d, %r14d
je 0x3795
callq 0x20d0
testl %eax, %eax
jns 0x387e
callq 0x2040
cmpl $0x2, (%rax)
je 0x37b3
jmp 0x38ae
movq %r12, %rsi
callq 0x2190
testl %eax, %eax
jns 0x387e
callq 0x2040
cmpl $0x2, (%rax)
jne 0x38c0
cmpq $0x0, 0x2a85(%rip)      # 0x6240
jne 0x37d7
movl $0x1011, %edi           # imm = 0x1011
callq 0x2220
movq %rax, 0x2a72(%rip)      # 0x6240
testq %rax, %rax
je 0x38b7
movl $0x1, %esi
movq %r13, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2280
testl %eax, %eax
jns 0x381c
callq 0x2040
movq %rax, %r15
movl (%r15), %eax
cmpl $0x4, %eax
je 0x3806
cmpl $0xb, %eax
jne 0x389b
movl $0x1, %esi
movq %r13, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2280
testl %eax, %eax
js 0x37f5
cmpw $0x0, 0x16(%rsp)
je 0x3771
movq 0x2a11(%rip), %rsi      # 0x6240
movl $0x1011, %edx           # imm = 0x1011
movl %ebp, %edi
callq 0x2180
testq %rax, %rax
js 0x385c
movq 0x29f9(%rip), %rsi      # 0x6240
addq $0x10, %rsi
movq %rbx, %rdi
callq 0x21a0
testl %eax, %eax
jne 0x37d7
jmp 0x3771
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x3771
cmpl $0xb, %eax
je 0x3771
leaq 0xefb(%rip), %rsi       # 0x4777
jmp 0x38a2
movl %ebp, %edi
movl 0xc(%rsp), %esi
callq 0x2380
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xed0(%rip), %rsi       # 0x4772
movl $0x1, %edi
xorl %eax, %eax
callq 0x2210
leaq 0xe8a(%rip), %rsi       # 0x473f
jmp 0x38c7
leaq 0xead(%rip), %rsi       # 0x476b
jmp 0x38a2
leaq 0xe9b(%rip), %rsi       # 0x4762
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2210
leaq 0xe73(%rip), %rsi       # 0x4750
jmp 0x38a2
leaq 0xe62(%rip), %rsi       # 0x4748
jmp 0x38a2