// File: /workspace/github_c_makefile_star2_O1/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)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movq $0x0, 0xc0(%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 0x7abb(%rip), %rdx      # 0x15e25
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x7627
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 0x532(%rip), %rsi       # 0xe8fa
leaq 0x7a5f(%rip), %rdx      # 0x15e2e
leaq 0x7a5d(%rip), %r8       # 0x15e33
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0xe65a
movq %rax, %r14
leaq 0x7a4f(%rip), %rsi      # 0x15e39
leaq 0x71b9(%rip), %rdx      # 0x155aa
movl $0x3, %edi
callq 0x44b4
testl %eax, %eax
je 0xe424
movl 0x38(%rbx), %esi
movq 0x30(%rbx), %rdx
movq 0x48(%rsp), %rcx
leaq 0x7a3d(%rip), %rdi      # 0x15e4f
movl %r13d, %r8d
movq 0x80(%rsp), %r9
xorl %eax, %eax
callq 0x45a0
testq %r14, %r14
je 0xe5ee
cmpq $0x0, 0x88(%r14)
jne 0xe45b
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%r14)
leaq 0x47cd(%rip), %rax      # 0x12c21
movq %rax, 0x90(%r14)
movq 0x88(%r14), %rax
movq %r15, 0x48(%rax)
cmpq $0x0, 0x88(%r15)
jne 0xe494
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%r15)
leaq 0x4794(%rip), %rax      # 0x12c21
movq %rax, 0x90(%r15)
movq 0x88(%r15), %rax
movq %r14, 0x48(%rax)
movl 0x28(%rbx), %edx
movq 0x20(%rbx), %rcx
movl 0x78(%rsp), %r8d
movq 0x70(%rsp), %r9
leaq 0x79d4(%rip), %rsi      # 0x15e8b
movq %r14, %rdi
xorl %eax, %eax
callq 0x80d3
movl 0x98(%rsp), %edx
movq 0x90(%rsp), %rcx
leaq 0x79c9(%rip), %rsi      # 0x15ea0
movq %r14, %rdi
xorl %eax, %eax
callq 0x80d3
leaq 0x2f8(%rbx), %rbp
movq $-0x280, %r13           # imm = 0xFD80
leaq 0x79b7(%rip), %r15      # 0x15ead
leaq 0x60(%rsp), %r12
cmpq $0x0, 0x8(%rbp,%r13)
je 0xe5c9
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 0x6c4a
testl %eax, %eax
je 0xe5a6
movq %r12, %rdi
leaq 0x7976(%rip), %rsi      # 0x15eb2
callq 0x6c4a
testl %eax, %eax
jne 0xe55a
leaq 0x50(%rsp), %rdi
leaq 0x74bb(%rip), %rsi      # 0x15a0c
callq 0x6c4a
testl %eax, %eax
je 0xe5b2
movq %r12, %rdi
leaq 0x7976(%rip), %rsi      # 0x15eda
callq 0x6c4a
testl %eax, %eax
jne 0xe582
leaq 0x50(%rsp), %rdi
leaq 0x7968(%rip), %rsi      # 0x15ee1
callq 0x6c4a
testl %eax, %eax
je 0xe5a6
movl 0x68(%rsp), %edx
movq 0x60(%rsp), %rcx
movl 0x58(%rsp), %r8d
movq 0x50(%rsp), %r9
movq %r14, %rdi
leaq 0x794f(%rip), %rsi      # 0x15eee
xorl %eax, %eax
callq 0x80d3
addq $0x10, %r13
jne 0xe4fb
jmp 0xe5c9
movq 0x18(%rbx), %rdx
movq %r14, %rdi
leaq 0x7904(%rip), %rsi      # 0x15ec4
xorl %eax, %eax
callq 0x80d3
jmp 0xe5a6
leaq 0x81fd(%rip), %rsi      # 0x167cd
movq %r14, %rdi
movl $0x2, %edx
callq 0x808e
movq 0x10(%rbx), %rsi
movl 0x18(%rbx), %edx
movq %r14, %rdi
callq 0x808e
jmp 0xe631
leaq 0x7844(%rip), %rsi      # 0x15e39
leaq 0x6fae(%rip), %rdx      # 0x155aa
xorl %edi, %edi
callq 0x44b4
testl %eax, %eax
je 0xe622
movq 0x48(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x7855(%rip), %rdi      # 0x15e70
xorl %eax, %eax
callq 0x45a0
movq %r15, %rdi
movl $0x1f6, %esi            # imm = 0x1F6
xorl %edx, %edx
callq 0xcd85
movq 0x48(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0xe648
callq 0x3060
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq