#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 read(int, char *, int);
int read_request(int fd, char *request, int maxlen) {
  int retval;
  char c;
  int bytesread = 0;
  while (bytesread < maxlen - 1) {
    if (read(fd, &c, 1) != 1) {
      return 0;
    }
    if (c == '\n') {
      break;
    }
    request[bytesread] = c;
    bytesread += 1;
  }
  if (bytesread > 0 && request[bytesread - 1] == '\r') {
    bytesread -= 1;
  }
  request[bytesread] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<read_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    118e <read_request+0x7e>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1160 <read_request+0x50>
movl   $0x0,-0x4(%rbp)
jmp    11ce <read_request+0xbe>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1172 <read_request+0x62>
jmp    118e <read_request+0x7e>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1129 <read_request+0x19>
cmpl   $0x0,-0x20(%rbp)
jle    11bb <read_request+0xab>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11bb <read_request+0xab>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
cmp    $0x2,%edx
jl     118f <read_request+0x7f>
mov    %edx,%r15d
mov    %edi,%ebp
add    $0xffffffff,%r15d
mov    %r15d,%ebx
lea    0x7(%rsp),%r12
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1183 <read_request+0x73>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     1187 <read_request+0x77>
mov    %al,0x0(%r13,%r14,1)
add    $0x1,%r14
cmp    %r14,%rbx
jne    1140 <read_request+0x30>
test   %r15d,%r15d
je     118f <read_request+0x7f>
lea    -0x1(%r15),%eax
cmpb   $0xd,0x0(%r13,%rax,1)
cmove  %eax,%r15d
movslq %r15d,%rax
jmp    1191 <read_request+0x81>
xor    %eax,%eax
jmp    119c <read_request+0x8c>
mov    %r14d,%r15d
test   %r15d,%r15d
jne    1170 <read_request+0x60>
xor    %eax,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_request(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
br label %11
11: ; preds = %26, %3
%12 = load i32, i32* %10
%13 = load i32, i32* %7
%14 = sub nsw i32 %13, 1
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %34
16: ; preds = %11
%17 = load i32, i32* %5
%18 = call i32 @read(i32 %17, i8* %9, i32 1)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 0, i32* %4
br label %54
21: ; preds = %16
%22 = load i8, i8* %9
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %21
br label %34
26: ; preds = %21
%27 = load i8, i8* %9
%28 = load i8*, i8** %6
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %27, i8* %31
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %11
34: ; preds = %25, %11
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = load i32, i32* %10
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 13
br i1 %45, label %46, label %49
46: ; preds = %37
%47 = load i32, i32* %10
%48 = sub nsw i32 %47, 1
store i32 %48, i32* %10
br label %49
49: ; preds = %46, %37, %34
%50 = load i8*, i8** %6
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
store i32 1, i32* %4
br label %54
54: ; preds = %49, %20
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
define i32 @read_request(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i8
%5 = add i32 %2, -1
%6 = icmp sgt i32 %2, 1
br i1 %6, label %7, label %33
7: ; preds = %3
%8 = zext i32 %5 to i64
br label %9
9: ; preds = %7, %16
%10 = phi i64 [ 0, %7 ], [ %18, %16 ]
%11 = call i32 @read(i32 %0, i8* %4, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %36
13: ; preds = %9
%14 = load i8, i8* %4
%15 = icmp eq i8 %14, 10
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 %14, i8* %17
%18 = add nuw nsw i64 %10, 1
%19 = icmp eq i64 %18, %8
br i1 %19, label %22, label %9
20: ; preds = %13
%21 = trunc i64 %10 to i32
br label %22
22: ; preds = %16, %20
%23 = phi i32 [ %21, %20 ], [ %5, %16 ]
%24 = icmp eq i32 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %22
%26 = add nsw i32 %23, -1
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %1, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 13
%31 = select i1 %30, i32 %26, i32 %23
%32 = sext i32 %31 to i64
br label %33
33: ; preds = %3, %25, %22
%34 = phi i64 [ 0, %22 ], [ %32, %25 ], [ 0, %3 ]
%35 = getelementptr inbounds i8, i8* %1, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %9, %33
%37 = phi i32 [ 1, %33 ], [ 0, %9 ]
ret i32 %37
}