#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, char *, int);
int MSN_Read(int fd, char *buffer, int nItems, int *count) {
  int loop = nItems;
  int bcount = 0;
  char c;
  int nread = 0;
  *count = 0;
  while (loop--) {
    nread = read(fd, &c, 1);
    if (nread <= 0) {
      perror("Error from read");
      buffer[bcount] = '\0';
      return -1;
    } else if (c == '\r') {
      *count += 1;
    } else if (c == '\n') {
      buffer[bcount] = '\0';
      *count += 1;
      return 0;
    } else {
      buffer[bcount++] = c;
      *count += 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<MSN_Read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x0,%eax
je     1229 <MSN_Read+0x109>
mov    -0x8(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jg     11aa <MSN_Read+0x8a>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <MSN_Read+0x110>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    11c7 <MSN_Read+0xa7>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    121f <MSN_Read+0xff>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    11f7 <MSN_Read+0xd7>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1230 <MSN_Read+0x110>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    121f <MSN_Read+0xff>
jmp    1224 <MSN_Read+0x104>
jmp    1154 <MSN_Read+0x34>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MSN_Read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rcx)
test   %edx,%edx
je     1194 <MSN_Read+0x74>
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r13d
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
jmp    115b <MSN_Read+0x3b>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
addl   $0x1,(%r15)
test   %ebp,%ebp
je     1194 <MSN_Read+0x74>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11a5 <MSN_Read+0x85>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
je     1150 <MSN_Read+0x30>
cmp    $0xa,%al
je     1188 <MSN_Read+0x68>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %al,(%r14,%rcx,1)
jmp    1150 <MSN_Read+0x30>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
addl   $0x1,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe54(%rip),%rdi
call   1040 <perror@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    $0xffffffff,%eax
jmp    1196 <MSN_Read+0x76>

LLVM IR (-O0):
@.str = [16 x i8] c"Error from read\00"
define i32 @MSN_Read(i32 %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%14 = load i32, i32* %8
store i32 %14, i32* %10
store i32 0, i32* %11
store i32 0, i32* %13
%15 = load i32*, i32** %9
store i32 0, i32* %15
br label %16
16: ; preds = %63, %4
%17 = load i32, i32* %10
%18 = add nsw i32 %17, -1
store i32 %18, i32* %10
%19 = icmp ne i32 %17, 0
br i1 %19, label %20, label %64
20: ; preds = %16
%21 = load i32, i32* %6
%22 = call i32 @read(i32 %21, i8* %12, i32 1)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%27 = load i8*, i8** %7
%28 = load i32, i32* %11
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
store i32 -1, i32* %5
br label %65
31: ; preds = %20
%32 = load i8, i8* %12
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 13
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32*, i32** %9
%37 = load i32, i32* %36
%38 = add nsw i32 %37, 1
store i32 %38, i32* %36
br label %62
39: ; preds = %31
%40 = load i8, i8* %12
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i8*, i8** %7
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i32*, i32** %9
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 1
store i32 %50, i32* %48
store i32 0, i32* %5
br label %65
51: ; preds = %39
%52 = load i8, i8* %12
%53 = load i8*, i8** %7
%54 = load i32, i32* %11
%55 = add nsw i32 %54, 1
store i32 %55, i32* %11
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 %52, i8* %57
%58 = load i32*, i32** %9
%59 = load i32, i32* %58
%60 = add nsw i32 %59, 1
store i32 %60, i32* %58
br label %61
61: ; preds = %51
br label %62
62: ; preds = %61, %35
br label %63
63: ; preds = %62
br label %16
64: ; preds = %16
store i32 0, i32* %5
br label %65
65: ; preds = %64, %43, %25
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@.str = [16 x i8] c"Error from read\00"
define i32 @MSN_Read(i32 %0, i8* nocapture writeonly %1, i32 %2, i32* nocapture %3) {
%5 = alloca i8
store i32 0, i32* %3
%6 = icmp eq i32 %2, 0
br i1 %6, label %33, label %7
7: ; preds = %4, %28
%8 = phi i32 [ %10, %28 ], [ %2, %4 ]
%9 = phi i32 [ %29, %28 ], [ 0, %4 ]
%10 = add nsw i32 %8, -1
%11 = call i32 @read(i32 %0, i8* %5, i32 1)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = sext i32 %9 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
br label %33
17: ; preds = %7
%18 = load i8, i8* %5
switch i8 %18, label %24 [
i8 13, label %28
i8 10, label %19
]
19: ; preds = %17
%20 = sext i32 %9 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %21
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %33
24: ; preds = %17
%25 = add nsw i32 %9, 1
%26 = sext i32 %9 to i64
%27 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 %18, i8* %27
br label %28
28: ; preds = %17, %24
%29 = phi i32 [ %25, %24 ], [ %9, %17 ]
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
%32 = icmp eq i32 %10, 0
br i1 %32, label %33, label %7
33: ; preds = %28, %4, %19, %13
%34 = phi i32 [ -1, %13 ], [ 0, %19 ], [ 0, %4 ], [ 0, %28 ]
ret i32 %34
}