#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ putenv(char *);
int /*<<< orphan*/ send_new(int, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void php_cgi(char *script_path, int fd) {
  send_new(fd,
           "HTTP/1.1 200 OK\n Server: Web Server in C\n Connection: close\n");
  dup2(fd, STDOUT_FILENO);
  char script[500];
  strcpy(script, "SCRIPT_FILENAME=");
  strcat(script, script_path);
  putenv("GATEWAY_INTERFACE=CGI/1.1");
  putenv(script);
  putenv("QUERY_STRING=");
  putenv("REQUEST_METHOD=GET");
  putenv("REDIRECT_STATUS=true");
  putenv("SERVER_PROTOCOL=HTTP/1.1");
  putenv("REMOTE_HOST=127.0.0.1");
  execl("/usr/bin/php-cgi", "php-cgi", NULL);
}


x86 assembly (clang, -O0):
<php_cgi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe84(%rip),%rsi
call   1070 <send_new@plt>
mov    -0xc(%rbp),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    -0x200(%rbp),%rdi
lea    0xe9d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x200(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strcat@plt>
lea    0xe92(%rip),%rdi
call   1050 <putenv@plt>
lea    -0x200(%rbp),%rdi
call   1050 <putenv@plt>
lea    0xe94(%rip),%rdi
call   1050 <putenv@plt>
lea    0xe96(%rip),%rdi
call   1050 <putenv@plt>
lea    0xe9d(%rip),%rdi
call   1050 <putenv@plt>
lea    0xea6(%rip),%rdi
call   1050 <putenv@plt>
lea    0xeb3(%rip),%rdi
call   1050 <putenv@plt>
lea    0xebd(%rip),%rdi
lea    0xebf(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execl@plt>
add    $0x200,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<php_cgi>:
push   %r14
push   %rbx
sub    $0x1f8,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xe8a(%rip),%rsi
mov    %ebx,%edi
call   1070 <send_new@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
lea    0xea9(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
lea    0xe9d(%rip),%rdi
call   1050 <putenv@plt>
mov    %rbx,%rdi
call   1050 <putenv@plt>
lea    0xea3(%rip),%rdi
call   1050 <putenv@plt>
lea    0xea5(%rip),%rdi
call   1050 <putenv@plt>
lea    0xeac(%rip),%rdi
call   1050 <putenv@plt>
lea    0xeb5(%rip),%rdi
call   1050 <putenv@plt>
lea    0xec2(%rip),%rdi
call   1050 <putenv@plt>
lea    0xecc(%rip),%rdi
lea    0xece(%rip),%rsi
xor    %edx,%edx
call   1080 <execl@plt>
add    $0x1f8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [61 x i8] c"HTTP/1.1 200 OK\0A Server: Web Server in C\0A Connection: close\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [17 x i8] c"SCRIPT_FILENAME=\00"
@.str.2 = [26 x i8] c"GATEWAY_INTERFACE=CGI/1.1\00"
@.str.3 = [14 x i8] c"QUERY_STRING=\00"
@.str.4 = [19 x i8] c"REQUEST_METHOD=GET\00"
@.str.5 = [21 x i8] c"REDIRECT_STATUS=true\00"
@.str.6 = [25 x i8] c"SERVER_PROTOCOL=HTTP/1.1\00"
@.str.7 = [22 x i8] c"REMOTE_HOST=127.0.0.1\00"
@.str.8 = [17 x i8] c"/usr/bin/php-cgi\00"
@.str.9 = [8 x i8] c"php-cgi\00"
define void @php_cgi(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [500 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = call i32 @send_new(i32 %6, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %4
%9 = load i32, i32* @STDOUT_FILENO
%10 = call i32 @dup2(i32 %8, i32 %9)
%11 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%14 = load i8*, i8** %3
%15 = call i32 @strcat(i8* %13, i8* %14)
%16 = call i32 @putenv(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%17 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%18 = call i32 @putenv(i8* %17)
%19 = call i32 @putenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @putenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @putenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @putenv(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%23 = call i32 @putenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%24 = call i32 @execl(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32* null)
ret void
}

LLVM IR (-O2):
@.str = [61 x i8] c"HTTP/1.1 200 OK\0A Server: Web Server in C\0A Connection: close\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [17 x i8] c"SCRIPT_FILENAME=\00"
@.str.2 = [26 x i8] c"GATEWAY_INTERFACE=CGI/1.1\00"
@.str.3 = [14 x i8] c"QUERY_STRING=\00"
@.str.4 = [19 x i8] c"REQUEST_METHOD=GET\00"
@.str.5 = [21 x i8] c"REDIRECT_STATUS=true\00"
@.str.6 = [25 x i8] c"SERVER_PROTOCOL=HTTP/1.1\00"
@.str.7 = [22 x i8] c"REMOTE_HOST=127.0.0.1\00"
@.str.8 = [17 x i8] c"/usr/bin/php-cgi\00"
@.str.9 = [8 x i8] c"php-cgi\00"
define void @php_cgi(i8* %0, i32 %1) {
%3 = alloca [500 x i8]
%4 = tail call i32 @send_new(i32 %1, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i32 @dup2(i32 %1, i32 %5)
%7 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @strcat(i8* %7, i8* %0)
%10 = call i32 @putenv(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @putenv(i8* %7)
%12 = call i32 @putenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 @putenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 @putenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%15 = call i32 @putenv(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%16 = call i32 @putenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%17 = call i32 @execl(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32* null)
ret void
}