// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movb $0x1, 0x9c(%rdi)
movq %rsi, %r14
leaq 0xb8(%rsp), %rbx
movl %edx, %ebp
movq %rdi, %r12
movl (%rdi), %esi
movq %rbx, %rdi
callq 0x325e0
leaq 0x598f(%rip), %rsi      # 0x4c8d8
leaq 0x78(%rsp), %rdi
movq %rbx, %rdx
callq 0x332a0
leaq 0x4047(%rip), %rdx      # 0x4afa4
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x30490
leaq 0x8(%r12), %rdx
leaq 0x18(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x32c70
leaq 0x5955(%rip), %rdx      # 0x4c8e2
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x30490
leaq 0x18(%rsp), %rdi
callq 0x33b38
leaq 0x98(%rsp), %rdi
callq 0x33b38
leaq 0x78(%rsp), %rdi
callq 0x33b38
leaq 0xb8(%rsp), %rdi
callq 0x33b38
xorl %edi, %edi
callq 0x31b20
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x31bb0
movl %ebp, 0xc(%rsp)
movq %r14, 0x10(%rsp)
leaq 0x48(%r12), %rdi
leaq 0x58f2(%rip), %rsi      # 0x4c8e5
leaq 0x18(%rsp), %rdx
callq 0x30b40
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x33b38
movq 0x60(%r12), %r14
movq %r12, (%rsp)
addq $0x50, %r12
leaq 0xb8(%rsp), %rbp
leaq 0x78(%rsp), %rbx
leaq 0x98(%rsp), %r13
cmpq %r12, %r14
je 0x470ab
leaq 0x20(%r14), %rsi
movq %r15, %rdi
callq 0x318b0
leaq 0x58a6(%rip), %rdx      # 0x4c8ea
movq %rbp, %rdi
movq %r15, %rsi
callq 0x324e0
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x32c70
leaq 0x587c(%rip), %rdx      # 0x4c8e2
movq %r13, %rdi
movq %rbx, %rsi
callq 0x30490
leaq 0x58(%rsp), %rdi
movq %r13, %rsi
callq 0x33a00
movq %r13, %rdi
callq 0x33b38
movq %rbx, %rdi
callq 0x33b38
movq %rbp, %rdi
callq 0x33b38
movq %r15, %rdi
callq 0x30390
movq %r14, %rdi
callq 0x31360
movq %rax, %r14
jmp 0x4702c
leaq 0x5830(%rip), %rsi      # 0x4c8e2
leaq 0x58(%rsp), %rdi
callq 0x33910
movq (%rsp), %rbx
movq 0x60(%rsp), %rdx
movl 0x98(%rbx), %edi
movq 0x58(%rsp), %rsi
movl $0x4040, %ecx           # imm = 0x4040
callq 0x30cc0
cmpb $0x0, 0xc(%rsp)
je 0x4712b
movq 0x90(%rbx), %r15
movq 0x10(%r15), %rax
cmpq (%r15), %rax
je 0x4712b
movl 0x98(%rbx), %ebx
movq 0x10(%rsp), %rsi
leaq 0x18(%rsp), %rdi
callq 0x33b20
movl %ebx, 0x38(%rsp)
movq 0x8(%r15), %rbp
cmpq 0x10(%r15), %rbp
je 0x47150
leaq 0x18(%rsp), %rsi
movq %rbp, %rdi
callq 0x479ba
addq $0x8, 0x8(%r15)
jmp 0x471d7
callq 0x32b70
movl 0x98(%rbx), %esi
movq 0x10(%rsp), %rdi
callq 0x31940
movl 0x98(%rbx), %edi
callq 0x33a20
jmp 0x471e1
leaq 0x37ff(%rip), %rdx      # 0x4a956
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x330d0
movq 0x8(%r15), %rcx
movq (%r15), %r13
movq %rax, %r14
movq %rcx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x31d70
movq %rbp, %r12
subq %r13, %r12
movq %rax, %rbx
addq %rax, %r12
leaq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x479ba
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x31430
movq (%rsp), %rsi
leaq 0x8(%rax), %rdx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x31430
movq %rax, %r12
testq %r13, %r13
je 0x471c8
movq %r13, %rdi
callq 0x31c00
leaq (%rbx,%r14,8), %rax
movq %rbx, (%r15)
movq %r12, 0x8(%r15)
movq %rax, 0x10(%r15)
leaq 0x18(%rsp), %rdi
callq 0x31390
leaq 0x58(%rsp), %rdi
callq 0x33b38
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x30a60
testq %rbx, %rbx
jne 0x47214
movq %r12, %rdi
callq 0x32e30
jmp 0x47227
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x33550
movq %rbx, %rdi
callq 0x31c00
callq 0x32b60
movq %rax, %rbx
callq 0x33100
jmp 0x47246
movq %rax, %rdi
callq 0x34aa2
jmp 0x472ed
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x31390
jmp 0x472f0
leaq 0x18(%rsp), %rdi
movq %rax, %rbx
callq 0x33b38
jmp 0x472f0
jmp 0x472ed
leaq 0x18(%rsp), %rdi
movq %rax, %rbx
callq 0x33b38
jmp 0x4727e
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x33b38
jmp 0x47290
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x33b38
jmp 0x4729f
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x472f5
jmp 0x472ed
movq %rax, %rbx
jmp 0x472e1
movq %rax, %rbx
jmp 0x472d4
movq %rax, %rbx
jmp 0x472ca
leaq 0x98(%rsp), %rdi
movq %rax, %rbx
callq 0x33b38
leaq 0x78(%rsp), %rdi
callq 0x33b38
leaq 0xb8(%rsp), %rdi
callq 0x33b38
leaq 0x18(%rsp), %rdi
callq 0x30390
jmp 0x472f0
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x33b38
movq %rbx, %rdi
callq 0x33510