#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fork();
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void http(struct sockaddr_in *client_addr) {
  char buffer[1512];
  char port[10];
  char *argv[3] = {"./hello_cgi", "./hello_cgi", 0};
  read(0, buffer, sizeof(buffer));
  setenv("REMOTE_ADDR", inet_ntoa(client_addr->sin_addr), 1);
  sprintf(port, "%d", ntohs(client_addr->sin_port));
  setenv("REMOTE_PORT", port, 1);
  if (!fork())
    execvp(argv[0], argv);
}


x86 assembly (clang, -O0):
<http>:
push   %rbp
mov    %rsp,%rbp
sub    $0x630,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2c7a(%rip),%rax
mov    %rax,-0x620(%rbp)
mov    0x2c74(%rip),%rax
mov    %rax,-0x618(%rbp)
mov    0x2c6e(%rip),%rax
mov    %rax,-0x610(%rbp)
lea    -0x5f0(%rbp),%rsi
xor    %edi,%edi
mov    $0x5e8,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xe3a(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
lea    -0x5fa(%rbp),%rax
mov    %rax,-0x628(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x628(%rbp),%rdi
mov    %eax,%edx
lea    0xe13(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x5fa(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    1245 <http+0xd5>
mov    -0x620(%rbp),%rdi
lea    -0x620(%rbp),%rsi
call   1070 <execvp@plt>
add    $0x630,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http>:
push   %rbx
sub    $0x620,%rsp
mov    %rdi,%rbx
mov    0x2c8e(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c72(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0x30(%rsp),%rsi
xor    %edi,%edi
mov    $0x5e8,%edx
call   1050 <read@plt>
mov    0x4(%rbx),%edi
call   1040 <inet_ntoa@plt>
lea    0xe5a(%rip),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    (%rbx),%edi
call   1060 <ntohs@plt>
lea    0xe4b(%rip),%rsi
lea    0x26(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
lea    0xe38(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    120a <http+0x9a>
lea    0xdfe(%rip),%rdi
mov    %rsp,%rsi
call   1070 <execvp@plt>
add    $0x620,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@.str = [12 x i8] c"./hello_cgi\00"
@__const.http.argv = [3 x i8*] [i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [12 x i8] c"REMOTE_ADDR\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [12 x i8] c"REMOTE_PORT\00"
define void @http(%struct.sockaddr_in* %0) {
%2 = alloca %struct.sockaddr_in*
%3 = alloca [1512 x i8]
%4 = alloca [10 x i8]
%5 = alloca [3 x i8*]
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %2
%6 = bitcast [3 x i8*]* %5 to i8*
%7 = getelementptr inbounds [1512 x i8], [1512 x i8]* %3, i64 0, i64 0
%8 = call i32 @read(i32 0, i8* %7, i32 1512)
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call i8* @inet_ntoa(i32 %11)
%13 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 1)
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @ntohs(i32 %17)
%19 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%21 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %20, i32 1)
%22 = call i32 (...) @fork()
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %1
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%26 = load i8*, i8** %25
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%28 = call i32 @execvp(i8* %26, i8** %27)
br label %29
29: ; preds = %24, %1
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@.str = [12 x i8] c"./hello_cgi\00"
@__const.http.argv = [3 x i8*] [i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [12 x i8] c"REMOTE_ADDR\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [12 x i8] c"REMOTE_PORT\00"
define void @http(%struct.sockaddr_in* nocapture %0) {
%2 = alloca [1512 x i8]
%3 = alloca [10 x i8]
%4 = alloca [3 x i8*]
%5 = getelementptr inbounds [1512 x i8], [1512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%7 = bitcast [3 x i8*]* %4 to i8*
%8 = call i32 @read(i32 0, i8* %5, i32 1512)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1
%10 = load i32, i32* %9
%11 = tail call i8* @inet_ntoa(i32 %10)
%12 = tail call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @ntohs(i32 %14)
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %6, i32 1)
%18 = call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%22 = call i32 @execvp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %21)
br label %23
23: ; preds = %20, %1
ret void
}