// File: /workspace/github_c_makefile_star2_O0/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_prepare_cgi_environment
static void mg_prepare_cgi_environment(struct mg_connection *nc,
                                       const char *prog,
                                       const struct mg_str *path_info,
                                       const struct http_message *hm,
                                       const struct mg_serve_http_opts *opts,
                                       struct mg_cgi_env_block *blk) {
  const char *s;
  struct mg_str *h;
  char *p;
  size_t i;
  char buf[100];

  blk->len = blk->nvars = 0;
  blk->nc = nc;

  if ((s = getenv("SERVER_NAME")) != NULL) {
    mg_addenv(blk, "SERVER_NAME=%s", s);
  } else {
    mg_sock_to_str(nc->sock, buf, sizeof(buf), 3);
    mg_addenv(blk, "SERVER_NAME=%s", buf);
  }
  mg_addenv(blk, "SERVER_ROOT=%s", opts->document_root);
  mg_addenv(blk, "DOCUMENT_ROOT=%s", opts->document_root);
  mg_addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MG_VERSION);

  /* Prepare the environment block */
  mg_addenv(blk, "%s", "GATEWAY_INTERFACE=CGI/1.1");
  mg_addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1");
  mg_addenv(blk, "%s", "REDIRECT_STATUS=200"); /* For PHP */

  mg_addenv(blk, "REQUEST_METHOD=%.*s", (int) hm->method.len, hm->method.p);

  mg_addenv(blk, "REQUEST_URI=%.*s%s%.*s", (int) hm->uri.len, hm->uri.p,
            hm->query_string.len == 0 ? "" : "?", (int) hm->query_string.len,
            hm->query_string.p);

  mg_conn_addr_to_str(nc, buf, sizeof(buf),
                      MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP);
  mg_addenv(blk, "REMOTE_ADDR=%s", buf);
  mg_conn_addr_to_str(nc, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT);
  mg_addenv(blk, "SERVER_PORT=%s", buf);

  s = hm->uri.p + hm->uri.len - path_info->len - 1;
  if (*s == '/') {
    const char *base_name = strrchr(prog, DIRSEP);
    mg_addenv(blk, "SCRIPT_NAME=%.*s/%s", (int) (s - hm->uri.p), hm->uri.p,
              (base_name != NULL ? base_name + 1 : prog));
  } else {
    mg_addenv(blk, "SCRIPT_NAME=%.*s", (int) (s - hm->uri.p + 1), hm->uri.p);
  }
  mg_addenv(blk, "SCRIPT_FILENAME=%s", prog);

  if (path_info != NULL && path_info->len > 0) {
    mg_addenv(blk, "PATH_INFO=%.*s", (int) path_info->len, path_info->p);
    /* Not really translated... */
    mg_addenv(blk, "PATH_TRANSLATED=%.*s", (int) path_info->len, path_info->p);
  }

#if MG_ENABLE_SSL
  mg_addenv(blk, "HTTPS=%s", (nc->flags & MG_F_SSL ? "on" : "off"));
#else
  mg_addenv(blk, "HTTPS=off");
#endif

  if ((h = mg_get_http_header((struct http_message *) hm, "Content-Type")) !=
      NULL) {
    mg_addenv(blk, "CONTENT_TYPE=%.*s", (int) h->len, h->p);
  }

  if (hm->query_string.len > 0) {
    mg_addenv(blk, "QUERY_STRING=%.*s", (int) hm->query_string.len,
              hm->query_string.p);
  }

  if ((h = mg_get_http_header((struct http_message *) hm, "Content-Length")) !=
      NULL) {
    mg_addenv(blk, "CONTENT_LENGTH=%.*s", (int) h->len, h->p);
  }

  mg_addenv2(blk, "PATH");
  mg_addenv2(blk, "TMP");
  mg_addenv2(blk, "TEMP");
  mg_addenv2(blk, "TMPDIR");
  mg_addenv2(blk, "PERLLIB");
  mg_addenv2(blk, MG_ENV_EXPORT_TO_CGI);

#ifdef _WIN32
  mg_addenv2(blk, "COMSPEC");
  mg_addenv2(blk, "SYSTEMROOT");
  mg_addenv2(blk, "SystemDrive");
  mg_addenv2(blk, "ProgramFiles");
  mg_addenv2(blk, "ProgramFiles(x86)");
  mg_addenv2(blk, "CommonProgramFiles(x86)");
#else
  mg_addenv2(blk, "LD_LIBRARY_PATH");
#endif /* _WIN32 */

  /* Add all headers as HTTP_* variables */
  for (i = 0; hm->header_names[i].len > 0; i++) {
    p = mg_addenv(blk, "HTTP_%.*s=%.*s", (int) hm->header_names[i].len,
                  hm->header_names[i].p, (int) hm->header_values[i].len,
                  hm->header_values[i].p);

    /* Convert variable name into uppercase, and change - to _ */
    for (; *p != '=' && *p != '\0'; p++) {
      if (*p == '-') *p = '_';
      *p = (char) toupper(*(unsigned char *) p);
    }
  }

  blk->vars[blk->nvars++] = NULL;
  blk->buf[blk->len++] = '\0';
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl $0x0, 0x220c(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x2208(%rax)
movq -0x8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
leaq 0x5dcd(%rip), %rdi      # 0x207ce
callq 0x3040
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x1aa28
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x5dbb(%rip), %rsi      # 0x207da
movb $0x0, %al
callq 0x1b410
jmp 0x1aa5e
movq -0x8(%rbp), %rax
movl 0x20(%rax), %edi
leaq -0xc0(%rbp), %rsi
movl $0x64, %edx
movl $0x3, %ecx
callq 0xee10
movq -0x30(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq 0x5d83(%rip), %rsi      # 0x207da
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x5d79(%rip), %rsi      # 0x207e9
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x5d6f(%rip), %rsi      # 0x207f8
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
leaq 0x5d6e(%rip), %rsi      # 0x20809
leaq 0x5d7d(%rip), %rdx      # 0x2081f
leaq 0x5a49(%rip), %rcx      # 0x204f2
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
leaq 0x54d7(%rip), %rsi      # 0x1ff92
leaq 0x5d66(%rip), %rdx      # 0x20828
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
leaq 0x54be(%rip), %rsi      # 0x1ff92
leaq 0x5d67(%rip), %rdx      # 0x20842
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
leaq 0x54a5(%rip), %rsi      # 0x1ff92
leaq 0x5d67(%rip), %rdx      # 0x2085b
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
leaq 0x5d57(%rip), %rsi      # 0x2086f
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rsi
leaq 0x4e4b(%rip), %r8       # 0x1f98f
leaq 0x5c60(%rip), %rax      # 0x207ab
cmpq $0x0, %rsi
cmoveq %rax, %r8
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movl %eax, %r9d
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
leaq 0x5d16(%rip), %rsi      # 0x20883
movq %rax, (%rsp)
movb $0x0, %al
callq 0x1b410
movq -0x8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movl $0x64, %edx
movl $0x5, %ecx
callq 0x16a80
movq -0x30(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq 0x5cf6(%rip), %rsi      # 0x2089a
movb $0x0, %al
callq 0x1b410
movq -0x8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movl $0x64, %edx
movl $0x2, %ecx
callq 0x16a80
movq -0x30(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq 0x5cd2(%rip), %rsi      # 0x208a9
movb $0x0, %al
callq 0x1b410
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
addq 0x38(%rcx), %rax
movq -0x18(%rbp), %rdx
xorl %ecx, %ecx
subq 0x8(%rdx), %rcx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x1acab
movq -0x10(%rbp), %rdi
movl $0x2f, %esi
callq 0x3200
movq %rax, -0xc8(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x30(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0xd4(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0x1ac75
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1ac80
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xd0(%rbp), %rcx
movl -0xd4(%rbp), %edx
movq -0xe0(%rbp), %rdi
movq -0xe8(%rbp), %r8
leaq 0x5c16(%rip), %rsi      # 0x208b8
movb $0x0, %al
callq 0x1b410
jmp 0x1acda
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x30(%rcx), %rcx
subq %rcx, %rax
addq $0x1, %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
leaq 0x5bf9(%rip), %rsi      # 0x208cc
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x5bf4(%rip), %rsi      # 0x208dd
movb $0x0, %al
callq 0x1b410
cmpq $0x0, -0x18(%rbp)
je 0x1ad48
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x1ad48
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
leaq 0x5bd2(%rip), %rsi      # 0x208f0
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
leaq 0x5bbe(%rip), %rsi      # 0x208ff
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
leaq 0x5bc1(%rip), %rsi      # 0x20914
movb $0x0, %al
callq 0x1b410
movq -0x20(%rbp), %rdi
leaq 0x5bb9(%rip), %rsi      # 0x2091e
callq 0x10830
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x1ad97
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
leaq 0x5b9b(%rip), %rsi      # 0x2092b
movb $0x0, %al
callq 0x1b410
movq -0x20(%rbp), %rax
cmpq $0x0, 0x70(%rax)
jbe 0x1adc6
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
leaq 0x5b7e(%rip), %rsi      # 0x2093d
movb $0x0, %al
callq 0x1b410
movq -0x20(%rbp), %rdi
leaq 0x5653(%rip), %rsi      # 0x20424
callq 0x10830
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x1ae03
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
leaq 0x5b53(%rip), %rsi      # 0x2094f
movb $0x0, %al
callq 0x1b410
movq -0x30(%rbp), %rdi
leaq 0x5b85(%rip), %rsi      # 0x20993
callq 0x1b560
movq -0x30(%rbp), %rdi
leaq 0x5b45(%rip), %rsi      # 0x20963
callq 0x1b560
movq -0x30(%rbp), %rdi
leaq 0x5b39(%rip), %rsi      # 0x20967
callq 0x1b560
movq -0x30(%rbp), %rdi
leaq 0x5b2e(%rip), %rsi      # 0x2096c
callq 0x1b560
movq -0x30(%rbp), %rdi
leaq 0x5b25(%rip), %rsi      # 0x20973
callq 0x1b560
movq -0x30(%rbp), %rdi
leaq 0x5b1d(%rip), %rsi      # 0x2097b
callq 0x1b560
movq -0x30(%rbp), %rdi
leaq 0x5b1a(%rip), %rsi      # 0x20988
callq 0x1b560
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
addq $0x78, %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jbe 0x1af8d
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
addq $0x78, %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
addq $0x78, %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x20(%rbp), %rax
addq $0x2f8, %rax            # imm = 0x2F8
movq -0x50(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rax
movq 0x8(%rax), %rax
movl %eax, %r8d
movq -0x20(%rbp), %rax
addq $0x2f8, %rax            # imm = 0x2F8
movq -0x50(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rax
movq (%rax), %r9
leaq 0x5a91(%rip), %rsi      # 0x20998
movb $0x0, %al
callq 0x1b410
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x3d, %ecx
movb %al, -0xe9(%rbp)
je 0x1af39
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x1af45
jmp 0x1af7a
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x1af58
movq -0x48(%rbp), %rax
movb $0x5f, (%rax)
movq -0x48(%rbp), %rax
movzbl (%rax), %edi
callq 0x30e0
movb %al, %cl
movq -0x48(%rbp), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1af12
jmp 0x1af7c
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1ae7b
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rdx
movl 0x220c(%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x220c(%rdx)
movslq %ecx, %rcx
movq $0x0, 0x2008(%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rdx
movl 0x2208(%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x2208(%rdx)
movslq %ecx, %rcx
movb $0x0, 0x8(%rax,%rcx)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nop