#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(char *);
int fread(char *, int, int, int /*<<< orphan*/);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
char *post_getquerystring(int *error) {
  char *ct;
  char *cl;
  int icl;
  char *qs;
  int i;
  *error = 0;
  ct = getenv("CONTENT_TYPE");
  cl = getenv("CONTENT_LENGTH");
  if (cl == NULL) {
    *error = (-1);
    return NULL;
  }
  icl = atoi(cl);
  if (strcmp(ct, "application/x-www-form-urlencoded")) {
    *error = (-2);
    return NULL;
  } else if (icl == 0) {
    *error = (-3);
    return NULL;
  }
  if ((qs = malloc(icl + 1)) == NULL) {
    *error = (-4);
    return NULL;
  }
  if ((i = fread(qs, icl, 1, stdin)) != 1) {
    *error = (-5);
    return NULL;
  }
  qs[icl] = '\0';
  return qs;
}


x86 assembly (clang, -O0):
<post_getquerystring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
lea    0xe93(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    0xe90(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a8 <post_getquerystring+0x58>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    128c <post_getquerystring+0x13c>
mov    -0x20(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11e5 <post_getquerystring+0x95>
mov    -0x10(%rbp),%rax
movl   $0xfffffffe,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    128c <post_getquerystring+0x13c>
cmpl   $0x0,-0x24(%rbp)
jne    1206 <post_getquerystring+0xb6>
mov    -0x10(%rbp),%rax
movl   $0xfffffffd,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    128c <post_getquerystring+0x13c>
jmp    120b <post_getquerystring+0xbb>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    123b <post_getquerystring+0xeb>
mov    -0x10(%rbp),%rax
movl   $0xfffffffc,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    128c <post_getquerystring+0x13c>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x1,%eax
je     1278 <post_getquerystring+0x128>
mov    -0x10(%rbp),%rax
movl   $0xfffffffb,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    128c <post_getquerystring+0x13c>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<post_getquerystring>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movl   $0x0,(%rdi)
lea    0xe9b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xe99(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a6 <post_getquerystring+0x56>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,%r15d
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11b8 <post_getquerystring+0x68>
movl   $0xfffffffe,(%r14)
jmp    11ad <post_getquerystring+0x5d>
movl   $0xffffffff,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
test   %r15d,%r15d
je     11f5 <post_getquerystring+0xa5>
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11fe <post_getquerystring+0xae>
mov    %rax,%rbx
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1207 <post_getquerystring+0xb7>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11af <post_getquerystring+0x5f>
movl   $0xfffffffd,(%r14)
jmp    11ad <post_getquerystring+0x5d>
movl   $0xfffffffc,(%r14)
jmp    11ad <post_getquerystring+0x5d>
movl   $0xfffffffb,(%r14)
jmp    11ad <post_getquerystring+0x5d>

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

LLVM IR (-O2):
@.str = [13 x i8] c"CONTENT_TYPE\00"
@.str.1 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.2 = [34 x i8] c"application/x-www-form-urlencoded\00"
@stdin = global i32 0
define i8* @post_getquerystring(i32* nocapture writeonly %0) {
store i32 0, i32* %0
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %6
5: ; preds = %1
store i32 -1, i32* %0
br label %27
6: ; preds = %1
%7 = tail call i32 @atoi(i8* %3)
%8 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %6
store i32 -2, i32* %0
br label %27
11: ; preds = %6
%12 = icmp eq i32 %7, 0
br i1 %12, label %13, label %14
13: ; preds = %11
store i32 -3, i32* %0
br label %27
14: ; preds = %11
%15 = add nsw i32 %7, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -4, i32* %0
br label %27
19: ; preds = %14
%20 = load i32, i32* @stdin
%21 = tail call i32 @fread(i8* %16, i32 %7, i32 1, i32 %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %24, label %23
23: ; preds = %19
store i32 -5, i32* %0
br label %27
24: ; preds = %19
%25 = sext i32 %7 to i64
%26 = getelementptr inbounds i8, i8* %16, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %24, %23, %18, %13, %10, %5
%28 = phi i8* [ null, %5 ], [ null, %10 ], [ null, %13 ], [ null, %18 ], [ null, %23 ], [ %16, %24 ]
ret i8* %28
}