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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
scalar_t__ strstr(char *, char *);
int read_httppost_data(FILE *f, char *data, size_t datalen) {
  char buf[256];
  int data_len = 0;
  *data = 0;
  while (fgets(buf, sizeof(buf), f) != NULL) {
    if (strstr(buf, "Content-Length:")) {
      data_len = atoi(buf + strlen("Content-Length:"));
    }
    if (!strcmp(buf, "\r\n"))
      break;
  }
  if (data_len >= datalen)
    return -1;
  if (!fgets(data, data_len + 1, f))
    return -1;
  *(data + data_len) = 0;
  return data_len;
}


x86 assembly (clang, -O0):
<read_httppost_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x124(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     120b <read_httppost_data+0xbb>
lea    -0x120(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11e5 <read_httppost_data+0x95>
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    0xe37(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x130(%rbp),%rdi
cltq
add    %rax,%rdi
call   1060 <atoi@plt>
mov    %eax,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1206 <read_httppost_data+0xb6>
jmp    120b <read_httppost_data+0xbb>
jmp    1178 <read_httppost_data+0x28>
movslq -0x124(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1228 <read_httppost_data+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <read_httppost_data+0x11c>
mov    -0x18(%rbp),%rdi
mov    -0x124(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1254 <read_httppost_data+0x104>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <read_httppost_data+0x11c>
mov    -0x18(%rbp),%rax
movslq -0x124(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_httppost_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r14
mov    %rdi,%r13
movb   $0x0,(%rsi)
xor    %r15d,%r15d
lea    0x10(%rsp),%rbx
lea    0xe92(%rip),%rbp
mov    $0xa0d,%r12d
jmp    1191 <read_httppost_data+0x51>
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%rsp),%eax
xor    %r12d,%eax
movzbl 0x12(%rsp),%ecx
or     %ax,%cx
je     11d2 <read_httppost_data+0x92>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11d2 <read_httppost_data+0x92>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     1180 <read_httppost_data+0x40>
mov    %rbp,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
call   1050 <atoi@plt>
mov    %eax,%r15d
jmp    1180 <read_httppost_data+0x40>
movslq %r15d,%rbp
mov    $0xffffffff,%ebx
cmp    0x8(%rsp),%rbp
jae    11fd <read_httppost_data+0xbd>
lea    0x1(%r15),%esi
mov    %r14,%rdi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11fd <read_httppost_data+0xbd>
movb   $0x0,(%r14,%rbp,1)
mov    %r15d,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [16 x i8] c"Content-Length:\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i32 @read_httppost_data(i32* nocapture %0, i8* %1, i64 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8 0, i8* %1
br label %6
6: ; preds = %18, %3
%7 = phi i32 [ 0, %3 ], [ %19, %18 ]
%8 = call i32* @fgets(i8* %5, i32 256, i32* %0)
%9 = icmp eq i32* %8, null
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %15
%17 = call i32 @atoi(i8* %16)
br label %18
18: ; preds = %13, %10
%19 = phi i32 [ %17, %13 ], [ %7, %10 ]
%20 = call i32 @bcmp(i8* dereferenceable(3) %5, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 3)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %6
22: ; preds = %18, %6
%23 = phi i32 [ %19, %18 ], [ %7, %6 ]
%24 = sext i32 %23 to i64
%25 = icmp ult i64 %24, %2
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = add nsw i32 %23, 1
%28 = call i32* @fgets(i8* %1, i32 %27, i32* %0)
%29 = icmp eq i32* %28, null
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %31
br label %32
32: ; preds = %26, %22, %30
%33 = phi i32 [ %23, %30 ], [ -1, %22 ], [ -1, %26 ]
ret i32 %33
}