#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*/ get_first_address(char *, int, char *);
char *getenv(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ strnlen(char *, int);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ unsetenv(char *);
int main() {
  char *header_forwarded_proto = getenv("HTTP_X_FORWARDED_PROTO");
  if (header_forwarded_proto) {
    if (strnlen(header_forwarded_proto, 5) == 5 &&
        strncmp(header_forwarded_proto, "https", 5) == 0) {
      setenv("SERVER_PORT", "443", 1);
      setenv("HTTPS", "on", 1);
    } else {
      setenv("SERVER_PORT", "80", 1);
      unsetenv("HTTPS");
    }
  }
  char *header_forwarded_for = getenv("HTTP_X_FORWARDED_FOR");
  if (header_forwarded_for) {
    char first_address[16];
    get_first_address(first_address, 16, header_forwarded_for);
    if (strnlen(first_address, 15) > 0) {
      setenv("REMOTE_ADDR", first_address, 1);
    }
  }
  unsetenv("HTTP_X_FORWARDED_FOR");
  unsetenv("HTTP_X_FORWARDED_PROTO");
  system("/usr/bin/php-cgi");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     122f <main+0xbf>
mov    -0x10(%rbp),%rdi
mov    $0x5,%esi
call   1080 <strnlen@plt>
cmp    $0x5,%rax
jne    1206 <main+0x96>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1206 <main+0x96>
lea    0xe45(%rip),%rdi
lea    0xe4a(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xe3d(%rip),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
jmp    122a <main+0xba>
lea    0xe10(%rip),%rdi
lea    0xe22(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xe08(%rip),%rdi
call   1090 <unsetenv@plt>
jmp    122f <main+0xbf>
lea    0xe03(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128e <main+0x11e>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x10,%esi
call   1070 <get_first_address@plt>
lea    -0x30(%rbp),%rdi
mov    $0xf,%esi
call   1080 <strnlen@plt>
cmp    $0x0,%rax
jle    1289 <main+0x119>
lea    -0x30(%rbp),%rsi
lea    0xdcf(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
jmp    128e <main+0x11e>
lea    0xda4(%rip),%rdi
call   1090 <unsetenv@plt>
lea    0xd5f(%rip),%rdi
call   1090 <unsetenv@plt>
lea    0xdad(%rip),%rdi
call   1060 <system@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe84(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     120f <main+0x9f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x5,%esi
call   1080 <strnlen@plt>
cmp    $0x5,%rax
jne    11b9 <main+0x49>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11df <main+0x6f>
lea    0xe5d(%rip),%rdi
lea    0xe6f(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xe55(%rip),%rdi
call   1090 <unsetenv@plt>
jmp    120f <main+0x9f>
lea    0xe37(%rip),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xe2f(%rip),%rdi
lea    0xe2e(%rip),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xe23(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1259 <main+0xe9>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %rax,%rdx
call   1070 <get_first_address@plt>
mov    %rbx,%rdi
mov    $0xf,%esi
call   1080 <strnlen@plt>
test   %rax,%rax
jle    1259 <main+0xe9>
lea    0xe02(%rip),%rdi
mov    %rsp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xdd9(%rip),%rdi
call   1090 <unsetenv@plt>
lea    0xd94(%rip),%rdi
call   1090 <unsetenv@plt>
lea    0xde2(%rip),%rdi
call   1060 <system@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"HTTP_X_FORWARDED_PROTO\00"
@.str.1 = [6 x i8] c"https\00"
@.str.2 = [12 x i8] c"SERVER_PORT\00"
@.str.3 = [4 x i8] c"443\00"
@.str.4 = [6 x i8] c"HTTPS\00"
@.str.5 = [3 x i8] c"on\00"
@.str.6 = [3 x i8] c"80\00"
@.str.7 = [21 x i8] c"HTTP_X_FORWARDED_FOR\00"
@.str.8 = [12 x i8] c"REMOTE_ADDR\00"
@.str.9 = [17 x i8] c"/usr/bin/php-cgi\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [16 x i8]
store i32 0, i32* %1
%5 = call i8* @getenv(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %23
8: ; preds = %0
%9 = load i8*, i8** %2
%10 = call i64 @strnlen(i8* %9, i32 5)
%11 = icmp eq i64 %10, 5
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 1)
%18 = call i32 @setenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 1)
br label %22
19: ; preds = %12, %8
%20 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 1)
%21 = call i32 @unsetenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
br label %23
23: ; preds = %22, %0
%24 = call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%29 = load i8*, i8** %3
%30 = call i32 @get_first_address(i8* %28, i32 16, i8* %29)
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%32 = call i64 @strnlen(i8* %31, i32 15)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%36 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %35, i32 1)
br label %37
37: ; preds = %34, %27
br label %38
38: ; preds = %37, %23
%39 = call i32 @unsetenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%40 = call i32 @unsetenv(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"HTTP_X_FORWARDED_PROTO\00"
@.str.1 = [6 x i8] c"https\00"
@.str.2 = [12 x i8] c"SERVER_PORT\00"
@.str.3 = [4 x i8] c"443\00"
@.str.4 = [6 x i8] c"HTTPS\00"
@.str.5 = [3 x i8] c"on\00"
@.str.6 = [3 x i8] c"80\00"
@.str.7 = [21 x i8] c"HTTP_X_FORWARDED_FOR\00"
@.str.8 = [12 x i8] c"REMOTE_ADDR\00"
@.str.9 = [17 x i8] c"/usr/bin/php-cgi\00"
define i32 @main() {
%1 = alloca [16 x i8]
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %16, label %4
4: ; preds = %0
%5 = tail call i64 @strnlen(i8* %2, i32 5)
%6 = icmp eq i64 %5, 5
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = tail call i64 @strncmp(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 1)
%12 = tail call i32 @setenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 1)
br label %16
13: ; preds = %7, %4
%14 = tail call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 1)
%15 = tail call i32 @unsetenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %10, %13, %0
%17 = tail call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%21 = call i32 @get_first_address(i8* %20, i32 16, i8* %17)
%22 = call i64 @strnlen(i8* %20, i32 15)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @setenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* %20, i32 1)
br label %26
26: ; preds = %24, %19
br label %27
27: ; preds = %26, %16
%28 = call i32 @unsetenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%29 = call i32 @unsetenv(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}