#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 *);
int /*<<< orphan*/ html_error(char *, char *);
char *malloc(int);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
char *getpost(void) {
  static char *buf = NULL;
  int bytes, length, cnt = 0;
  buf = getenv("CONTENT_TYPE");
  if (buf == NULL || strcmp(buf, "application/x-www-form-urlencoded")) {
    html_error("POST Error", "No Content-type");
    return (NULL);
  }
  buf = getenv("CONTENT_LENGTH");
  if (buf == NULL) {
    html_error("POST Error", "No Content Length");
    return (NULL);
  }
  length = atoi(buf);
  buf = malloc((length + 1) * sizeof(char));
  if (!buf) {
    html_error("Malloc Error", "Unable to allocate memory in getpost()");
    return (NULL);
  }
  do {
    bytes = fread(buf + cnt, 1, length - cnt, stdin);
    cnt += bytes;
  } while (bytes && cnt < length);
  buf[cnt] = '\0';
  return (buf);
}


x86 assembly (clang, -O0):
<getpost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
lea    0xe8a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ed6(%rip)
cmpq   $0x0,0x2ece(%rip)
je     11ad <getpost+0x4d>
mov    0x2ec1(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11cd <getpost+0x6d>
lea    0xe7b(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1060 <html_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <getpost+0x17d>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2e78(%rip)
cmpq   $0x0,0x2e70(%rip)
jne    120e <getpost+0xae>
lea    0xe3a(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1060 <html_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <getpost+0x17d>
mov    0x2e43(%rip),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,0x2e21(%rip)
cmpq   $0x0,0x2e19(%rip)
jne    1265 <getpost+0x105>
lea    0xe1f(%rip),%rdi
lea    0xe25(%rip),%rsi
call   1060 <html_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <getpost+0x17d>
jmp    126a <getpost+0x10a>
mov    0x2de7(%rip),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%edx
sub    -0x14(%rbp),%edx
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x15(%rbp)
je     12b8 <getpost+0x158>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    126a <getpost+0x10a>
mov    0x2d8e(%rip),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d7f(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpost>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ee0(%rip)
test   %rax,%rax
je     1191 <getpost+0x31>
lea    0xe89(%rip),%rsi
mov    %rax,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11ac <getpost+0x4c>
lea    0xe97(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <html_error@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2e99(%rip)
test   %rax,%rax
je     124d <getpost+0xed>
mov    %rax,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1070 <malloc@plt>
mov    %rax,0x2e75(%rip)
test   %rax,%rax
je     1260 <getpost+0x100>
mov    0x2df9(%rip),%r15
mov    (%r15),%ecx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <fread@plt>
mov    %eax,%ebx
test   %eax,%eax
je     123a <getpost+0xda>
nopl   0x0(%rax,%rax,1)
cmp    %r14d,%ebx
jge    123a <getpost+0xda>
movslq %ebx,%rbx
mov    0x2e39(%rip),%rdi
add    %rbx,%rdi
mov    %r14d,%edx
sub    %ebx,%edx
mov    (%r15),%ecx
mov    $0x1,%esi
call   1040 <fread@plt>
add    %eax,%ebx
test   %eax,%eax
jne    1210 <getpost+0xb0>
mov    0x2e17(%rip),%rax
movslq %ebx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a6 <getpost+0x46>
lea    0xddb(%rip),%rdi
lea    0xdfe(%rip),%rsi
jmp    119f <getpost+0x3f>
lea    0xe04(%rip),%rdi
lea    0xe0a(%rip),%rsi
jmp    119f <getpost+0x3f>

LLVM IR (-O0):
@getpost.buf = internal global i8* null
@.str = [13 x i8] c"CONTENT_TYPE\00"
@.str.1 = [34 x i8] c"application/x-www-form-urlencoded\00"
@.str.2 = [11 x i8] c"POST Error\00"
@.str.3 = [16 x i8] c"No Content-type\00"
@.str.4 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.5 = [18 x i8] c"No Content Length\00"
@.str.6 = [13 x i8] c"Malloc Error\00"
@.str.7 = [39 x i8] c"Unable to allocate memory in getpost()\00"
@stdin = global i32 0
define i8* @getpost() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %4
%5 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** @getpost.buf
%6 = load i8*, i8** @getpost.buf
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = load i8*, i8** @getpost.buf
%10 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8, %0
%13 = call i32 @html_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %1
br label %62
14: ; preds = %8
%15 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i8* %15, i8** @getpost.buf
%16 = load i8*, i8** @getpost.buf
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @html_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %1
br label %62
20: ; preds = %14
%21 = load i8*, i8** @getpost.buf
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** @getpost.buf
%29 = load i8*, i8** @getpost.buf
%30 = icmp ne i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %20
%32 = call i32 @html_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
store i8* null, i8** %1
br label %62
33: ; preds = %20
br label %34
34: ; preds = %54, %33
%35 = load i8*, i8** @getpost.buf
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i32, i32* %3
%40 = load i32, i32* %4
%41 = sub nsw i32 %39, %40
%42 = load i32, i32* @stdin
%43 = call i32 @fread(i8* %38, i32 1, i32 %41, i32 %42)
store i32 %43, i32* %2
%44 = load i32, i32* %2
%45 = load i32, i32* %4
%46 = add nsw i32 %45, %44
store i32 %46, i32* %4
br label %47
47: ; preds = %34
%48 = load i32, i32* %2
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i32, i32* %4
%52 = load i32, i32* %3
%53 = icmp slt i32 %51, %52
br label %54
54: ; preds = %50, %47
%55 = phi i1 [ false, %47 ], [ %53, %50 ]
br i1 %55, label %34, label %56
56: ; preds = %54
%57 = load i8*, i8** @getpost.buf
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** @getpost.buf
store i8* %61, i8** %1
br label %62
62: ; preds = %56, %31, %18, %12
%63 = load i8*, i8** %1
ret i8* %63
}

LLVM IR (-O2):
@getpost.buf = internal unnamed_addr global i8* null
@.str = [13 x i8] c"CONTENT_TYPE\00"
@.str.1 = [34 x i8] c"application/x-www-form-urlencoded\00"
@.str.2 = [11 x i8] c"POST Error\00"
@.str.3 = [16 x i8] c"No Content-type\00"
@.str.4 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.5 = [18 x i8] c"No Content Length\00"
@.str.6 = [13 x i8] c"Malloc Error\00"
@.str.7 = [39 x i8] c"Unable to allocate memory in getpost()\00"
@stdin = global i32 0
define i8* @getpost() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %1, i8** @getpost.buf
%2 = icmp eq i8* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3, %0
%7 = tail call i32 @html_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %43
8: ; preds = %3
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i8* %9, i8** @getpost.buf
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @html_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %43
13: ; preds = %8
%14 = tail call i32 @atoi(i8* %9)
%15 = add nsw i32 %14, 1
%16 = tail call i8* @malloc(i32 %15)
store i8* %16, i8** @getpost.buf
%17 = icmp eq i8* %16, null
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = load i32, i32* @stdin
%20 = tail call i32 @fread(i8* %16, i32 1, i32 %14, i32 %19)
%21 = icmp ne i32 %20, 0
%22 = icmp slt i32 %20, %14
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %26, label %38
24: ; preds = %13
%25 = tail call i32 @html_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
br label %43
26: ; preds = %18, %26
%27 = phi i32 [ %34, %26 ], [ %20, %18 ]
%28 = load i8*, i8** @getpost.buf
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = sub nsw i32 %14, %27
%32 = load i32, i32* @stdin
%33 = tail call i32 @fread(i8* %30, i32 1, i32 %31, i32 %32)
%34 = add nsw i32 %33, %27
%35 = icmp ne i32 %33, 0
%36 = icmp slt i32 %34, %14
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %26, label %38
38: ; preds = %26, %18
%39 = phi i32 [ %20, %18 ], [ %34, %26 ]
%40 = load i8*, i8** @getpost.buf
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %38, %24, %11, %6
%44 = phi i8* [ null, %6 ], [ null, %11 ], [ %40, %38 ], [ null, %24 ]
ret i8* %44
}