// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: mg_read
int mg_read(struct mg_connection *conn, void *buf, size_t len) {
  int n, buffered_len, nread;
  const char *buffered;

  assert((conn->content_len == -1 && conn->consumed_content == 0) ||
         conn->consumed_content <= conn->content_len);
  DEBUG_TRACE(("%p %zu %lld %lld", buf, len,
               conn->content_len, conn->consumed_content));
  nread = 0;
  if (conn->consumed_content < conn->content_len) {

    // Adjust number of bytes to read.
    int64_t to_read = conn->content_len - conn->consumed_content;
    if (to_read < (int64_t) len) {
      len = (int) to_read;
    }

    // How many bytes of data we have buffered in the request buffer?
    buffered = conn->buf + conn->request_len + conn->consumed_content;
    buffered_len = conn->data_len - conn->request_len;
    assert(buffered_len >= 0);

    // Return buffered data back if we haven't done that yet.
    if (conn->consumed_content < (int64_t) buffered_len) {
      buffered_len -= (int) conn->consumed_content;
      if (len < (size_t) buffered_len) {
        buffered_len = len;
      }
      memcpy(buf, buffered, (size_t)buffered_len);
      len -= buffered_len;
      buf = (char *) buf + buffered_len;
      conn->consumed_content += buffered_len;
      nread = buffered_len;
    }

    // We have returned all buffered data. Read new data from the remote socket.
    while (len > 0) {
      n = pull(NULL, conn->client.sock, conn->ssl, (char *) buf, (int) len);
      if (n <= 0) {
        break;
      }
      buf = (char *) buf + n;
      conn->consumed_content += n;
      nread += n;
      len -= n;
    }
  }
  return nread;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x30(%rsp), %rax
movq 0x4c0(%rax), %rax
movq 0x30(%rsp), %rcx
cmpq 0x4b8(%rcx), %rax
jge 0x16e1a
movq 0x30(%rsp), %rax
movq 0x4b8(%rax), %rax
movq 0x30(%rsp), %rcx
subq 0x4c0(%rcx), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
cmpq 0x20(%rsp), %rax
jge 0x16c9b
movq (%rsp), %rax
cltq
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movq 0x4c8(%rax), %rax
movq 0x30(%rsp), %rcx
movslq 0x4d4(%rcx), %rcx
addq %rcx, %rax
movq 0x30(%rsp), %rcx
addq 0x4c0(%rcx), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rax
movl 0x4d8(%rax), %eax
movq 0x30(%rsp), %rcx
subl 0x4d4(%rcx), %eax
movl %eax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movq 0x4c0(%rax), %rax
movslq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jge 0x16d83
movq 0x30(%rsp), %rax
movq 0x4c0(%rax), %rax
movl %eax, %ecx
movl 0x18(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jae 0x16d2b
movq 0x20(%rsp), %rax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movslq 0x18(%rsp), %rdx
callq 0x5450
movslq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rax
movslq 0x18(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movslq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rax
addq 0x4c0(%rax), %rcx
movq %rcx, 0x4c0(%rax)
movl 0x18(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x16d85
cmpq $0x0, 0x20(%rsp)
jbe 0x16e18
movq 0x30(%rsp), %rax
movl 0x470(%rax), %esi
movq 0x30(%rsp), %rax
movq 0x460(%rax), %rdx
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rax
movl %eax, %r8d
xorl %eax, %eax
movl %eax, %edi
callq 0x16e30
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jg 0x16dcb
jmp 0x16e18
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movslq 0x1c(%rsp), %rcx
movq 0x30(%rsp), %rax
addq 0x4c0(%rax), %rcx
movq %rcx, 0x4c0(%rax)
movl 0x1c(%rsp), %eax
addl 0x14(%rsp), %eax
movl %eax, 0x14(%rsp)
movslq 0x1c(%rsp), %rcx
movq 0x20(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x20(%rsp)
jmp 0x16d85
jmp 0x16e1a
movl 0x14(%rsp), %eax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)