// File: /workspace/github_c_makefile_star2_O2/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 0x32df
movq %rbx, %rdi
callq 0x20d0
testl %eax, %eax
jns 0x348d
callq 0x2040
cmpl $0x2, (%rax)
jne 0x34b0
cmpb $0x2f, (%rbx)
jne 0x330b
leaq 0x1407(%rip), %rdi      # 0x46f2
callq 0x20d0
testl %eax, %eax
js 0x34df
decq %rbx
movq %rbx, %rax
leaq 0x1(%rax), %rbx
cmpb $0x2f, 0x1(%rax)
movq %rbx, %rax
je 0x32fe
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x2120
testq %rax, %rax
je 0x335d
movq %rax, %r15
cmpb $0x0, 0x1(%rax)
jne 0x3346
jmp 0x332d
movb $0x0, (%r15)
decq %r15
cmpb $0x2f, (%r15)
je 0x3326
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x2120
movq %rax, %r15
testq %rax, %rax
je 0x335d
movb $0x0, (%r15)
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3298
incq %r15
movq %r15, %rbx
cmpb $0x0, (%rbx)
je 0x348d
leaq 0xd80(%rip), %rsi       # 0x40ed
movl %ebp, %edi
movl $0x180, %edx            # imm = 0x180
callq 0x2230
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x34d6
leaq 0x18(%rsp), %r12
leaq 0x10(%rsp), %r13
pushq $0x1
popq %r15
movq %rbx, %rdi
testl %r14d, %r14d
je 0x33b7
callq 0x20d0
testl %eax, %eax
jns 0x3482
callq 0x2040
cmpl $0x2, (%rax)
je 0x33d5
jmp 0x34b0
movq %r12, %rsi
callq 0x2190
testl %eax, %eax
jns 0x3482
callq 0x2040
cmpl $0x2, (%rax)
jne 0x34c2
cmpq $0x0, 0x2e63(%rip)      # 0x6240
jne 0x33f9
movl $0x1011, %edi           # imm = 0x1011
callq 0x2220
movq %rax, 0x2e50(%rip)      # 0x6240
testq %rax, %rax
je 0x34b9
movq %r13, %rdi
movq %r15, %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2280
testl %eax, %eax
js 0x344d
cmpw $0x0, 0x16(%rsp)
je 0x3393
movq 0x2e20(%rip), %rsi      # 0x6240
movl $0x1011, %edx           # imm = 0x1011
movl %ebp, %edi
callq 0x2180
testq %rax, %rax
js 0x3460
movq 0x2e08(%rip), %rsi      # 0x6240
addq $0x10, %rsi
movq %rbx, %rdi
callq 0x21a0
testl %eax, %eax
jne 0x33f9
jmp 0x3393
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x33f9
cmpl $0xb, %eax
je 0x33f9
jmp 0x349f
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x3393
cmpl $0xb, %eax
je 0x3393
leaq 0x129b(%rip), %rsi      # 0x471b
jmp 0x34a6
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 0x1270(%rip), %rsi      # 0x4716
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x2210
leaq 0x122c(%rip), %rsi      # 0x46e3
jmp 0x34c9
leaq 0x124f(%rip), %rsi      # 0x470f
jmp 0x34a6
leaq 0x123d(%rip), %rsi      # 0x4706
pushq $0x1
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2210
leaq 0x1217(%rip), %rsi      # 0x46f4
jmp 0x34a6
leaq 0x1206(%rip), %rsi      # 0x46ec
jmp 0x34a6