// File: /workspace/github_cpp_makefile_star2_O2/tylerkaraszewski[P]Argyle/HttpRequest.cpp
// Function: HttpRequest::parse(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
size_t HttpRequest::parse(const string& buffer)
{
  size_t headerEnd = buffer.find("\r\n\r\n");
  if (headerEnd == buffer.npos)
  {
    return 0;
  }

  // Ok, now we parse this.
  size_t startOffset = 0;
  size_t endOffset = 0;

  // Special handling for the first line.
  endOffset = buffer.find_first_of(SPACES);
  if (endOffset == buffer.npos)
  {
    m_httpError = 400;
    return headerEnd + 4;
  }
  m_method = buffer.substr(startOffset, endOffset);
  startOffset = buffer.find_first_not_of(SPACES, endOffset);
  if (startOffset == buffer.npos)
  {
    m_httpError = 400;
    return headerEnd + 4;
  }
  endOffset = buffer.find_first_of(SPACES, startOffset);
  if (endOffset == buffer.npos)
  {
    m_httpError = 400;
    return headerEnd + 4;
  }
  m_path = buffer.substr(startOffset, endOffset - startOffset);
  startOffset = buffer.find_first_not_of(SPACES, endOffset);
  if (startOffset == buffer.npos)
  {
    m_httpError = 400;
    return headerEnd + 4;
  }
  endOffset = buffer.find_first_of(NEWLINES, startOffset);
  if (endOffset == buffer.npos)
  {
    m_httpError = 400;
    return headerEnd + 4;
  }
  m_httpVersion = buffer.substr(startOffset, endOffset - startOffset);

  size_t pathOffset = m_path.find_first_of("#");
  if (pathOffset != m_path.npos)
  {
    m_path = m_path.substr(0, pathOffset);
  }
  pathOffset = m_path.find_first_of("?");
  if (pathOffset != m_path.npos)
  {
    m_queryString = m_path.substr(pathOffset);
    m_path = m_path.substr(0, pathOffset);
  }

  // Now we loop across all the headers we've got.
  startOffset = buffer.find_first_not_of(NEWLINES, endOffset);
  while (endOffset < headerEnd)
  {
    endOffset = buffer.find_first_of(SEPARATOR, startOffset);
    if (endOffset == buffer.npos)
    {
      break;
    }
    string headerName = buffer.substr(startOffset, endOffset - startOffset);
    headerName = headerName.substr(0, headerName.find_last_not_of(SPACES) + 1);
    startOffset = buffer.find_first_not_of(SPACES, endOffset + 1);
    if (startOffset == buffer.npos)
    {
      break;
    }
    endOffset = buffer.find_first_of(NEWLINES, startOffset);
    if (endOffset == buffer.npos)
    {
      break;
    }
    string value = buffer.substr(startOffset, endOffset - startOffset);
    startOffset = buffer.find_first_not_of(NEWLINES, endOffset);
    while ((buffer.find_first_of(SPACES) == startOffset) && (startOffset < headerEnd))
    {
      endOffset = buffer.find_first_of(NEWLINES, startOffset);
      if (endOffset == buffer.npos)
      {
        break;
      }
      value += buffer.substr(startOffset, endOffset - startOffset);
      startOffset = buffer.find_first_not_of(NEWLINES, endOffset);
      if (startOffset == buffer.npos)
      {
        break;
      }
    }

    toLower(headerName);
    if (headerName == "user-agent")
    {
      m_userAgent = value;
    }
    else if (headerName == "if-modified-since")
    {
      m_ifModifiedSince = Utils::parseHttpDate(value);
    }
    m_headers.insert(pair<string, string>(headerName, value));
  }

  // Set our keep-alive flag.
  HeaderMap::const_iterator it = m_headers.find("connection");
  {
    string c = it->second;
    toLower(c);
    if (c.find("keep-alive") != c.npos)
    {
      m_keepAlive = true;
    }
  }

  // Now we need to parse the request body.
  it = m_headers.find("content-length");
  if (it != m_headers.end())
  {
    const string& l = it->second;
    m_contentLength = strtoll(l.c_str(), NULL, 10);
  }

  // '4' is the \r\n\r\n following the headers.
  unsigned long long totalSize = headerEnd + 4 + m_contentLength;
  if (buffer.size() >= totalSize)
  {
    m_body = buffer.substr(headerEnd + 4, m_contentLength);
  }
  else
  {
    totalSize = 0;
  }

  return totalSize;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r15
movq %rdi, %rbp
leaq 0x39de(%rip), %rsi      # 0x9234
xorl %ebx, %ebx
movq %r15, %rdi
xorl %edx, %edx
callq 0x35c0
cmpq $-0x1, %rax
je 0x5d92
movq %rax, %r14
leaq 0x8aaa(%rip), %rsi      # 0xe320
movq %r15, %rdi
xorl %edx, %edx
callq 0x3280
cmpq $-0x1, %rax
je 0x5d81
movq %rax, %rbx
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x32e0
leaq 0x10(%rbp), %rdi
movq %r12, %rsi
callq 0x3380
movq %r12, %rdi
callq 0x35d8
leaq 0x8a63(%rip), %rsi      # 0xe320
movq %r15, %rdi
movq %rbx, %rdx
callq 0x3240
cmpq $-0x1, %rax
je 0x5d81
movq %rax, %rbx
leaq 0x8a44(%rip), %rsi      # 0xe320
movq %r15, %rdi
movq %rax, %rdx
callq 0x3280
cmpq $-0x1, %rax
je 0x5d81
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %rax, %rcx
subq %rbx, %rcx
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x32e0
leaq 0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3380
movq %r13, %rdi
callq 0x35d8
leaq 0x89f0(%rip), %rsi      # 0xe320
movq %r15, %rdi
movq %r12, %rdx
callq 0x3240
cmpq $-0x1, %rax
je 0x5da7
movq %rax, %r13
leaq 0x89f1(%rip), %rsi      # 0xe340
movq %r15, %rdi
movq %rax, %rdx
callq 0x3280
cmpq $-0x1, %rax
je 0x5da7
movq %rax, %r12
movq %rax, %rcx
subq %r13, %rcx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x32e0
movq %rbp, 0x10(%rsp)
leaq 0x50(%rbp), %rdi
movq %r14, %rsi
callq 0x3380
movq %r14, %rdi
callq 0x35d8
leaq 0x3899(%rip), %rsi      # 0x9239
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3180
cmpq $-0x1, %rax
je 0x59d8
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x32e0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3380
movq %r13, %rdi
callq 0x35d8
leaq 0x385c(%rip), %rsi      # 0x923b
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3180
cmpq $-0x1, %rax
movq 0x10(%rsp), %r14
je 0x5a4c
movq %rax, %r13
leaq 0x20(%rsp), %rbp
pushq $-0x1
popq %rcx
movq %rbp, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x32e0
leaq 0xc0(%r14), %rdi
movq %rbp, %rsi
callq 0x3380
movq %rbp, %rdi
callq 0x35d8
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0x32e0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3380
movq %rbp, %rdi
callq 0x35d8
leaq 0x88ed(%rip), %r13      # 0xe340
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3240
movq %rax, %rbp
leaq 0xe8(%r14), %rax
movq %rax, 0x60(%rsp)
leaq 0x70(%r14), %rax
movq %rax, 0x18(%rsp)
cmpq 0x8(%rsp), %r12
jae 0x5cb3
movq %r15, %rdi
leaq 0x88d2(%rip), %rsi      # 0xe360
movq %rbp, %rdx
callq 0x3280
cmpq $-0x1, %rax
je 0x5cb3
movq %rax, %r12
movq %rax, %rcx
subq %rbp, %rcx
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x32e0
movq %r14, %rdi
leaq 0x8857(%rip), %rbp      # 0xe320
movq %rbp, %rsi
pushq $-0x1
popq %rdx
callq 0x3540
leaq 0x1(%rax), %rcx
movq %r15, %rbx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x32e0
movq %r14, %rdi
movq %r15, %rsi
callq 0x3380
movq %r15, %rdi
callq 0x35d8
leaq 0x1(%r12), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3240
cmpq $-0x1, %rax
movq %rbx, %r15
je 0x5be7
movq %rax, %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x3280
cmpq $-0x1, %rax
je 0x5bf2
movq %rax, %r12
movq %rax, %rcx
subq %rbp, %rcx
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x32e0
leaq 0x87c8(%rip), %rbx      # 0xe320
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3240
movq %rax, %rbp
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3280
cmpq 0x8(%rsp), %rbp
jae 0x5c02
cmpq %rbp, %rax
jne 0x5c02
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3280
cmpq $-0x1, %rax
je 0x5bfe
movq %rax, %r12
movq %rax, %rcx
subq %rbp, %rcx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x32e0
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x3590
movq %r14, %rdi
callq 0x35d8
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3240
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x5b69
pushq $-0x1
popq %rbp
jmp 0x5c02
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
jmp 0x5c96
xorl %r13d, %r13d
pushq $-0x1
popq %r12
jmp 0x5c96
pushq $-0x1
popq %r12
leaq 0x88(%rsp), %r13
movq %r13, %rdi
callq 0x5e68
movq %r13, %rdi
leaq 0x3621(%rip), %rsi      # 0x923d
callq 0x5f4f
testb %al, %al
je 0x5c36
movq 0x60(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x31a0
jmp 0x5c5f
movq %r13, %rdi
leaq 0x352d(%rip), %rsi      # 0x916d
callq 0x5f4f
testb %al, %al
je 0x5c5f
leaq 0x68(%rsp), %rdi
callq 0x823a
movq 0x10(%rsp), %rcx
movq %rax, 0x108(%rcx)
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x68(%rsp), %rdx
callq 0x5f5c
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x5fa8
movq %rbx, %rdi
callq 0x5f92
leaq 0x68(%rsp), %rdi
callq 0x35d8
movb $0x1, %r13b
leaq 0x88(%rsp), %rdi
callq 0x35d8
testb %r13b, %r13b
leaq 0x8693(%rip), %r13      # 0xe340
jne 0x5a79
leaq 0x358e(%rip), %rsi      # 0x9248
leaq 0x20(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x438c
leaq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x614a
movq 0x10(%rsp), %r13
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35d8
addq $0x40, %rbx
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x33d0
movq %r12, %rdi
callq 0x5e68
leaq 0x3543(%rip), %rsi      # 0x9253
movq %r12, %rdi
xorl %edx, %edx
callq 0x35c0
cmpq $-0x1, %rax
je 0x5d25
movb $0x1, 0x8(%r13)
leaq 0x20(%rsp), %rdi
callq 0x35d8
leaq 0x3528(%rip), %rsi      # 0x925e
leaq 0x20(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x438c
leaq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x614a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35d8
leaq 0x78(%r13), %rax
cmpq %rax, %rbx
je 0x5dbc
movq 0x40(%rbx), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x3080
movq %rax, (%r13)
jmp 0x5dc0
movl $0x190, 0xe0(%rbp)      # imm = 0x190
addq $0x4, %r14
movq %r14, %rbx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x190, 0xe0(%rbp)      # imm = 0x190
movq 0x8(%rsp), %rbx
addq $0x4, %rbx
jmp 0x5d92
movq (%r13), %rax
movq 0x8(%rsp), %rdx
leaq (%rax,%rdx), %rbx
addq $0x4, %rbx
cmpq %rbx, 0x8(%r15)
jae 0x5dd7
xorl %ebx, %ebx
jmp 0x5d92
addq $0x4, %rdx
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rcx
callq 0x32e0
addq $0xa0, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3380
movq %r12, %rdi
callq 0x35d8
jmp 0x5d92
jmp 0x5e0e
jmp 0x5e18
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x5e5b
movq %rax, %rbx
jmp 0x5e60
jmp 0x5e46
jmp 0x5e32
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5f92
jmp 0x5e49
jmp 0x5e46
movq %rax, %rbx
jmp 0x5e53
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35d8
jmp 0x5e49
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x35d8
leaq 0x88(%rsp), %rdi
callq 0x35d8
movq %rbx, %rdi
callq 0x3510