#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*/ printf(char *);
int read(int, unsigned char *, int);
int serial_readline2(int sid, unsigned char *buf, int size) {
  int n = 0;
  int m = 0;
  unsigned char *ptr;
  ptr = buf;
  while (m < size) {
    n = read(sid, ptr, 1);
    if (n == 0)
      continue;
    if (n < 0) {
      printf("error reading!\n");
      return n;
    }
    if (*ptr == '\r' || *ptr == '\n')
      break;
    m += n;
    ptr++;
  }
  *(++ptr) = '\0';
  return m;
}


x86 assembly (clang, -O0):
<serial_readline2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d7 <serial_readline2+0xb7>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1177 <serial_readline2+0x57>
jmp    1148 <serial_readline2+0x28>
cmpl   $0x0,-0x18(%rbp)
jge    1198 <serial_readline2+0x78>
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <serial_readline2+0xd0>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xd,%eax
je     11b8 <serial_readline2+0x98>
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xa,%eax
jne    11bd <serial_readline2+0x9d>
jmp    11d7 <serial_readline2+0xb7>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1148 <serial_readline2+0x28>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,0x1(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_readline2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
test   %edx,%edx
jle    1172 <serial_readline2+0x52>
mov    %edx,%r14d
mov    %edi,%ebp
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1140 <serial_readline2+0x20>
mov    %eax,%ebx
js     1187 <serial_readline2+0x67>
movzbl (%r12),%eax
cmp    $0xa,%al
je     1175 <serial_readline2+0x55>
cmp    $0xd,%al
je     1175 <serial_readline2+0x55>
add    %ebx,%r15d
add    $0x1,%r12
cmp    %r14d,%r15d
jl     1140 <serial_readline2+0x20>
jmp    1175 <serial_readline2+0x55>
xor    %r15d,%r15d
movb   $0x0,0x1(%r12)
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%r15d
jmp    117b <serial_readline2+0x5b>

LLVM IR (-O0):
@.str = [16 x i8] c"error reading!\0A\00"
define i32 @serial_readline2(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %6
store i8* %11, i8** %10
br label %12
12: ; preds = %40, %22, %3
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %46
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = call i32 @read(i32 %17, i8* %18, i32 1)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
br label %12
23: ; preds = %16
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %50
29: ; preds = %23
%30 = load i8*, i8** %10
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = icmp eq i32 %32, 13
br i1 %33, label %39, label %34
34: ; preds = %29
%35 = load i8*, i8** %10
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %40
39: ; preds = %34, %29
br label %46
40: ; preds = %34
%41 = load i32, i32* %8
%42 = load i32, i32* %9
%43 = add nsw i32 %42, %41
store i32 %43, i32* %9
%44 = load i8*, i8** %10
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %10
br label %12
46: ; preds = %39, %12
%47 = load i8*, i8** %10
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %10
store i8 0, i8* %48
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %26
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@str = [15 x i8] c"error reading!\00"
define i32 @serial_readline2(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %21
5: ; preds = %3, %17
%6 = phi i8* [ %19, %17 ], [ %1, %3 ]
%7 = phi i32 [ %18, %17 ], [ 0, %3 ]
br label %8
8: ; preds = %5, %8
%9 = tail call i32 @read(i32 %0, i8* %6, i32 1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %8, label %11
11: ; preds = %8
%12 = icmp slt i32 %9, 0
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %25
15: ; preds = %11
%16 = load i8, i8* %6
switch i8 %16, label %17 [
i8 13, label %21
i8 10, label %21
]
17: ; preds = %15
%18 = add nsw i32 %9, %7
%19 = getelementptr inbounds i8, i8* %6, i64 1
%20 = icmp slt i32 %18, %2
br i1 %20, label %5, label %21
21: ; preds = %15, %15, %17, %3
%22 = phi i32 [ 0, %3 ], [ %18, %17 ], [ %7, %15 ], [ %7, %15 ]
%23 = phi i8* [ %1, %3 ], [ %19, %17 ], [ %6, %15 ], [ %6, %15 ]
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8 0, i8* %24
br label %25
25: ; preds = %21, %13
%26 = phi i32 [ %9, %13 ], [ %22, %21 ]
ret i32 %26
}