// File: CoinBlack[P]blackcoin-seeder/main.cpp
// Function: main
int main(int argc, char **argv) {
  signal(SIGPIPE, SIG_IGN);
  setbuf(stdout, NULL);
  CDnsSeedOpts opts;
  opts.ParseCommandLine(argc, argv);
  printf("Supporting whitelisted filters: ");
  for (std::set<uint64_t>::const_iterator it = opts.filter_whitelist.begin(); it != opts.filter_whitelist.end(); it++) {
      if (it != opts.filter_whitelist.begin()) {
          printf(",");
      }
      printf("0x%lx", (unsigned long)*it);
  }
  printf("\n");
  if (opts.tor) {
    CService service(opts.tor, 9050);
    if (service.IsValid()) {
      printf("Using Tor proxy at %s\n", service.ToStringIPPort().c_str());
      SetProxy(NET_TOR, service);
    }
  }
  if (opts.ipv4_proxy) {
    CService service(opts.ipv4_proxy, 9050);
    if (service.IsValid()) {
      printf("Using IPv4 proxy at %s\n", service.ToStringIPPort().c_str());
      SetProxy(NET_IPV4, service);
    }
  }
  if (opts.ipv6_proxy) {
    CService service(opts.ipv6_proxy, 9050);
    if (service.IsValid()) {
      printf("Using IPv6 proxy at %s\n", service.ToStringIPPort().c_str());
      SetProxy(NET_IPV6, service);
    }
  }
  bool fDNS = true;
  if (opts.fUseTestNet) {
      printf("Using testnet.\n");
      pchMessageStart[0] = 0xcd;
      pchMessageStart[1] = 0xf2;
      pchMessageStart[2] = 0xc0;
      pchMessageStart[3] = 0xef;
      seeds = testnet_seeds;
      fTestNet = true;
  }
  if (opts.nP2Port) {
    printf("Using P2P port %i\n", opts.nP2Port);
    nDefaultP2Port = opts.nP2Port;
  }
  if (opts.magic) {
    printf("Using magic %s\n", opts.magic);
    for (int n=0; n<4; ++n) {
      unsigned int c = 0;
      sscanf(&opts.magic[n*2], "%2x", &c);
      pchMessageStart[n] = (unsigned char) (c & 0xff);
    }
  }
  if (opts.nMinimumHeight) {
    printf("Using minimum height %i\n", opts.nMinimumHeight);
    nMinimumHeight = opts.nMinimumHeight;
  }
  if (!opts.vSeeds.empty()) {
    printf("Overriding DNS seeds\n");
    swap(opts.vSeeds, vSeeds);
  } else {
    for (int i=0; seeds[i][0]; i++) {
      vSeeds.emplace_back(seeds[i]);
    }
  }
  if (!opts.ns) {
    printf("No nameserver set. Not starting DNS server.\n");
    fDNS = false;
  }
  if (fDNS && !opts.host) {
    fprintf(stderr, "No hostname set. Please use -h.\n");
    exit(1);
  }
  if (fDNS && !opts.mbox) {
    fprintf(stderr, "No e-mail address set. Please use -m.\n");
    exit(1);
  }
  FILE *f = fopen("dnsseed.dat","r");
  if (f) {
    printf("Loading dnsseed.dat...");
    CAutoFile cf(f);
    cf >> db;
    if (opts.fWipeBan)
        db.banned.clear();
    if (opts.fWipeIgnore)
        db.ResetIgnores();
    printf("done\n");
  }
  pthread_t threadDns, threadSeed, threadDump, threadStats;
  if (fDNS) {
    printf("Starting %i DNS threads for %s on %s (port %i)...", opts.nDnsThreads, opts.host, opts.ns, opts.nPort);
    dnsThread.clear();
    for (int i=0; i<opts.nDnsThreads; i++) {
      dnsThread.push_back(new CDnsThread(&opts, i));
      pthread_create(&threadDns, NULL, ThreadDNS, dnsThread[i]);
      printf(".");
      Sleep(20);
    }
    printf("done\n");
  }
  printf("Starting seeder...");
  pthread_create(&threadSeed, NULL, ThreadSeeder, NULL);
  printf("done\n");
  printf("Starting %i crawler threads...", opts.nThreads);
  pthread_attr_t attr_crawler;
  pthread_attr_init(&attr_crawler);
  pthread_attr_setstacksize(&attr_crawler, 0x20000);
  for (int i=0; i<opts.nThreads; i++) {
    pthread_t thread;
    pthread_create(&thread, &attr_crawler, ThreadCrawler, &opts.nThreads);
  }
  pthread_attr_destroy(&attr_crawler);
  printf("done\n");
  pthread_create(&threadStats, NULL, ThreadStats, NULL);
  pthread_create(&threadDump, NULL, ThreadDumper, NULL);
  void* res;
  pthread_join(threadDump, &res);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xd, %edi
movl $0x1, %esi
callq 0x45b0
movq 0x28e61(%rip), %rax     # 0x4cf80
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4720
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x26220
movq -0x298(%rbp), %rdi
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0x262d0
jmp 0x24153
leaq 0x11c6c(%rip), %rdi     # 0x35dc6
xorl %eax, %eax
callq 0x4070
jmp 0x24163
leaq -0xb8(%rbp), %rdi
addq $0x78, %rdi
callq 0x25250
movq %rax, -0xd0(%rbp)
leaq -0xb8(%rbp), %rdi
addq $0x78, %rdi
callq 0x25280
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x251d0
testb $0x1, %al
jne 0x241ad
jmp 0x24241
leaq -0xb8(%rbp), %rdi
addq $0x78, %rdi
callq 0x25250
movq %rax, -0xe0(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x251d0
testb $0x1, %al
jne 0x241dd
jmp 0x24206
leaq 0x11c03(%rip), %rdi     # 0x35de7
xorl %eax, %eax
callq 0x4070
jmp 0x241ed
jmp 0x24206
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
jmp 0x24b3a
leaq -0xd0(%rbp), %rdi
callq 0x26d50
movq (%rax), %rsi
leaq 0x11bcd(%rip), %rdi     # 0x35de9
xorl %eax, %eax
callq 0x4070
jmp 0x24225
jmp 0x24227
leaq -0xd0(%rbp), %rdi
xorl %esi, %esi
callq 0x26d70
movq %rax, -0xe8(%rbp)
jmp 0x2417a
leaq 0x1212a(%rip), %rdi     # 0x36372
xorl %eax, %eax
callq 0x4070
jmp 0x24251
cmpq $0x0, -0x80(%rbp)
je 0x24344
movq -0x80(%rbp), %rsi
leaq -0xfa(%rbp), %rdi
movl $0x235a, %edx           # imm = 0x235A
xorl %ecx, %ecx
callq 0x14e70
jmp 0x24275
leaq -0xfa(%rbp), %rdi
callq 0x106c0
movb %al, -0x299(%rbp)
jmp 0x24289
movb -0x299(%rbp), %al
testb $0x1, %al
jne 0x24298
jmp 0x24342
leaq -0x120(%rbp), %rdi
leaq -0xfa(%rbp), %rsi
callq 0x15350
jmp 0x242ad
leaq -0x120(%rbp), %rdi
callq 0x41e0
movq %rax, %rsi
leaq 0x11b2c(%rip), %rdi     # 0x35def
xorl %eax, %eax
callq 0x4070
jmp 0x242cc
leaq -0x120(%rbp), %rdi
callq 0x42e0
movw -0xea(%rbp), %ax
movw %ax, -0x130(%rbp)
movups -0xfa(%rbp), %xmm0
movaps %xmm0, -0x140(%rbp)
movw -0x130(%rbp), %cx
movq %rsp, %rax
movw %cx, 0x10(%rax)
movaps -0x140(%rbp), %xmm0
movups %xmm0, (%rax)
movl $0x3, %edi
movl $0x5, %esi
callq 0x10600
jmp 0x2431d
jmp 0x24342
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x42e0
jmp 0x24b3a
jmp 0x24344
cmpq $0x0, -0x70(%rbp)
je 0x24437
movq -0x70(%rbp), %rsi
leaq -0x152(%rbp), %rdi
movl $0x235a, %edx           # imm = 0x235A
xorl %ecx, %ecx
callq 0x14e70
jmp 0x24368
leaq -0x152(%rbp), %rdi
callq 0x106c0
movb %al, -0x29a(%rbp)
jmp 0x2437c
movb -0x29a(%rbp), %al
testb $0x1, %al
jne 0x2438b
jmp 0x24435
leaq -0x178(%rbp), %rdi
leaq -0x152(%rbp), %rsi
callq 0x15350
jmp 0x243a0
leaq -0x178(%rbp), %rdi
callq 0x41e0
movq %rax, %rsi
leaq 0x11a50(%rip), %rdi     # 0x35e06
xorl %eax, %eax
callq 0x4070
jmp 0x243bf
leaq -0x178(%rbp), %rdi
callq 0x42e0
movw -0x142(%rbp), %ax
movw %ax, -0x180(%rbp)
movups -0x152(%rbp), %xmm0
movaps %xmm0, -0x190(%rbp)
movw -0x180(%rbp), %cx
movq %rsp, %rax
movw %cx, 0x10(%rax)
movaps -0x190(%rbp), %xmm0
movups %xmm0, (%rax)
movl $0x1, %edi
movl $0x5, %esi
callq 0x10600
jmp 0x24410
jmp 0x24435
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x42e0
jmp 0x24b3a
jmp 0x24437
cmpq $0x0, -0x68(%rbp)
je 0x2452a
movq -0x68(%rbp), %rsi
leaq -0x1a2(%rbp), %rdi
movl $0x235a, %edx           # imm = 0x235A
xorl %ecx, %ecx
callq 0x14e70
jmp 0x2445b
leaq -0x1a2(%rbp), %rdi
callq 0x106c0
movb %al, -0x29b(%rbp)
jmp 0x2446f
movb -0x29b(%rbp), %al
testb $0x1, %al
jne 0x2447e
jmp 0x24528
leaq -0x1c8(%rbp), %rdi
leaq -0x1a2(%rbp), %rsi
callq 0x15350
jmp 0x24493
leaq -0x1c8(%rbp), %rdi
callq 0x41e0
movq %rax, %rsi
leaq 0x11975(%rip), %rdi     # 0x35e1e
xorl %eax, %eax
callq 0x4070
jmp 0x244b2
leaq -0x1c8(%rbp), %rdi
callq 0x42e0
movw -0x192(%rbp), %ax
movw %ax, -0x1d0(%rbp)
movups -0x1a2(%rbp), %xmm0
movaps %xmm0, -0x1e0(%rbp)
movw -0x1d0(%rbp), %cx
movq %rsp, %rax
movw %cx, 0x10(%rax)
movaps -0x1e0(%rbp), %xmm0
movups %xmm0, (%rax)
movl $0x2, %edi
movl $0x5, %esi
callq 0x10600
jmp 0x24503
jmp 0x24528
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x42e0
jmp 0x24b3a
jmp 0x2452a
movb $0x1, -0x1e1(%rbp)
cmpl $0x0, -0xa4(%rbp)
je 0x2458a
leaq 0x118f5(%rip), %rdi     # 0x35e36
xorl %eax, %eax
callq 0x4070
jmp 0x2454a
leaq 0x28f9f(%rip), %rax     # 0x4d4f0
movb $-0x33, (%rax)
leaq 0x28f95(%rip), %rax     # 0x4d4f0
movb $-0xe, 0x1(%rax)
leaq 0x28f8a(%rip), %rax     # 0x4d4f0
movb $-0x40, 0x2(%rax)
leaq 0x28f7f(%rip), %rax     # 0x4d4f0
movb $-0x11, 0x3(%rax)
leaq 0x292f4(%rip), %rax     # 0x4d870
movq %rax, 0x28f95(%rip)     # 0x4d518
movb $0x1, 0x29056(%rip)     # 0x4d5e0
cmpl $0x0, -0xb0(%rbp)
je 0x245bc
movl -0xb0(%rbp), %esi
leaq 0x118a6(%rip), %rdi     # 0x35e46
xorl %eax, %eax
callq 0x4070
jmp 0x245a9
movl -0xb0(%rbp), %eax
movw %ax, %cx
leaq 0x29019(%rip), %rax     # 0x4d5d2
movw %cx, (%rax)
cmpq $0x0, -0x60(%rbp)
je 0x2464f
movq -0x60(%rbp), %rsi
leaq 0x11887(%rip), %rdi     # 0x35e59
xorl %eax, %eax
callq 0x4070
jmp 0x245db
movl $0x0, -0x1e8(%rbp)
cmpl $0x4, -0x1e8(%rbp)
jge 0x2464d
movl $0x0, -0x1ec(%rbp)
movq -0x60(%rbp), %rdi
movl -0x1e8(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
leaq 0x11859(%rip), %rsi     # 0x35e69
leaq -0x1ec(%rbp), %rdx
movb $0x0, %al
callq 0x44d0
movl -0x1ec(%rbp), %eax
andl $0xff, %eax
movb %al, %dl
movslq -0x1e8(%rbp), %rcx
leaq 0x28eb7(%rip), %rax     # 0x4d4f0
movb %dl, (%rax,%rcx)
movl -0x1e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e8(%rbp)
jmp 0x245e5
jmp 0x2464f
cmpl $0x0, -0xac(%rbp)
je 0x2467d
movl -0xac(%rbp), %esi
leaq 0x11808(%rip), %rdi     # 0x35e6d
xorl %eax, %eax
callq 0x4070
jmp 0x2466e
movl -0xac(%rbp), %ecx
leaq 0x28f59(%rip), %rax     # 0x4d5d4
movl %ecx, (%rax)
leaq -0xb8(%rbp), %rdi
addq $0x60, %rdi
callq 0x26db0
testb $0x1, %al
jne 0x246ba
leaq 0x117ee(%rip), %rdi     # 0x35e86
xorl %eax, %eax
callq 0x4070
jmp 0x246a1
leaq -0xb8(%rbp), %rdi
addq $0x60, %rdi
leaq 0x2925d(%rip), %rsi     # 0x4d910
callq 0x26df0
jmp 0x2471f
movl $0x0, -0x1f0(%rbp)
movq 0x28e4d(%rip), %rdi     # 0x4d518
movslq -0x1f0(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4560
cmpb $0x0, (%rax)
je 0x2471d
movq 0x28e2a(%rip), %rsi     # 0x4d518
movslq -0x1f0(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rsi
leaq 0x2920d(%rip), %rdi     # 0x4d910
callq 0x26e20
jmp 0x2470a
jmp 0x2470c
movl -0x1f0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1f0(%rbp)
jmp 0x246c4
jmp 0x2471f
cmpq $0x0, -0x90(%rbp)
jne 0x24740
leaq 0x1176c(%rip), %rdi     # 0x35e9c
xorl %eax, %eax
callq 0x4070
jmp 0x24739
movb $0x0, -0x1e1(%rbp)
testb $0x1, -0x1e1(%rbp)
je 0x24775
cmpq $0x0, -0x88(%rbp)
jne 0x24775
movq 0x28876(%rip), %rax     # 0x4cfd0
movq (%rax), %rdi
leaq 0x11765(%rip), %rsi     # 0x35ec9
movb $0x0, %al
callq 0x4740
movl $0x1, %edi
callq 0x45c0
testb $0x1, -0x1e1(%rbp)
je 0x247aa
cmpq $0x0, -0x98(%rbp)
jne 0x247aa
movq 0x28841(%rip), %rax     # 0x4cfd0
movq (%rax), %rdi
leaq 0x11751(%rip), %rsi     # 0x35eea
movb $0x0, %al
callq 0x4740
movl $0x1, %edi
callq 0x45c0
leaq 0x113c6(%rip), %rdi     # 0x35b77
leaq 0x10a87(%rip), %rsi     # 0x3523f
callq 0x45a0
movq %rax, -0x2a8(%rbp)
jmp 0x247c6
movq -0x2a8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
cmpq $0x0, -0x1f8(%rbp)
je 0x2489c
leaq 0x11728(%rip), %rdi     # 0x35f11
xorl %eax, %eax
callq 0x4070
jmp 0x247f2
movq -0x1f8(%rbp), %rsi
leaq -0x210(%rbp), %rdi
movl $0x2, %edx
movl $0x11180, %ecx          # imm = 0x11180
callq 0x25a30
jmp 0x24811
leaq 0x28dd0(%rip), %rsi     # 0x4d5e8
leaq -0x210(%rbp), %rdi
callq 0x26e90
jmp 0x24826
cmpl $0x0, -0xa0(%rbp)
je 0x24867
leaq 0x28db2(%rip), %rdi     # 0x4d5e8
addq $0x158, %rdi            # imm = 0x158
callq 0x26f40
jmp 0x24867
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x25b40
jmp 0x24b3a
cmpl $0x0, -0x9c(%rbp)
je 0x24880
leaq 0x28d71(%rip), %rdi     # 0x4d5e8
callq 0x26f60
jmp 0x2487e
jmp 0x24880
leaq 0x116a1(%rip), %rdi     # 0x35f28
xorl %eax, %eax
callq 0x4070
jmp 0x24890
leaq -0x210(%rbp), %rdi
callq 0x25b40
testb $0x1, -0x1e1(%rbp)
je 0x249eb
movl -0xb4(%rbp), %r8d
movl -0xa8(%rbp), %esi
movq -0x88(%rbp), %rdx
movq -0x90(%rbp), %rcx
leaq 0x11663(%rip), %rdi     # 0x35f2e
xorl %eax, %eax
callq 0x4070
jmp 0x248d4
leaq 0x28e95(%rip), %rdi     # 0x4d770
callq 0x26fd0
movl $0x0, -0x234(%rbp)
movl -0x234(%rbp), %eax
cmpl -0xa8(%rbp), %eax
jge 0x249d9
movl $0xb0, %edi
callq 0x44f0
movq %rax, -0x2b0(%rbp)
jmp 0x2490f
movq -0x2b0(%rbp), %rdi
movl -0x234(%rbp), %edx
leaq -0xb8(%rbp), %rsi
callq 0x27020
jmp 0x2492a
movq -0x2b0(%rbp), %rax
movq %rax, -0x240(%rbp)
leaq 0x28e31(%rip), %rdi     # 0x4d770
leaq -0x240(%rbp), %rsi
callq 0x26ff0
jmp 0x2494d
movslq -0x234(%rbp), %rsi
leaq 0x28e15(%rip), %rdi     # 0x4d770
callq 0x25e90
movq (%rax), %rcx
leaq -0x13da(%rip), %rdx     # 0x23590
xorl %eax, %eax
movl %eax, -0x2b4(%rbp)
movl %eax, %esi
leaq -0x218(%rbp), %rdi
callq 0x48b0
movl -0x2b4(%rbp), %eax
leaq 0x115d1(%rip), %rdi     # 0x35f5e
callq 0x4070
jmp 0x24994
movl $0x14, %edi
callq 0x23130
jmp 0x249a0
jmp 0x249a2
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
jmp 0x248ea
movq -0x2b0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
callq 0x44b0
jmp 0x24b3a
leaq 0x11548(%rip), %rdi     # 0x35f28
xorl %eax, %eax
callq 0x4070
jmp 0x249e9
jmp 0x249eb
leaq 0x1156e(%rip), %rdi     # 0x35f60
xorl %eax, %eax
callq 0x4070
jmp 0x249fb
leaq -0xcd2(%rip), %rdx      # 0x23d30
xorl %eax, %eax
movl %eax, -0x2b8(%rbp)
movl %eax, %ecx
leaq -0x220(%rbp), %rdi
movq %rcx, %rsi
callq 0x48b0
movl -0x2b8(%rbp), %eax
leaq 0x11500(%rip), %rdi     # 0x35f28
callq 0x4070
jmp 0x24a2f
movl -0xb8(%rbp), %esi
leaq 0x11537(%rip), %rdi     # 0x35f73
xorl %eax, %eax
callq 0x4070
jmp 0x24a45
leaq -0x278(%rbp), %rdi
callq 0x45d0
leaq -0x278(%rbp), %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x46b0
movl $0x0, -0x27c(%rbp)
movl -0x27c(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jge 0x24aac
leaq -0xb8(%rbp), %rcx
leaq -0x288(%rbp), %rdi
leaq -0x278(%rbp), %rsi
leaq -0x1c16(%rip), %rdx     # 0x22e80
callq 0x48b0
movl -0x27c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x27c(%rbp)
jmp 0x24a6c
leaq -0x278(%rbp), %rdi
callq 0x4800
leaq 0x11469(%rip), %rdi     # 0x35f28
xorl %eax, %eax
callq 0x4070
jmp 0x24ac8
leaq -0xf8f(%rip), %rdx      # 0x23b40
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, -0x2c0(%rbp)
leaq -0x230(%rbp), %rdi
movq %rcx, %rsi
callq 0x48b0
movq -0x2c0(%rbp), %rcx
leaq -0x1497(%rip), %rdx     # 0x23660
leaq -0x228(%rbp), %rdi
movq %rcx, %rsi
callq 0x48b0
movq -0x228(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x4460
jmp 0x24b1b
movl $0x0, -0x4(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x27120
movl -0x4(%rbp), %eax
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
leaq -0xb8(%rbp), %rdi
callq 0x27120
movq -0xc0(%rbp), %rdi
callq 0x4840
nopw %cs:(%rax,%rax)