// File: ahupp[P]mural/mural_server.cpp
// Function: main
int main(int argc, char** argv) {

  if (argc != 2) {
    cerr << "usage: " << argv[0] << " tagfile" << endl;
    return 1;
  }

  string tags_file(argv[1]);

  vector<TagInfo> tags;
  read_tags_file(tags_file, tags);

#ifndef __APPLE__
  int inotify_fd = enforce(inotify_init(), "inotify_init");
  int efd = enforce(epoll_create(2), "epoll_create");

  mural_epoll_add(efd, 0);
  mural_epoll_add(efd, inotify_fd);

  enforce(
    inotify_add_watch(inotify_fd, tags_file.c_str(), IN_CLOSE_WRITE),
    "inotify_add_watch");

  const int MAXEVENTS = 64;
  struct epoll_event* events = new struct epoll_event[MAXEVENTS];

  while (1) {
    int n = epoll_wait(efd, events, MAXEVENTS, -1);
    for (int i = 0; i < n; ++i) {
      int active_fd = events[i].data.fd;
      if (active_fd == 0) {
        handle_one_query(tags);
      } else if (active_fd == inotify_fd) {
        read_inotify_events(inotify_fd, tags_file, tags);
      } else {
        assert(false);
      }
    }
    if (cin.eof()) {
      break;
    }
  }
#else

  while (!cin.eof()) {
    handle_one_query(tags);
  }

#endif
  return 0;
}

// Assembly:
subq $0xc8, %rsp
movl $0x0, 0xc4(%rsp)
movl %edi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
cmpl $0x2, 0xc0(%rsp)
je 0x3aef
movq 0xa54e(%rip), %rdi      # 0xdff0
leaq 0x658f(%rip), %rsi      # 0xa038
callq 0x3230
movq %rax, %rdi
movq 0xb8(%rsp), %rax
movq (%rax), %rsi
callq 0x3230
movq %rax, %rdi
leaq 0x6575(%rip), %rsi      # 0xa040
callq 0x3230
movq %rax, %rdi
movq 0xa4de(%rip), %rsi      # 0xdfb8
callq 0x3280
movl $0x1, 0xc4(%rsp)
jmp 0x3d81
movq 0xb8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0x97(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x3400
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
leaq 0x98(%rsp), %rdi
callq 0x3ec0
jmp 0x3b2b
leaq 0x97(%rsp), %rdi
callq 0x32a0
leaq 0x68(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x3f80
movq 0x28(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x7c10
jmp 0x3b5b
callq 0x3420
movl %eax, %edi
leaq 0x64e0(%rip), %rsi      # 0xa049
callq 0x3620
movl %eax, 0x24(%rsp)
jmp 0x3b74
movl 0x24(%rsp), %eax
movl %eax, 0x64(%rsp)
movl $0x2, %edi
callq 0x3460
movl %eax, %edi
leaq 0x64c7(%rip), %rsi      # 0xa056
callq 0x3620
movl %eax, 0x20(%rsp)
jmp 0x3b9a
movl 0x20(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %edi
xorl %esi, %esi
callq 0x3960
jmp 0x3baf
movl 0x60(%rsp), %edi
movl 0x64(%rsp), %esi
callq 0x3960
jmp 0x3bbe
movl 0x64(%rsp), %eax
movl %eax, 0x1c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x30e0
movl 0x1c(%rsp), %edi
movq %rax, %rsi
movl $0x8, %edx
callq 0x3140
movl %eax, %edi
leaq 0x6476(%rip), %rsi      # 0xa063
callq 0x3620
jmp 0x3bf4
movl $0x40, 0x5c(%rsp)
movl $0x300, %edi            # imm = 0x300
callq 0x3030
movq %rax, 0x10(%rsp)
jmp 0x3c0d
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x3c19
movl 0x60(%rsp), %edi
movq 0x50(%rsp), %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x3120
movl %eax, 0xc(%rsp)
jmp 0x3c37
movl 0xc(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0x3d32
movq 0x50(%rsp), %rax
movslq 0x48(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
jne 0x3cdb
leaq 0x68(%rsp), %rdi
callq 0x3660
jmp 0x3c80
jmp 0x3d20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x97(%rsp), %rdi
callq 0x32a0
jmp 0x3d90
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x3e60
leaq 0x98(%rsp), %rdi
callq 0x3170
jmp 0x3d90
movl 0x44(%rsp), %eax
cmpl 0x64(%rsp), %eax
jne 0x3cff
movl 0x64(%rsp), %edi
leaq 0x98(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x39b0
jmp 0x3cfd
jmp 0x3d1e
leaq 0x636f(%rip), %rdi      # 0xa075
leaq 0x636e(%rip), %rsi      # 0xa07b
movl $0x90, %edx
leaq 0x6373(%rip), %rcx      # 0xa08c
callq 0x3180
jmp 0x3d20
jmp 0x3d22
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3c47
movq 0xa29f(%rip), %rdi      # 0xdfd8
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3450
movb %al, 0xb(%rsp)
jmp 0x3d4e
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0x3d58
jmp 0x3d5a
jmp 0x3d5f
jmp 0x3c17
movl $0x0, 0xc4(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x3e60
leaq 0x98(%rsp), %rdi
callq 0x3170
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq
movq 0x88(%rsp), %rdi
callq 0x33f0
nopl (%rax)