// File: king1600[P]cordpp/src/http.cc
// Function: cordpp::RestClient::handle_response()
void RestClient::handle_response() {
  // if response.gzipped: response.body = ....
  const Json data = Json::parse(
    response.body.empty() ? "{}" : response.body);
  const RestTask task = std::move(tasks.front());
  std::map<std::string, std::string> &headers = response.headers;
  tasks.pop_front();

  // check if next request will be rate limited
  if (headers.find("X-RateLimit-Remaining") != headers.end()) {
    if (std::stoi(headers["X-RateLimit-Remaining"], nullptr, 10) < 1) {

      // get time to wait for rate limit
      long wait_time;
      if (headers.find("X-RateLimit-Reset") != headers.end()) {
        const long now = static_cast<const long>(std::time(nullptr));
        long reset = std::stol(headers["X-RateLimit-Reset"], nullptr, 10);
        wait_time = (reset - now) * 1000;
      } else if (headers.find("Retry-After") != headers.end()) {
        wait_time = std::stol(headers["Retry-After"], nullptr, 10);
      }

      // if global, limit global route
      if (data.find("global") != data.end()) {
        if (data["global"].get<std::string>() == "true") {
          global_route.rate_limited = true;
          service.call_later(wait_time, nullptr, [this](void *data) {
            flush_route(global_route);
          });
        }
      }

      // limit request route as well
      RestRoute &route = get_route(task.req);
      route.rate_limited = true;
      service.call_later(wait_time, &route, [this](void *data) {
        flush_route(*reinterpret_cast<RestRoute*>(data));
      });
    }
  }

  // start parsing again and perform callback with data
  parse_data();
  task.action(data);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x338(%rbp)
addq $0x28, %rdi
addq $0x8, %rdi
callq 0xc410
movb $0x0, -0x4a(%rbp)
testb $0x1, %al
jne 0x50021
jmp 0x50050
leaq -0x49(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0xcc80
movq -0x340(%rbp), %rdx
movb $0x1, -0x4a(%rbp)
leaq 0x414dc(%rip), %rsi     # 0x9151f
leaq -0x48(%rbp), %rdi
callq 0x107e0
jmp 0x5004e
jmp 0x50068
movq -0x338(%rbp), %rsi
addq $0x30, %rsi
leaq -0x48(%rbp), %rdi
callq 0xc1f0
jmp 0x50066
jmp 0x50068
leaq -0x28(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x44e20
jmp 0x50077
xorl %eax, %eax
movl %eax, %esi
leaq -0x80(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x44e70
movq -0x348(%rbp), %rdx
leaq -0x18(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x1, %ecx
callq 0x44c90
jmp 0x500a6
leaq -0x80(%rbp), %rdi
callq 0x44eb0
leaq -0x28(%rbp), %rdi
callq 0x44ed0
leaq -0x48(%rbp), %rdi
callq 0xcfb8
testb $0x1, -0x4a(%rbp)
jne 0x500c9
jmp 0x500d2
leaq -0x49(%rbp), %rdi
callq 0xcfa0
movq -0x338(%rbp), %rdi
addq $0xd8, %rdi
movq %rdi, -0x360(%rbp)
callq 0x54800
movq %rax, %rsi
leaq -0x110(%rbp), %rdi
callq 0x548f0
movq -0x338(%rbp), %rax
movq -0x360(%rbp), %rdi
addq $0x50, %rax
movq %rax, -0x118(%rbp)
callq 0x54870
movq -0x118(%rbp), %rax
movq %rax, -0x358(%rbp)
leaq -0x141(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0xcc80
movq -0x350(%rbp), %rdx
leaq 0x43eb2(%rip), %rsi     # 0x93ffa
leaq -0x140(%rbp), %rdi
callq 0x107e0
jmp 0x50156
movq -0x358(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x54960
movq %rax, -0x368(%rbp)
jmp 0x50172
movq -0x368(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x118(%rbp), %rdi
callq 0x54990
movq %rax, -0x150(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x54930
movb %al, -0x369(%rbp)
leaq -0x140(%rbp), %rdi
callq 0xcfb8
leaq -0x141(%rbp), %rdi
callq 0xcfa0
movb -0x369(%rbp), %al
testb $0x1, %al
jne 0x501d3
jmp 0x509c6
movq -0x118(%rbp), %rax
movq %rax, -0x380(%rbp)
leaq -0x171(%rbp), %rdi
movq %rdi, -0x378(%rbp)
callq 0xcc80
movq -0x378(%rbp), %rdx
leaq 0x43df8(%rip), %rsi     # 0x93ffa
leaq -0x170(%rbp), %rdi
callq 0x107e0
jmp 0x50210
movq -0x380(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x54a00
movq %rax, -0x388(%rbp)
jmp 0x5022c
movq -0x388(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x549c0
movl %eax, -0x38c(%rbp)
jmp 0x50249
leaq -0x170(%rbp), %rdi
callq 0xcfb8
leaq -0x171(%rbp), %rdi
callq 0xcfa0
movl -0x38c(%rbp), %eax
cmpl $0x1, %eax
jge 0x509c4
movq -0x118(%rbp), %rax
movq %rax, -0x3a0(%rbp)
leaq -0x1a9(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0xcc80
movq -0x398(%rbp), %rdx
leaq 0x43d71(%rip), %rsi     # 0x94010
leaq -0x1a8(%rbp), %rdi
callq 0x107e0
jmp 0x502ad
movq -0x3a0(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x54960
movq %rax, -0x3a8(%rbp)
jmp 0x502c9
movq -0x3a8(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x118(%rbp), %rdi
callq 0x54990
movq %rax, -0x1b8(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x54930
movb %al, -0x3a9(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0xcfb8
leaq -0x1a9(%rbp), %rdi
callq 0xcfa0
movb -0x3a9(%rbp), %al
testb $0x1, %al
jne 0x5032a
jmp 0x5052d
xorl %eax, %eax
movl %eax, %edi
callq 0xc970
movq %rax, -0x1c0(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x3c0(%rbp)
leaq -0x1e9(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0xcc80
movq -0x3b8(%rbp), %rdx
leaq 0x43ca7(%rip), %rsi     # 0x94010
leaq -0x1e8(%rbp), %rdi
callq 0x107e0
jmp 0x50377
movq -0x3c0(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x54a00
movq %rax, -0x3c8(%rbp)
jmp 0x50393
movq -0x3c8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x54ae0
movq %rax, -0x3d0(%rbp)
jmp 0x503b1
leaq -0x1e8(%rbp), %rdi
callq 0xcfb8
leaq -0x1e9(%rbp), %rdi
callq 0xcfa0
movq -0x3d0(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rax
subq -0x1c0(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x180(%rbp)
jmp 0x506f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5043b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x50432
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x44eb0
leaq -0x28(%rbp), %rdi
callq 0x44ed0
leaq -0x48(%rbp), %rdi
callq 0xcfb8
testb $0x1, -0x4a(%rbp)
jne 0x50443
jmp 0x5044c
leaq -0x49(%rbp), %rdi
callq 0xcfa0
jmp 0x50a19
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x50477
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0xcfb8
leaq -0x141(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x504ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0xcfb8
leaq -0x171(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x504e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0xcfb8
leaq -0x1a9(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5051c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0xcfb8
leaq -0x1e9(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
movq -0x118(%rbp), %rax
movq %rax, -0x3e0(%rbp)
leaq -0x219(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0xcc80
movq -0x3d8(%rbp), %rdx
leaq 0x43ac6(%rip), %rsi     # 0x94022
leaq -0x218(%rbp), %rdi
callq 0x107e0
jmp 0x5056a
movq -0x3e0(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x54960
movq %rax, -0x3e8(%rbp)
jmp 0x50586
movq -0x3e8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movq -0x118(%rbp), %rdi
callq 0x54990
movq %rax, -0x228(%rbp)
leaq -0x1f8(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x54930
movb %al, -0x3e9(%rbp)
leaq -0x218(%rbp), %rdi
callq 0xcfb8
leaq -0x219(%rbp), %rdi
callq 0xcfa0
movb -0x3e9(%rbp), %al
testb $0x1, %al
jne 0x505e7
jmp 0x506f4
movq -0x118(%rbp), %rax
movq %rax, -0x400(%rbp)
leaq -0x249(%rbp), %rdi
movq %rdi, -0x3f8(%rbp)
callq 0xcc80
movq -0x3f8(%rbp), %rdx
leaq 0x43a0c(%rip), %rsi     # 0x94022
leaq -0x248(%rbp), %rdi
callq 0x107e0
jmp 0x50624
movq -0x400(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x54a00
movq %rax, -0x408(%rbp)
jmp 0x50640
movq -0x408(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x54ae0
movq %rax, -0x410(%rbp)
jmp 0x5065e
movq -0x410(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x248(%rbp), %rdi
callq 0xcfb8
leaq -0x249(%rbp), %rdi
callq 0xcfa0
jmp 0x506f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x506ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x218(%rbp), %rdi
callq 0xcfb8
leaq -0x219(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x506e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x248(%rbp), %rdi
callq 0xcfb8
leaq -0x249(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
jmp 0x506f6
leaq -0x291(%rbp), %rdi
movq %rdi, -0x418(%rbp)
callq 0xcc80
movq -0x418(%rbp), %rdx
leaq 0x43917(%rip), %rsi     # 0x9402e
leaq -0x290(%rbp), %rdi
callq 0x107e0
jmp 0x50725
leaq -0x270(%rbp), %rdi
leaq -0x18(%rbp), %rsi
leaq -0x290(%rbp), %rdx
callq 0x54b20
jmp 0x5073e
leaq -0x2b8(%rbp), %rdi
movq %rdi, -0x428(%rbp)
leaq -0x18(%rbp), %rsi
callq 0x54bc0
movq -0x428(%rbp), %rsi
leaq -0x270(%rbp), %rdi
callq 0x54b90
movb %al, -0x419(%rbp)
jmp 0x50770
leaq -0x290(%rbp), %rdi
callq 0xcfb8
leaq -0x291(%rbp), %rdi
callq 0xcfa0
movb -0x419(%rbp), %al
testb $0x1, %al
jne 0x50797
jmp 0x508f7
leaq 0x43890(%rip), %rsi     # 0x9402e
leaq -0x18(%rbp), %rdi
callq 0x1f270
movq %rax, -0x430(%rbp)
jmp 0x507b0
movq -0x430(%rbp), %rsi
leaq -0x2d8(%rbp), %rdi
callq 0x1f5e0
jmp 0x507c5
leaq 0x40e93(%rip), %rsi     # 0x9165f
leaq -0x2d8(%rbp), %rdi
callq 0x21800
movb %al, -0x431(%rbp)
jmp 0x507e0
leaq -0x2d8(%rbp), %rdi
callq 0xcfb8
movb -0x431(%rbp), %al
testb $0x1, %al
jne 0x507fb
jmp 0x508f5
movq -0x338(%rbp), %rax
movb $0x1, 0x80(%rax)
movq (%rax), %rcx
movq %rcx, -0x450(%rbp)
movq -0x180(%rbp), %rcx
movq %rcx, -0x448(%rbp)
movq %rax, -0x300(%rbp)
leaq -0x2f8(%rbp), %rdi
movq %rdi, -0x440(%rbp)
leaq -0x300(%rbp), %rsi
callq 0x50a30
movq -0x450(%rbp), %rdi
movq -0x448(%rbp), %rsi
movq -0x440(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0x5a240
jmp 0x50862
leaq -0x2f8(%rbp), %rdi
callq 0x54bf0
jmp 0x508f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x50899
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x290(%rbp), %rdi
callq 0xcfb8
leaq -0x291(%rbp), %rdi
callq 0xcfa0
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0xcfb8
jmp 0x50a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x2f8(%rbp), %rdi
callq 0x54bf0
jmp 0x50a04
jmp 0x508f7
movq -0x338(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x4f370
movq %rax, -0x458(%rbp)
jmp 0x50913
movq -0x338(%rbp), %rax
movq -0x458(%rbp), %rcx
movq %rcx, -0x308(%rbp)
movq -0x308(%rbp), %rcx
movb $0x1, (%rcx)
movq (%rax), %rcx
movq %rcx, -0x478(%rbp)
movq -0x180(%rbp), %rcx
movq %rcx, -0x470(%rbp)
movq -0x308(%rbp), %rcx
movq %rcx, -0x468(%rbp)
movq %rax, -0x330(%rbp)
leaq -0x328(%rbp), %rdi
movq %rdi, -0x460(%rbp)
leaq -0x330(%rbp), %rsi
callq 0x50ac0
movq -0x478(%rbp), %rdi
movq -0x470(%rbp), %rsi
movq -0x468(%rbp), %rdx
movq -0x460(%rbp), %rcx
callq 0x5a240
jmp 0x5099c
leaq -0x328(%rbp), %rdi
callq 0x54bf0
jmp 0x509c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x54bf0
jmp 0x50a04
jmp 0x509c6
movq -0x338(%rbp), %rdi
callq 0x50b50
jmp 0x509d4
leaq -0xa0(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x54c10
jmp 0x509e6
leaq -0x110(%rbp), %rdi
callq 0x1a640
leaq -0x18(%rbp), %rdi
callq 0x12560
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
leaq -0x110(%rbp), %rdi
callq 0x1a640
leaq -0x18(%rbp), %rdi
callq 0x12560
movq -0x58(%rbp), %rdi
callq 0xc8c0
nopw %cs:(%rax,%rax)