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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ memmove(char *, char *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t read(int, char *, size_t);
char *strstr(char *, char *);
ssize_t readreq(int fd, char *req, size_t size) {
  memset(req, 0, size);
  char buf[512];
  ssize_t n;
  for (n = 0; size > 0;) {
    ssize_t nr = read(
        fd, buf,
        (((size - n) < (sizeof(buf) - 1)) ? (size - n) : (sizeof(buf) - 1)));
    if (nr < 0) {
      if (errno == EINTR)
        continue;
      return -1;
    }
    buf[nr] = '\0';
    char *p = strstr(buf, "\r\n");
    if (p)
      nr = p - buf;
    memmove(req, buf, nr);
    req += nr;
    n += nr;
    if (p)
      break;
  }
  return n;
}


x86 assembly (clang, -O0):
<readreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
movq   $0x0,-0x228(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    12cd <readreq+0x18d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x244(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x228(%rbp),%rax
cmp    $0x1ff,%rax
jae    11c0 <readreq+0x80>
mov    -0x20(%rbp),%rax
sub    -0x228(%rbp),%rax
mov    %rax,-0x250(%rbp)
jmp    11d1 <readreq+0x91>
mov    $0x1ff,%eax
mov    %rax,-0x250(%rbp)
jmp    11d1 <readreq+0x91>
mov    -0x240(%rbp),%rsi
mov    -0x244(%rbp),%edi
mov    -0x250(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x230(%rbp)
cmpq   $0x0,-0x230(%rbp)
jae    122b <readreq+0xeb>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    121e <readreq+0xde>
jmp    1170 <readreq+0x30>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12d8 <readreq+0x198>
mov    -0x230(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
lea    0xdb8(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     127a <readreq+0x13a>
mov    -0x238(%rbp),%rax
lea    -0x220(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x230(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    -0x230(%rbp),%rdx
call   1050 <memmove@plt>
mov    -0x230(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x230(%rbp),%rax
add    -0x228(%rbp),%rax
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     12c8 <readreq+0x188>
jmp    12cd <readreq+0x18d>
jmp    1170 <readreq+0x30>
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readreq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,0xc(%rsp)
xor    %r15d,%r15d
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
nopl   0x0(%rax,%rax,1)
test   %rbp,%rbp
je     11e0 <readreq+0xa0>
mov    %rbp,%rdx
sub    %r15,%rdx
cmp    $0x1ff,%rdx
mov    $0x1ff,%eax
cmovae %rax,%rdx
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1040 <read@plt>
mov    %rax,%r13
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
lea    0xe52(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,%r14
mov    %rax,%r12
lea    0x10(%rsp),%rsi
sub    %rsi,%r12
test   %rax,%rax
cmove  %r13,%r12
mov    %rbx,%rdi
mov    %r12,%rdx
call   1050 <memmove@plt>
add    %r12,%rbx
add    %r12,%r15
test   %r14,%r14
je     1170 <readreq+0x30>
jmp    11e3 <readreq+0xa3>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [3 x i8] c"\0D\0A\00"
define i64 @readreq(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [512 x i8]
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = call i32 @memset(i8* %12, i32 0, i64 %13)
store i64 0, i64* %9
br label %15
15: ; preds = %68, %39, %3
%16 = load i64, i64* %7
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %69
18: ; preds = %15
%19 = load i32, i32* %5
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%21 = load i64, i64* %7
%22 = load i64, i64* %9
%23 = sub i64 %21, %22
%24 = icmp ult i64 %23, 511
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i64, i64* %7
%27 = load i64, i64* %9
%28 = sub i64 %26, %27
br label %30
29: ; preds = %18
br label %30
30: ; preds = %29, %25
%31 = phi i64 [ %28, %25 ], [ 511, %29 ]
%32 = call i64 @read(i32 %19, i8* %20, i64 %31)
store i64 %32, i64* %10
%33 = load i64, i64* %10
%34 = icmp ult i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %15
40: ; preds = %35
store i64 -1, i64* %4
br label %71
41: ; preds = %30
%42 = load i64, i64* %10
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %42
store i8 0, i8* %43
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%45 = call i8* @strstr(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %54
48: ; preds = %41
%49 = load i8*, i8** %11
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
store i64 %53, i64* %10
br label %54
54: ; preds = %48, %41
%55 = load i8*, i8** %6
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%57 = load i64, i64* %10
%58 = call i32 @memmove(i8* %55, i8* %56, i64 %57)
%59 = load i64, i64* %10
%60 = load i8*, i8** %6
%61 = getelementptr inbounds i8, i8* %60, i64 %59
store i8* %61, i8** %6
%62 = load i64, i64* %10
%63 = load i64, i64* %9
%64 = add i64 %63, %62
store i64 %64, i64* %9
%65 = load i8*, i8** %11
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %68
67: ; preds = %54
br label %69
68: ; preds = %54
br label %15
69: ; preds = %67, %15
%70 = load i64, i64* %9
store i64 %70, i64* %4
br label %71
71: ; preds = %69, %40
%72 = load i64, i64* %4
ret i64 %72
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [3 x i8] c"\0D\0A\00"
define i64 @readreq(i32 %0, i8* %1, i64 %2) {
%4 = alloca [512 x i8]
%5 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = icmp eq i64 %2, 0
%8 = ptrtoint [512 x i8]* %4 to i64
br label %9
9: ; preds = %12, %3
%10 = phi i8* [ %1, %3 ], [ %24, %12 ]
%11 = phi i64 [ 0, %3 ], [ %25, %12 ]
br i1 %7, label %26, label %12
12: ; preds = %9
%13 = sub i64 %2, %11
%14 = icmp ult i64 %13, 511
%15 = select i1 %14, i64 %13, i64 511
%16 = call i64 @read(i32 %0, i8* %6, i64 %15)
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i8* %18, null
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %20, %8
%22 = select i1 %19, i64 %16, i64 %21
%23 = call i32 @memmove(i8* %10, i8* %6, i64 %22)
%24 = getelementptr inbounds i8, i8* %10, i64 %22
%25 = add i64 %22, %11
br i1 %19, label %9, label %26
26: ; preds = %12, %9
%27 = phi i64 [ %25, %12 ], [ 0, %9 ]
ret i64 %27
}