// File: /workspace/github_c_makefile_star2_O2/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 $0x1e8, %rsp            # imm = 0x1E8
movq %r9, %rax
movq %rcx, %r13
movq %rdx, 0x80(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xe0(%rsp), %rcx
leaq 0x48(%rsp), %r14
movq %rcx, (%r14)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rcx
movaps %xmm0, (%rcx)
leaq 0xd0(%rsp), %r12
movaps %xmm0, (%r12)
leaq 0x90(%rsp), %rbp
movaps %xmm0, (%rbp)
andq $0x0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
leaq 0x88(%rsp), %rcx
movq %rcx, 0xb0(%rsp)
movl 0x38(%rsi), %r9d
subl %r13d, %r9d
movq 0x30(%rsi), %rcx
addq %r13, %rcx
movq %rcx, (%rsp)
leaq 0x791e(%rip), %rdx      # 0x14e29
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x71ab
movq 0x18(%r15), %rdi
movq (%r14), %rax
movq 0xc0(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbp, 0x40(%rsp)
movq %r12, 0x38(%rsp)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x28(%rsp)
leaq 0x4e7(%rip), %rsi       # 0xda50
leaq 0x78c2(%rip), %rdx      # 0x14e32
leaq 0x78c0(%rip), %r8       # 0x14e37
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0xd7a3
movq %rax, %r14
leaq 0x78b2(%rip), %rsi      # 0x14e3d
leaq 0x701c(%rip), %rdx      # 0x145ae
pushq $0x3
popq %rdi
callq 0x4227
testl %eax, %eax
je 0xd5c3
movl 0x38(%rbx), %esi
movq 0x30(%rbx), %rdx
movq 0x48(%rsp), %rcx
leaq 0x78a2(%rip), %rdi      # 0x14e53
movl %r13d, %r8d
movq 0x80(%rsp), %r9
xorl %eax, %eax
callq 0x430a
testq %r14, %r14
je 0xd737
movq %r14, %rdi
callq 0xb106
movq %r15, 0x48(%rax)
movq %r15, %rdi
callq 0xb106
movq %r14, 0x48(%rax)
movl 0x28(%rbx), %edx
movq 0x20(%rbx), %rcx
movl 0x78(%rsp), %r8d
movq 0x70(%rsp), %r9
leaq 0x7893(%rip), %rsi      # 0x14e8f
movq %r14, %rdi
xorl %eax, %eax
callq 0x7bce
movl 0x98(%rsp), %edx
movq 0x90(%rsp), %rcx
leaq 0x7888(%rip), %rsi      # 0x14ea4
movq %r14, %rdi
xorl %eax, %eax
callq 0x7bce
leaq 0x2f8(%rbx), %rbp
movq $-0x280, %r13           # imm = 0xFD80
leaq 0x7876(%rip), %r15      # 0x14eb1
leaq 0x60(%rsp), %r12
testq %r13, %r13
je 0xd714
cmpq $0x0, 0x8(%rbp,%r13)
je 0xd714
movq %rbp, %rax
addq %r13, %rax
movups (%rax), %xmm0
movaps %xmm0, 0x60(%rsp)
movups 0x280(%rbp,%r13), %xmm0
movaps %xmm0, 0x50(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x68d7
testl %eax, %eax
je 0xd6f4
movq %r12, %rdi
leaq 0x782c(%rip), %rsi      # 0x14eb6
callq 0x68d7
testl %eax, %eax
jne 0xd6a8
leaq 0x50(%rsp), %rdi
leaq 0x7371(%rip), %rsi      # 0x14a10
callq 0x68d7
testl %eax, %eax
je 0xd6fd
movq %r12, %rdi
leaq 0x782c(%rip), %rsi      # 0x14ede
callq 0x68d7
testl %eax, %eax
jne 0xd6d0
leaq 0x50(%rsp), %rdi
leaq 0x781e(%rip), %rsi      # 0x14ee5
callq 0x68d7
testl %eax, %eax
je 0xd6f4
movl 0x68(%rsp), %edx
movq 0x60(%rsp), %rcx
movl 0x58(%rsp), %r8d
movq 0x50(%rsp), %r9
movq %r14, %rdi
leaq 0x7805(%rip), %rsi      # 0x14ef2
xorl %eax, %eax
callq 0x7bce
addq $0x10, %r13
jmp 0xd640
movq 0x18(%rbx), %rdx
movq %r14, %rdi
leaq 0x77bd(%rip), %rsi      # 0x14ec8
xorl %eax, %eax
callq 0x7bce
jmp 0xd6f4
leaq 0x80b3(%rip), %rsi      # 0x157ce
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x7b89
movq 0x10(%rbx), %rsi
movl 0x18(%rbx), %edx
movq %r14, %rdi
callq 0x7b89
jmp 0xd77a
leaq 0x76ff(%rip), %rsi      # 0x14e3d
leaq 0x6e69(%rip), %rdx      # 0x145ae
xorl %edi, %edi
callq 0x4227
testl %eax, %eax
je 0xd76b
movq 0x48(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x7710(%rip), %rdi      # 0x14e74
xorl %eax, %eax
callq 0x430a
movq %r15, %rdi
movl $0x1f6, %esi            # imm = 0x1F6
xorl %edx, %edx
callq 0xc060
movq 0x48(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0xd791
callq 0x3060
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq