#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 */
typedef int off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, int, int /*<<< orphan*/);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
char *getenv(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[], char *envp[]) {
  FILE *output;
  int clen;
  size_t read, write;
  off_t offset;
  char *querystring, *e;
  if ((e = getenv("CONTENT_LENGTH")) == NULL) {
    fprintf(stderr, "Unable to get content length.");
    return -1;
  }
  clen = strtol(e, NULL, 10);
  if (errno == ERANGE) {
    fprintf(stderr, "Unable to convert Content-Length to integer.\n");
    return -1;
  }
  read = 0;
  offset = 0;
  querystring = (char *)malloc(clen + 1);
  memset(querystring, 0, clen + 1);
  do {
    read = fread(querystring + offset, sizeof(char), clen - offset, stdin);
    if (ferror(stdin))
      return -1;
    else if (read <= 0)
      continue;
    offset += read;
  } while (offset < clen && read > 0);
  if ((output = fopen("./output", "w")) == NULL)
    fprintf(stderr, "Unable to open output!");
  write = fwrite(querystring, sizeof(char), clen, output);
  fclose(output);
  fprintf(stdout, "Content-Type: text/plain\r\n\r\nDone.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe3f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    11f5 <main+0x55>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <main+0x1de>
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    1245 <main+0xa5>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <main+0x1de>
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x24(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x48(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x24(%rbp),%edx
sub    -0x3c(%rbp),%edx
mov    0x2d5c(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12c0 <main+0x120>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <main+0x1de>
cmpq   $0x0,-0x30(%rbp)
ja     12d0 <main+0x130>
jmp    12e3 <main+0x143>
jmp    12d5 <main+0x135>
mov    -0x30(%rbp),%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x51(%rbp)
jge    12ff <main+0x15f>
cmpq   $0x0,-0x30(%rbp)
seta   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1274 <main+0xd4>
lea    0xd4a(%rip),%rdi
lea    0xd4c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1340 <main+0x1a0>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%r14d
mov    0x2e0f(%rip),%rbp
test   %rax,%rax
je     11fc <main+0x5c>
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,%r15d
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <main+0x68>
lea    0xe36(%rip),%rsi
jmp    12c3 <main+0x123>
lea    0xe0c(%rip),%rsi
jmp    12c3 <main+0x123>
lea    0x1(%r15),%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <memset@plt>
mov    0x2dbf(%rip),%r12
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%r14,%rax,1),%rdi
mov    %r15d,%edx
sub    %eax,%edx
mov    (%r12),%ecx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,%rbp
mov    (%r12),%edi
call   1040 <ferror@plt>
test   %rax,%rax
jne    12d7 <main+0x137>
add    %ebp,%ebx
cmp    %r15d,%ebx
jge    1267 <main+0xc7>
test   %rbp,%rbp
jne    1230 <main+0x90>
lea    0xded(%rip),%rdi
lea    0xdef(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1297 <main+0xf7>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbp,%rcx
call   10c0 <fwrite@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
xor    %r14d,%r14d
lea    0xdc1(%rip),%rsi
mov    0x2cfd(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <fprintf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r14d
jmp    12cb <main+0x12b>

LLVM IR (-O0):
@.str = [15 x i8] c"CONTENT_LENGTH\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Unable to get content length.\00"
@errno = global i64 0
@ERANGE = global i64 0
@.str.2 = [46 x i8] c"Unable to convert Content-Length to integer.\0A\00"
@stdin = global i32 0
@.str.3 = [9 x i8] c"./output\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [23 x i8] c"Unable to open output!\00"
@stdout = global i32 0
@.str.6 = [35 x i8] c"Content-Type: text/plain\0D\0A\0D\0ADone.\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%15 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %87
20: ; preds = %3
%21 = load i8*, i8** %14
%22 = call i32 @strtol(i8* %21, i32* null, i32 10)
store i32 %22, i32* %9
%23 = load i64, i64* @errno
%24 = load i64, i64* @ERANGE
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %87
29: ; preds = %20
store i64 0, i64* %10
store i32 0, i32* %12
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %13
%34 = load i8*, i8** %13
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
%37 = call i32 @memset(i8* %34, i32 0, i32 %36)
br label %38
38: ; preds = %70, %29
%39 = load i8*, i8** %13
%40 = load i32, i32* %12
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i32, i32* %9
%44 = load i32, i32* %12
%45 = sub nsw i32 %43, %44
%46 = load i32, i32* @stdin
%47 = call i64 @fread(i8* %42, i32 1, i32 %45, i32 %46)
store i64 %47, i64* %10
%48 = load i32, i32* @stdin
%49 = call i64 @ferror(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %38
store i32 -1, i32* %4
br label %87
52: ; preds = %38
%53 = load i64, i64* %10
%54 = icmp ule i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %52
br label %63
56: ; preds = %52
br label %57
57: ; preds = %56
%58 = load i64, i64* %10
%59 = load i32, i32* %12
%60 = sext i32 %59 to i64
%61 = add i64 %60, %58
%62 = trunc i64 %61 to i32
store i32 %62, i32* %12
br label %63
63: ; preds = %57, %55
%64 = load i32, i32* %12
%65 = load i32, i32* %9
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i64, i64* %10
%69 = icmp ugt i64 %68, 0
br label %70
70: ; preds = %67, %63
%71 = phi i1 [ false, %63 ], [ %69, %67 ]
br i1 %71, label %38, label %72
72: ; preds = %70
%73 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %73, i32** %8
%74 = icmp eq i32* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* @stderr
%77 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %78
78: ; preds = %75, %72
%79 = load i8*, i8** %13
%80 = load i32, i32* %9
%81 = load i32*, i32** %8
%82 = call i64 @fwrite(i8* %79, i32 1, i32 %80, i32* %81)
store i64 %82, i64* %11
%83 = load i32*, i32** %8
%84 = call i32 @fclose(i32* %83)
%85 = load i32, i32* @stdout
%86 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %4
br label %87
87: ; preds = %78, %51, %26, %17
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
@.str = [15 x i8] c"CONTENT_LENGTH\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Unable to get content length.\00"
@errno = global i64 0
@ERANGE = global i64 0
@.str.2 = [46 x i8] c"Unable to convert Content-Length to integer.\0A\00"
@stdin = global i32 0
@.str.3 = [9 x i8] c"./output\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [23 x i8] c"Unable to open output!\00"
@stdout = global i32 0
@.str.6 = [35 x i8] c"Content-Type: text/plain\0D\0A\0D\0ADone.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %41, label %6
6: ; preds = %3
%7 = tail call i32 @strtol(i8* nocapture %4, i32* null, i32 10)
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
br i1 %10, label %41, label %11
11: ; preds = %6
%12 = add nsw i32 %7, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @memset(i8* %14, i32 0, i32 %12)
br label %16
16: ; preds = %26, %11
%17 = phi i32 [ 0, %11 ], [ %28, %26 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
%20 = sub nsw i32 %7, %17
%21 = load i32, i32* @stdin
%22 = tail call i64 @fread(i8* %19, i32 1, i32 %20, i32 %21)
%23 = load i32, i32* @stdin
%24 = tail call i64 @ferror(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %47
26: ; preds = %16
%27 = trunc i64 %22 to i32
%28 = add i32 %17, %27
%29 = icmp slt i32 %28, %7
%30 = icmp ne i64 %22, 0
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %16, label %32
32: ; preds = %26
%33 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %35, %32
%39 = tail call i64 @fwrite(i8* %14, i32 1, i32 %7, i32* %33)
%40 = tail call i32 @fclose(i32* %33)
br label %41
41: ; preds = %6, %3, %38
%42 = phi i32* [ @stdout, %38 ], [ @stderr, %3 ], [ @stderr, %6 ]
%43 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), %38 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), %6 ]
%44 = phi i32 [ 0, %38 ], [ -1, %3 ], [ -1, %6 ]
%45 = load i32, i32* %42
%46 = tail call i32 @fprintf(i32 %45, i8* %43)
br label %47
47: ; preds = %16, %41
%48 = phi i32 [ %44, %41 ], [ -1, %16 ]
ret i32 %48
}