// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_http_reverse_proxy
void mg_http_reverse_proxy(struct mg_connection *nc,
                           const struct http_message *hm, struct mg_str mount,
                           struct mg_str upstream) {
  struct mg_connection *be;
  char burl[256], *purl = burl;
  int i;
  const char *error;
  struct mg_connect_opts opts;
  struct mg_str path = MG_NULL_STR, user_info = MG_NULL_STR, host = MG_NULL_STR;
  memset(&opts, 0, sizeof(opts));
  opts.error_string = &error;

  mg_asprintf(&purl, sizeof(burl), "%.*s%.*s", (int) upstream.len, upstream.p,
              (int) (hm->uri.len - mount.len), hm->uri.p + mount.len);

  be = mg_connect_http_base(nc->mgr, MG_CB(mg_reverse_proxy_handler, NULL),
                            opts, "http", NULL, "https", NULL, purl, &path,
                            &user_info, &host);
  LOG(LL_DEBUG, ("Proxying %.*s to %s (rule: %.*s)", (int) hm->uri.len,
                 hm->uri.p, purl, (int) mount.len, mount.p));

  if (be == NULL) {
    LOG(LL_ERROR, ("Error connecting to %s: %s", purl, error));
    mg_http_send_error(nc, 502, NULL);
    goto cleanup;
  }

  /* link connections to each other, they must live and die together */
  mg_http_get_proto_data(be)->reverse_proxy_data.linked_conn = nc;
  mg_http_get_proto_data(nc)->reverse_proxy_data.linked_conn = be;

  /* send request upstream */
  mg_printf(be, "%.*s %.*s HTTP/1.1\r\n", (int) hm->method.len, hm->method.p,
            (int) path.len, path.p);

  mg_printf(be, "Host: %.*s\r\n", (int) host.len, host.p);
  for (i = 0; i < MG_MAX_HTTP_HEADERS && hm->header_names[i].len > 0; i++) {
    struct mg_str hn = hm->header_names[i];
    struct mg_str hv = hm->header_values[i];

    /* we rewrite the host header */
    if (mg_vcasecmp(&hn, "Host") == 0) continue;
    /*
     * Don't pass chunked transfer encoding to the client because hm->body is
     * already dechunked when we arrive here.
     */
    if (mg_vcasecmp(&hn, "Transfer-encoding") == 0 &&
        mg_vcasecmp(&hv, "chunked") == 0) {
      mg_printf(be, "Content-Length: %" SIZE_T_FMT "\r\n", hm->body.len);
      continue;
    }
    /* We don't support proxying Expect: 100-continue. */
    if (mg_vcasecmp(&hn, "Expect") == 0 &&
        mg_vcasecmp(&hv, "100-continue") == 0) {
      continue;
    }

    mg_printf(be, "%.*s: %.*s\r\n", (int) hn.len, hn.p, (int) hv.len, hv.p);
  }

  mg_send(be, "\r\n", 2);
  mg_send(be, hm->body.p, hm->body.len);

cleanup:
  if (purl != burl) MG_FREE(purl);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %r9, %rax
movq %rcx, %r13
movq %rdx, 0x50(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xf0(%rsp), %rcx
leaq 0x58(%rsp), %r14
movq %rcx, (%r14)
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rcx
movaps %xmm0, (%rcx)
leaq 0xe0(%rsp), %rbp
movaps %xmm0, (%rbp)
leaq 0xa0(%rsp), %r12
movaps %xmm0, (%r12)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movq $0x0, 0xd0(%rsp)
leaq 0x98(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movl 0x38(%rsi), %r9d
subl %r13d, %r9d
movq 0x30(%rsi), %rcx
addq %r13, %rcx
movq %rcx, (%rsp)
leaq 0x7c09(%rip), %rdx      # 0x15e46
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x7557
movq 0x18(%r15), %rdi
movq (%r14), %rax
movq 0xd0(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movapd 0xb0(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r12, 0x40(%rsp)
movq %rbp, 0x38(%rsp)
leaq 0x80(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x28(%rsp)
leaq 0x5a0(%rip), %rsi       # 0xe840
leaq 0x7ba8(%rip), %rdx      # 0x15e4f
leaq 0x7ba6(%rip), %r8       # 0x15e54
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0xe587
movq %rax, %r14
leaq 0x7b98(%rip), %rsi      # 0x15e5a
leaq 0x7305(%rip), %rdx      # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0xe2f9
movl 0x38(%rbx), %esi
movq 0x30(%rbx), %rdx
movq 0x58(%rsp), %rcx
leaq 0x7b86(%rip), %rdi      # 0x15e70
movl %r13d, %r8d
movq 0x50(%rsp), %r9
xorl %eax, %eax
callq 0x4539
testq %r14, %r14
je 0xe51b
movq 0x88(%r14), %rax
testq %rax, %rax
jne 0xe332
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%r14)
leaq 0x4dd3(%rip), %rcx      # 0x130fe
movq %rcx, 0x90(%r14)
movq %r15, 0x48(%rax)
movq 0x88(%r15), %rax
testq %rax, %rax
jne 0xe366
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%r15)
leaq 0x4d9f(%rip), %rcx      # 0x130fe
movq %rcx, 0x90(%r15)
movq %r14, 0x48(%rax)
movl 0x28(%rbx), %edx
movq 0x20(%rbx), %rcx
movl 0x88(%rsp), %r8d
movq 0x80(%rsp), %r9
leaq 0x7b24(%rip), %rsi      # 0x15eac
movq %r14, %rdi
xorl %eax, %eax
callq 0x7ff5
movl 0xa8(%rsp), %edx
movq 0xa0(%rsp), %rcx
leaq 0x7b19(%rip), %rsi      # 0x15ec1
movq %r14, %rdi
xorl %eax, %eax
callq 0x7ff5
movq %rbx, 0x50(%rsp)
leaq 0x2f8(%rbx), %rbp
movq $-0x280, %rbx           # imm = 0xFD80
leaq 0x7b02(%rip), %r15      # 0x15ece
leaq 0x70(%rsp), %r12
leaq 0x7afb(%rip), %r13      # 0x15ed3
cmpq $0x0, 0x8(%rbp,%rbx)
je 0xe4a6
leaq (%rbx,%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, 0x70(%rsp)
movupd 0x280(%rbp,%rbx), %xmm0
movapd %xmm0, 0x60(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x6b88
testl %eax, %eax
je 0xe47e
movq %r12, %rdi
movq %r13, %rsi
callq 0x6b88
testl %eax, %eax
jne 0xe432
leaq 0x60(%rsp), %rdi
leaq 0x7604(%rip), %rsi      # 0x15a2d
callq 0x6b88
testl %eax, %eax
je 0xe48a
movq %r12, %rdi
leaq 0x7abf(%rip), %rsi      # 0x15efb
callq 0x6b88
testl %eax, %eax
jne 0xe45a
leaq 0x60(%rsp), %rdi
leaq 0x7ab1(%rip), %rsi      # 0x15f02
callq 0x6b88
testl %eax, %eax
je 0xe47e
movl 0x78(%rsp), %edx
movq 0x70(%rsp), %rcx
movl 0x68(%rsp), %r8d
movq 0x60(%rsp), %r9
movq %r14, %rdi
leaq 0x7a98(%rip), %rsi      # 0x15f0f
xorl %eax, %eax
callq 0x7ff5
addq $0x10, %rbx
jne 0xe3d8
jmp 0xe4a6
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rdx
movq %r14, %rdi
leaq 0x7a48(%rip), %rsi      # 0x15ee5
xorl %eax, %eax
callq 0x7ff5
jmp 0xe47e
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
leaq 0x831b(%rip), %rsi      # 0x167eb
movl $0x2, %edx
movq %r14, %rdi
callq *0x48(%rcx,%rax,4)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %r15
movslq 0x18(%rax), %rbx
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq *0x48(%rcx,%rax,4)
jmp 0xe55e
leaq 0x7938(%rip), %rsi      # 0x15e5a
leaq 0x70a5(%rip), %rdx      # 0x155ce
xorl %edi, %edi
callq 0x4447
testl %eax, %eax
je 0xe54f
movq 0x58(%rsp), %rsi
movq 0x98(%rsp), %rdx
leaq 0x7949(%rip), %rdi      # 0x15e91
xorl %eax, %eax
callq 0x4539
movq %r15, %rdi
movl $0x1f6, %esi            # imm = 0x1F6
xorl %edx, %edx
callq 0xcb3c
movq 0x58(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0xe575
callq 0x3060
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq