// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdx, -0x10(%rbp)
movq %rcx, -0x8(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x18(%rbp)
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
leaq -0x140(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x190(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3260
leaq -0x1a0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3260
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3260
leaq -0x180(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x3260
leaq -0x158(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %r8
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
subq -0x8(%rbp), %rax
movl %eax, %r9d
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
addq -0x8(%rbp), %rax
leaq -0x148(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
leaq 0xbf82(%rip), %rdx      # 0x1fdc5
movq %rax, (%rsp)
movb $0x0, %al
callq 0xa0f0
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x148(%rbp), %rcx
xorl %eax, %eax
movq -0x160(%rbp), %rdx
movq %rsp, %rax
movq %rdx, 0x20(%rax)
movups -0x180(%rbp), %xmm0
movups -0x170(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x1b0(%rbp), %rdx
movq %rdx, 0x40(%rax)
leaq -0x1a0(%rbp), %rdx
movq %rdx, 0x38(%rax)
leaq -0x190(%rbp), %rdx
movq %rdx, 0x30(%rax)
movq %rcx, 0x28(%rax)
leaq 0x5e2(%rip), %rsi       # 0x14490
leaq 0xbf19(%rip), %rdx      # 0x1fdce
leaq 0xbf17(%rip), %r8       # 0x1fdd3
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %rcx
callq 0x141f0
movq %rax, -0x38(%rbp)
movl $0x3, %edi
leaq 0xbf00(%rip), %rsi      # 0x1fdd9
leaq 0xb66a(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x13f1c
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %esi
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x148(%rbp), %rcx
movq -0x8(%rbp), %rax
movl %eax, %r8d
movq -0x10(%rbp), %r9
leaq 0xbeda(%rip), %rdi      # 0x1fdef
movb $0x0, %al
callq 0x47a0
jmp 0x13f1e
cmpq $0x0, -0x38(%rbp)
jne 0x13f76
jmp 0x13f27
xorl %edi, %edi
leaq 0xbea9(%rip), %rsi      # 0x1fdd9
leaq 0xb613(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x13f5d
movq -0x148(%rbp), %rsi
movq -0x158(%rbp), %rdx
leaq 0xbeba(%rip), %rdi      # 0x1fe10
movb $0x0, %al
callq 0x47a0
jmp 0x13f5f
movq -0x28(%rbp), %rdi
movl $0x1f6, %esi            # imm = 0x1F6
xorl %eax, %eax
movl %eax, %edx
callq 0x11e30
jmp 0x141c3
movq -0x28(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x109b0
movq -0x1e0(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x109b0
movq -0x1d8(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x188(%rbp), %rax
movl %eax, %r8d
movq -0x190(%rbp), %r9
leaq 0xbe49(%rip), %rsi      # 0x1fe2b
movb $0x0, %al
callq 0xb2f0
movq -0x38(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movl %eax, %edx
movq -0x1b0(%rbp), %rcx
leaq 0xbe3c(%rip), %rsi      # 0x1fe40
movb $0x0, %al
callq 0xb2f0
movl $0x0, -0x14c(%rbp)
xorl %eax, %eax
cmpl $0x28, -0x14c(%rbp)
movb %al, -0x1e1(%rbp)
jge 0x1404a
movq -0x30(%rbp), %rax
addq $0x78, %rax
movslq -0x14c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
seta %al
movb %al, -0x1e1(%rbp)
movb -0x1e1(%rbp), %al
testb $0x1, %al
jne 0x14059
jmp 0x14193
movq -0x30(%rbp), %rax
addq $0x78, %rax
movslq -0x14c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x1c0(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x1b8(%rbp)
movq -0x30(%rbp), %rax
addq $0x2f8, %rax            # imm = 0x2F8
movslq -0x14c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x1d0(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x1c8(%rbp)
leaq -0x1c0(%rbp), %rdi
leaq 0xbd8e(%rip), %rsi      # 0x1fe4d
callq 0x8b60
cmpl $0x0, %eax
jne 0x140ce
jmp 0x1417f
leaq -0x1c0(%rbp), %rdi
leaq 0xbd76(%rip), %rsi      # 0x1fe52
callq 0x8b60
cmpl $0x0, %eax
jne 0x1411a
leaq -0x1d0(%rbp), %rdi
leaq 0xb8b8(%rip), %rsi      # 0x1f9ac
callq 0x8b60
cmpl $0x0, %eax
jne 0x1411a
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xbd53(%rip), %rsi      # 0x1fe64
movb $0x0, %al
callq 0xb2f0
jmp 0x1417f
leaq -0x1c0(%rbp), %rdi
leaq 0xbd52(%rip), %rsi      # 0x1fe7a
callq 0x8b60
cmpl $0x0, %eax
jne 0x1414c
leaq -0x1d0(%rbp), %rdi
leaq 0xbd41(%rip), %rsi      # 0x1fe81
callq 0x8b60
cmpl $0x0, %eax
jne 0x1414c
jmp 0x1417f
movq -0x38(%rbp), %rdi
movq -0x1b8(%rbp), %rax
movl %eax, %edx
movq -0x1c0(%rbp), %rcx
movq -0x1c8(%rbp), %rax
movl %eax, %r8d
movq -0x1d0(%rbp), %r9
leaq 0xbd16(%rip), %rsi      # 0x1fe8e
movb $0x0, %al
callq 0xb2f0
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x14015
movq -0x38(%rbp), %rdi
leaq 0xc60b(%rip), %rsi      # 0x207a9
movl $0x2, %edx
callq 0xb260
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
callq 0xb260
movq -0x148(%rbp), %rax
leaq -0x140(%rbp), %rcx
cmpq %rcx, %rax
je 0x141e2
movq -0x148(%rbp), %rdi
callq 0x3050
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
nopl (%rax,%rax)