// File: alfanick[P]rest-cpp/src/rest/response.cpp
// Function: REST::Response::send()
size_t Response::send() {
  if (is_streamed)
    return 0;

  size_t bytes_sent = 0;

  std::string payload;

  if (!data.empty()) {
    headers["Content-Type"] = "application/json; charset=utf-8";
    payload = data.dump();
  } else {
    payload = raw;
  }

  // start http
  std::string content = "HTTP/1.1 " + std::to_string(status) + " " + status_message + "\r\n";

  // content size
  headers["Server"] += ", took " + std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start_time).count() / 1000.0f) + "ms";
  headers.emplace("Content-Length", std::to_string(payload.size()));
  headers.emplace("Date", Utils::rfc1123_datetime(time(0)));

  // add headers
  for (auto header : headers)
    content += header.first + ": " + header.second + "\r\n";

  content += "\r\n";

  content += payload;

  // send every byte
  bytes_sent = ::send(handle, content.c_str(), content.size(), MSG_DONTWAIT | MSG_NOSIGNAL);

  // close connection with client
  close(handle);

  return bytes_sent;
}

// Assembly:
subq $0x388, %rsp            # imm = 0x388
movq %rdi, 0x378(%rsp)
movq 0x378(%rsp), %rax
movq %rax, 0x98(%rsp)
testb $0x1, 0x9c(%rax)
je 0xdcf49
movq $0x0, 0x380(%rsp)
jmp 0xdd842
movq $0x0, 0x370(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x959e0
movq 0x98(%rsp), %rdi
addq $0x78, %rdi
callq 0x93860
testb $0x1, %al
jne 0xdd0b7
movq 0x98(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x88(%rsp)
leaq 0x32f(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x98200
movq 0x90(%rsp), %rdx
leaq 0x72cb(%rip), %rsi      # 0xe427e
leaq 0x330(%rsp), %rdi
callq 0x95700
jmp 0xdcfc2
movq 0x88(%rsp), %rdi
leaq 0x330(%rsp), %rsi
callq 0x98d00
movq %rax, 0x80(%rsp)
jmp 0xdcfe1
movq 0x80(%rsp), %rdi
leaq 0x9cd7(%rip), %rsi      # 0xe6cc7
callq 0x955e0
jmp 0xdcff7
leaq 0x330(%rsp), %rdi
callq 0x99830
leaq 0x32f(%rsp), %rdi
callq 0x938a0
movq 0x98(%rsp), %rsi
addq $0x78, %rsi
leaq 0x2f8(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x948b0
jmp 0xdd031
leaq 0x350(%rsp), %rdi
leaq 0x2f8(%rsp), %rsi
callq 0x94870
leaq 0x2f8(%rsp), %rdi
callq 0x99830
jmp 0xdd0d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd08c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x330(%rsp), %rdi
callq 0x99830
leaq 0x32f(%rsp), %rdi
callq 0x938a0
jmp 0xdd833
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd833
movq 0x98(%rsp), %rsi
addq $0x28, %rsi
leaq 0x350(%rsp), %rdi
callq 0x8d2c0
jmp 0xdd0d2
jmp 0xdd0d4
movq 0x98(%rsp), %rax
movl (%rax), %esi
leaq 0x258(%rsp), %rdi
callq 0x950d0
jmp 0xdd0ed
leaq 0x9bbe(%rip), %rsi      # 0xe6cb2
leaq 0x278(%rsp), %rdi
leaq 0x258(%rsp), %rdx
callq 0x97a00
jmp 0xdd10b
leaq 0x8032(%rip), %rdx      # 0xe5144
leaq 0x298(%rsp), %rdi
leaq 0x278(%rsp), %rsi
callq 0x8df90
jmp 0xdd129
movq 0x98(%rsp), %rdx
addq $0x8, %rdx
leaq 0x2b8(%rsp), %rdi
leaq 0x298(%rsp), %rsi
callq 0x96560
jmp 0xdd14c
leaq 0x9b69(%rip), %rdx      # 0xe6cbc
leaq 0x2d8(%rsp), %rdi
leaq 0x2b8(%rsp), %rsi
callq 0x8df90
jmp 0xdd16a
leaq 0x2b8(%rsp), %rdi
callq 0x99830
leaq 0x298(%rsp), %rdi
callq 0x99830
leaq 0x278(%rsp), %rdi
callq 0x99830
leaq 0x258(%rsp), %rdi
callq 0x99830
callq 0x8d460
movq 0x98(%rsp), %rsi
movq %rax, 0x1e0(%rsp)
addq $0x88, %rsi
leaq 0x1e0(%rsp), %rdi
callq 0x90650
movq %rax, 0x78(%rsp)
jmp 0xdd1ce
movq 0x78(%rsp), %rax
movq %rax, 0x1e8(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x8dc80
movq %rax, 0x70(%rsp)
jmp 0xdd1ef
movq 0x70(%rsp), %rax
movq %rax, 0x1f0(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x91320
movq %rax, 0x68(%rsp)
jmp 0xdd210
movq 0x68(%rsp), %rax
vcvtsi2ss %rax, %xmm0, %xmm0
vmovss 0x9a2a(%rip), %xmm1     # 0xe6c4c
vdivss %xmm1, %xmm0, %xmm0
leaq 0x1f8(%rsp), %rdi
callq 0x902a0
jmp 0xdd235
leaq 0x9aab(%rip), %rsi      # 0xe6ce7
leaq 0x218(%rsp), %rdi
leaq 0x1f8(%rsp), %rdx
callq 0x97a00
jmp 0xdd253
leaq 0x9a95(%rip), %rdx      # 0xe6cef
leaq 0x238(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x8df90
jmp 0xdd271
movq 0x98(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x58(%rsp)
leaq 0x1bf(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x98200
movq 0x60(%rsp), %rdx
leaq 0x96a5(%rip), %rsi      # 0xe6945
leaq 0x1c0(%rsp), %rdi
callq 0x95700
jmp 0xdd2af
movq 0x58(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x98d00
movq %rax, 0x50(%rsp)
jmp 0xdd2c8
movq 0x50(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x94d40
jmp 0xdd2dc
leaq 0x1c0(%rsp), %rdi
callq 0x99830
leaq 0x1bf(%rsp), %rdi
callq 0x938a0
leaq 0x238(%rsp), %rdi
callq 0x99830
leaq 0x218(%rsp), %rdi
callq 0x99830
leaq 0x1f8(%rsp), %rdi
callq 0x99830
movq 0x98(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x48(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x91060
movq %rax, %rsi
leaq 0x198(%rsp), %rdi
callq 0x8ed70
jmp 0xdd34d
movq 0x48(%rsp), %rdi
leaq 0x6f16(%rip), %rsi      # 0xe426f
leaq 0x198(%rsp), %rdx
callq 0x98e90
movb %dl, 0x3f(%rsp)
movq %rax, 0x40(%rsp)
jmp 0xdd371
movb 0x3f(%rsp), %al
movq 0x40(%rsp), %rcx
movq %rcx, 0x188(%rsp)
movb %al, 0x190(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x99830
movq 0x98(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x92a80
movq %rax, %rsi
leaq 0x168(%rsp), %rdi
callq 0x92ba0
jmp 0xdd3c2
movq 0x30(%rsp), %rdi
leaq 0x98f1(%rip), %rsi      # 0xe6cbf
leaq 0x168(%rsp), %rdx
callq 0x97c00
movb %dl, 0x27(%rsp)
movq %rax, 0x28(%rsp)
jmp 0xdd3e6
movb 0x27(%rsp), %al
movq 0x28(%rsp), %rcx
movq %rcx, 0x158(%rsp)
movb %al, 0x160(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x99830
movq 0x98(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x150(%rsp)
movq 0x150(%rsp), %rdi
callq 0x95350
movq %rax, 0x148(%rsp)
movq 0x150(%rsp), %rdi
callq 0x90f60
movq %rax, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x92560
testb $0x1, %al
jne 0xdd467
jmp 0xdd75d
leaq 0x148(%rsp), %rdi
callq 0x91a50
movq %rax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x92220
jmp 0xdd486
leaq 0x9837(%rip), %rdx      # 0xe6cc4
leaq 0xa0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x94d90
jmp 0xdd4a4
leaq 0x120(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x96560
jmp 0xdd4c3
leaq 0x97f2(%rip), %rdx      # 0xe6cbc
leaq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x8df90
jmp 0xdd4e1
leaq 0x2d8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x94d40
jmp 0xdd4f8
leaq 0xe0(%rsp), %rdi
callq 0x99830
leaq 0xc0(%rsp), %rdi
callq 0x99830
leaq 0xa0(%rsp), %rdi
callq 0x99830
leaq 0x100(%rsp), %rdi
callq 0x8dbc0
leaq 0x148(%rsp), %rdi
callq 0x98ae0
jmp 0xdd449
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd5bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd5ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd5a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0x99830
leaq 0x298(%rsp), %rdi
callq 0x99830
leaq 0x278(%rsp), %rdi
callq 0x99830
leaq 0x258(%rsp), %rdi
callq 0x99830
jmp 0xdd833
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd670
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd649
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x99830
leaq 0x1bf(%rsp), %rdi
callq 0x938a0
leaq 0x238(%rsp), %rdi
callq 0x99830
leaq 0x218(%rsp), %rdi
callq 0x99830
leaq 0x1f8(%rsp), %rdi
callq 0x99830
jmp 0xdd826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x99830
jmp 0xdd826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x99830
jmp 0xdd826
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd74b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd73e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0xdd731
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x99830
leaq 0xc0(%rsp), %rdi
callq 0x99830
leaq 0xa0(%rsp), %rdi
callq 0x99830
leaq 0x100(%rsp), %rdi
callq 0x8dbc0
jmp 0xdd826
leaq 0x9558(%rip), %rsi      # 0xe6cbc
leaq 0x2d8(%rsp), %rdi
callq 0x92d80
jmp 0xdd773
leaq 0x2d8(%rsp), %rdi
leaq 0x350(%rsp), %rsi
callq 0x94d40
jmp 0xdd78a
movq 0x98(%rsp), %rax
movl 0x98(%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0x2d8(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x8f110
movq (%rsp), %rdi
movq %rax, 0x10(%rsp)
callq 0x91060
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
movl $0x4040, %ecx           # imm = 0x4040
callq 0x8fa00
movq %rax, 0x18(%rsp)
jmp 0xdd7d8
movq 0x98(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x370(%rsp)
movl 0x98(%rax), %edi
callq 0x99370
jmp 0xdd7fa
movq 0x370(%rsp), %rax
movq %rax, 0x380(%rsp)
leaq 0x2d8(%rsp), %rdi
callq 0x99830
leaq 0x350(%rsp), %rdi
callq 0x99830
jmp 0xdd842
leaq 0x2d8(%rsp), %rdi
callq 0x99830
leaq 0x350(%rsp), %rdi
callq 0x99830
jmp 0xdd852
movq 0x380(%rsp), %rax
addq $0x388, %rsp            # imm = 0x388
retq
movq 0x320(%rsp), %rdi
callq 0x981c0
nop