// File: /workspace/github_c_makefile_star2_O1/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 0x33bb
movq %rbx, %rdi
callq 0x20d0
testl %eax, %eax
jns 0x3599
callq 0x2040
cmpl $0x2, (%rax)
jne 0x35c7
cmpb $0x2f, (%rbx)
jne 0x33e7
leaq 0x132b(%rip), %rdi      # 0x46f2
callq 0x20d0
testl %eax, %eax
js 0x35ef
decq %rbx
movq %rbx, %rax
leaq 0x1(%rax), %rbx
cmpb $0x2f, 0x1(%rax)
movq %rbx, %rax
je 0x33da
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2120
testq %rax, %rax
je 0x3429
movq %rax, %r15
cmpb $0x0, 0x1(%rax)
jne 0x342c
cmpb $0x2f, (%r15)
jne 0x3417
movb $0x0, (%r15)
cmpb $0x2f, -0x1(%r15)
leaq -0x1(%r15), %r15
je 0x3408
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2120
movq %rax, %r15
jmp 0x342c
xorl %r15d, %r15d
testq %r15, %r15
je 0x344a
movb $0x0, (%r15)
movq %rbx, %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x3374
incq %r15
movq %r15, %rbx
cmpb $0x0, (%rbx)
je 0x3599
leaq 0xc93(%rip), %rsi       # 0x40ed
movl %ebp, %edi
movl $0x180, %edx            # imm = 0x180
callq 0x2230
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x35e6
leaq 0x18(%rsp), %r12
leaq 0x10(%rsp), %r13
movq %rbx, %rdi
testl %r14d, %r14d
je 0x34a0
callq 0x20d0
testl %eax, %eax
jns 0x358e
callq 0x2040
cmpl $0x2, (%rax)
je 0x34be
jmp 0x35c7
movq %r12, %rsi
callq 0x2190
testl %eax, %eax
jns 0x358e
callq 0x2040
cmpl $0x2, (%rax)
jne 0x35d0
cmpq $0x0, 0x2d7a(%rip)      # 0x6240
jne 0x34d9
movl $0x1011, %edi           # imm = 0x1011
callq 0x2220
movq %rax, 0x2d67(%rip)      # 0x6240
cmpq $0x0, 0x2d5f(%rip)      # 0x6240
je 0x35be
movl $0x1, %esi
movq %r13, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2280
testl %eax, %eax
jns 0x352c
callq 0x2040
movq %rax, %r15
movl (%r15), %eax
cmpl $0x4, %eax
je 0x3516
cmpl $0xb, %eax
jne 0x35ab
movl $0x1, %esi
movq %r13, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2280
testl %eax, %eax
js 0x3505
cmpw $0x0, 0x16(%rsp)
je 0x347c
movq 0x2d01(%rip), %rsi      # 0x6240
movl $0x1011, %edx           # imm = 0x1011
movl %ebp, %edi
callq 0x2180
testq %rax, %rax
js 0x356c
movq 0x2ce9(%rip), %rsi      # 0x6240
addq $0x10, %rsi
movq %rbx, %rdi
callq 0x21a0
testl %eax, %eax
jne 0x34e7
jmp 0x347c
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x347c
cmpl $0xb, %eax
je 0x347c
leaq 0x118f(%rip), %rsi      # 0x471b
jmp 0x35b2
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 0x1164(%rip), %rsi      # 0x4716
movl $0x1, %edi
xorl %eax, %eax
callq 0x2210
leaq 0x114a(%rip), %rsi      # 0x470f
jmp 0x35b2
leaq 0x1115(%rip), %rsi      # 0x46e3
jmp 0x35d7
leaq 0x112f(%rip), %rsi      # 0x4706
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2210
leaq 0x1107(%rip), %rsi      # 0x46f4
jmp 0x35b2
leaq 0x10f6(%rip), %rsi      # 0x46ec
jmp 0x35b2