// File: alfanick[P]rest-cpp/src/rest/response.cpp
// Function: REST::Response::stream(std::function<void (int)>, bool)
void Response::stream(std::function<void(int)> streamer, bool async) {
  is_streamed = true;

  std::string content = "HTTP/1.1 " + std::to_string(status) + " " + status_message + "\r\n";
  headers.emplace("Date", Utils::rfc1123_datetime(time(0)));

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

  content += "\r\n";

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

  if (async && streamers.capacity() > 0) {
    int h = handle;
    streamers.emplace_back([streamer, h]() {
      signal(SIGPIPE, SIG_IGN);
      try {
        // std::this_thread::yield();
        streamer(h);
        close(h);
      } catch (...) {
      }
    });
  } else {
    std::this_thread::yield();
    streamer(handle);
    close(handle);
  }
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rsi, 0x40(%rsp)
movb %dl, %al
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
andb $0x1, %al
movb %al, 0x227(%rsp)
movq 0x230(%rsp), %rax
movq %rax, 0x48(%rsp)
movb $0x1, 0x9c(%rax)
movl (%rax), %esi
leaq 0x180(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x950d0
movq 0x50(%rsp), %rdx
leaq 0xa327(%rip), %rsi      # 0xe6cb2
leaq 0x1a0(%rsp), %rdi
callq 0x97a00
jmp 0xdc99a
leaq 0x87a3(%rip), %rdx      # 0xe5144
leaq 0x1c0(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x8df90
jmp 0xdc9b8
movq 0x48(%rsp), %rdx
addq $0x8, %rdx
leaq 0x1e0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x96560
jmp 0xdc9d8
leaq 0xa2dd(%rip), %rdx      # 0xe6cbc
leaq 0x200(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x8df90
jmp 0xdc9f6
leaq 0x1e0(%rsp), %rdi
callq 0x99830
leaq 0x1c0(%rsp), %rdi
callq 0x99830
leaq 0x1a0(%rsp), %rdi
callq 0x99830
leaq 0x180(%rsp), %rdi
callq 0x99830
movq 0x48(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x92a80
movq %rax, %rsi
leaq 0x150(%rsp), %rdi
callq 0x92ba0
jmp 0xdca53
movq 0x38(%rsp), %rdi
leaq 0xa260(%rip), %rsi      # 0xe6cbf
leaq 0x150(%rsp), %rdx
callq 0x97c00
movb %dl, 0x2f(%rsp)
movq %rax, 0x30(%rsp)
jmp 0xdca77
movb 0x2f(%rsp), %al
movq 0x30(%rsp), %rcx
movq %rcx, 0x140(%rsp)
movb %al, 0x148(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x99830
movq 0x48(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rdi
callq 0x95350
movq %rax, 0x130(%rsp)
movq 0x138(%rsp), %rdi
callq 0x90f60
movq %rax, 0x128(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x92560
testb $0x1, %al
jne 0xdcaf5
jmp 0xdcd29
leaq 0x130(%rsp), %rdi
callq 0x91a50
movq %rax, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x92220
jmp 0xdcb14
leaq 0xa1a9(%rip), %rdx      # 0xe6cc4
leaq 0x88(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x94d90
jmp 0xdcb32
leaq 0x108(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x96560
jmp 0xdcb51
leaq 0xa164(%rip), %rdx      # 0xe6cbc
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x8df90
jmp 0xdcb6f
leaq 0x200(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x94d40
jmp 0xdcb86
leaq 0xc8(%rsp), %rdi
callq 0x99830
leaq 0xa8(%rsp), %rdi
callq 0x99830
leaq 0x88(%rsp), %rdi
callq 0x99830
leaq 0xe8(%rsp), %rdi
callq 0x8dbc0
leaq 0x130(%rsp), %rdi
callq 0x98ae0
jmp 0xdcad7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdcc49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdcc3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdcc2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x99830
leaq 0x1c0(%rsp), %rdi
callq 0x99830
leaq 0x1a0(%rsp), %rdi
callq 0x99830
leaq 0x180(%rsp), %rdi
callq 0x99830
jmp 0xdce75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdce68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x99830
jmp 0xdce68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdcd17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdcd0a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0xdccfd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x99830
leaq 0xa8(%rsp), %rdi
callq 0x99830
leaq 0x88(%rsp), %rdi
callq 0x99830
leaq 0xe8(%rsp), %rdi
callq 0x8dbc0
jmp 0xdce68
leaq 0x9f8c(%rip), %rsi      # 0xe6cbc
leaq 0x200(%rsp), %rdi
callq 0x92d80
jmp 0xdcd3f
movq 0x48(%rsp), %rax
movl 0x98(%rax), %eax
movl %eax, 0x1c(%rsp)
leaq 0x200(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x8f110
movq 0x10(%rsp), %rdi
movq %rax, 0x20(%rsp)
callq 0x91060
movl 0x1c(%rsp), %edi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
movl $0x4040, %ecx           # imm = 0x4040
callq 0x8fa00
jmp 0xdcd87
testb $0x1, 0x227(%rsp)
je 0xdce23
movq 0x48(%rsp), %rax
movq 0x90(%rax), %rdi
callq 0x91fd0
cmpq $0x0, %rax
jbe 0xdce23
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rax
movl 0x98(%rax), %ecx
movl %ecx, 0x84(%rsp)
movq 0x90(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x997f0
jmp 0xdcddb
movq 0x8(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x78(%rsp)
leaq 0x58(%rsp), %rsi
callq 0xdce90
jmp 0xdcdf7
leaq 0x58(%rsp), %rdi
callq 0xdcf00
jmp 0xdce53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xdcf00
jmp 0xdce68
callq 0x8db30
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rdi
movl 0x98(%rax), %esi
callq 0x92420
jmp 0xdce3f
movq 0x48(%rsp), %rax
movl 0x98(%rax), %edi
callq 0x99370
jmp 0xdce51
jmp 0xdce53
leaq 0x200(%rsp), %rdi
callq 0x99830
addq $0x238, %rsp            # imm = 0x238
retq
leaq 0x200(%rsp), %rdi
callq 0x99830
movq 0x178(%rsp), %rdi
callq 0x981c0
nopw %cs:(%rax,%rax)