#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 /*<<< orphan*/ perror(char *);
int read(int const, char *, int const);
scalar_t__ strchr(char *, char);
int read_request(const int s, char *req, const int length) {
  int pos = 0;
  while (pos < length) {
    int r = read(s, req + pos, length - pos);
    req[length - 1] = '\0';
    if (r == -1) {
      perror("Read error: ");
      return -1;
    } else if (r == 0) {
      return 1;
    } else {
      if (strchr(req + pos, '\n')) {
        return 0;
      }
    }
    pos += r;
  }
  req[length - 1] = '\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,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11fd <read_request+0xcd>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a4 <read_request+0x74>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <read_request+0xe5>
cmpl   $0x0,-0x1c(%rbp)
jne    11ba <read_request+0x8a>
movl   $0x1,-0x4(%rbp)
jmp    1215 <read_request+0xe5>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11e5 <read_request+0xb5>
movl   $0x0,-0x4(%rbp)
jmp    1215 <read_request+0xe5>
jmp    11ea <read_request+0xba>
jmp    11ef <read_request+0xbf>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <read_request+0x19>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%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    %edx,%r12d
mov    %rsi,%r14
mov    %edi,0x4(%rsp)
lea    -0x1(%r12),%eax
test   %edx,%edx
jle    11a4 <read_request+0x74>
mov    %eax,%r13d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %r15d,%rax
lea    (%r14,%rax,1),%rbp
mov    %r12d,%edx
sub    %eax,%edx
mov    0x4(%rsp),%edi
mov    %rbp,%rsi
call   1040 <read@plt>
movb   $0x0,(%r14,%r13,1)
test   %eax,%eax
je     11ac <read_request+0x7c>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11c4 <read_request+0x94>
mov    %rbp,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    11b3 <read_request+0x83>
add    %ebx,%r15d
cmp    %r12d,%r15d
jl     1160 <read_request+0x30>
jmp    11a7 <read_request+0x77>
movslq %eax,%r13
movb   $0x0,(%r14,%r13,1)
mov    $0x1,%eax
jmp    11b5 <read_request+0x85>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11b5 <read_request+0x85>

LLVM IR (-O0):
@.str = [13 x i8] c"Read error: \00"
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 i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %47, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %51
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %14
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %57
33: ; preds = %14
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 1, i32* %4
br label %57
37: ; preds = %33
%38 = load i8*, i8** %6
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i64 @strchr(i8* %41, i8 signext 10)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %37
store i32 0, i32* %4
br label %57
45: ; preds = %37
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
%48 = load i32, i32* %9
%49 = load i32, i32* %8
%50 = add nsw i32 %49, %48
store i32 %50, i32* %8
br label %10
51: ; preds = %10
%52 = load i8*, i8** %6
%53 = load i32, i32* %7
%54 = sub nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 0, i8* %56
store i32 1, i32* %4
br label %57
57: ; preds = %51, %44, %36, %31
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@.str = [13 x i8] c"Read error: \00"
define i32 @read_request(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
%5 = add nsw i32 %2, -1
br i1 %4, label %8, label %6
6: ; preds = %3
%7 = sext i32 %5 to i64
br label %25
8: ; preds = %3
%9 = zext i32 %5 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
br label %13
11: ; preds = %21
%12 = icmp slt i32 %24, %2
br i1 %12, label %13, label %25
13: ; preds = %8, %11
%14 = phi i32 [ 0, %8 ], [ %24, %11 ]
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = sub nsw i32 %2, %14
%18 = tail call i32 @read(i32 %0, i8* %16, i32 %17)
store i8 0, i8* %10
switch i32 %18, label %21 [
i32 -1, label %19
i32 0, label %28
]
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %28
21: ; preds = %13
%22 = tail call i64 @strchr(i8* %16, i8 signext 10)
%23 = icmp eq i64 %22, 0
%24 = add nsw i32 %18, %14
br i1 %23, label %11, label %28
25: ; preds = %11, %6
%26 = phi i64 [ %7, %6 ], [ %9, %11 ]
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %21, %13, %19, %25
%29 = phi i32 [ 1, %25 ], [ -1, %19 ], [ 1, %13 ], [ 0, %21 ]
ret i32 %29
}