// File: /workspace/github_cpp_makefile_star2_O3/nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: handle_cgi_request
static void handle_cgi_request(struct mg_connection *conn, const char *prog) {
  int headers_len, data_len, i, fd_stdin[2], fd_stdout[2];
  const char *status;
  char buf[BUFSIZ], *pbuf, dir[PATH_MAX], *p;
  struct mg_request_info ri;
  struct cgi_env_block blk;
  FILE *in, *out;
  pid_t pid;

  prepare_cgi_environment(conn, prog, &blk);

  // CGI must be executed in its own directory. 'dir' must point to the
  // directory containing executable program, 'p' must point to the
  // executable program name relative to 'dir'.
  (void) mg_snprintf(conn, dir, sizeof(dir), "%s", prog);
  if ((p = strrchr(dir, DIRSEP)) != NULL) {
    *p++ = '\0';
  } else {
    dir[0] = '.', dir[1] = '\0';
    p = (char *) prog;
  }

  pid = (pid_t) -1;
  fd_stdin[0] = fd_stdin[1] = fd_stdout[0] = fd_stdout[1] = -1;
  in = out = NULL;

  if (pipe(fd_stdin) != 0 || pipe(fd_stdout) != 0) {
    send_http_error(conn, 500, http_500_error,
        "Cannot create CGI pipe: %s", strerror(ERRNO));
    goto done;
  } else if ((pid = spawn_process(conn, p, blk.buf, blk.vars,
          fd_stdin[0], fd_stdout[1], dir)) == (pid_t) -1) {
    goto done;
  } else if ((in = fdopen(fd_stdin[1], "wb")) == NULL ||
      (out = fdopen(fd_stdout[0], "rb")) == NULL) {
    send_http_error(conn, 500, http_500_error,
        "fopen: %s", strerror(ERRNO));
    goto done;
  }

  setbuf(in, NULL);
  setbuf(out, NULL);

  // spawn_process() must close those!
  // If we don't mark them as closed, close() attempt before
  // return from this function throws an exception on Windows.
  // Windows does not like when closed descriptor is closed again.
  fd_stdin[0] = fd_stdout[1] = -1;

  // Send POST data to the CGI process if needed
  if (!strcmp(conn->request_info.request_method, "POST") &&
      !forward_body_data(conn, in, INVALID_SOCKET, NULL)) {
    goto done;
  }

  // Now read CGI reply into a buffer. We need to set correct
  // status code, thus we need to see all HTTP headers first.
  // Do not send anything back to client, until we buffer in all
  // HTTP headers.
  data_len = 0;
  headers_len = read_request(out, INVALID_SOCKET, NULL,
      buf, sizeof(buf), &data_len);
  if (headers_len <= 0) {
    send_http_error(conn, 500, http_500_error,
                    "CGI program sent malformed HTTP headers: [%.*s]",
                    data_len, buf);
    goto done;
  }
  pbuf = buf;
  buf[headers_len - 1] = '\0';
  parse_http_headers(&pbuf, &ri);

  // Make up and send the status line
  status = get_header(&ri, "Status");
  conn->request_info.status_code = status == NULL ? 200 : atoi(status);
  (void) mg_printf(conn, "HTTP/1.1 %d OK\r\n", conn->request_info.status_code);

  // Send headers
  for (i = 0; i < ri.num_headers; i++) {
    mg_printf(conn, "%s: %s\r\n",
              ri.http_headers[i].name, ri.http_headers[i].value);
  }
  (void) mg_write(conn, "\r\n", 2);

  // Send chunk of data that may be read after the headers
  conn->num_bytes_sent += mg_write(conn, buf + headers_len,
                                   (size_t)(data_len - headers_len));

  // Read the rest of CGI output and send to the client
  send_file_data(conn, out, INT64_MAX);

done:
  if (pid != (pid_t) -1) {
    kill(pid, SIGKILL);
#if !defined(_WIN32)
    do {} while (waitpid(-1, &i, WNOHANG) > 0);
#endif
  }
  if (fd_stdin[0] != -1) {
    (void) close(fd_stdin[0]);
  }
  if (fd_stdout[1] != -1) {
    (void) close(fd_stdout[1]);
  }

  if (in != NULL) {
    (void) fclose(in);
  } else if (fd_stdin[1] != -1) {
    (void) close(fd_stdin[1]);
  }

  if (out != NULL) {
    (void) fclose(out);
  } else if (fd_stdout[0] != -1) {
    (void) close(fd_stdout[0]);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x46a8, %rsp           # imm = 0x46A8
movq %rsi, %r14
movq %rdi, %rbx
xorl %eax, %eax
leaq 0x38(%rsp), %r15
movl %eax, 0x1210(%r15)
movl %eax, 0x1008(%r15)
movq %rdi, (%r15)
movq 0x18(%rdi), %rdi
movq 0x458(%rbx), %rax
movq 0xa0(%rax), %rsi
leaq 0x16a0(%rsp), %r12
movq %r12, %rdx
callq 0x14089
movq 0x458(%rbx), %rax
movq 0x38(%rax), %rdx
leaq 0x3487(%rip), %rsi      # 0x165b7
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
movq (%r12), %r13
movq 0x8(%r12), %r12
leaq 0x347c(%rip), %rsi      # 0x165c6
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x14669
leaq 0x3476(%rip), %rsi      # 0x165d7
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x14669
leaq 0x35f5(%rip), %r12      # 0x1676d
leaq 0x346b(%rip), %rdx      # 0x165ea
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14669
leaq 0x3471(%rip), %rdx      # 0x16604
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14669
leaq 0x3476(%rip), %rdx      # 0x1661d
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14669
movzwl 0x47a(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x3468(%rip), %rsi      # 0x16631
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
movq 0x10(%rbx), %rdx
leaq 0x3462(%rip), %rsi      # 0x16640
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
movl 0x490(%rbx), %edi
callq 0x5220
leaq 0x3458(%rip), %rsi      # 0x16652
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14669
movl 0x48(%rbx), %edx
leaq 0x3450(%rip), %rsi      # 0x16661
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
movq 0x18(%rbx), %rdx
leaq 0x344a(%rip), %rsi      # 0x16670
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
movq 0x18(%rbx), %r12
movq %r12, %rdi
movl $0x2f, %esi
callq 0x5530
movq %rax, %r13
movq %r14, %rdi
movl $0x2f, %esi
callq 0x5530
testq %rax, %rax
cmoveq %r14, %rax
subq %r12, %r13
leaq 0x341d(%rip), %rsi      # 0x1667f
movq %r15, %rdi
movq %r13, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x14669
leaq 0x3416(%rip), %rsi      # 0x16692
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x14669
leaq 0x3415(%rip), %rsi      # 0x166a5
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x14669
cmpq $0x0, 0x460(%rbx)
leaq 0x3415(%rip), %rax      # 0x166c1
leaq 0x3838(%rip), %rdx      # 0x16aeb
cmoveq %rax, %rdx
leaq 0x33fa(%rip), %rsi      # 0x166b8
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
leaq 0x8(%rbx), %r15
leaq 0x33f2(%rip), %rsi      # 0x166c5
movq %r15, %rdi
callq 0xe5cf
testq %rax, %rax
je 0x132f6
leaq 0x33eb(%rip), %rsi      # 0x166d2
leaq 0x38(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14669
movq 0x28(%rbx), %rdx
testq %rdx, %rdx
je 0x13312
leaq 0x33dc(%rip), %rsi      # 0x166e2
leaq 0x38(%rsp), %rdi
xorl %eax, %eax
callq 0x14669
leaq 0x2b72(%rip), %rsi      # 0x15e8b
movq %r15, %rdi
callq 0xe5cf
testq %rax, %rax
je 0x1333c
leaq 0x33c5(%rip), %rsi      # 0x166f2
leaq 0x38(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14669
leaq 0x33cc(%rip), %rdi      # 0x1670f
callq 0x5610
testq %rax, %rax
je 0x13363
leaq 0x33cb(%rip), %rsi      # 0x1671f
leaq 0x38(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14669
leaq 0x339a(%rip), %rdi      # 0x16704
callq 0x5610
testq %rax, %rax
je 0x1338a
leaq 0x3399(%rip), %rsi      # 0x16714
leaq 0x38(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14669
movq %r14, 0x20(%rsp)
leaq 0x3391(%rip), %rdi      # 0x16727
callq 0x5610
testq %rax, %rax
je 0x133b6
leaq 0x3388(%rip), %rsi      # 0x1672f
leaq 0x38(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x14669
movq 0x30(%rbx), %rdx
testq %rdx, %rdx
je 0x133ed
leaq 0x3374(%rip), %rsi      # 0x1673a
leaq 0x38(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
leaq 0x3391(%rip), %rsi      # 0x1676d
leaq 0x3366(%rip), %rdx      # 0x16749
movq %r15, %rdi
xorl %eax, %eax
callq 0x14669
cmpl $0x0, 0x54(%rbx)
jle 0x13469
leaq 0x58(%rbx), %rbp
leaq 0x38(%rsp), %r12
xorl %r14d, %r14d
movq %rbx, %r15
movq %r14, %rax
shlq $0x4, %rax
movq (%rbp,%rax), %rdx
movq 0x8(%rbp,%rax), %rcx
movq %r12, %rdi
leaq 0x333d(%rip), %rsi      # 0x1675a
xorl %eax, %eax
callq 0x14669
movq %rax, %r13
movzbl (%r13), %ebx
cmpl $0x2d, %ebx
je 0x1343c
testl %ebx, %ebx
je 0x1345a
cmpl $0x3d, %ebx
jne 0x13443
jmp 0x1345a
movb $0x5f, (%r13)
movb $0x5f, %bl
callq 0x50b0
movq (%rax), %rax
movzbl %bl, %ecx
movb (%rax,%rcx,4), %al
movb %al, (%r13)
incq %r13
jmp 0x13427
incq %r14
movq %r15, %rbx
movslq 0x54(%r15), %rax
cmpq %rax, %r14
jl 0x133ff
movq 0x458(%rbx), %rax
movq 0x18(%rax), %rdi
leaq 0x26a0(%rsp), %rsi
xorl %edx, %edx
callq 0x11d0e
testq %rax, %rax
je 0x134d1
movq %rax, %r15
leaq 0x3140(%rip), %r12      # 0x165d2
leaq 0x38(%rsp), %r13
leaq 0x26a0(%rsp), %rbp
movq 0x26a0(%rsp), %rcx
movq 0x26a8(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x14669
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x11d0e
movq %rax, %r15
testq %rax, %rax
jne 0x1349f
movslq 0x1248(%rsp), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x1248(%rsp)
movq $0x0, 0x1048(%rsp,%rax,8)
movslq 0x1040(%rsp), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x1040(%rsp)
movb $0x0, 0x40(%rsp,%rax)
leaq 0x3260(%rip), %rcx      # 0x1676d
leaq 0x16a0(%rsp), %r15
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %r14
movq %r14, %r8
xorl %eax, %eax
callq 0x12579
movq %r15, %rdi
movl $0x2f, %esi
callq 0x5530
testq %rax, %rax
je 0x13549
movq %rax, %r14
incq %r14
jmp 0x13555
leaq 0x16a1(%rsp), %rax
movb $0x2e, -0x1(%rax)
movb $0x0, (%rax)
movq $-0x1, %rax
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
callq 0x5670
leaq 0x2fcc(%rip), %r15      # 0x16544
testl %eax, %eax
jne 0x136de
leaq 0x8(%rsp), %rdi
callq 0x5670
testl %eax, %eax
jne 0x136de
movl 0x10(%rsp), %r12d
movl 0xc(%rsp), %r15d
callq 0x5120
cmpl $-0x1, %eax
je 0x136d7
movl %eax, %ebp
testl %eax, %eax
je 0x138e7
movl %r12d, %edi
callq 0x59c0
movl %r15d, %edi
callq 0x59c0
movl 0x14(%rsp), %edi
leaq 0x2f90(%rip), %rsi      # 0x1655f
callq 0x5500
movq %rax, %r15
testq %rax, %rax
je 0x13774
movl 0x8(%rsp), %edi
leaq 0x2f77(%rip), %rsi      # 0x16562
callq 0x5500
testq %rax, %rax
je 0x13774
movq %rax, %r14
movq %r15, %rdi
xorl %esi, %esi
callq 0x56e0
movq %r14, %rdi
xorl %esi, %esi
callq 0x56e0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0xc(%rsp)
movl %eax, 0x10(%rsp)
movq 0x10(%rbx), %rdi
leaq 0x28eb(%rip), %rsi      # 0x15f13
callq 0x56b0
testl %eax, %eax
jne 0x1364b
movq %rbx, %rdi
movq %r15, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
callq 0x12797
testl %eax, %eax
je 0x137a3
leaq 0x1c(%rsp), %r9
movl $0x0, (%r9)
leaq 0x26a0(%rsp), %r13
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movq %r13, %rcx
movl $0x2000, %r8d           # imm = 0x2000
callq 0x121ae
testl %eax, %eax
jle 0x137cf
movl %eax, %r12d
movl %ebp, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movq %r13, (%rdi)
leal -0x1(%r12), %eax
movb $0x0, 0x26a0(%rsp,%rax)
leaq 0x1250(%rsp), %r13
movq %r13, %rsi
callq 0x125f5
leaq 0x2eed(%rip), %rsi      # 0x1659f
movq %r13, %rdi
callq 0xe5cf
testq %rax, %rax
movq %r12, 0x28(%rsp)
je 0x137fb
movq %rax, %rdi
callq 0x5810
movl %eax, %edx
jmp 0x13800
leaq 0x3087(%rip), %r15      # 0x16765
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x299e(%rip), %rdx      # 0x1608f
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %r15, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x12224
xorl %r15d, %r15d
movl 0x10(%rsp), %edi
cmpl $-0x1, %edi
je 0x1371a
callq 0x59c0
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x13728
callq 0x59c0
testq %r15, %r15
je 0x13737
movq %r15, %rdi
callq 0x54a0
jmp 0x13745
movl 0x14(%rsp), %edi
cmpl $-0x1, %edi
je 0x13745
callq 0x59c0
testq %r14, %r14
je 0x13754
movq %r14, %rdi
callq 0x54a0
jmp 0x13762
movl 0x8(%rsp), %edi
cmpl $-0x1, %edi
je 0x13762
callq 0x59c0
addq $0x46a8, %rsp           # imm = 0x46A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2908(%rip), %rdx      # 0x1608f
leaq 0x2dd7(%rip), %rcx      # 0x16565
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %rax, %r8
xorl %eax, %eax
callq 0x12224
movl %ebp, %edi
movl $0x9, %esi
callq 0x56d0
leaq 0x18(%rsp), %rbx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rsi
movl $0x1, %edx
callq 0x5380
testl %eax, %eax
jg 0x137b4
jmp 0x1370c
movl 0x1c(%rsp), %r8d
leaq 0x28b4(%rip), %rdx      # 0x1608f
leaq 0x2d8d(%rip), %rcx      # 0x1656f
leaq 0x26a0(%rsp), %r9
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %eax, %eax
callq 0x12224
jmp 0x137a3
movl $0xc8, %edx
movl %edx, 0x4c(%rbx)
leaq 0x2d9c(%rip), %rsi      # 0x165a6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe860
movl 0x129c(%rsp), %r12d
movl $0x0, 0x18(%rsp)
testl %r12d, %r12d
jle 0x13865
movl $0x1, %r13d
leaq 0x12a8(%rsp), %rbp
movq -0x8(%rbp), %rdx
movq (%rbp), %rcx
movq %rbx, %rdi
leaq 0x2746(%rip), %rsi      # 0x15f8f
xorl %eax, %eax
callq 0xe860
movl %r13d, 0x18(%rsp)
addq $0x10, %rbp
leal 0x1(%r13), %eax
cmpl %r12d, %r13d
movl %eax, %r13d
jl 0x13837
movl 0x470(%rbx), %esi
movq 0x460(%rbx), %rdx
leaq 0x29c4(%rip), %rcx      # 0x1623d
movl $0x2, %r8d
xorl %edi, %edi
callq 0xe799
movq 0x28(%rsp), %rdx
movl %edx, %eax
leaq (%rsp,%rax), %rcx
addq $0x26a0, %rcx           # imm = 0x26A0
movslq 0x1c(%rsp), %r8
movslq %edx, %rax
subq %rax, %r8
movl 0x470(%rbx), %esi
movq 0x460(%rbx), %rdx
xorl %edi, %edi
callq 0xe799
cltq
addq %rax, 0x4b0(%rbx)
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
movq %rbx, %rdi
movq %r14, %rsi
callq 0x145dd
movl 0x20(%rsp), %ebp
cmpl $-0x1, %ebp
jne 0x137a3
jmp 0x1370c
leaq 0x16a0(%rsp), %rdi
callq 0x5750
testl %eax, %eax
jne 0x13929
movl %r12d, %edi
xorl %esi, %esi
callq 0x5440
cmpl $-0x1, %eax
jne 0x1395a
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2e76(%rip), %rsi      # 0x16790
leaq 0x2e61(%rip), %rdx      # 0x16782
movq %rbx, %rdi
movl %r12d, %ecx
jmp 0x1398c
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2e34(%rip), %rsi      # 0x16770
leaq 0x2e3f(%rip), %rdx      # 0x16782
leaq 0x16a0(%rsp), %rcx
movq %rbx, %rdi
movq %rax, %r8
xorl %eax, %eax
callq 0xfb6a
jmp 0x13996
movl %r15d, %edi
movl $0x1, %esi
callq 0x5440
cmpl $-0x1, %eax
jne 0x139a0
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2e25(%rip), %rsi      # 0x167a4
leaq 0x2dfc(%rip), %rdx      # 0x16782
movq %rbx, %rdi
movl %r15d, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0xfb6a
movl $0x1, %edi
callq 0x55f0
leaq 0x1048(%rsp), %r13
movl %r15d, %edi
movl $0x2, %esi
callq 0x5440
movl %r12d, %edi
callq 0x59c0
movl %r15d, %edi
callq 0x59c0
movq 0x458(%rbx), %rax
movq 0x28(%rax), %r15
testq %r15, %r15
jne 0x13a0c
movq %r14, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x5790
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2dbe(%rip), %rsi      # 0x167b8
leaq 0x2d81(%rip), %rdx      # 0x16782
movq %rbx, %rdi
movq %r14, %rcx
jmp 0x1394e
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
movq %r13, %r8
xorl %eax, %eax
callq 0x5790
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2d97(%rip), %rsi      # 0x167cb
leaq 0x2d47(%rip), %rdx      # 0x16782
movq %rbx, %rdi
movq %r15, %rcx
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0xfb6a
jmp 0x13996