// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: prepare_cgi_environment
static void prepare_cgi_environment(struct connection *conn,
                                    const char *prog,
                                    struct cgi_env_block *blk) {
  struct mg_connection *ri = &conn->mg_conn;
  const char *s, *slash;
  char *p, **opts = conn->server->config_options;
  int  i;

  blk->len = blk->nvars = 0;
  blk->conn = ri;

  if ((s = getenv("SERVER_NAME")) != NULL) {
    addenv(blk, "SERVER_NAME=%s", s);
  } else {
    addenv(blk, "SERVER_NAME=%s", ri->local_ip);
  }
  addenv(blk, "SERVER_ROOT=%s", opts[DOCUMENT_ROOT]);
  addenv(blk, "DOCUMENT_ROOT=%s", opts[DOCUMENT_ROOT]);
  addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MONGOOSE_VERSION);

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

  // TODO(lsm): fix this for IPv6 case
  //addenv(blk, "SERVER_PORT=%d", ri->remote_port);

  addenv(blk, "REQUEST_METHOD=%s", ri->request_method);
  addenv(blk, "REMOTE_ADDR=%s", ri->remote_ip);
  addenv(blk, "REMOTE_PORT=%d", ri->remote_port);
  addenv(blk, "REQUEST_URI=%s%s%s", ri->uri,
         ri->query_string == NULL ? "" : "?",
         ri->query_string == NULL ? "" : ri->query_string);

  // SCRIPT_NAME
  if (conn->path_info != NULL) {
    addenv(blk, "SCRIPT_NAME=%.*s",
           (int) (strlen(ri->uri) - strlen(conn->path_info)), ri->uri);
    addenv(blk, "PATH_INFO=%s", conn->path_info);
  } else {
    s = strrchr(prog, '/');
    slash = strrchr(ri->uri, '/');
    addenv(blk, "SCRIPT_NAME=%.*s%s",
           slash == NULL ? 0 : (int) (slash - ri->uri), ri->uri,
           s == NULL ? prog : s);
  }

  addenv(blk, "SCRIPT_FILENAME=%s", prog);
  addenv(blk, "PATH_TRANSLATED=%s", prog);
  addenv(blk, "HTTPS=%s", conn->ns_conn->ssl != NULL ? "on" : "off");

  if ((s = mg_get_header(ri, "Content-Type")) != NULL)
    addenv(blk, "CONTENT_TYPE=%s", s);

  if (ri->query_string != NULL)
    addenv(blk, "QUERY_STRING=%s", ri->query_string);

  if ((s = mg_get_header(ri, "Content-Length")) != NULL)
    addenv(blk, "CONTENT_LENGTH=%s", s);

  addenv2(blk, "PATH");
  addenv2(blk, "TMP");
  addenv2(blk, "TEMP");
  addenv2(blk, "TMPDIR");
  addenv2(blk, "PERLLIB");
  addenv2(blk, ENV_EXPORT_TO_CGI);

#if defined(_WIN32)
  addenv2(blk, "COMSPEC");
  addenv2(blk, "SYSTEMROOT");
  addenv2(blk, "SystemDrive");
  addenv2(blk, "ProgramFiles");
  addenv2(blk, "ProgramFiles(x86)");
  addenv2(blk, "CommonProgramFiles(x86)");
#else
  addenv2(blk, "LD_LIBRARY_PATH");
#endif // _WIN32

  // Add all headers as HTTP_* variables
  for (i = 0; i < ri->num_headers; i++) {
    p = addenv(blk, "HTTP_%s=%s",
        ri->http_headers[i].name, ri->http_headers[i].value);

    // 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';

  assert(blk->nvars < (int) ARRAY_SIZE(blk->vars));
  assert(blk->len > 0);
  assert(blk->len < (int) sizeof(blk->buf));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x2a8(%rax), %rax
addq $0x38, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movl $0x0, 0x220c(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x2208(%rax)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
leaq 0xfd22(%rip), %rdi      # 0xc7776
callq 0xd9a0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0xb7a7b
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0xfd10(%rip), %rsi      # 0xc7782
movb $0x0, %al
callq 0xb81e0
jmp 0xb7a95
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
addq $0x50, %rdx
leaq 0xfcf4(%rip), %rsi      # 0xc7782
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0xfce9(%rip), %rsi      # 0xc7791
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0xfcde(%rip), %rsi      # 0xc77a0
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
leaq 0xfcdd(%rip), %rsi      # 0xc77b1
leaq 0xfcec(%rip), %rdx      # 0xc77c7
leaq 0xfcee(%rip), %rcx      # 0xc77d0
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
leaq 0xcd3c(%rip), %rsi      # 0xc4830
leaq 0xfcd9(%rip), %rdx      # 0xc77d4
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
leaq 0xcd23(%rip), %rsi      # 0xc4830
leaq 0xfcda(%rip), %rdx      # 0xc77ee
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
leaq 0xcd0a(%rip), %rsi      # 0xc4830
leaq 0xfcda(%rip), %rdx      # 0xc7807
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
leaq 0xfcd5(%rip), %rsi      # 0xc781b
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
addq $0x20, %rdx
leaq 0xfccd(%rip), %rsi      # 0xc782d
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movzwl 0x80(%rax), %edx
leaq 0xfcbf(%rip), %rsi      # 0xc783c
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xc010(%rip), %rax      # 0xc3bb7
leaq 0xdf9d(%rip), %rcx      # 0xc5b4b
cmpq $0x0, %rdx
cmoveq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xb7bd2
leaq 0xdf7f(%rip), %rax      # 0xc5b4b
movq %rax, -0x68(%rbp)
jmp 0xb7bde
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %r8
leaq 0xfc56(%rip), %rsi      # 0xc784b
movb $0x0, %al
callq 0xb81e0
movq -0x8(%rbp), %rax
cmpq $0x0, 0x2c0(%rax)
je 0xb7c7b
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xd550
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x2c0(%rax), %rdi
callq 0xd550
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movq -0x70(%rbp), %rax
subq %rcx, %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0xfc0c(%rip), %rsi      # 0xc785e
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x2c0(%rax), %rdx
leaq 0xfc00(%rip), %rsi      # 0xc786f
movb $0x0, %al
callq 0xb81e0
jmp 0xb7d31
movq -0x10(%rbp), %rdi
movl $0x2f, %esi
callq 0xdbe0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2f, %esi
callq 0xdbe0
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xb7cbc
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0xb7cd1
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xb7d00
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0xb7d0b
movq -0x28(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rcx
movl -0x94(%rbp), %edx
movq -0x80(%rbp), %rdi
movq -0xa0(%rbp), %r8
leaq 0xfb52(%rip), %rsi      # 0xc787c
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0xfb4f(%rip), %rsi      # 0xc788f
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0xfb4c(%rip), %rsi      # 0xc78a2
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x70(%rax), %rcx
leaq 0x7e8e(%rip), %rdx      # 0xbfc01
leaq 0xf481(%rip), %rax      # 0xc71fb
cmpq $0x0, %rcx
cmovneq %rax, %rdx
leaq 0xfb2c(%rip), %rsi      # 0xc78b5
movb $0x0, %al
callq 0xb81e0
movq -0x20(%rbp), %rdi
leaq 0x7b55(%rip), %rsi      # 0xbf8f0
callq 0xb0fe0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0xb7dc0
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0xfb05(%rip), %rsi      # 0xc78be
movb $0x0, %al
callq 0xb81e0
movq -0x20(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xb7de5
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xfaf0(%rip), %rsi      # 0xc78ce
movb $0x0, %al
callq 0xb81e0
movq -0x20(%rbp), %rdi
leaq 0xb045(%rip), %rsi      # 0xc2e35
callq 0xb0fe0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0xb7e15
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0xfad0(%rip), %rsi      # 0xc78de
movb $0x0, %al
callq 0xb81e0
movq -0x18(%rbp), %rdi
leaq 0xcf75(%rip), %rsi      # 0xc4d95
callq 0xb8350
movq -0x18(%rbp), %rdi
leaq 0xfac0(%rip), %rsi      # 0xc78f0
callq 0xb8350
movq -0x18(%rbp), %rdi
leaq 0xfab4(%rip), %rsi      # 0xc78f4
callq 0xb8350
movq -0x18(%rbp), %rdi
leaq 0xe00e(%rip), %rsi      # 0xc5e5e
callq 0xb8350
movq -0x18(%rbp), %rdi
leaq 0xfa99(%rip), %rsi      # 0xc78f9
callq 0xb8350
movq -0x18(%rbp), %rdi
leaq 0xfa91(%rip), %rsi      # 0xc7901
callq 0xb8350
movq -0x18(%rbp), %rdi
leaq 0xfa8e(%rip), %rsi      # 0xc790e
callq 0xb8350
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x84(%rcx), %eax
jge 0xb7f5e
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
addq $0x88, %rax
movslq -0x44(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rax
addq $0x88, %rax
movslq -0x44(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
leaq 0xfa43(%rip), %rsi      # 0xc791e
movb $0x0, %al
callq 0xb81e0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x3d, %ecx
movb %al, -0xa1(%rbp)
je 0xb7f0d
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0xb7f19
jmp 0xb7f4e
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0xb7f2c
movq -0x38(%rbp), %rax
movb $0x5f, (%rax)
movq -0x38(%rbp), %rax
movzbl (%rax), %edi
callq 0xd2a0
movb %al, %cl
movq -0x38(%rbp), %rax
movb %cl, (%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb7ee6
jmp 0xb7f50
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xb7e8c
movq -0x18(%rbp), %rax
movq -0x18(%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 -0x18(%rbp), %rax
movq -0x18(%rbp), %rdx
movl 0x2208(%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x2208(%rdx)
movslq %ecx, %rcx
movb $0x0, 0x8(%rax,%rcx)
movq -0x18(%rbp), %rax
cmpl $0x40, 0x220c(%rax)
jge 0xb7fb6
jmp 0xb7fd5
leaq 0xf96c(%rip), %rdi      # 0xc7929
leaq 0xeb97(%rip), %rsi      # 0xc6b5b
movl $0x904, %edx            # imm = 0x904
leaq 0xf982(%rip), %rcx      # 0xc7952
callq 0xd280
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2208(%rax)
jle 0xb7fe4
jmp 0xb8003
leaq 0xf9bf(%rip), %rdi      # 0xc79aa
leaq 0xeb69(%rip), %rsi      # 0xc6b5b
movl $0x905, %edx            # imm = 0x905
leaq 0xf954(%rip), %rcx      # 0xc7952
callq 0xd280
movq -0x18(%rbp), %rax
cmpl $0x2000, 0x2208(%rax)   # imm = 0x2000
jge 0xb8015
jmp 0xb8034
leaq 0xf99b(%rip), %rdi      # 0xc79b7
leaq 0xeb38(%rip), %rsi      # 0xc6b5b
movl $0x906, %edx            # imm = 0x906
leaq 0xf923(%rip), %rcx      # 0xc7952
callq 0xd280
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)