#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 atoi(scalar_t__);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/);
scalar_t__ getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(scalar_t__, char *);
int /*<<< orphan*/ strcpy(char *, scalar_t__);
int strlen(scalar_t__);
char *cgi_get_data(int *data_length) {
  char *cgi_data;
  if (strcmp(getenv("REQUEST_METHOD"), "POST") == 0) {
    if (getenv("CONTENT_LENGTH")) {
      *data_length = atoi(getenv("CONTENT_LENGTH"));
      cgi_data = (char *)malloc(*data_length + 1);
      fread(cgi_data, 1, *data_length, stdin);
      cgi_data[*data_length] = '\0';
    }
  }
  if (strcmp(getenv("REQUEST_METHOD"), "GET") == 0) {
    *data_length = strlen(getenv("QUERY_STRING"));
    cgi_data = (char *)malloc(*data_length + 1);
    strcpy(cgi_data, getenv("QUERY_STRING"));
    cgi_data[*data_length] = '\0';
  }
  return cgi_data;
}


x86 assembly (clang, -O0):
<cgi_get_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
lea    0xe7d(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1216 <cgi_get_data+0xa6>
lea    0xe6c(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1211 <cgi_get_data+0xa1>
lea    0xe56(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1216 <cgi_get_data+0xa6>
lea    0xde3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
lea    0xdf7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1298 <cgi_get_data+0x128>
lea    0xde5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    0xdaf(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x18(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgi_get_data>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe84(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11e1 <cgi_get_data+0x71>
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11e1 <cgi_get_data+0x71>
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,(%r14)
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    (%r14),%edx
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <fread@plt>
movslq (%r14),%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11e1 <cgi_get_data+0x71>
lea    0xe18(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe2f(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1240 <cgi_get_data+0xd0>
lea    0xe1f(%rip),%r15
mov    %r15,%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,(%r14)
lea    0x1(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <getenv@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
movslq (%r14),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"REQUEST_METHOD\00"
@.str.1 = [5 x i8] c"POST\00"
@.str.2 = [15 x i8] c"CONTENT_LENGTH\00"
@stdin = global i32 0
@.str.3 = [4 x i8] c"GET\00"
@.str.4 = [13 x i8] c"QUERY_STRING\00"
define i8* @cgi_get_data(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
store i32* %0, i32** %2
%4 = call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = call i64 @strcmp(i64 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %30
7: ; preds = %1
%8 = call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %7
%11 = call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @atoi(i64 %11)
%13 = load i32*, i32** %2
store i32 %12, i32* %13
%14 = load i32*, i32** %2
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = load i32*, i32** %2
%21 = load i32, i32* %20
%22 = load i32, i32* @stdin
%23 = call i32 @fread(i8* %19, i32 1, i32 %21, i32 %22)
%24 = load i8*, i8** %3
%25 = load i32*, i32** %2
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %10, %7
br label %30
30: ; preds = %29, %1
%31 = call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%32 = call i64 @strcmp(i64 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %51
34: ; preds = %30
%35 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @strlen(i64 %35)
%37 = load i32*, i32** %2
store i32 %36, i32* %37
%38 = load i32*, i32** %2
%39 = load i32, i32* %38
%40 = add nsw i32 %39, 1
%41 = call i64 @malloc(i32 %40)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %3
%43 = load i8*, i8** %3
%44 = call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @strcpy(i8* %43, i64 %44)
%46 = load i8*, i8** %3
%47 = load i32*, i32** %2
%48 = load i32, i32* %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %34, %30
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@.str = [15 x i8] c"REQUEST_METHOD\00"
@.str.1 = [5 x i8] c"POST\00"
@.str.2 = [15 x i8] c"CONTENT_LENGTH\00"
@stdin = global i32 0
@.str.3 = [4 x i8] c"GET\00"
@.str.4 = [13 x i8] c"QUERY_STRING\00"
define i8* @cgi_get_data(i32* nocapture %0) {
%2 = tail call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%3 = tail call i64 @strcmp(i64 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %19
5: ; preds = %1
%6 = tail call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = tail call i32 @atoi(i64 %6)
store i32 %9, i32* %0
%10 = add nsw i32 %9, 1
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = load i32, i32* %0
%14 = load i32, i32* @stdin
%15 = tail call i32 @fread(i8* %12, i32 1, i32 %13, i32 %14)
%16 = load i32, i32* %0
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %12, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %5, %8, %1
%20 = phi i8* [ %12, %8 ], [ undef, %5 ], [ undef, %1 ]
%21 = tail call i64 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%22 = tail call i64 @strcmp(i64 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %19
%25 = tail call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 @strlen(i64 %25)
store i32 %26, i32* %0
%27 = add nsw i32 %26, 1
%28 = tail call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = tail call i64 @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @strcpy(i8* %29, i64 %30)
%32 = load i32, i32* %0
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %29, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %24, %19
%36 = phi i8* [ %29, %24 ], [ %20, %19 ]
ret i8* %36
}